Skip to content

Instantly share code, notes, and snippets.

@samleb
Created October 22, 2009 22:09
Show Gist options
  • Select an option

  • Save samleb/216388 to your computer and use it in GitHub Desktop.

Select an option

Save samleb/216388 to your computer and use it in GitHub Desktop.
/**
* Bouncer.tokenize(expression) -> [Array...]
* - expression (String): Valid stripped CSS selector expression.
*
* Splits the given expression in an array whose elements represents
* comma-separated grouped rules as specified in the
* [CSS Specification](http://www.w3.org/TR/CSS2/selector.html#grouping).
*
* Each rule is in turn split into tokens which are objects with `symbol`,
* `lexeme`, and `captures` properties :
*
* - `symbol`: A symbol representing the atomic selector.
* - `*`: tag name
* - `#`: id
* - `.`: class name
* - `[`: attribute
* - `:`: pseudo
* - ` `: descendant
* - `>`: child
* - `~`: later sibling
* - `+`: adjacent sibling
*
* - `lexeme`: The matched string.
*
* - `captures`: An array of parts depending on the selector's type.
*
* ### Examples
*
* Bouncer.tokenize("a, p");
* // =>
* [ [ { symbol: "*", lexeme: "a", captures: [] } ],
* [ { symbol: "*", lexeme: "p", captures: [] } ] ]
*
* Bouncer.tokenize("p > a");
* // =>
* [ [ { symbol: "*", lexeme: "p", captures: ["p"] },
* { symbol: ">", lexeme: " > ", captures: [] },
* { symbol: "*", lexeme: "a", captures: ["a"] } ] ]
*
* Bouncer.tokenize("a[href^=#]");
* // =>
* [ [ { symbol: "*", lexeme: "a", captures: ["a"] },
* { symbol: "[", lexeme: "[href^=#]", captures: ["href", "^=", "#"] } ] ]
*
* Bouncer.tokenize("p:not(:first-child)");
* // =>
* [ [ { symbol: "*", lexeme: "p", captures: ["p"] },
* { symbol: ":", lexeme: ":not(:first-child)", captures: ["not", ":first-child"] } ] ]
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment