b-fuze / deno-dom

Browser DOM & HTML parser in Deno
https://jsr.io/@b-fuze/deno-dom
MIT License
425 stars 47 forks source link

Cannot select element with duplicate id but different element class #116

Open wilt00 opened 2 years ago

wilt00 commented 2 years ago

In the case where two different elements of different classes (e.g. div and img) share the same id:

import { DOMParser } from "https://deno.land/x/deno_dom@v0.1.32-alpha/deno-dom-wasm.ts";
const h = '<div id="i"><img id="i"/></div>';
const d = new DOMParser().parseFromString(h, "text/html");
d.querySelectorAll('#i'); // only returns the div tag
d.querySelector('img#i'); // returns null

deno-dom is unable to select the second tag.

Firefox and Chrome are both able to select the img tag - see screenshot of Firefox's behavior below:

image (Edit: replaced with a slightly more rigorous non-Quirks-mode screenshot)

Duplicating ids is banned by the spec, but can be found in the wild - e.g. Google sites like youtube.com make heavy usage of nonunique ids in shared components.

Tested with https://deno.land/x/deno_dom@v0.1.32-alpha/deno-dom-wasm.ts

b-fuze commented 2 years ago

This is likely an issue upstream with the nwsapi dependency... I'll have to look into why it's doing this :thinking:

jsejcksn commented 2 years ago

FYI: A document's having multiple elements with the same ID is not spec (regardless of how an agent's runtime allows interactions).