選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

133 行
5.7 KiB

4年前
  1. # css-select [![NPM version](http://img.shields.io/npm/v/css-select.svg)](https://npmjs.org/package/css-select) [![Build Status](https://travis-ci.org/fb55/css-select.svg?branch=master)](http://travis-ci.org/fb55/css-select) [![Downloads](https://img.shields.io/npm/dm/css-select.svg)](https://npmjs.org/package/css-select) [![Coverage](https://coveralls.io/repos/fb55/css-select/badge.svg?branch=master)](https://coveralls.io/r/fb55/css-select)
  2. a CSS selector compiler/engine
  3. ## What?
  4. css-select turns CSS selectors into functions that tests if elements match them. When searching for elements, testing is executed "from the top", similar to how browsers execute CSS selectors.
  5. In its default configuration, css-select queries the DOM structure of the [`domhandler`](https://github.com/fb55/domhandler) module (also known as htmlparser2 DOM).
  6. __Features:__
  7. - Full implementation of CSS3 selectors
  8. - Partial implementation of jQuery/Sizzle extensions
  9. - Very high test coverage
  10. - Pretty good performance
  11. ## Why?
  12. The traditional approach of executing CSS selectors, named left-to-right execution, is to execute every component of the selector in order, from left to right _(duh)_. The execution of the selector `a b` for example will first query for `a` elements, then search these for `b` elements. (That's the approach of eg. [`Sizzle`](https://github.com/jquery/sizzle), [`nwmatcher`](https://github.com/dperini/nwmatcher/) and [`qwery`](https://github.com/ded/qwery).)
  13. While this works, it has some downsides: Children of `a`s will be checked multiple times; first, to check if they are also `a`s, then, for every superior `a` once, if they are `b`s. Using [Big O notation](http://en.wikipedia.org/wiki/Big_O_notation), that would be `O(n^(k+1))`, where `k` is the number of descendant selectors (that's the space in the example above).
  14. The far more efficient approach is to first look for `b` elements, then check if they have superior `a` elements: Using big O notation again, that would be `O(n)`. That's called right-to-left execution.
  15. And that's what css-select does – and why it's quite performant.
  16. ## How does it work?
  17. By building a stack of functions.
  18. _Wait, what?_
  19. Okay, so let's suppose we want to compile the selector `a b` again, for right-to-left execution. We start by _parsing_ the selector, which means we turn the selector into an array of the building-blocks of the selector, so we can distinguish them easily. That's what the [`css-what`](https://github.com/fb55/css-what) module is for, if you want to have a look.
  20. Anyway, after parsing, we end up with an array like this one:
  21. ```js
  22. [
  23. { type: 'tag', name: 'a' },
  24. { type: 'descendant' },
  25. { type: 'tag', name: 'b' }
  26. ]
  27. ```
  28. Actually, this array is wrapped in another array, but that's another story (involving commas in selectors).
  29. Now that we know the meaning of every part of the selector, we can compile it. That's where it becomes interesting.
  30. The basic idea is to turn every part of the selector into a function, which takes an element as its only argument. The function checks whether a passed element matches its part of the selector: If it does, the element is passed to the next turned-into-a-function part of the selector, which does the same. If an element is accepted by all parts of the selector, it _matches_ the selector and double rainbow ALL THE WAY.
  31. As said before, we want to do right-to-left execution with all the big O improvements nonsense, so elements are passed from the rightmost part of the selector (`b` in our example) to the leftmost (~~which would be `c`~~ of course `a`).
  32. _//TODO: More in-depth description. Implementation details. Build a spaceship._
  33. ## API
  34. ```js
  35. var CSSselect = require("css-select");
  36. ```
  37. #### `CSSselect(query, elems, options)`
  38. Queries `elems`, returns an array containing all matches.
  39. - `query` can be either a CSS selector or a function.
  40. - `elems` can be either an array of elements, or a single element. If it is an element, its children will be queried.
  41. - `options` is described below.
  42. Aliases: `CSSselect.selectAll(query, elems)`, `CSSselect.iterate(query, elems)`.
  43. #### `CSSselect.compile(query)`
  44. Compiles the query, returns a function.
  45. #### `CSSselect.is(elem, query, options)`
  46. Tests whether or not an element is matched by `query`. `query` can be either a CSS selector or a function.
  47. #### `CSSselect.selectOne(query, elems, options)`
  48. Arguments are the same as for `CSSselect(query, elems)`. Only returns the first match, or `null` if there was no match.
  49. ### Options
  50. - `xmlMode`: When enabled, tag names will be case-sensitive. Default: `false`.
  51. - `strict`: Limits the module to only use CSS3 selectors. Default: `false`.
  52. - `rootFunc`: The last function in the stack, will be called with the last element that's looked at. Should return `true`.
  53. ## Supported selectors
  54. _As defined by CSS 4 and / or jQuery._
  55. * Universal (`*`)
  56. * Tag (`<tagname>`)
  57. * Descendant (` `)
  58. * Child (`>`)
  59. * Parent (`<`) *
  60. * Sibling (`+`)
  61. * Adjacent (`~`)
  62. * Attribute (`[attr=foo]`), with supported comparisons:
  63. * `[attr]` (existential)
  64. * `=`
  65. * `~=`
  66. * `|=`
  67. * `*=`
  68. * `^=`
  69. * `$=`
  70. * `!=` *
  71. * Also, `i` can be added after the comparison to make the comparison case-insensitive (eg. `[attr=foo i]`) *
  72. * Pseudos:
  73. * `:not`
  74. * `:contains` *
  75. * `:icontains` * (case-insensitive version of `:contains`)
  76. * `:has` *
  77. * `:root`
  78. * `:empty`
  79. * `:parent` *
  80. * `:[first|last]-child[-of-type]`
  81. * `:only-of-type`, `:only-child`
  82. * `:nth-[last-]child[-of-type]`
  83. * `:link`, `:visited` (the latter doesn't match any elements)
  84. * `:selected` *, `:checked`
  85. * `:enabled`, `:disabled`
  86. * `:required`, `:optional`
  87. * `:header`, `:button`, `:input`, `:text`, `:checkbox`, `:file`, `:password`, `:reset`, `:radio` etc. *
  88. * `:matches` *
  89. __*__: Not part of CSS3
  90. ---
  91. License: BSD-like