Skip to content

Commit 2a8601e

Browse files
- Moving XML methods out of util.ts;
- `XNode` will work with `transformed` values.
1 parent f06a049 commit 2a8601e

20 files changed

+209
-179
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ module.exports = {
8888
'no-bitwise': 'error',
8989
'no-buffer-constructor': 'error',
9090
'no-caller': 'error',
91+
'no-case-declarations': 'off',
9192
'no-catch-shadow': 'error',
9293
'no-confusing-arrow': 'error',
9394
'no-console': 'warn',

src/dom/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './functions';
22
export * from './xdocument';
3+
export * from './xml-functions';
34
export * from './xnode';

src/dom/util.ts

Lines changed: 12 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,8 @@
88
// Dummy implmentation for the logging functions. Replace by something
99
// useful when you want to debug.
1010

11-
import {
12-
DOM_ATTRIBUTE_NODE,
13-
DOM_CDATA_SECTION_NODE,
14-
DOM_COMMENT_NODE,
15-
DOM_DOCUMENT_FRAGMENT_NODE,
16-
DOM_DOCUMENT_NODE,
17-
DOM_ELEMENT_NODE,
18-
DOM_TEXT_NODE
19-
} from '../constants';
11+
2012
import { FunctionCallExpr, BinaryExpr, UnaryMinusExpr, NumberExpr } from '../xpath/expressions';
21-
import { XDocument } from './xdocument';
22-
import { XNode } from './xnode';
2313

2414
// Throws an exception if false.
2515
export function assert(b) {
@@ -83,132 +73,6 @@ export function copyArrayIgnoringAttributesWithoutValue(dst, src) {
8373
}
8474
}
8575

86-
// Returns the text value of a node; for nodes without children this
87-
// is the nodeValue, for nodes with children this is the concatenation
88-
// of the value of all children. Browser-specific optimizations are used by
89-
// default; they can be disabled by passing "true" in as the second parameter.
90-
export function xmlValue(node: any, disallowBrowserSpecificOptimization: boolean = false) {
91-
if (!node) {
92-
return '';
93-
}
94-
95-
let ret = '';
96-
if (node.nodeType == DOM_TEXT_NODE || node.nodeType == DOM_CDATA_SECTION_NODE) {
97-
ret += node.nodeValue;
98-
} else if (node.nodeType == DOM_ATTRIBUTE_NODE) {
99-
ret += node.nodeValue;
100-
} else if (
101-
node.nodeType == DOM_ELEMENT_NODE ||
102-
node.nodeType == DOM_DOCUMENT_NODE ||
103-
node.nodeType == DOM_DOCUMENT_FRAGMENT_NODE
104-
) {
105-
if (!disallowBrowserSpecificOptimization) {
106-
// IE, Safari, Opera, and friends
107-
const innerText = node.innerText;
108-
if (innerText != undefined) {
109-
return innerText;
110-
}
111-
// Firefox
112-
const textContent = node.textContent;
113-
if (textContent != undefined) {
114-
return textContent;
115-
}
116-
}
117-
// pobrecito!
118-
const len = node.childNodes.length;
119-
for (let i = 0; i < len; ++i) {
120-
ret += xmlValue(node.childNodes[i]);
121-
}
122-
}
123-
return ret;
124-
}
125-
126-
// Returns the representation of a node as XML text.
127-
export function xmlText(node: any, opt_cdata: boolean = false) {
128-
const buf = [];
129-
xmlTextR(node, buf, opt_cdata);
130-
return buf.join('');
131-
}
132-
133-
function xmlTextR(node, buf, cdata) {
134-
if (node.nodeType == DOM_TEXT_NODE) {
135-
buf.push(xmlEscapeText(node.nodeValue));
136-
} else if (node.nodeType == DOM_CDATA_SECTION_NODE) {
137-
if (cdata) {
138-
buf.push(node.nodeValue);
139-
} else {
140-
buf.push(`<![CDATA[${node.nodeValue}]]>`);
141-
}
142-
} else if (node.nodeType == DOM_COMMENT_NODE) {
143-
buf.push(`<!--${node.nodeValue}-->`);
144-
} else if (node.nodeType == DOM_ELEMENT_NODE) {
145-
buf.push(`<${xmlFullNodeName(node)}`);
146-
for (let i = 0; i < node.attributes.length; ++i) {
147-
const a = node.attributes[i];
148-
if (a && a.nodeName && a.nodeValue) {
149-
buf.push(` ${xmlFullNodeName(a)}="${xmlEscapeAttr(a.nodeValue)}"`);
150-
}
151-
}
152-
153-
if (node.childNodes.length == 0) {
154-
buf.push('/>');
155-
} else {
156-
buf.push('>');
157-
for (let i = 0; i < node.childNodes.length; ++i) {
158-
xmlTextR(node.childNodes[i], buf, cdata);
159-
}
160-
buf.push(`</${xmlFullNodeName(node)}>`);
161-
}
162-
} else if (node.nodeType == DOM_DOCUMENT_NODE || node.nodeType == DOM_DOCUMENT_FRAGMENT_NODE) {
163-
for (let i = 0; i < node.childNodes.length; ++i) {
164-
xmlTextR(node.childNodes[i], buf, cdata);
165-
}
166-
}
167-
}
168-
169-
function xmlFullNodeName(n) {
170-
if (n.prefix && n.nodeName.indexOf(`${n.prefix}:`) != 0) {
171-
return `${n.prefix}:${n.nodeName}`;
172-
} else {
173-
return n.nodeName;
174-
}
175-
}
176-
177-
// Escape XML special markup chracters: tag delimiter < > and entity
178-
// reference start delimiter &. The escaped string can be used in XML
179-
// text portions (i.e. between tags).
180-
export function xmlEscapeText(s) {
181-
return `${s}`
182-
.replace(/&/g, '&amp;')
183-
.replace(/&amp;amp;/g, '&amp;')
184-
.replace(/</g, '&lt;')
185-
.replace(/>/g, '&gt;');
186-
}
187-
188-
// Escape XML special markup characters: tag delimiter < > entity
189-
// reference start delimiter & and quotes ". The escaped string can be
190-
// used in double quoted XML attribute value portions (i.e. in
191-
// attributes within start tags).
192-
function xmlEscapeAttr(s) {
193-
return xmlEscapeText(s).replace(/"/g, '&quot;');
194-
}
195-
196-
/**
197-
* Wrapper function to access the owner document uniformly for document
198-
* and other nodes: for the document node, the owner document is the
199-
* node itself, for all others it's the ownerDocument property.
200-
*
201-
* @param {Node} node
202-
* @return {Document}
203-
*/
204-
export function xmlOwnerDocument(node: any) {
205-
if (node.nodeType == DOM_DOCUMENT_NODE) {
206-
return node;
207-
}
208-
209-
return node.ownerDocument;
210-
}
211-
21276
/**
21377
* Escape the special regular expression characters when the regular expression
21478
* is specified as a string.
@@ -265,9 +129,13 @@ function exprReturnsNumberValue(expr) {
265129
round: true
266130
};
267131
return isMember[(expr as any).name.value];
268-
} else if (expr instanceof UnaryMinusExpr) {
132+
}
133+
134+
if (expr instanceof UnaryMinusExpr) {
269135
return true;
270-
} else if (expr instanceof BinaryExpr) {
136+
}
137+
138+
if (expr instanceof BinaryExpr) {
271139
let isMember = {
272140
'+': true,
273141
'-': true,
@@ -276,15 +144,18 @@ function exprReturnsNumberValue(expr) {
276144
div: true
277145
};
278146
return isMember[expr.op.value];
279-
} else if (expr instanceof NumberExpr) {
147+
}
148+
149+
if (expr instanceof NumberExpr) {
280150
return true;
281151
}
152+
282153
return false;
283154
}
284155

285156
// (viat) given an XNode (see dom.js), returns an object mapping prefixes to their corresponding namespaces in its scope.
286157
// default namespace is treated as if its prefix were the empty string.
287-
export function namespaceMapAt(node) {
158+
export function namespaceMapAt(node: any) {
288159
const map = {
289160
// reserved namespaces https://www.w3.org/TR/REC-xml-names/#xmlReserved
290161
xmlns: 'http://www.w3.org/2000/xmlns/',

src/dom/xdocument.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
import { DOM_ATTRIBUTE_NODE, DOM_CDATA_SECTION_NODE, DOM_COMMENT_NODE, DOM_DOCUMENT_FRAGMENT_NODE, DOM_DOCUMENT_NODE, DOM_ELEMENT_NODE, DOM_TEXT_NODE } from "../constants";
2-
import { XNode } from "./xnode";
1+
import {
2+
DOM_ATTRIBUTE_NODE,
3+
DOM_CDATA_SECTION_NODE,
4+
DOM_COMMENT_NODE,
5+
DOM_DOCUMENT_FRAGMENT_NODE,
6+
DOM_DOCUMENT_NODE,
7+
DOM_ELEMENT_NODE,
8+
DOM_TEXT_NODE
9+
} from '../constants';
10+
import { XNode } from './xnode';
311

412
export class XDocument extends XNode {
513
documentElement: any;
@@ -16,40 +24,40 @@ export class XDocument extends XNode {
1624
this.documentElement = null;
1725
}
1826

19-
appendChild(node) {
27+
appendChild(node: any) {
2028
super.appendChild(node);
2129
this.documentElement = this.childNodes[0];
2230
}
2331

24-
createElement(name) {
32+
createElement(name: any) {
2533
return XNode.create(DOM_ELEMENT_NODE, name, null, this);
2634
}
2735

28-
createElementNS(namespace, name) {
36+
createElementNS(namespace: any, name: any) {
2937
return XNode.create(DOM_ELEMENT_NODE, name, null, this, namespace);
3038
}
3139

3240
createDocumentFragment() {
3341
return XNode.create(DOM_DOCUMENT_FRAGMENT_NODE, '#document-fragment', null, this);
3442
}
3543

36-
createTextNode(value) {
44+
createTextNode(value: any) {
3745
return XNode.create(DOM_TEXT_NODE, '#text', value, this);
3846
}
3947

40-
createAttribute(name) {
48+
createAttribute(name: any) {
4149
return XNode.create(DOM_ATTRIBUTE_NODE, name, null, this);
4250
}
4351

44-
createAttributeNS(namespace, name) {
52+
createAttributeNS(namespace: any, name: any) {
4553
return XNode.create(DOM_ATTRIBUTE_NODE, name, null, this, namespace);
4654
}
4755

48-
createComment(data) {
56+
createComment(data: any) {
4957
return XNode.create(DOM_COMMENT_NODE, '#comment', data, this);
5058
}
5159

52-
createCDATASection(data) {
60+
createCDATASection(data: any) {
5361
return XNode.create(DOM_CDATA_SECTION_NODE, '#cdata-section', data, this);
5462
}
5563
}

0 commit comments

Comments
 (0)