|
Line 0
Link Here
|
|
|
1 |
--- src/xml.erl.orig 2016-08-23 11:33:47 UTC |
| 2 |
+++ src/xml.erl |
| 3 |
@@ -98,21 +98,11 @@ terminate(_Reason, _State) -> |
| 4 |
ok. |
| 5 |
|
| 6 |
%% |
| 7 |
--spec(element_to_binary/1 :: |
| 8 |
-( |
| 9 |
- El :: xmlel() | cdata()) |
| 10 |
- -> binary() |
| 11 |
-). |
| 12 |
|
| 13 |
element_to_binary(El) -> |
| 14 |
iolist_to_binary(element_to_string(El)). |
| 15 |
|
| 16 |
%% |
| 17 |
--spec(element_to_string/1 :: |
| 18 |
-( |
| 19 |
- El :: xmlel() | cdata()) |
| 20 |
- -> string() |
| 21 |
-). |
| 22 |
|
| 23 |
element_to_string(El) -> |
| 24 |
case catch element_to_string_nocatch(El) of |
| 25 |
@@ -120,12 +110,6 @@ element_to_string(El) -> |
| 26 |
Result -> Result |
| 27 |
end. |
| 28 |
|
| 29 |
--spec(element_to_string_nocatch/1 :: |
| 30 |
-( |
| 31 |
- El :: xmlel() | cdata()) |
| 32 |
- -> iolist() |
| 33 |
-). |
| 34 |
- |
| 35 |
element_to_string_nocatch(El) -> |
| 36 |
case El of |
| 37 |
#xmlel{name = Name, attrs = Attrs, children = Els} -> |
| 38 |
@@ -157,11 +141,6 @@ crypt(S) -> |
| 39 |
|| <<C>> <= S >>. |
| 40 |
|
| 41 |
%% |
| 42 |
--spec(make_text_node/1 :: |
| 43 |
-( |
| 44 |
- CData :: binary()) |
| 45 |
- -> binary() |
| 46 |
-). |
| 47 |
|
| 48 |
make_text_node(CData) -> |
| 49 |
case cdata_need_escape(CData) of |
| 50 |
@@ -221,34 +200,17 @@ escape_cdata(CData, Index, [Pos | Positi |
| 51 |
[CDATA2, Part, CDATA1 | Acc]). |
| 52 |
|
| 53 |
%% |
| 54 |
--spec(remove_cdata_p/1 :: |
| 55 |
-( |
| 56 |
- El :: xmlel() | cdata()) |
| 57 |
- -> boolean() |
| 58 |
-). |
| 59 |
|
| 60 |
remove_cdata_p(#xmlel{}) -> true; |
| 61 |
remove_cdata_p(_) -> false. |
| 62 |
|
| 63 |
%% |
| 64 |
--spec(remove_cdata/1 :: |
| 65 |
-( |
| 66 |
- L :: [xmlel() | cdata()]) |
| 67 |
- -> [xmlel()] |
| 68 |
-). |
| 69 |
|
| 70 |
remove_cdata(L) -> [E || E <- L, remove_cdata_p(E)]. |
| 71 |
|
| 72 |
%% This function is intended to remove subtags based on a name and an |
| 73 |
%% attribute, usually an xmlns attribute for a specific XMPP |
| 74 |
%% extension. |
| 75 |
--spec(remove_subtags/3 :: |
| 76 |
-( |
| 77 |
- Xmlel :: xmlel(), |
| 78 |
- Name :: binary(), |
| 79 |
- Attr :: attr()) |
| 80 |
- -> Xmlel :: xmlel() |
| 81 |
-). |
| 82 |
|
| 83 |
remove_subtags(#xmlel{name = TagName, attrs = TagAttrs, children = Els}, |
| 84 |
Name, Attr) -> |
| 85 |
@@ -256,14 +218,6 @@ remove_subtags(#xmlel{name = TagName, at |
| 86 |
children = remove_subtags1(Els, [], Name, Attr)}. |
| 87 |
|
| 88 |
%% |
| 89 |
--spec(remove_subtags1/4 :: |
| 90 |
-( |
| 91 |
- Els :: [xmlel() | cdata()], |
| 92 |
- NewEls :: [xmlel()], |
| 93 |
- Name :: binary(), |
| 94 |
- Attr :: attr()) |
| 95 |
- -> NewEls :: [xmlel()] |
| 96 |
-). |
| 97 |
|
| 98 |
remove_subtags1([], NewEls, _Name, _Attr) -> |
| 99 |
lists:reverse(NewEls); |
| 100 |
@@ -281,43 +235,17 @@ remove_subtags1([El | Els], NewEls, Name |
| 101 |
_ -> remove_subtags1(Els, [El | NewEls], Name, Attr) |
| 102 |
end. |
| 103 |
|
| 104 |
--spec(get_cdata/1 :: |
| 105 |
-( |
| 106 |
- L :: [xmlel() | cdata()]) |
| 107 |
- -> binary() |
| 108 |
-). |
| 109 |
- |
| 110 |
get_cdata(L) -> |
| 111 |
(iolist_to_binary(get_cdata(L, <<"">>))). |
| 112 |
|
| 113 |
--spec(get_cdata/2 :: |
| 114 |
-( |
| 115 |
- L :: [xmlel() | cdata()], |
| 116 |
- S :: binary() | iolist()) |
| 117 |
- -> binary() | iolist() |
| 118 |
-). |
| 119 |
- |
| 120 |
get_cdata([{xmlcdata, CData} | L], S) -> |
| 121 |
get_cdata(L, [S, CData]); |
| 122 |
get_cdata([_ | L], S) -> get_cdata(L, S); |
| 123 |
get_cdata([], S) -> S. |
| 124 |
|
| 125 |
--spec(get_tag_cdata/1 :: |
| 126 |
-( |
| 127 |
- Xmlel :: xmlel()) |
| 128 |
- -> binary() |
| 129 |
-). |
| 130 |
- |
| 131 |
get_tag_cdata(#xmlel{children = Els}) -> get_cdata(Els). |
| 132 |
|
| 133 |
%% |
| 134 |
--spec(get_attr/2 :: |
| 135 |
-( |
| 136 |
- AttrName :: binary(), |
| 137 |
- Attrs :: [attr()]) |
| 138 |
- -> {value, binary()} |
| 139 |
- | false |
| 140 |
-). |
| 141 |
|
| 142 |
get_attr(AttrName, Attrs) -> |
| 143 |
case lists:keysearch(AttrName, 1, Attrs) of |
| 144 |
@@ -326,12 +254,6 @@ get_attr(AttrName, Attrs) -> |
| 145 |
end. |
| 146 |
|
| 147 |
%% |
| 148 |
--spec(get_attr_s/2 :: |
| 149 |
-( |
| 150 |
- AttrName :: binary(), |
| 151 |
- Attrs :: [attr()]) |
| 152 |
- -> Val :: binary() |
| 153 |
-). |
| 154 |
|
| 155 |
get_attr_s(AttrName, Attrs) -> |
| 156 |
case lists:keysearch(AttrName, 1, Attrs) of |
| 157 |
@@ -340,46 +262,21 @@ get_attr_s(AttrName, Attrs) -> |
| 158 |
end. |
| 159 |
|
| 160 |
%% |
| 161 |
--spec(get_tag_attr/2 :: |
| 162 |
-( |
| 163 |
- AttrName :: binary(), |
| 164 |
- Xmlel :: xmlel()) |
| 165 |
- -> {value, binary()} |
| 166 |
- | false |
| 167 |
-). |
| 168 |
|
| 169 |
get_tag_attr(AttrName, #xmlel{attrs = Attrs}) -> |
| 170 |
get_attr(AttrName, Attrs). |
| 171 |
|
| 172 |
%% |
| 173 |
--spec(get_tag_attr_s/2 :: |
| 174 |
-( |
| 175 |
- AttrName :: binary(), |
| 176 |
- Xmlel :: xmlel()) |
| 177 |
- -> binary() |
| 178 |
-). |
| 179 |
|
| 180 |
get_tag_attr_s(AttrName, #xmlel{attrs = Attrs}) -> |
| 181 |
get_attr_s(AttrName, Attrs). |
| 182 |
|
| 183 |
%% |
| 184 |
--spec(get_subtag/2 :: |
| 185 |
-( |
| 186 |
- Xmlel :: xmlel(), |
| 187 |
- Name :: binary()) |
| 188 |
- -> xmlel() | false |
| 189 |
-). |
| 190 |
|
| 191 |
get_subtag(#xmlel{children = Els}, Name) -> |
| 192 |
get_subtag1(Els, Name). |
| 193 |
|
| 194 |
%% |
| 195 |
--spec(get_subtag1/2 :: |
| 196 |
-( |
| 197 |
- Els :: [xmlel() | cdata()], |
| 198 |
- Name :: binary()) |
| 199 |
- -> xmlel() | false |
| 200 |
-). |
| 201 |
|
| 202 |
get_subtag1( [El | Els], Name) -> |
| 203 |
case El of |
| 204 |
@@ -388,13 +285,6 @@ get_subtag1( [El | Els], Name) -> |
| 205 |
end; |
| 206 |
get_subtag1([], _) -> false. |
| 207 |
|
| 208 |
--spec(get_subtags/2 :: |
| 209 |
-( |
| 210 |
- Xmlel :: xmlel(), |
| 211 |
- Name :: binary()) |
| 212 |
- -> [xmlel()] |
| 213 |
-). |
| 214 |
- |
| 215 |
get_subtags(#xmlel{children = Els}, Name) -> |
| 216 |
get_subtags1(Els, Name, []). |
| 217 |
|
| 218 |
@@ -407,25 +297,11 @@ get_subtags1([El | Els], Name, Acc) -> |
| 219 |
end. |
| 220 |
|
| 221 |
%% |
| 222 |
--spec(get_subtag_with_xmlns/3 :: |
| 223 |
-( |
| 224 |
- Xmlel :: xmlel(), |
| 225 |
- Name :: binary(), |
| 226 |
- XMLNS :: binary()) |
| 227 |
- -> xmlel() | false |
| 228 |
-). |
| 229 |
|
| 230 |
get_subtag_with_xmlns(#xmlel{children = Els}, Name, XMLNS) -> |
| 231 |
get_subtag_with_xmlns1(Els, Name, XMLNS). |
| 232 |
|
| 233 |
%% |
| 234 |
--spec(get_subtag_with_xmlns1/3 :: |
| 235 |
-( |
| 236 |
- Els :: [xmlel() | cdata()], |
| 237 |
- Name :: binary(), |
| 238 |
- XMLNS :: binary()) |
| 239 |
- -> xmlel() | false |
| 240 |
-). |
| 241 |
|
| 242 |
get_subtag_with_xmlns1([El | Els], Name, XMLNS) -> |
| 243 |
case El of |
| 244 |
@@ -442,14 +318,6 @@ get_subtag_with_xmlns1([El | Els], Name, |
| 245 |
get_subtag_with_xmlns1([], _, _) -> |
| 246 |
false. |
| 247 |
|
| 248 |
--spec(get_subtags_with_xmlns/3 :: |
| 249 |
-( |
| 250 |
- Xmlel :: xmlel(), |
| 251 |
- Name :: binary(), |
| 252 |
- XMLNS :: binary()) |
| 253 |
- -> [xmlel()] |
| 254 |
-). |
| 255 |
- |
| 256 |
get_subtags_with_xmlns(#xmlel{children = Els}, Name, XMLNS) -> |
| 257 |
get_subtags_with_xmlns1(Els, Name, XMLNS, []). |
| 258 |
|
| 259 |
@@ -469,12 +337,6 @@ get_subtags_with_xmlns1([El | Els], Name |
| 260 |
end. |
| 261 |
|
| 262 |
%% |
| 263 |
--spec(get_subtag_cdata/2 :: |
| 264 |
-( |
| 265 |
- Tag :: xmlel(), |
| 266 |
- Name :: binary()) |
| 267 |
- -> binary() |
| 268 |
-). |
| 269 |
|
| 270 |
get_subtag_cdata(Tag, Name) -> |
| 271 |
case get_subtag(Tag, Name) of |
| 272 |
@@ -483,26 +345,11 @@ get_subtag_cdata(Tag, Name) -> |
| 273 |
end. |
| 274 |
|
| 275 |
%% |
| 276 |
--spec(append_subtags/2 :: |
| 277 |
-( |
| 278 |
- Xmlel :: xmlel(), |
| 279 |
- SubTags2 :: [xmlel() | cdata()]) |
| 280 |
- -> Xmlel :: xmlel() |
| 281 |
-). |
| 282 |
|
| 283 |
append_subtags(#xmlel{name = Name, attrs = Attrs, children = SubTags1}, SubTags2) -> |
| 284 |
#xmlel{name = Name, attrs = Attrs, children = SubTags1 ++ SubTags2}. |
| 285 |
|
| 286 |
%% |
| 287 |
--spec(get_path_s/2 :: |
| 288 |
-( |
| 289 |
- El :: xmlel(), |
| 290 |
- Path :: [{elem, Name::binary()} |
| 291 |
- |{attr, Name::binary()} |
| 292 |
- |cdata]) |
| 293 |
- -> xmlel() |
| 294 |
- | binary() |
| 295 |
-). |
| 296 |
|
| 297 |
get_path_s(El, []) -> El; |
| 298 |
get_path_s(El, [{elem, Name} | Path]) -> |
| 299 |
@@ -515,35 +362,12 @@ get_path_s(El, [{attr, Name}]) -> |
| 300 |
get_path_s(El, [cdata]) -> get_tag_cdata(El). |
| 301 |
|
| 302 |
%% |
| 303 |
--spec(replace_tag_attr/3 :: |
| 304 |
-( |
| 305 |
- Name :: binary(), |
| 306 |
- Value :: binary(), |
| 307 |
- Xmlel :: xmlel()) |
| 308 |
- -> Xmlel :: #xmlel{ |
| 309 |
- name :: binary(), |
| 310 |
- attrs :: [attr(),...], |
| 311 |
- children :: [xmlel() | cdata()] |
| 312 |
- } |
| 313 |
-). |
| 314 |
|
| 315 |
replace_tag_attr(Name, Value, Xmlel) -> |
| 316 |
Xmlel#xmlel{ |
| 317 |
attrs = [{Name, Value} | lists:keydelete(Name, 1, Xmlel#xmlel.attrs)] |
| 318 |
}. |
| 319 |
|
| 320 |
- |
| 321 |
--spec(replace_subtag/2 :: |
| 322 |
-( |
| 323 |
- Tag :: xmlel(), |
| 324 |
- Xmlel :: xmlel()) |
| 325 |
- -> Xmlel :: #xmlel{ |
| 326 |
- name :: binary(), |
| 327 |
- attrs :: [attr(),...], |
| 328 |
- children :: [xmlel() | cdata()] |
| 329 |
- } |
| 330 |
-). |
| 331 |
- |
| 332 |
replace_subtag(#xmlel{name = Name} = Tag, Xmlel) -> |
| 333 |
Xmlel#xmlel{ |
| 334 |
children = [Tag | lists:keydelete(Name, #xmlel.name, Xmlel#xmlel.children)] |