Skip to content

Handle style values containing ; - #370

Open
alphaNumericEntity wants to merge 1 commit into
WebReflection:mainfrom
alphaNumericEntity:fix/style-semicolon-in-value
Open

alphaNumericEntity wants to merge 1 commit into
WebReflection:mainfrom
alphaNumericEntity:fix/style-semicolon-in-value

Conversation

@alphaNumericEntity

Copy link
Copy Markdown

A ; inside an inline style value cuts the declaration in two. This happens with data URIs (url(data:image/png;base64,...)) and with quoted strings (content: "a;b", font-family: "a;b", serif). Reading the value gives back only the part before the ;, and setting any other property rewrites the attribute from that truncated parse, so the rest of the value is lost:

const {document} = parseHTML('<!doctype html><html><body><div style="background-image: url(data:image/png;base64,AAAA); color: blue"></div></body></html>');
const div = document.querySelector('div');

div.style.backgroundImage;
// url(data:image/png

div.style.color = 'red';
div.getAttribute('style');
// background-image:url(data:image/png;color:red

The cause is updateKeys in esm/interface/css-style-declaration.js, which splits the attribute with split(/\s*;\s*/). #129 fixed the same problem for : (as in url(https://...)); this is the ; half.

The fix replaces the regex split with a small splitRules helper that walks the string once and splits on ; only outside quotes, parentheses and /* */ comments, skipping backslash-escaped characters. Keys and values are still trimmed as before, so a style string with no quotes, parentheses, backslashes or comments parses exactly as it did.

Testing: new cases in test/interface/css-style-declaration.js cover a data URI (read, length, and the attribute after setting another property), ; inside double and single quotes, a stray ), a quote inside a comment, and a /* inside url() (not a comment there). The data URI case fails on main with Got instead: url(data:image/png. npm test passes with coverage unchanged at 100%, and cjs/ is regenerated with npm run cjs.

Inline style declarations were split on every `;`, so a value such as `url(data:image/png;base64,...)` or `"a;b"` was cut in two. Reading it returned `url(data:image/png`, and setting any other property rewrote the attribute without the rest of the value.

Declarations are now split only on a `;` outside quotes, parentheses and comments.
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.

1 participant