Skip to content

Commit 19b85e4

Browse files
Fixes in xsl:choose and xsl:copy-of + better docs (#90)
* Checking a corresponding `outputNode` in `xsltChoose` instead of using the `output` directly. * - Resolving predence with `copy-of`; - Improving `options.cData` documentation; - Escaping text under `CDATA` as especified by XSLT CDATA spec.
1 parent d1f2674 commit 19b85e4

File tree

20 files changed

+151
-60
lines changed

20 files changed

+151
-60
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const options = {
7474
const xslt = new Xslt(options);
7575
```
7676

77+
- `cData` (`boolean`, default `true`): resolves CDATA elements in the output. Content under CDATA is resolved as text. This overrides `escape` for CDATA content.
7778
- `escape` (`boolean`, default `true`): replaces symbols like `<`, `>`, `&` and `"` by the corresponding [XML entities](https://www.tutorialspoint.com/xml/xml_character_entities.htm).
7879
- `selfClosingTags` (`boolean`, default `true`): Self-closes tags that don't have inner elements, if `true`. For instance, `<test></test>` becomes `<test />`.
7980
- `outputMethod` (`string`, default `xml`): Specifies the default output method. if `<xsl:output>` is declared in your XSLT file, this will be overridden.

interactive-tests/js/simplelog.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Design Liquido
1+
// Copyright 2023-2024 Design Liquido
22
// Copyright 2018 Johannes Wilm
33
// Copyright 2005-2006 Google
44
//

interactive-tests/xpath.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<head>
33
<script type="application/javascript" src="js/xslt-processor.js"></script>
44
<script>
5-
// Copyright 2023 Design Liquido
5+
// Copyright 2023-2024 Design Liquido
66
// Copyright 2018 Johannes Wilm
77
// Copyright 2005 Google Inc.
88
// All Rights Reserved

src/dom/functions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Design Liquido
1+
// Copyright 2023-2024 Design Liquido
22
// Copyright 2018 Johannes Wilm
33
// Copyright 2005 Google Inc.
44
// All Rights Reserved

src/dom/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Design Liquido
1+
// Copyright 2023-2024 Design Liquido
22
// Copyright 2018 Johannes Wilm
33
// Copyright 2005 Google
44
//

src/dom/xml-functions.ts

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -77,35 +77,40 @@ export function xmlValue2(node: any, disallowBrowserSpecificOptimization: boolea
7777
return '';
7878
}
7979

80-
let ret = '';
81-
if (node.nodeType == DOM_TEXT_NODE || node.nodeType == DOM_CDATA_SECTION_NODE) {
82-
ret += node.nodeValue;
83-
} else if (node.nodeType == DOM_ATTRIBUTE_NODE) {
84-
ret += node.nodeValue;
85-
} else if (
86-
node.nodeType == DOM_ELEMENT_NODE ||
87-
node.nodeType == DOM_DOCUMENT_NODE ||
88-
node.nodeType == DOM_DOCUMENT_FRAGMENT_NODE
89-
) {
90-
if (!disallowBrowserSpecificOptimization) {
91-
// IE, Safari, Opera, and friends
92-
const innerText = node.innerText;
93-
if (innerText != undefined) {
94-
return innerText;
80+
let returnedXmlString = '';
81+
switch (node.nodeType) {
82+
case DOM_ATTRIBUTE_NODE:
83+
case DOM_TEXT_NODE:
84+
returnedXmlString += node.nodeValue;
85+
break;
86+
case DOM_CDATA_SECTION_NODE:
87+
returnedXmlString += node.nodeValue;
88+
break;
89+
case DOM_DOCUMENT_NODE:
90+
case DOM_DOCUMENT_FRAGMENT_NODE:
91+
case DOM_ELEMENT_NODE:
92+
if (!disallowBrowserSpecificOptimization) {
93+
// IE, Safari, Opera, and friends
94+
const innerText = node.innerText;
95+
if (innerText != undefined) {
96+
return innerText;
97+
}
98+
// Firefox
99+
const textContent = node.textContent;
100+
if (textContent != undefined) {
101+
return textContent;
102+
}
95103
}
96-
// Firefox
97-
const textContent = node.textContent;
98-
if (textContent != undefined) {
99-
return textContent;
104+
105+
const len = node.transformedChildNodes.length;
106+
for (let i = 0; i < len; ++i) {
107+
returnedXmlString += xmlValue(node.transformedChildNodes[i]);
100108
}
101-
}
102109

103-
const len = node.transformedChildNodes.length;
104-
for (let i = 0; i < len; ++i) {
105-
ret += xmlValue(node.transformedChildNodes[i]);
106-
}
110+
break;
107111
}
108-
return ret;
112+
113+
return returnedXmlString;
109114
}
110115

111116
/**
@@ -117,7 +122,7 @@ export function xmlValue2(node: any, disallowBrowserSpecificOptimization: boolea
117122
* @see xmlTransformedText
118123
*/
119124
export function xmlText(node: XNode, options: XmlOutputOptions = {
120-
cData: false,
125+
cData: true,
121126
escape: true,
122127
selfClosingTags: true,
123128
outputMethod: 'xml'
@@ -181,7 +186,7 @@ function xmlTextRecursive(node: XNode, buffer: string[], options: XmlOutputOptio
181186
export function xmlTransformedText(
182187
node: XNode,
183188
options: XmlOutputOptions = {
184-
cData: false,
189+
cData: true,
185190
escape: true,
186191
selfClosingTags: true,
187192
outputMethod: 'xml'
@@ -205,7 +210,7 @@ function xmlTransformedTextRecursive(node: XNode, buffer: any[], options: XmlOut
205210
}
206211
} else if (nodeType === DOM_CDATA_SECTION_NODE) {
207212
if (options.cData) {
208-
buffer.push(nodeValue);
213+
buffer.push(xmlEscapeText(nodeValue));
209214
} else {
210215
buffer.push(`<![CDATA[${nodeValue}]]>`);
211216
}

src/dom/xmltoken.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Design Liquido
1+
// Copyright 2023-2024 Design Liquido
22
// Copyright 2018 Johannes Wilm
33
// Copyright 2006 Google Inc.
44
// All Rights Reserved

src/xpath/xpath.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Design Liquido
1+
// Copyright 2023-2024 Design Liquido
22
// Copyright 2018 Johannes Wilm
33
// Copyright 2005 Google Inc.
44
// All Rights Reserved

src/xpathdebug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Design Liquido
1+
// Copyright 2023-2024 Design Liquido
22
// Copyright 2018 Johannes Wilm
33
// Copyright 2005 Google Inc.
44
// All Rights Reserved

src/xslt/xslt-options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { XsltParameter } from "./xslt-parameter"
22

33
export type XsltOptions = {
4+
cData: boolean,
45
escape: boolean,
56
selfClosingTags: boolean,
67
parameters?: XsltParameter[]

0 commit comments

Comments
 (0)