|
| 1 | +/*--- |
| 2 | +includes: [compatFs.js, compatNjs.js, runTsuite.js] |
| 3 | +flags: [async] |
| 4 | +---*/ |
| 5 | + |
| 6 | +import xml from 'xml'; |
| 7 | + |
| 8 | +let parse_tsuite = { |
| 9 | + name: "parse()", |
| 10 | + skip: () => (!has_njs()), |
| 11 | + T: async (params) => { |
| 12 | + let doc = xml.parse(params.doc); |
| 13 | + let r = params.get(doc); |
| 14 | + |
| 15 | + if (r !== params.expected) { |
| 16 | + throw Error(`unexpected output "${r}" != "${params.expected}"`); |
| 17 | + } |
| 18 | + |
| 19 | + return 'SUCCESS'; |
| 20 | + }, |
| 21 | + |
| 22 | + opts: { |
| 23 | + doc: `<note><to b=\"bar\" a= \"foo\" >Tove</to><from>Jani</from></note>`, |
| 24 | + }, |
| 25 | + |
| 26 | + tests: [ |
| 27 | + { get: (doc) => doc.nonexist, expected: undefined }, |
| 28 | + { get: (doc) => doc.note.$name, expected: 'note' }, |
| 29 | + { get: (doc) => doc.note.$text, expected: 'ToveJani' }, |
| 30 | + { get: (doc) => doc.note.to.$text, expected: 'Tove' }, |
| 31 | + { get: (doc) => doc.note.$tag$to.$text, expected: 'Tove' }, |
| 32 | + { get: (doc) => doc.note.$attrs, expected: undefined }, |
| 33 | + { get: (doc) => doc.note.to.$attrs.a, expected: 'foo' }, |
| 34 | + { get: (doc) => doc.note.to.$attrs.b, expected: 'bar' }, |
| 35 | + { get: (doc) => doc.note.to.$attr$b, expected: 'bar' }, |
| 36 | + { get: (doc) => doc.note.$attr$a, expected: undefined }, |
| 37 | + { get: (doc) => Array.isArray(doc.note.$tags), expected: true }, |
| 38 | + { get: (doc) => doc.note.$tags[0].$text, expected: 'Tove' }, |
| 39 | + { get: (doc) => doc.note.$tags[1].$text, expected: 'Jani' }, |
| 40 | + { get: (doc) => doc.note.$tags$from[0].$text, expected: 'Jani' }, |
| 41 | + { get: (doc) => doc.note.$parent, expected: undefined }, |
| 42 | + { get: (doc) => doc.note.to.$parent.$name, expected: 'note' }, |
| 43 | + { doc: `<n0:pdu xmlns:n0=\"http://a\"></n0:pdu>`, |
| 44 | + get: (doc) => doc.pdu.$ns, |
| 45 | + expected: 'http://a' }, |
| 46 | + { doc: `<root><foo>FOO</foo><foo>BAR</foo></root>`, |
| 47 | + get: (doc) => doc.root.$tags$foo[0].$text, |
| 48 | + expected: 'FOO' }, |
| 49 | + { doc: `<root><foo>FOO</foo><foo>BAR</foo></root>`, |
| 50 | + get: (doc) => doc.root.$tags$foo[1].$text, |
| 51 | + expected: 'BAR' }, |
| 52 | + { doc: `<root><foo>FOO</foo><foo>BAR</foo></root>`, |
| 53 | + get: (doc) => doc.root.$tags$bar.length, |
| 54 | + expected: 0 }, |
| 55 | + { doc: `<root><foo>FOO</foo><foo>BAR</foo></root>`, |
| 56 | + get: (doc) => doc.root.$tags.length, |
| 57 | + expected: 2 }, |
| 58 | + { doc: `<r><a></a>TEXT</r>`, |
| 59 | + get: (doc) => doc.r.$text, |
| 60 | + expected: 'TEXT' }, |
| 61 | + { doc: `<r><a></a>TEXT</r>`, |
| 62 | + get: (doc) => doc.$root.$text, |
| 63 | + expected: 'TEXT' }, |
| 64 | + { doc: `<r>俄语<a></a>данные</r>`, |
| 65 | + get: (doc) => doc.r.$text[2], |
| 66 | + expected: 'д' }, |
| 67 | + { doc: `<俄语 լեզու=\"ռուսերեն\">данные</俄语>`, |
| 68 | + get: (doc) => JSON.stringify([doc['俄语'].$name[1],doc['俄语']['$attr$լեզու'][7],doc['俄语'].$text[5]]), |
| 69 | + expected: '["语","ն","е"]' }, |
| 70 | + |
| 71 | + { get: (doc) => JSON.stringify(Object.getOwnPropertyNames(doc)), |
| 72 | + expected: '["note"]' }, |
| 73 | + { get: (doc) => JSON.stringify(Object.getOwnPropertyNames(doc.note)), |
| 74 | + expected: '["$name","$tags"]' }, |
| 75 | + { get: (doc) => JSON.stringify(Object.getOwnPropertyNames(doc.note.to)), |
| 76 | + expected: '["$name","$attrs","$text"]' }, |
| 77 | + |
| 78 | + { get: (doc) => JSON.stringify(doc.note.to.$attrs), |
| 79 | + expected: '{"b":"bar","a":"foo"}' }, |
| 80 | + { get: (doc) => JSON.stringify(doc.note.$tags), |
| 81 | + expected: '[{"$name":"to","$attrs":{"b":"bar","a":"foo"},"$text":"Tove"},{"$name":"from","$text":"Jani"}]' }, |
| 82 | + { get: (doc) => JSON.stringify(doc), |
| 83 | + expected: '{"note":{"$name":"note","$tags":[{"$name":"to","$attrs":{"b":"bar","a":"foo"},"$text":"Tove"},{"$name":"from","$text":"Jani"}]}}' }, |
| 84 | + { get: (doc) => JSON.stringify(doc.note), |
| 85 | + expected: '{"$name":"note","$tags":[{"$name":"to","$attrs":{"b":"bar","a":"foo"},"$text":"Tove"},{"$name":"from","$text":"Jani"}]}' }, |
| 86 | + |
| 87 | + { doc: `GARBAGE`, |
| 88 | + exception: 'Error: failed to parse XML (libxml2: "Start tag expected, \'<\' not found" at 1:1)' }, |
| 89 | +]}; |
| 90 | + |
| 91 | +let c14n_tsuite = { |
| 92 | + name: "c14n()", |
| 93 | + skip: () => (!has_njs()), |
| 94 | + T: async (params) => { |
| 95 | + let doc = xml.parse(params.doc); |
| 96 | + let r = params.call(doc); |
| 97 | + |
| 98 | + if (params.buffer) { |
| 99 | + r = new TextDecoder().decode(r); |
| 100 | + } |
| 101 | + |
| 102 | + if (r !== params.expected) { |
| 103 | + throw Error(`unexpected output "${r}" != "${params.expected}"`); |
| 104 | + } |
| 105 | + |
| 106 | + return 'SUCCESS'; |
| 107 | + }, |
| 108 | + |
| 109 | + opts: { |
| 110 | + buffer: true, |
| 111 | + doc: `<n0:pdu xmlns:n0=\"http://a\"><n1:elem1 xmlns:n1=\"http://b\"><!-- comment -->foo</n1:elem1></n0:pdu>`, |
| 112 | + }, |
| 113 | + |
| 114 | + tests: [ |
| 115 | + { call: (doc) => xml.c14n(doc.pdu.elem1), |
| 116 | + expected: `<n1:elem1 xmlns:n0="http://a" xmlns:n1="http://b">foo</n1:elem1>` }, |
| 117 | + { call: (doc) => xml.serialize(doc.pdu.elem1), |
| 118 | + expected: `<n1:elem1 xmlns:n0="http://a" xmlns:n1="http://b">foo</n1:elem1>` }, |
| 119 | + { call: (doc) => xml.serializeToString(doc.pdu.elem1), |
| 120 | + buffer: false, |
| 121 | + expected: `<n1:elem1 xmlns:n0="http://a" xmlns:n1="http://b">foo</n1:elem1>` }, |
| 122 | + { call: (doc) => xml.exclusiveC14n(doc.pdu.elem1), |
| 123 | + expected: `<n1:elem1 xmlns:n1="http://b">foo</n1:elem1>` }, |
| 124 | + { call: (doc) => xml.exclusiveC14n(doc.pdu.elem1, null, true), |
| 125 | + expected: `<n1:elem1 xmlns:n1="http://b"><!-- comment -->foo</n1:elem1>` }, |
| 126 | + { call: (doc) => xml.exclusiveC14n(doc.pdu.elem1, null, false, 'n1'), |
| 127 | + expected: `<n1:elem1 xmlns:n1="http://b">foo</n1:elem1>` }, |
| 128 | + { call: (doc) => xml.exclusiveC14n(doc.pdu.elem1, null, false, 'a b c d e f g h i j'), |
| 129 | + expected: `<n1:elem1 xmlns:n1="http://b">foo</n1:elem1>` }, |
| 130 | + { doc: `<note><to a="foo" b="bar">Tove</to><from>Jani</from></note>`, |
| 131 | + call: (doc) => xml.c14n(doc.note), |
| 132 | + expected: `<note><to a="foo" b="bar">Tove</to><from>Jani</from></note>` }, |
| 133 | + { doc: `<note><to a="foo" b="bar">Tove</to><from>Jani</from></note>`, |
| 134 | + call: (doc) => xml.exclusiveC14n(doc.note), |
| 135 | + expected: `<note><to a="foo" b="bar">Tove</to><from>Jani</from></note>` }, |
| 136 | + { doc: `<note><to a="foo" b="bar">Tove</to><from>Jani</from></note>`, |
| 137 | + call: (doc) => xml.exclusiveC14n(doc.note, doc.note.to), |
| 138 | + expected: `<note><from>Jani</from></note>` }, |
| 139 | + { doc: `<r></r>`, |
| 140 | + call: (doc) => xml.exclusiveC14n(doc, 1), |
| 141 | + exception: 'TypeError: "excluding" argument is not a XMLNode object' }, |
| 142 | +]}; |
| 143 | + |
| 144 | +let modify_tsuite = { |
| 145 | + name: "modifying XML", |
| 146 | + skip: () => (!has_njs()), |
| 147 | + T: async (params) => { |
| 148 | + let doc = xml.parse(params.doc); |
| 149 | + let r = params.get(doc); |
| 150 | + |
| 151 | + if (r !== params.expected) { |
| 152 | + throw Error(`unexpected output "${r}" != "${params.expected}"`); |
| 153 | + } |
| 154 | + |
| 155 | + return 'SUCCESS'; |
| 156 | + }, |
| 157 | + |
| 158 | + opts: { |
| 159 | + doc: `<note><to b=\"bar\" a= \"foo\" >Tove</to><from>Jani</from></note>`, |
| 160 | + }, |
| 161 | + |
| 162 | + tests: [ |
| 163 | + { get: (doc) => { |
| 164 | + doc.note.setText('WAKA'); |
| 165 | + return doc.note.$text; |
| 166 | + }, |
| 167 | + expected: 'WAKA' }, |
| 168 | + { get: (doc) => { |
| 169 | + doc.note.setText('WAKA'); |
| 170 | + return xml.serializeToString(doc); |
| 171 | + }, |
| 172 | + expected: `<note>WAKA</note>` }, |
| 173 | + { get: (doc) => { |
| 174 | + doc.note.$text = '<WA&KA>'; |
| 175 | + return xml.serializeToString(doc); |
| 176 | + }, |
| 177 | + expected: `<note><WA&KA></note>` }, |
| 178 | + { get: (doc) => { |
| 179 | + doc.note.$text = ''; |
| 180 | + return xml.serializeToString(doc); |
| 181 | + }, |
| 182 | + expected: `<note></note>` }, |
| 183 | + { get: (doc) => { |
| 184 | + doc.note.setText('<WA&KA>'); |
| 185 | + return doc.note.$text; |
| 186 | + }, |
| 187 | + expected: '<WA&KA>' }, |
| 188 | + { get: (doc) => { |
| 189 | + doc.note.setText('<WA&KA>'); |
| 190 | + return xml.serializeToString(doc); |
| 191 | + }, |
| 192 | + expected: `<note><WA&KA></note>` }, |
| 193 | + { get: (doc) => { |
| 194 | + doc.note.setText('"WAKA"'); |
| 195 | + return xml.serializeToString(doc); |
| 196 | + }, |
| 197 | + expected: `<note>"WAKA"</note>` }, |
| 198 | + { get: (doc) => { |
| 199 | + doc.note.setText(''); |
| 200 | + return doc.note.$text; |
| 201 | + }, |
| 202 | + expected: '' }, |
| 203 | + { get: (doc) => { |
| 204 | + doc.note.setText(''); |
| 205 | + return xml.serializeToString(doc); |
| 206 | + }, |
| 207 | + expected: `<note></note>` }, |
| 208 | + { get: (doc) => { |
| 209 | + doc.note.setText(null); |
| 210 | + return doc.note.$text; |
| 211 | + }, |
| 212 | + expected: '' }, |
| 213 | + { get: (doc) => { |
| 214 | + doc.note.setText(undefined); |
| 215 | + return doc.note.$text; |
| 216 | + }, |
| 217 | + expected: '' }, |
| 218 | + { get: (doc) => { |
| 219 | + doc.note.removeText(); |
| 220 | + return doc.note.$text; |
| 221 | + }, |
| 222 | + expected: '' }, |
| 223 | + { get: (doc) => { |
| 224 | + delete doc.note.$text; |
| 225 | + return xml.serializeToString(doc); |
| 226 | + }, |
| 227 | + expected: `<note></note>` }, |
| 228 | + { get: (doc) => { |
| 229 | + doc.note.removeText(); |
| 230 | + return xml.serializeToString(doc); |
| 231 | + }, |
| 232 | + expected: `<note></note>` }, |
| 233 | + { get: (doc) => { |
| 234 | + let to = doc.note.to; |
| 235 | + doc.$root.$text = ''; |
| 236 | + return to.$name; |
| 237 | + }, |
| 238 | + expected: 'to' }, |
| 239 | + { get: (doc) => { |
| 240 | + let to = doc.note.to; |
| 241 | + doc.$root.$text = ''; |
| 242 | + return [to.$name, to.$text, to.$attr$b, to.$parent.$name].toString(); |
| 243 | + }, |
| 244 | + expected: 'to,Tove,bar,note' }, |
| 245 | + { get: (doc) => { |
| 246 | + doc.note.to.setAttribute('aaa', 'foo'); |
| 247 | + doc.note.to.setAttribute('bbb', '<bar\"'); |
| 248 | + return xml.serializeToString(doc.note.to); |
| 249 | + }, |
| 250 | + expected: `<to a="foo" aaa="foo" b="bar" bbb="<bar"">Tove</to>` }, |
| 251 | + { get: (doc) => { |
| 252 | + doc.note.to.$attr$aaa = 'foo'; |
| 253 | + doc.note.to.$attr$bbb = '<bar\"'; |
| 254 | + return xml.serializeToString(doc.note.to); |
| 255 | + }, |
| 256 | + expected: `<to a="foo" aaa="foo" b="bar" bbb="<bar"">Tove</to>` }, |
| 257 | + { get: (doc) => { |
| 258 | + doc.note.to.$attr$aaa = 'foo'; |
| 259 | + return doc.note.to.$attr$aaa; |
| 260 | + }, |
| 261 | + expected: `foo` }, |
| 262 | + { get: (doc) => { |
| 263 | + doc.note.to.setAttribute('aaa', 'foo'); |
| 264 | + doc.note.to.setAttribute('aaa', 'foo2'); |
| 265 | + return xml.serializeToString(doc.note.to); |
| 266 | + }, |
| 267 | + expected: `<to a="foo" aaa="foo2" b="bar">Tove</to>` }, |
| 268 | + { get: (doc) => { |
| 269 | + doc.note.to.removeAttribute('a'); |
| 270 | + return xml.serializeToString(doc.note.to); |
| 271 | + }, |
| 272 | + expected: `<to b="bar">Tove</to>` }, |
| 273 | + { get: (doc) => { |
| 274 | + doc.note.to.removeAllAttributes(); |
| 275 | + return xml.serializeToString(doc.note.to); |
| 276 | + }, |
| 277 | + expected: `<to>Tove</to>` }, |
| 278 | + { get: (doc) => { |
| 279 | + delete doc.note.to.$attr$a; |
| 280 | + return xml.serializeToString(doc.note.to); |
| 281 | + }, |
| 282 | + expected: `<to b="bar">Tove</to>` }, |
| 283 | + { get: (doc) => { |
| 284 | + doc.note.to.setAttribute('a', null); |
| 285 | + return xml.serializeToString(doc.note.to); |
| 286 | + }, |
| 287 | + expected: `<to b="bar">Tove</to>` }, |
| 288 | + { get: (doc) => { |
| 289 | + doc.note.to.setAttribute('<', 'foo'); |
| 290 | + return xml.serializeToString(doc.note.to); |
| 291 | + }, |
| 292 | + exception: 'TypeError: attribute name "<" is not valid' }, |
| 293 | + { get: (doc) => { |
| 294 | + let doc2 = xml.parse(`<n0:pdu xmlns:n0=\"http://a\"></n0:pdu>`); |
| 295 | + doc.note.addChild(doc2); |
| 296 | + return xml.serializeToString(doc); |
| 297 | + }, |
| 298 | + expected: `<note xmlns:n0="http://a"><to a="foo" b="bar">Tove</to><from>Jani</from><n0:pdu></n0:pdu></note>` }, |
| 299 | + { get: (doc) => { |
| 300 | + let doc2 = xml.parse(`<n0:pdu xmlns:n0=\"http://a\"></n0:pdu>`); |
| 301 | + doc.note.addChild(doc2); |
| 302 | + doc.note.addChild(doc2); |
| 303 | + return xml.serializeToString(doc); |
| 304 | + }, |
| 305 | + expected: `<note xmlns:n0="http://a"><to a="foo" b="bar">Tove</to><from>Jani</from><n0:pdu></n0:pdu><n0:pdu></n0:pdu></note>` }, |
| 306 | + { get: (doc) => { |
| 307 | + doc.note.removeChildren('to'); |
| 308 | + return xml.serializeToString(doc); |
| 309 | + }, |
| 310 | + expected: `<note><from>Jani</from></note>` }, |
| 311 | + { get: (doc) => { |
| 312 | + delete doc.note.$tag$to; |
| 313 | + return xml.serializeToString(doc); |
| 314 | + }, |
| 315 | + expected: `<note><from>Jani</from></note>` }, |
| 316 | + { get: (doc) => { |
| 317 | + delete doc.note.to; |
| 318 | + return xml.serializeToString(doc); |
| 319 | + }, |
| 320 | + expected: `<note><from>Jani</from></note>` }, |
| 321 | + { get: (doc) => { |
| 322 | + doc.note.removeChildren('xxx'); |
| 323 | + return xml.serializeToString(doc); |
| 324 | + }, |
| 325 | + expected: `<note><to a="foo" b="bar">Tove</to><from>Jani</from></note>` }, |
| 326 | + { get: (doc) => { |
| 327 | + delete doc.note.$tag$xxx; |
| 328 | + return xml.serializeToString(doc); |
| 329 | + }, |
| 330 | + expected: `<note><to a="foo" b="bar">Tove</to><from>Jani</from></note>` }, |
| 331 | + { doc: `<root><a>A</a><b>B</b><a>C</a></root>`, |
| 332 | + get: (doc) => { |
| 333 | + doc.$root.removeChildren('a'); |
| 334 | + return xml.serializeToString(doc); |
| 335 | + }, |
| 336 | + expected: `<root><b>B</b></root>` }, |
| 337 | + { doc: `<root><a>A</a><b>B</b><a>C</a></root>`, |
| 338 | + get: (doc) => { |
| 339 | + doc.$root.removeChildren(); |
| 340 | + return xml.serializeToString(doc); |
| 341 | + }, |
| 342 | + expected: `<root></root>` }, |
| 343 | + { doc: `<root><a>A</a><b>B</b><a>C</a></root>`, |
| 344 | + get: (doc) => { |
| 345 | + doc.$root.$tags = []; |
| 346 | + return xml.serializeToString(doc); |
| 347 | + }, |
| 348 | + expected: `<root></root>` }, |
| 349 | + { doc: `<root><a>A</a><b>B</b><a>C</a></root>`, |
| 350 | + get: (doc) => { |
| 351 | + doc.$root.$tags$ = []; |
| 352 | + return xml.serializeToString(doc); |
| 353 | + }, |
| 354 | + expected: `<root></root>` }, |
| 355 | + { doc: `<root><a>A</a><b>B</b><a>C</a></root>`, |
| 356 | + get: (doc) => { |
| 357 | + delete doc.$root.$tag$a; |
| 358 | + return xml.serializeToString(doc); |
| 359 | + }, |
| 360 | + expected: `<root><b>B</b></root>` }, |
| 361 | + { get: (doc) => { |
| 362 | + doc.note.$tags = [doc.note.to]; |
| 363 | + return xml.serializeToString(doc); |
| 364 | + }, |
| 365 | + expected: `<note><to a="foo" b="bar">Tove</to></note>` }, |
| 366 | + { get: (doc) => { |
| 367 | + let doc2 = xml.parse(`<n0:pdu xmlns:n0=\"http://a\"></n0:pdu>`); |
| 368 | + doc.note.$tags = [doc.note.to, doc2]; |
| 369 | + return xml.serializeToString(doc); |
| 370 | + }, |
| 371 | + expected: `<note xmlns:n0="http://a"><to a="foo" b="bar">Tove</to><n0:pdu></n0:pdu></note>` }, |
| 372 | + { get: (doc) => { |
| 373 | + let doc2 = xml.parse(`<n0:pdu xmlns:n0=\"http://a\"></n0:pdu>`); |
| 374 | + doc.note.$tags = [doc2, doc.note.to]; |
| 375 | + return xml.serializeToString(doc); |
| 376 | + }, |
| 377 | + expected: `<note xmlns:n0="http://a"><n0:pdu></n0:pdu><to a="foo" b="bar">Tove</to></note>` }, |
| 378 | +]}; |
| 379 | + |
| 380 | +run([ |
| 381 | + parse_tsuite, |
| 382 | + c14n_tsuite, |
| 383 | + modify_tsuite, |
| 384 | +]) |
| 385 | +.then($DONE, $DONE); |
0 commit comments