Skip to content

Commit c2f834a

Browse files
committed
test: verify DOMParser in Node.js
1 parent c7fbd42 commit c2f834a

File tree

6 files changed

+104
-38
lines changed

6 files changed

+104
-38
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
"vitest": "^3.2.4",
117117
"webpack": "5.101.0",
118118
"webpack-cli": "4.10.0",
119+
"xmldom": "^0.6.0",
119120
"yargs": "17.5.1"
120121
},
121122
"overrides": {

packages/xml-builder/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "XML builder for the AWS SDK",
55
"dependencies": {
66
"@smithy/types": "^4.5.0",
7-
"fast-xml-parser": "5.2.5",
7+
"@xmldom/xmldom": "0.8.11",
88
"tslib": "^2.6.2"
99
},
1010
"scripts": {
Lines changed: 86 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,89 @@
1-
import { XMLParser } from "fast-xml-parser";
2-
3-
const parser = new XMLParser({
4-
attributeNamePrefix: "",
5-
htmlEntities: true,
6-
ignoreAttributes: false,
7-
ignoreDeclaration: true,
8-
parseTagValue: false,
9-
trimValues: false,
10-
tagValueProcessor: (_: any, val: any) => (val.trim() === "" && val.includes("\n") ? "" : undefined),
11-
});
12-
parser.addEntity("#xD", "\r");
13-
parser.addEntity("#10", "\n");
1+
//
2+
//
3+
// const parser = new XMLParser({
4+
// attributeNamePrefix: "",
5+
// htmlEntities: true,
6+
// ignoreAttributes: false,
7+
// ignoreDeclaration: true,
8+
// parseTagValue: false,
9+
// trimValues: false,
10+
// tagValueProcessor: (_: any, val: any) => (val.trim() === "" && val.includes("\n") ? "" : undefined),
11+
// });
12+
// parser.addEntity("#xD", "\r");
13+
// parser.addEntity("#10", "\n");
1414

15+
// export function parseXML(xmlString: string): any {
16+
// return parser.parse(xmlString, true);
17+
// }
18+
19+
// temporary replacement for compatibility testing.
20+
import { DOMParser } from "@xmldom/xmldom";
21+
const parser = new DOMParser();
22+
23+
/**
24+
* Cases where this differs from fast-xml-parser:
25+
*
26+
* 1. mixing text with nested tags
27+
* <mixed-text> hello, <bold>world</bold>, how are you?</mixed-text>
28+
*
29+
* @internal
30+
*/
1531
export function parseXML(xmlString: string): any {
16-
return parser.parse(xmlString, true);
32+
const xmlDocument = parser.parseFromString(xmlString, "application/xml");
33+
34+
// Recursive function to convert XML nodes to JS object
35+
const xmlToObj = (node: Node): any => {
36+
if (node.nodeType === 3) {
37+
if (node.textContent?.trim()) {
38+
return node.textContent;
39+
}
40+
}
41+
42+
if (node.nodeType === 1) {
43+
const element = node as Element;
44+
if (element.attributes.length === 0 && element.childNodes.length === 0) {
45+
return "";
46+
}
47+
48+
const obj: any = {};
49+
50+
const attributes = Array.from(element.attributes);
51+
for (const attr of attributes) {
52+
obj[`${attr.name}`] = attr.value;
53+
}
54+
55+
const childNodes = Array.from(element.childNodes);
56+
for (const child of childNodes) {
57+
const childResult = xmlToObj(child);
58+
59+
if (childResult != null) {
60+
const childName = child.nodeName;
61+
62+
if (childNodes.length === 1 && attributes.length === 0 && childName === "#text") {
63+
return childResult;
64+
}
65+
66+
if (obj[childName]) {
67+
if (Array.isArray(obj[childName])) {
68+
obj[childName].push(childResult);
69+
} else {
70+
obj[childName] = [obj[childName], childResult];
71+
}
72+
} else {
73+
obj[childName] = childResult;
74+
}
75+
} else if (childNodes.length === 1 && attributes.length === 0) {
76+
return element.textContent;
77+
}
78+
}
79+
80+
return obj;
81+
}
82+
83+
return null;
84+
};
85+
86+
return {
87+
[xmlDocument.documentElement.nodeName]: xmlToObj(xmlDocument.documentElement),
88+
};
1789
}

private/aws-protocoltests-restxml-schema/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
"@smithy/util-stream": "^4.3.1",
6161
"@smithy/util-utf8": "^4.1.0",
6262
"entities": "2.2.0",
63-
"fast-xml-parser": "5.2.5",
6463
"tslib": "^2.6.2"
6564
},
6665
"devDependencies": {

private/aws-protocoltests-restxml/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
"@smithy/util-utf8": "^4.1.0",
6363
"@types/uuid": "^9.0.1",
6464
"entities": "2.2.0",
65-
"fast-xml-parser": "5.2.5",
6665
"tslib": "^2.6.2",
6766
"uuid": "^9.0.1"
6867
},

yarn.lock

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,6 @@ __metadata:
11811181
concurrently: "npm:7.0.0"
11821182
downlevel-dts: "npm:0.10.1"
11831183
entities: "npm:2.2.0"
1184-
fast-xml-parser: "npm:5.2.5"
11851184
rimraf: "npm:3.0.2"
11861185
tslib: "npm:^2.6.2"
11871186
typescript: "npm:~5.8.3"
@@ -1241,7 +1240,6 @@ __metadata:
12411240
concurrently: "npm:7.0.0"
12421241
downlevel-dts: "npm:0.10.1"
12431242
entities: "npm:2.2.0"
1244-
fast-xml-parser: "npm:5.2.5"
12451243
rimraf: "npm:3.0.2"
12461244
tslib: "npm:^2.6.2"
12471245
typescript: "npm:~5.8.3"
@@ -25003,9 +25001,9 @@ __metadata:
2500325001
dependencies:
2500425002
"@smithy/types": "npm:^4.5.0"
2500525003
"@tsconfig/recommended": "npm:1.0.1"
25004+
"@xmldom/xmldom": "npm:0.8.11"
2500625005
concurrently: "npm:7.0.0"
2500725006
downlevel-dts: "npm:0.10.1"
25008-
fast-xml-parser: "npm:5.2.5"
2500925007
rimraf: "npm:3.0.2"
2501025008
tslib: "npm:^2.6.2"
2501125009
typescript: "npm:~5.8.3"
@@ -30534,6 +30532,13 @@ __metadata:
3053430532
languageName: node
3053530533
linkType: hard
3053630534

30535+
"@xmldom/xmldom@npm:0.8.11":
30536+
version: 0.8.11
30537+
resolution: "@xmldom/xmldom@npm:0.8.11"
30538+
checksum: 10c0/e768623de72c95d3dae6b5da8e33dda0d81665047811b5498d23a328d45b13feb5536fe921d0308b96a4a8dd8addf80b1f6ef466508051c0b581e63e0dc74ed5
30539+
languageName: node
30540+
linkType: hard
30541+
3053730542
"@xtuc/ieee754@npm:^1.2.0":
3053830543
version: 1.2.0
3053930544
resolution: "@xtuc/ieee754@npm:1.2.0"
@@ -31138,6 +31143,7 @@ __metadata:
3113831143
vitest: "npm:^3.2.4"
3113931144
webpack: "npm:5.101.0"
3114031145
webpack-cli: "npm:4.10.0"
31146+
xmldom: "npm:^0.6.0"
3114131147
yargs: "npm:17.5.1"
3114231148
languageName: unknown
3114331149
linkType: soft
@@ -33975,17 +33981,6 @@ __metadata:
3397533981
languageName: node
3397633982
linkType: hard
3397733983

33978-
"fast-xml-parser@npm:5.2.5":
33979-
version: 5.2.5
33980-
resolution: "fast-xml-parser@npm:5.2.5"
33981-
dependencies:
33982-
strnum: "npm:^2.1.0"
33983-
bin:
33984-
fxparser: src/cli/cli.js
33985-
checksum: 10c0/d1057d2e790c327ccfc42b872b91786a4912a152d44f9507bf053f800102dfb07ece3da0a86b33ff6a0caa5a5cad86da3326744f6ae5efb0c6c571d754fe48cd
33986-
languageName: node
33987-
linkType: hard
33988-
3398933984
"fastest-levenshtein@npm:^1.0.12":
3399033985
version: 1.0.16
3399133986
resolution: "fastest-levenshtein@npm:1.0.16"
@@ -40839,13 +40834,6 @@ __metadata:
4083940834
languageName: node
4084040835
linkType: hard
4084140836

40842-
"strnum@npm:^2.1.0":
40843-
version: 2.1.1
40844-
resolution: "strnum@npm:2.1.1"
40845-
checksum: 10c0/1f9bd1f9b4c68333f25c2b1f498ea529189f060cd50aa59f1876139c994d817056de3ce57c12c970f80568d75df2289725e218bd9e3cdf73cd1a876c9c102733
40846-
languageName: node
40847-
linkType: hard
40848-
4084940837
"strong-log-transformer@npm:^2.1.0":
4085040838
version: 2.1.0
4085140839
resolution: "strong-log-transformer@npm:2.1.0"
@@ -42535,6 +42523,13 @@ __metadata:
4253542523
languageName: node
4253642524
linkType: hard
4253742525

42526+
"xmldom@npm:^0.6.0":
42527+
version: 0.6.0
42528+
resolution: "xmldom@npm:0.6.0"
42529+
checksum: 10c0/93cbb4854f73f431a402e7942baa87a59e8445fd0b4ae6bf6af3a1007809e849da8ac70bbe1509242212bce5d19e30b55ea282cbd9d87281701d1bdc13b74623
42530+
languageName: node
42531+
linkType: hard
42532+
4253842533
"xtend@npm:^4.0.0, xtend@npm:^4.0.2, xtend@npm:~4.0.0, xtend@npm:~4.0.1":
4253942534
version: 4.0.2
4254042535
resolution: "xtend@npm:4.0.2"

0 commit comments

Comments
 (0)