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

SyntaxError: '#1' is not a valid selector. #123

Closed
ksbrooksjr opened this issue Feb 21, 2025 · 2 comments · Fixed by #125
Closed

SyntaxError: '#1' is not a valid selector. #123

ksbrooksjr opened this issue Feb 21, 2025 · 2 comments · Fixed by #125

Comments

@ksbrooksjr
Copy link

ksbrooksjr commented Feb 21, 2025

I noticed while running the js-framework-benchmark (which uses numeric IDs), that Idiomorph throws an error if it encounters an ID that begins with a number in certain scenarios.

Minimal Reproduction: https://codepen.io/ksbrooksjr/pen/QwWNoxM

Given the following initial HTML:

 <div id="1"></div>

This throws an error:

import { Idiomorph } from "https://unpkg.com/idiomorph@0.7.2/dist/idiomorph.esm.js";

const div = document.getElementById("1");

try {
  Idiomorph.morph(div, `<div><div id="1"></div></div>`);
} catch (e) {
  console.error(e);
  div.innerHTML = e.message;
}
@botandrose
Copy link
Collaborator

botandrose commented Feb 21, 2025

Hmm, this is a weird case. The error message is correct, in that #1 is not a valid CSS selector: https://developer.mozilla.org/en-US/docs/Web/CSS/ident

However, id attributes aren't technically required to be valid CSS selectors when prepended with #, it's just discouraged: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id#syntax

I honestly don't know if this is something we should support, or if we even can support. I'll need to explore the ramifications of not being able to use querySelector

@ksbrooksjr
Copy link
Author

ksbrooksjr commented Feb 21, 2025

I honestly don't know if this is something we should support, or if we even can support. I'll need to explore the ramifications of not being able to use querySelector

You can still use querySelector, you just have to use the attribute selector syntax:

document.querySelector(`[id="1"]`)

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 a pull request may close this issue.

2 participants