Skip to content

Commit 3b6b9ae

Browse files
committed
test: verify DOMParser in Node.js
1 parent 0527ca5 commit 3b6b9ae

File tree

6 files changed

+98
-39
lines changed

6 files changed

+98
-39
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"description": "XML builder for the AWS SDK",
55
"dependencies": {
66
"@smithy/types": "^4.5.0",
7-
"fast-xml-parser": "5.2.5",
8-
"tslib": "^2.6.2"
7+
"tslib": "^2.6.2",
8+
"xmldom": "0.6.0"
99
},
1010
"scripts": {
1111
"build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",
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";
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: 9 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"
@@ -25005,10 +25003,10 @@ __metadata:
2500525003
"@tsconfig/recommended": "npm:1.0.1"
2500625004
concurrently: "npm:7.0.0"
2500725005
downlevel-dts: "npm:0.10.1"
25008-
fast-xml-parser: "npm:5.2.5"
2500925006
rimraf: "npm:3.0.2"
2501025007
tslib: "npm:^2.6.2"
2501125008
typescript: "npm:~5.8.3"
25009+
xmldom: "npm:0.6.0"
2501225010
languageName: unknown
2501325011
linkType: soft
2501425012

@@ -31138,6 +31136,7 @@ __metadata:
3113831136
vitest: "npm:^3.2.4"
3113931137
webpack: "npm:5.101.0"
3114031138
webpack-cli: "npm:4.10.0"
31139+
xmldom: "npm:^0.6.0"
3114131140
yargs: "npm:17.5.1"
3114231141
languageName: unknown
3114331142
linkType: soft
@@ -33975,17 +33974,6 @@ __metadata:
3397533974
languageName: node
3397633975
linkType: hard
3397733976

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-
3398933977
"fastest-levenshtein@npm:^1.0.12":
3399033978
version: 1.0.16
3399133979
resolution: "fastest-levenshtein@npm:1.0.16"
@@ -40839,13 +40827,6 @@ __metadata:
4083940827
languageName: node
4084040828
linkType: hard
4084140829

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-
4084940830
"strong-log-transformer@npm:^2.1.0":
4085040831
version: 2.1.0
4085140832
resolution: "strong-log-transformer@npm:2.1.0"
@@ -42535,6 +42516,13 @@ __metadata:
4253542516
languageName: node
4253642517
linkType: hard
4253742518

42519+
"xmldom@npm:0.6.0, xmldom@npm:^0.6.0":
42520+
version: 0.6.0
42521+
resolution: "xmldom@npm:0.6.0"
42522+
checksum: 10c0/93cbb4854f73f431a402e7942baa87a59e8445fd0b4ae6bf6af3a1007809e849da8ac70bbe1509242212bce5d19e30b55ea282cbd9d87281701d1bdc13b74623
42523+
languageName: node
42524+
linkType: hard
42525+
4253842526
"xtend@npm:^4.0.0, xtend@npm:^4.0.2, xtend@npm:~4.0.0, xtend@npm:~4.0.1":
4253942527
version: 4.0.2
4254042528
resolution: "xtend@npm:4.0.2"

0 commit comments

Comments
 (0)