Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/oozcitak/xmlbuilder2
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Apr 2, 2022
2 parents fe8e3d4 + dd2623a commit 9605208
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/pages/using-with-external-libraries.md
Expand Up @@ -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 %}
{% include warning.html content=ts_tip %}
7 changes: 7 additions & 0 deletions src/interfaces.ts
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -116,6 +122,7 @@ export const DefaultBuilderOptions: XMLBuilderOptions = {
keepNullNodes: false,
keepNullAttributes: false,
ignoreConverters: false,
skipWhitespaceOnlyText: true,
convert: {
att: "@",
ins: "?",
Expand Down
4 changes: 3 additions & 1 deletion src/readers/XMLReader.ts
Expand Up @@ -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"
Expand All @@ -20,7 +21,7 @@ export class XMLReader extends BaseReader<string> {
* @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
Expand Down Expand Up @@ -62,6 +63,7 @@ export class XMLReader extends BaseReader<string> {
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 = <TextToken>token
context = this.text(context, this._decodeText(this.sanitize(text.data))) || context
break
Expand Down
27 changes: 27 additions & 0 deletions test/readers/XMLReader.custom.test.ts
Expand Up @@ -91,4 +91,31 @@ describe('custom XMLReader', () => {
})
})

test("skip whitespace only text", () => {
const xml = $$.t`
<?xml version="1.0"?>
<root>
<ele> </ele>
</root>`

const doc = $$.create({ skipWhitespaceOnlyText: true }, xml).doc()
expect(doc.end()).toBe(`<?xml version="1.0"?><root><ele/></root>`)
})

test("retain whitespace only text", () => {
const xml = $$.t`
<?xml version="1.0"?>
<root>
<ele> </ele>
</root>`

const doc = $$.create({ skipWhitespaceOnlyText: false }, xml).doc()

expect(doc.end()).toBe(
$$.t`
<?xml version="1.0"?><root>
<ele> </ele>
</root>
`)
})
})

0 comments on commit 9605208

Please sign in to comment.