Skip to content

API to parse selectors #11

Open
Open

Description

@valtlai

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions