|
2 | 2 |
|
3 | 3 | JavaScript library for creating CSS selectors for a given DOM element or multiple DOM elements. |
4 | 4 |
|
5 | | -It also generates shorter selectors and is faster and/or more robust than many other libraries - see this [comparison](https://github.com/fczbkk/css-selector-generator-benchmark) and select the best alternative for your use case. |
| 5 | +See the [benchmark](https://fczbkk.github.io/css-selector-generator-benchmark/) to compare the speed and features of CSS Selector Generator with similar libraries. |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | + |
| 9 | +- [Install](#install) |
| 10 | +- [How to use](#how-to-use) |
| 11 | + - [Usage without NPM](#usage-without-npm) |
| 12 | + - [Usage with virtual DOM (e.g. JSDOM)](#usage-with-virtual-dom-eg-jsdom) |
| 13 | + - [Shadow DOM](#shadow-dom) |
| 14 | + - [TypeScript](#typescript) |
| 15 | + - [Multi-element selector](#multi-element-selector) |
| 16 | + - [Fallback](#fallback) |
| 17 | +- [Options](#options) |
| 18 | + - [Selector types](#selector-types) |
| 19 | + - [Root element](#root-element) |
| 20 | + - [Blacklist](#blacklist) |
| 21 | + - [Whitelist](#whitelist) |
| 22 | + - [Combine within selector](#combine-within-selector) |
| 23 | + - [Combine between selectors](#combine-between-selectors) |
| 24 | + - [Include tag](#include-tag) |
| 25 | + - [Max combinations](#max-combinations) |
| 26 | + - [Max candidates](#max-candidates) |
| 27 | + - [Max results](#max-results) |
| 28 | + - [Use scope](#use-scope) |
| 29 | +- [Bug reports, feature requests and contact](#bug-reports-feature-requests-and-contact) |
| 30 | +- [License](#license) |
6 | 31 |
|
7 | 32 | ## Try it |
8 | 33 |
|
@@ -90,6 +115,24 @@ If you don't want to use this library with NPM, you can download it from the "bu |
90 | 115 | If you want to use this library with Node, usually for testing, don't require it directly into the Node process. It will not work, because there's no `window` object and there are no elements to select. Instead, you have to add the library to the virtual `window` object. Here are instructions how to do it in JSDOM, other libraries will work in a similar way: |
91 | 116 | https://github.com/jsdom/jsdom/wiki/Don't-stuff-jsdom-globals-onto-the-Node-global |
92 | 117 |
|
| 118 | +### Shadow DOM |
| 119 | + |
| 120 | +This library supports generating selectors for elements within shadow DOM. When you provide an element that is part of a shadow DOM tree, the library will automatically infer the shadow root and generate a selector relative to it. |
| 121 | + |
| 122 | +```javascript |
| 123 | +const shadowRoot = element.attachShadow({ mode: "open" }); |
| 124 | +const shadowElement = shadowRoot.appendChild(document.createElement("div")); |
| 125 | +shadowElement.className = "shadowElement"; |
| 126 | + |
| 127 | +// Automatically detects shadow root |
| 128 | +getCssSelector(shadowElement); |
| 129 | +// ".shadowElement" |
| 130 | + |
| 131 | +// Or explicitly specify the shadow root |
| 132 | +getCssSelector(shadowElement, { root: shadowRoot }); |
| 133 | +// ".shadowElement" |
| 134 | +``` |
| 135 | + |
93 | 136 | ### TypeScript |
94 | 137 |
|
95 | 138 | This library is written in TypeScript and includes type definitions. You can import the types directly: |
|
0 commit comments