NaturalIntelligence / fast-xml-parser

Validate XML, Parse XML and Build XML rapidly without C/C++ based libraries and no callback.
https://naturalintelligence.github.io/fast-xml-parser/
MIT License
2.43k stars 297 forks source link

How to whitelist characters in attribute name? #614

Open UltraCakeBakery opened 9 months ago

UltraCakeBakery commented 9 months ago

I want to parse markup where there are $ and other characters in the attribute names. How can I add these characters? Is there already an option?

Right now the parser throws: Error: Attribute '$banana' is an invalid name

amitguptagwl commented 9 months ago

I believe the error must be thrown by the validator. So you can disable validator. Can you share your sample XML please?

UltraCakeBakery commented 9 months ago

I'm parsing a superset of HTML; see below for snippet.

The error is thrown by the util.js isName function, which is used by the parser. I do not see any option to disable validation. I do not use the validator explicitly. I'm just using the Parser and Builder.

<html>
<div $banana="🍌"></div>
</html>
UltraCakeBakery commented 9 months ago

For now I worked around this by modifying my node_modules, but that is not maintainable solution in the long run.

const nameStartChar = ':A-Za-z_\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD@\$'; // Note the additional @ and $ I added to fit my needs.

Preferably an option which I can pass to the XML parser gets added where I can specify more characters to be valid in the attribute name.

amitguptagwl commented 9 months ago

When you're calling parse method, are you passing any 2nd argument?

UltraCakeBakery commented 9 months ago

Yes;


const config = {
    allowBooleanAttributes: true,
    alwaysCreateTextNode: true,
    preserveOrder: true,
    parseAttributeValue: false,
    htmlEntities: false,
    isArray: true,
    attributeNamePrefix: '',
    ignoreAttributes: false,
    unpairedTags: ["hr", "br", "link", "meta"],
    stopNodes : [ "*.pre", "*.script"],
    processEntities: false,
}
amitguptagwl commented 9 months ago

You shared the config. I was asking what 2nd parameter you're passing in parse method?

UltraCakeBakery commented 9 months ago

Sorry, I misunderstood your question; this is what I pass to the second parameter in the parse method:

{ allowBooleanAttributes: true }

amitguptagwl commented 9 months ago

pass false as 2nd parameter and try