css-parser-api icon indicating copy to clipboard operation
css-parser-api copied to clipboard

API to parse selectors

Open valtlai opened this issue 7 years ago • 0 comments

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.

valtlai avatar Apr 06 '18 14:04 valtlai