From d724b64fe5aa6a93e0df4e5678dbfc356ceb0282 Mon Sep 17 00:00:00 2001 From: Shane Kenney Date: Tue, 10 Aug 2021 10:56:40 +1000 Subject: [PATCH 1/4] Allow skipWhitespaceOnlyText lexer option to be configurable --- src/interfaces.ts | 7 +++++++ src/readers/XMLReader.ts | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/interfaces.ts b/src/interfaces.ts index 27c91a0..f8566d2 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -37,6 +37,12 @@ export interface XMLBuilderOptions { * objects to nodes */ ignoreConverters: boolean + + /** + * Whether whitespace-only text nodes are skipped or not. + */ + skipWhitespaceOnlyText: boolean + /** * Defines string keys used while converting JS objects to nodes. */ @@ -116,6 +122,7 @@ export const DefaultBuilderOptions: XMLBuilderOptions = { keepNullNodes: false, keepNullAttributes: false, ignoreConverters: false, + skipWhitespaceOnlyText: true, convert: { att: "@", ins: "?", diff --git a/src/readers/XMLReader.ts b/src/readers/XMLReader.ts index 052a9e8..b1b9f7c 100644 --- a/src/readers/XMLReader.ts +++ b/src/readers/XMLReader.ts @@ -20,7 +20,7 @@ export class XMLReader extends BaseReader { * @param str - XML document string to parse */ _parse(node: XMLBuilder, str: string): XMLBuilder { - const lexer = new XMLStringLexer(str, { skipWhitespaceOnlyText: true }) + const lexer = new XMLStringLexer(str, { skipWhitespaceOnlyText: this._builderOptions.skipWhitespaceOnlyText }) let lastChild = node let context = node From 89b68bb68bb108c26b46f9dfe585397e8581420a Mon Sep 17 00:00:00 2001 From: Shane Kenney Date: Thu, 12 Aug 2021 11:58:29 +1000 Subject: [PATCH 2/4] Tests for skipWhitespaceOnlyText --- src/readers/XMLReader.ts | 2 ++ test/readers/XMLReader.custom.test.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/readers/XMLReader.ts b/src/readers/XMLReader.ts index b1b9f7c..f64c95b 100644 --- a/src/readers/XMLReader.ts +++ b/src/readers/XMLReader.ts @@ -3,6 +3,7 @@ import { TokenType, DeclarationToken, DocTypeToken, CDATAToken, CommentToken, TextToken, PIToken, ElementToken } from "@oozcitak/dom/lib/parser/interfaces" +import { NodeType } from "@oozcitak/dom/lib/dom/interfaces" import { namespace as infraNamespace } from "@oozcitak/infra" import { namespace_extractQName } from "@oozcitak/dom/lib/algorithm" import { XMLBuilder, XMLBuilderOptions } from "../interfaces" @@ -62,6 +63,7 @@ export class XMLReader extends BaseReader { context = this.instruction(context, this.sanitize(pi.target), this.sanitize(pi.data)) || context break case TokenType.Text: + if(context.node.nodeType === NodeType.Document) break const text = token context = this.text(context, this._decodeText(this.sanitize(text.data))) || context break diff --git a/test/readers/XMLReader.custom.test.ts b/test/readers/XMLReader.custom.test.ts index 78fe541..ccb6927 100644 --- a/test/readers/XMLReader.custom.test.ts +++ b/test/readers/XMLReader.custom.test.ts @@ -91,4 +91,31 @@ describe('custom XMLReader', () => { }) }) + test("skip whitespace only text", () => { + const xml = $$.t` + + + + ` + + const doc = $$.create({ skipWhitespaceOnlyText: true }, xml).doc() + expect(doc.end()).toBe(``) + }) + + test("retain whitespace only text", () => { + const xml = $$.t` + + + + ` + + const doc = $$.create({ skipWhitespaceOnlyText: false }, xml).doc() + + expect(doc.end()).toBe( + $$.t` + + + + `) + }) }) From 4286fad9eda52afa0956f37209deac97e3c4dfed Mon Sep 17 00:00:00 2001 From: Michael Hadley Date: Tue, 19 Oct 2021 12:00:58 -0700 Subject: [PATCH 3/4] Fix typo in `using-with-external-libraries.md` --- docs/pages/using-with-external-libraries.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/pages/using-with-external-libraries.md b/docs/pages/using-with-external-libraries.md index 5d65f47..ab9f683 100644 --- a/docs/pages/using-with-external-libraries.md +++ b/docs/pages/using-with-external-libraries.md @@ -59,6 +59,6 @@ const result = xml.validate(xsd); // true ``` {% capture ts_tip %} - `XMLBuilder2` uses its own TypeScript interfaces for DOM nodes; which typically will not be compatible withe the interfaces exported by other libraries. TypeScript users should cast those interfaces accordingly to prevent TS 2345 errors. + `XMLBuilder2` uses its own TypeScript interfaces for DOM nodes; which typically will not be compatible with the interfaces exported by other libraries. TypeScript users should cast those interfaces accordingly to prevent TS 2345 errors. {% endcapture %} -{% include warning.html content=ts_tip %} \ No newline at end of file +{% include warning.html content=ts_tip %} From fec26640f0c89232a088e53ec37847b8590a0ee3 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 2 Apr 2022 09:11:07 +0000 Subject: [PATCH 4/4] Bump node-fetch from 2.6.1 to 2.6.7 Bumps [node-fetch](https://github.com/node-fetch/node-fetch) from 2.6.1 to 2.6.7. - [Release notes](https://github.com/node-fetch/node-fetch/releases) - [Commits](https://github.com/node-fetch/node-fetch/compare/v2.6.1...v2.6.7) --- updated-dependencies: - dependency-name: node-fetch dependency-type: indirect ... Signed-off-by: dependabot[bot] --- package-lock.json | 74 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index c38559d..117573c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8585,12 +8585,45 @@ "dev": true }, "node_modules/node-fetch": { - "version": "2.6.1", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", - "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==", + "version": "2.6.7", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", + "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", "dev": true, + "dependencies": { + "whatwg-url": "^5.0.0" + }, "engines": { "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } + } + }, + "node_modules/node-fetch/node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=", + "dev": true + }, + "node_modules/node-fetch/node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=", + "dev": true + }, + "node_modules/node-fetch/node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=", + "dev": true, + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" } }, "node_modules/node-int64": { @@ -19068,10 +19101,37 @@ "dev": true }, "node-fetch": { - "version": "2.6.1", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", - "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==", - "dev": true + "version": "2.6.7", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", + "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", + "dev": true, + "requires": { + "whatwg-url": "^5.0.0" + }, + "dependencies": { + "tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=", + "dev": true + }, + "webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=", + "dev": true + }, + "whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=", + "dev": true, + "requires": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + } + } }, "node-int64": { "version": "0.4.0",