Skip to content

Commit a37a6f3

Browse files
Adding parent to attribute nodes.
1 parent 2ea1b43 commit a37a6f3

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

src/dom/functions.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,28 +85,28 @@ export function domCreateDTDSection(doc: XDocument, data: any) {
8585
* @author Steffen Meschkat <mesch@google.com>
8686
*/
8787
export function xmlParse(xml: string): XDocument {
88-
const regex_empty = /\/$/;
88+
const regexEmpty = /\/$/;
8989

90-
let regex_tagname;
91-
let regex_attribute;
90+
let regexTagname;
91+
let regexAttribute;
9292
if (xml.match(/^<\?xml/)) {
9393
// When an XML document begins with an XML declaration
9494
// VersionInfo must appear.
9595
if (xml.search(new RegExp(XML10_VERSION_INFO)) == 5) {
96-
regex_tagname = XML10_TAGNAME_REGEXP;
97-
regex_attribute = XML10_ATTRIBUTE_REGEXP;
96+
regexTagname = XML10_TAGNAME_REGEXP;
97+
regexAttribute = XML10_ATTRIBUTE_REGEXP;
9898
} else if (xml.search(new RegExp(XML11_VERSION_INFO)) == 5) {
99-
regex_tagname = XML11_TAGNAME_REGEXP;
100-
regex_attribute = XML11_ATTRIBUTE_REGEXP;
99+
regexTagname = XML11_TAGNAME_REGEXP;
100+
regexAttribute = XML11_ATTRIBUTE_REGEXP;
101101
} else {
102102
// VersionInfo is missing, or unknown version number.
103103
// TODO : Fallback to XML 1.0 or XML 1.1, or just return null?
104104
throw new Error('VersionInfo is missing, or unknown version number.');
105105
}
106106
} else {
107107
// When an XML declaration is missing it's an XML 1.0 document.
108-
regex_tagname = XML10_TAGNAME_REGEXP;
109-
regex_attribute = XML10_ATTRIBUTE_REGEXP;
108+
regexTagname = XML10_TAGNAME_REGEXP;
109+
regexAttribute = XML10_ATTRIBUTE_REGEXP;
110110
}
111111

112112
const xmldoc = new XDocument();
@@ -137,14 +137,14 @@ export function xmlParse(xml: string): XDocument {
137137
// Ignore comments
138138
// console.log(`Ignored ${text}`);
139139
} else {
140-
const empty = text.match(regex_empty);
141-
const tagname = regex_tagname.exec(text)[1];
140+
const empty = text.match(regexEmpty);
141+
const tagname = regexTagname.exec(text)[1];
142142
let node = domCreateElement(xmldoc, tagname);
143143

144-
let att;
145-
while ((att = regex_attribute.exec(text))) {
146-
const val = he.decode(att[5] || att[7] || '');
147-
domSetAttribute(node, att[1], val);
144+
let attribute;
145+
while ((attribute = regexAttribute.exec(text))) {
146+
const val = he.decode(attribute[5] || attribute[7] || '');
147+
domSetAttribute(node, attribute[1], val);
148148
}
149149

150150
node.siblingPosition = parent.childNodes.length;

src/dom/xnode.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class XNode {
4646

4747
static _unusedXNodes: any[] = [];
4848

49-
constructor(type: any, name: any, opt_value: any, opt_owner: any, opt_namespace?: any) {
49+
constructor(type: any, name: string, opt_value: any, opt_owner: any, opt_namespace?: any) {
5050
this.id = Math.random() * (Number.MAX_SAFE_INTEGER - 1) + 1;
5151
this.attributes = [];
5252
this.childNodes = [];
@@ -151,7 +151,7 @@ export class XNode {
151151
node.init.call(0, '', '', null);
152152
}
153153

154-
static create(type: any, name: any, value: any, owner: any, namespace?: any): XNode {
154+
static create(type: any, name: string, value: any, owner: any, namespace?: any): XNode {
155155
if (this._unusedXNodes.length > 0) {
156156
const node = this._unusedXNodes.pop();
157157
node.init(type, name, value, owner, namespace);
@@ -333,15 +333,17 @@ export class XNode {
333333
return this.attributes.length > 0;
334334
}
335335

336-
setAttribute(name: any, value: any) {
336+
setAttribute(name: string, value: any) {
337337
for (let i = 0; i < this.attributes.length; ++i) {
338338
if (this.attributes[i].nodeName == name) {
339339
this.attributes[i].nodeValue = `${value}`;
340340
return;
341341
}
342342
}
343343

344-
this.attributes.push(XNode.create(DOM_ATTRIBUTE_NODE, name, value, this));
344+
const newAttribute = XNode.create(DOM_ATTRIBUTE_NODE, name, value, this);
345+
newAttribute.parentNode = this;
346+
this.attributes.push(newAttribute);
345347
}
346348

347349
setTransformedAttribute(name: string, value: any) {
@@ -356,6 +358,7 @@ export class XNode {
356358
const newAttribute = XNode.create(DOM_ATTRIBUTE_NODE, name, value, this);
357359
newAttribute.transformedNodeName = name;
358360
newAttribute.transformedNodeValue = value;
361+
newAttribute.parentNode = this;
359362
this.transformedAttributes.push(newAttribute);
360363
}
361364

0 commit comments

Comments
 (0)