xmlbuilder2
xmlbuilder2 copied to clipboard
Adjustment to spaceBeforeSlash to add a spaceBeforeSlash only if the tag has attributes?
Is your feature request related to a problem? Please describe. Adjustment to spaceBeforeSlash to add a spaceBeforeSlash only if the tag has attributes
Describe the solution you'd like
<namespace key="0" case="first-letter" /> // space before slash here (okay)
<text bytes="0" sha1="phoiac9h4m842xq45sp7s6u21eteeq1" xml:space="preserve" /> // space before slash here (okay)
<minor/> // do not add a space before slash here (not possible yet)
Describe alternatives you've considered I can write my own post processing code to handle this, but I thought I'd mention it as a feature suggestion also.
Additional context
- Asking in MediaWiki Discord server why in Wikipedia/MediaWiki XML exports, some tags have spaces before slash, and some don't, consensus was:
"space before slash if the tag has attributes, otherwise no space"- I can't find any additional context to cite sources to further explain why I got this response, but I'm looking for further information. Actually, let me dig through the MediaWiki source code.
- https://github.com/wikimedia/mediawiki/blob/master/includes/export/XmlDumpWriter.php#L373
- https://github.com/wikimedia/mediawiki/blob/master/includes/export/XmlDumpWriter.php#L402
- I can't find any additional context to cite sources to further explain why I got this response, but I'm looking for further information. Actually, let me dig through the MediaWiki source code.
- Not quite relevant, but https://github.com/prettier/prettier/issues/1985
- https://stackoverflow.com/questions/462741/space-before-closing-slash
- https://w3.org/TR/xhtml1/#guidelines
- Note that this is compatibility for HTML, and not necessary otherwise
- C.2. Empty Elements: "
Include a space before the trailing / and > of empty elements, e.g. <br />, <hr /> and <img src="karen.jpg" alt="Karen" />. Also, use the minimized tag syntax for empty elements, e.g. <br />, as the alternative syntax <br></br> allowed by XML gives uncertain results in many existing user agents."
- https://w3.org/TR/xhtml1/#guidelines
- https://stackoverflow.com/questions/462741/space-before-closing-slash
- Also "https://github.com/prettier/plugin-xml/issues/11 might be illustrative of why it's futile to ask people to change their software for your absurd requirement"
Note that rather than this ""feature"" idea being built in to the library, I whipped up this code to match what appears to be hard-coded inconsistency in MediaWiki Special:Export XML dumps (and as far as this point in time, I haven't found any further inconsistency than these two instances: "minor/sha1" tags):
xmlbuilder2.convert(
{ keepEntityCharRefs: true },
xmlobj,
{ format: 'xml', prettyPrint: true, spaceBeforeSlash: true })
.replace(/<(minor|sha1) \/>/g, '<$1/>');