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

innerHTML parser interprets / in unquoted element attributes incorrectly #999

Closed
maxmilton opened this issue Jul 23, 2023 · 0 comments · Fixed by #1020
Closed

innerHTML parser interprets / in unquoted element attributes incorrectly #999

maxmilton opened this issue Jul 23, 2023 · 0 comments · Fixed by #1020
Labels
bug Something isn't working

Comments

@maxmilton
Copy link
Contributor

Describe the bug
When setting el.innerHTML, the resulting DOM is incorrect when an element's attribute value is unquoted and contains a forward slash. Rather than the slash becoming part of the value, it's removed and the remaining value is tokenized into a separate attribute.

To Reproduce
Steps to reproduce the behavior:

  1. Create a test file; index.test.ts:
...

test('renders data-x attribute', () => {
  const container = document.createElement('div');

  container.innerHTML = '<div data-x=a/b></div>';

  expect(container.innerHTML).toBe('<div data-x="a/b"></div>');
});

test('renders href attribute', () => {
  const container = document.createElement('div');

  container.innerHTML = '<a href=https://example.com/foo/bar>link</a>';

  expect(container.innerHTML).toBe('<a href="https://example.com/foo/bar">link</a>');
});
  1. Run the tests.

Expected behavior
In the repro, both tests should pass. Forward slashes should be included as part of the attribute value.

Screenshots

Failing test output:

...

error: expect(received).toBe(expected)

Expected: "<div data-x=\"a/b\"></div>"
Received: "<div data-x=\"a\" b=\"\"></div>"

...

error: expect(received).toBe(expected)

Expected: "<a href=\"https://example.com/foo/bar\">link</a>"
Received: "<a href=\"https:\" example.com=\"\" foo=\"\" bar=\"\">link</a>"

Device:

  • OS: Linux 6.4.4-arch1-1 x86_64
  • happy-dom version: 10.5.2

Additional context
Since under the hood XMLParser is used to parse when setting innerHTML, there will be many other characters which exhibit the same behavior. However, forward slashes are the biggest impact for me as I'm running into issues with setting URLs in anchor href tags.

@maxmilton maxmilton added the bug Something isn't working label Jul 23, 2023
capricorn86 added a commit that referenced this issue Aug 18, 2023
capricorn86 added a commit that referenced this issue Aug 18, 2023
…nterprets-in-unquoted-element-attributes-incorrectly

#999@patch: Adds support for URLs in attributes that doesn't use apos…
capricorn86 added a commit that referenced this issue Aug 18, 2023
…nterprets-in-unquoted-element-attributes-incorrectly

#999@patch: Adds support for calc() in CSS values.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant