fast-xml-parser icon indicating copy to clipboard operation
fast-xml-parser copied to clipboard

How to whitelist characters in attribute name?

Open UltraCakeBakery opened this issue 2 years ago • 8 comments

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

UltraCakeBakery avatar Sep 30 '23 17:09 UltraCakeBakery

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

amitguptagwl avatar Oct 01 '23 01:10 amitguptagwl

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 avatar Oct 01 '23 12:10 UltraCakeBakery

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.

UltraCakeBakery avatar Oct 01 '23 12:10 UltraCakeBakery

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

amitguptagwl avatar Oct 02 '23 12:10 amitguptagwl

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,
}

UltraCakeBakery avatar Oct 03 '23 18:10 UltraCakeBakery

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

amitguptagwl avatar Oct 04 '23 14:10 amitguptagwl

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

{ allowBooleanAttributes: true }

UltraCakeBakery avatar Oct 05 '23 21:10 UltraCakeBakery

pass false as 2nd parameter and try

amitguptagwl avatar Oct 13 '23 15:10 amitguptagwl