Skip to content

feat: support equals signs in \htmlData attributes#4109

Closed
AljoschaMeyer wants to merge 4 commits intoKaTeX:mainfrom
AljoschaMeyer:main
Closed

feat: support equals signs in \htmlData attributes#4109
AljoschaMeyer wants to merge 4 commits intoKaTeX:mainfrom
AljoschaMeyer:main

Conversation

@AljoschaMeyer
Copy link
Copy Markdown
Contributor

@AljoschaMeyer AljoschaMeyer commented Nov 21, 2025

What is the previous behavior before this PR?

\htmlData{foo=<value>}{} results in a parse error if <value> contains equals signs.

What is the new behavior after this PR?

All equals signs but the first (which marks where the attribute name ends and the value starts) are ignored. Values can contain equals signs now.

Closes #4108


Did this in the Github WebUI, I'm unable to run the yarn tests locally =/

@AljoschaMeyer AljoschaMeyer changed the title feat: support equals sign in data attributes feat: support equals signs in \htmlData attributes Nov 21, 2025
Copy link
Copy Markdown
Member

@edemaine edemaine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this!

const keyVal = data[i].split("=");
if (keyVal.length !== 2) {
const keyVal = data[i].split(/=(.*)/s);
if (keyVal.length !== 3) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a weird use of split to me, though it does appear to work, provided there's no newline in the value (. doesn't match newline). Perhaps it would make more sense to do a regular expression match? Something like const match = data[i].match(/^([^=]+)=([^]*)$/). This also makes it easy to correctly reject an empty key.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had assumed an empty key should be allowed, since data attributes are allowed to be empty. Do you prefer a different syntax for setting empty data attributes?

Newlines are matched already of the /s modifier.

As for using split like this, that was simply the first result of a web search for splitting a string into two parts in js: https://stackoverflow.com/questions/4607745/split-string-only-on-first-instance-of-specified-character/4607799#4607799

I'll wait for your response and then implement whichever variant you prefer =)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, TIL /s option!

I guess while we're debating the best form, the indexOf plus two slices would actually be much more efficient (as discussed on that StackOverflow page), so maybe we should go for that. Allowing an empty key (so data- attribute) is fine I guess; seems we currently allow it, so probably better to still allow it.

Now handles inputs with zero equals signs. And I deleted the prior logic...
@AljoschaMeyer
Copy link
Copy Markdown
Contributor Author

Apologies, I'm doing things with git that are not very clever... Will open a new PR from a clean branch with the commits squashed.

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

Successfully merging this pull request may close these issues.

Data attributes via \htmlData cannot contain equals signs

2 participants