Skip to content

Commit b54f2e2

Browse files
Moving xmlGetAttribute out of Xslt class.
1 parent 2a8601e commit b54f2e2

File tree

2 files changed

+52
-46
lines changed

2 files changed

+52
-46
lines changed

src/dom/xml-functions.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import he from 'he';
2+
13
import {
24
DOM_ATTRIBUTE_NODE,
35
DOM_CDATA_SECTION_NODE,
@@ -7,6 +9,8 @@ import {
79
DOM_ELEMENT_NODE,
810
DOM_TEXT_NODE
911
} from '../constants';
12+
import { domGetAttribute } from './functions';
13+
import { XNode } from './xnode';
1014

1115
// Returns the text value of a node; for nodes without children this
1216
// is the nodeValue, for nodes with children this is the concatenation
@@ -126,6 +130,28 @@ function xmlEscapeAttr(s: string) {
126130
return xmlEscapeText(s).replace(/"/g, '"');
127131
}
128132

133+
/**
134+
* Wrapper function to access attribute values of template element
135+
* nodes. Currently this calls he.decode because in some DOM
136+
* implementations the return value of node.getAttributeValue()
137+
* contains unresolved XML entities, although the DOM spec requires
138+
* that entity references are resolved by the DOM.
139+
* @param node TODO
140+
* @param name TODO
141+
* @returns TODO
142+
*/
143+
export function xmlGetAttribute(node: XNode, name: string) {
144+
// TODO(mesch): This should not be necessary if the DOM is working
145+
// correctly. The DOM is responsible for resolving entities, not the
146+
// application.
147+
const value = domGetAttribute(node, name);
148+
if (value) {
149+
return he.decode(value);
150+
}
151+
152+
return value;
153+
}
154+
129155
/**
130156
* Wrapper function to access the owner document uniformly for document
131157
* and other nodes: for the document node, the owner document is the

src/xslt.ts

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
//
3434
//
3535
// Original author: Steffen Meschkat <mesch@google.com>
36-
import he from 'he';
3736

3837
import {
3938
XDocument,
@@ -46,6 +45,7 @@ import {
4645
domCreateTextNode,
4746
domGetAttribute,
4847
domSetAttribute,
48+
xmlGetAttribute,
4949
xmlOwnerDocument,
5050
xmlText,
5151
xmlValue
@@ -99,7 +99,7 @@ export class Xslt {
9999
* @param output the root of the generated output, as DOM node.
100100
* @param _parameters Extra parameters.
101101
*/
102-
protected xsltProcessContext(input: ExprContext, template: XNode, output: XNode, _parameters?: any) {
102+
protected xsltProcessContext(input: ExprContext, template: XNode, output: XNode, _parameters?: { [key: string]: any }) {
103103
const outputDocument = xmlOwnerDocument(output);
104104

105105
if (!this.isXsltElement(template)) {
@@ -125,7 +125,7 @@ export class Xslt {
125125
case 'apply-imports':
126126
throw `not implemented: ${template.localName}`;
127127
case 'apply-templates':
128-
select = this.xmlGetAttribute(template, 'select');
128+
select = xmlGetAttribute(template, 'select');
129129
if (select) {
130130
nodes = this.xPath.xPathEval(select, input).nodeSetValue();
131131
} else {
@@ -136,7 +136,7 @@ export class Xslt {
136136
this.xsltWithParam(sortContext, template);
137137
this.xsltSort(sortContext, template);
138138

139-
mode = this.xmlGetAttribute(template, 'mode');
139+
mode = xmlGetAttribute(template, 'mode');
140140
top = template.ownerDocument.documentElement;
141141

142142
templates = [];
@@ -165,7 +165,7 @@ export class Xslt {
165165
}
166166
break;
167167
case 'attribute':
168-
nameexpr = this.xmlGetAttribute(template, 'name');
168+
nameexpr = xmlGetAttribute(template, 'name');
169169
name = this.xsltAttributeValue(nameexpr, input);
170170
node = domCreateDocumentFragment(outputDocument);
171171
this.xsltChildNodes(input, template, node);
@@ -175,7 +175,7 @@ export class Xslt {
175175
case 'attribute-set':
176176
throw `not implemented: ${template.localName}`;
177177
case 'call-template':
178-
name = this.xmlGetAttribute(template, 'name');
178+
name = xmlGetAttribute(template, 'name');
179179
top = template.ownerDocument.documentElement;
180180

181181
paramContext = input.clone();
@@ -210,7 +210,7 @@ export class Xslt {
210210
}
211211
break;
212212
case 'copy-of':
213-
select = this.xmlGetAttribute(template, 'select');
213+
select = xmlGetAttribute(template, 'select');
214214
value = this.xPath.xPathEval(select, input);
215215
if (value.type == 'node-set') {
216216
nodes = value.nodeSetValue();
@@ -225,7 +225,7 @@ export class Xslt {
225225
case 'decimal-format':
226226
throw `not implemented: ${template.localName}`;
227227
case 'element':
228-
nameexpr = this.xmlGetAttribute(template, 'name');
228+
nameexpr = xmlGetAttribute(template, 'name');
229229
name = this.xsltAttributeValue(nameexpr, input);
230230
node = domCreateElement(outputDocument, name);
231231
domAppendChild(output, node);
@@ -237,7 +237,7 @@ export class Xslt {
237237
this.xsltForEach(input, template, output);
238238
break;
239239
case 'if':
240-
test = this.xmlGetAttribute(template, 'test');
240+
test = xmlGetAttribute(template, 'test');
241241
if (this.xPath.xPathEval(test, input).booleanValue()) {
242242
this.xsltChildNodes(input, template, output);
243243
}
@@ -276,7 +276,7 @@ export class Xslt {
276276
this.xsltChildNodes(input, template, output);
277277
break;
278278
case 'template':
279-
match = this.xmlGetAttribute(template, 'match');
279+
match = xmlGetAttribute(template, 'match');
280280
if (match && this.xsltMatch(match, input)) {
281281
this.xsltChildNodes(input, template, output);
282282
}
@@ -287,7 +287,7 @@ export class Xslt {
287287
output.appendChild(node);
288288
break;
289289
case 'value-of':
290-
select = this.xmlGetAttribute(template, 'select');
290+
select = xmlGetAttribute(template, 'select');
291291
value = this.xPath.xPathEval(select, input).stringValue();
292292
node = domCreateTextNode(outputDocument, value);
293293
output.appendChild(node);
@@ -318,7 +318,7 @@ export class Xslt {
318318
*/
319319
protected xsltWithParam(input: any, template: any) {
320320
for (const c of template.childNodes) {
321-
if (c.nodeType == DOM_ELEMENT_NODE && this.isXsltElement(c, 'with-param')) {
321+
if (c.nodeType === DOM_ELEMENT_NODE && this.isXsltElement(c, 'with-param')) {
322322
this.xsltVariable(input, c, true);
323323
}
324324
}
@@ -338,10 +338,10 @@ export class Xslt {
338338

339339
for (const c of template.childNodes) {
340340
if (c.nodeType == DOM_ELEMENT_NODE && this.isXsltElement(c, 'sort')) {
341-
const select = this.xmlGetAttribute(c, 'select');
341+
const select = xmlGetAttribute(c, 'select');
342342
const expr = this.xPath.xPathParse(select);
343-
const type = this.xmlGetAttribute(c, 'data-type') || 'text';
344-
const order = this.xmlGetAttribute(c, 'order') || 'ascending';
343+
const type = xmlGetAttribute(c, 'data-type') || 'text';
344+
const order = xmlGetAttribute(c, 'order') || 'ascending';
345345
sort.push({
346346
expr,
347347
type,
@@ -361,9 +361,9 @@ export class Xslt {
361361
// case. I.e. decides if this is a default value or a local
362362
// value. xsl:variable and xsl:with-param override; xsl:param doesn't.
363363

364-
protected xsltVariable(input, template, override) {
365-
const name = this.xmlGetAttribute(template, 'name');
366-
const select = this.xmlGetAttribute(template, 'select');
364+
protected xsltVariable(input: any, template: any, override: any) {
365+
const name = xmlGetAttribute(template, 'name');
366+
const select = xmlGetAttribute(template, 'select');
367367

368368
let value;
369369

@@ -385,12 +385,14 @@ export class Xslt {
385385
// Implements xsl:chose and its child nodes xsl:when and
386386
// xsl:otherwise.
387387

388-
protected xsltChoose(input, template, output) {
388+
protected xsltChoose(input: any, template: any, output: any) {
389389
for (const childNode of template.childNodes) {
390-
if (childNode.nodeType != DOM_ELEMENT_NODE) {
390+
if (childNode.nodeType !== DOM_ELEMENT_NODE) {
391391
continue;
392-
} else if (this.isXsltElement(childNode, 'when')) {
393-
const test = this.xmlGetAttribute(childNode, 'test');
392+
}
393+
394+
if (this.isXsltElement(childNode, 'when')) {
395+
const test = xmlGetAttribute(childNode, 'test');
394396
if (this.xPath.xPathEval(test, input).booleanValue()) {
395397
this.xsltChildNodes(input, childNode, output);
396398
break;
@@ -408,8 +410,8 @@ export class Xslt {
408410
* @param template TODO
409411
* @param output TODO
410412
*/
411-
protected xsltForEach(input, template, output) {
412-
const select = this.xmlGetAttribute(template, 'select');
413+
protected xsltForEach(input: any, template: any, output: any) {
414+
const select = xmlGetAttribute(template, 'select');
413415
const nodes = this.xPath.xPathEval(select, input).nodeSetValue();
414416
const sortContext = input.clone(nodes[0], 0, nodes);
415417
this.xsltSort(sortContext, template);
@@ -538,28 +540,6 @@ export class Xslt {
538540
return ret;
539541
}
540542

541-
/**
542-
* Wrapper function to access attribute values of template element
543-
// nodes. Currently this calls he.decode because in some DOM
544-
// implementations the return value of node.getAttributeValue()
545-
// contains unresolved XML entities, although the DOM spec requires
546-
// that entity references are resolved by the DOM.
547-
* @param node TODO
548-
* @param name TODO
549-
* @returns TODO
550-
*/
551-
protected xmlGetAttribute(node: XNode, name: string) {
552-
// TODO(mesch): This should not be necessary if the DOM is working
553-
// correctly. The DOM is responsible for resolving entities, not the
554-
// application.
555-
const value = domGetAttribute(node, name);
556-
if (value) {
557-
return he.decode(value);
558-
}
559-
560-
return value;
561-
}
562-
563543
/**
564544
* Implements xsl:copy-of for node-set values of the select
565545
// expression. Recurses down the source node tree, which is part of

0 commit comments

Comments
 (0)