Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow skipWhitespaceOnlyText lexer option to be configurable #101

Merged
merged 2 commits into from
Apr 2, 2022

Conversation

shanekenney
Copy link
Contributor

@shanekenney shanekenney commented Aug 10, 2021

I have a use case where I need whitespace inside elements to be preserved when parsing an XML string. Currently whitespace only text nodes are skipped due to the hardcoded configuration supplied to the XMLStringLexer. Ideally I would like this to be configurable. This PR shows what I have in mind, but I would appreciate feedback if there's a solution that would be more appropriate.

Example

create(`<Element> </Element>`)

Currently this results in XML equivalent to the following with significant whitespace removed:

<Element></Element>

What I would like is to preserve the whitespace inside an element.

<Element> </Element>

I'm dealing with a rich text format where it's possible I'll encounter significant whitespace within elements and currently this is being lost.

Example

<Paragraph><Run format="bold">Hello</Run><Run> </Run><Run format="bold">world</Run></Paragraph>

Type of change:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

@oozcitak
Copy link
Owner

Hey, thanks for that. Can you add some tests as well?

@@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to prevent text nodes from being created within a document node.

@shanekenney
Copy link
Contributor Author

@oozcitak I've added some tests. With skipWhitespaceOnlyText=false there was an issue creating text nodes within the document node so I'm skipping those cases. There's probably an argument to be made for only skipping whitespace text though? Let me know what you think.

@gapspt
Copy link

gapspt commented Mar 28, 2022

Thank you @shanekenney, I also need this functionality ^_^

It seems to be related to #121 (and #122). @oozcitak any chance on getting this merged? Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants