NaturalIntelligence / NaturalIntelligence/fast-xml-parser

Don't create text node for self closing tags when `alwaysCreateTextNode: true`

Open
#708 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
JavaScript
Stars
3.1k
Forks
395
Avg merge
1d 20h
Merged PRs (30d)
1

Description

Currently I can have the parser create a text node on tags without attributes by setting alwaysCreateTextNode: true. However, for self closing tags, I would like the parser to not create a text node. Right now it creates a text node set to an empty string.

I also noticed that if a self closing tags have any attributes, a text node is not created. It only seems like it's created when a self closing tags have no attributes.

// Given these settings:
const parser = new XMLParser({
	ignoreAttributes: false,
	allowBooleanAttributes: true,
	parseTagValue: false,
	alwaysCreateTextNode: true,
});

parser.parse("<workbookPr defaultThemeVersion="153222" />") 
// "workbookPr": {
//   "@_defaultThemeVersion": "153222"
// }

parser.parse("<workbookPr />") 
// "workbookPr": {
//   "#text": ""
// }

I expect that "#text" is always present, no matter if the self closing tag have attributes or not. But my primary expectation is to be able to have no text nodes at all for self closing tags while still allowing the parser to make text nodes for tags without any attributes.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Start by reproducing both parser.parse examples with alwaysCreateTextNode enabled, then trace the self-closing-tag handling from the parser.parse entry point. Done should be confirmed by tests covering self-closing tags with and without attributes and the intended text-node behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.