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

How to whitelist characters in attribute name? #614

Open
UltraCakeBakery opened this issue Sep 30, 2023 · 8 comments
Open

How to whitelist characters in attribute name? #614

UltraCakeBakery opened this issue Sep 30, 2023 · 8 comments

Comments

@UltraCakeBakery
Copy link

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
Copy link
Member

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

@UltraCakeBakery
Copy link
Author

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

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
Copy link
Member

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

@UltraCakeBakery
Copy link
Author

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
Copy link
Member

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

@UltraCakeBakery
Copy link
Author

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

{ allowBooleanAttributes: true }

@amitguptagwl
Copy link
Member

pass false as 2nd parameter and try

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

No branches or pull requests

3 participants
@amitguptagwl @UltraCakeBakery and others