WICG / css-parser-api

This is the repo where the CSS Houdini parser API will be worked on
Other
71 stars 14 forks source link

API to parse selectors #11

Open valtlai opened 6 years ago

valtlai commented 6 years ago

This spec doesn’t seem contain an API to parse CSS selectors.

I would like to create a function that turns a CSS selector into a HTML element. For example,

const elem = buildElem('button[type=button]#foo.bar.baz:disabled');

would do this:

const elem = document.createElement('button');
elem.setAttribute('type', 'button');
elem.id = 'foo';
elem.classList.add('bar');
elem.classList.add('baz');
elem.disabled = true;

which produces:

<button type="button" id="foo" class="bar baz" disabled></button>

For now, I would use regexes (buggy, hard to maintain) or a JS library (too much code), but it would be very nice to have a native API for selector parsing.