forked from LeaVerou/parsel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
151 lines (118 loc) · 5.14 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Parsel: A tiny, permissive CSS selector parser</title>
<meta name="viewport" content="width=device-width">
<link rel="shortcut icon" href="favicon.svg">
<link rel="stylesheet" href="prism.css">
<link rel="stylesheet" href="style.css">
</head>
<body class="language-javascript">
<header>
<h1>parsel</h1>
<h2>A tiny, permissive CSS selector parser</h2>
<ul>
<li>Parse & traverse CSS selectors</li>
<li>Calculate specificity</li>
<li>Only <a href="https://bundlephobia.com/[email protected]">2KB</a> and no dependencies</li>
<li>Supports the entire <a href="https://w3.org/TR/selectors-4">Selectors 4</a> syntax</li>
<li>Permissive enough to work with a lot of potential future syntax</li>
</ul>
</header>
<section id="tryout">
<header>
<label>
Selector:
<input type="text" id="selectorText" autofocus value="#foo > .bar + div.k1.k2 [id='baz']:hello(2):not(:where(#yolo))::before">
</label>
<label>
<input type="checkbox" id="recursive" checked>
Recursive (parse arguments of <code>:not()</code>, <code>:is()</code>, <code>:where()</code> etc)
</label>
</header>
<div id="specificity-display">
Specificity:
<code>parsel.specificity(selector);</code>
<strong id="specificity"></strong></div>
<div id="results">
<article>
<h2>Tokens <code>parsel.tokenize(selector);</code></h2>
<pre><code id="tokens" class="language-json"></code></pre>
</article>
<article>
<h2>AST <code>parsel.parse(selector);</code></h2>
<pre><code id="tree" class="language-json"></code></pre>
</article>
</div>
</section>
<section>
<h2>Usage</h2>
<p>Parsel is an ES module. You import it like so:</p>
<pre><code>import * as parsel from "https://projects.verou.me/parsel/parsel.js"</code></pre>
<p>Note that to use that your script needs to use <code>type="module"</code> or be imported from a script that does.
If you can't use ES modules, here is a workaround to convert Parsel to a global:
</p>
<pre class="language-markup"><code>
<script type="module">
import * as parsel from "https://projects.verou.me/parsel/parsel.js";
window.parsel = parsel;
</script>
</code></pre>
<p>After that, you can use <code>parsel</code> as a global, assuming your code runs after the <code>DOMContentLoaded</code> event.
In fact, we are assigning <code>parsel</code> to a global in this very page, so you can open your console and play with it!</p>
<p>You can also intall via npm: <code>npm install <a href="https://www.npmjs.com/package/parsel-js">parsel-js</a></code></p>
</section>
<section>
<h2>API</h2>
<p>Get list of tokens as a flat array:</p>
<pre><code>parsel.tokenize(selector)</code></pre>
<p>Get AST:</p>
<pre><code>parsel.parse(selector)</code></pre>
<p>You can also provide options:</p>
<pre><code>parsel.parse(selector, {recursive: false, list: false})</code></pre>
<p>The <code>recursive</code> option parses the arguments of pseudo-classes whose argument is a selector like <code>:not()</code>, <code>:is()</code>, <code>:where()</code> etc into a <code>subtree</code> property.
The <code>list</code> option parses selector lists (<code>A, B, C</code>). The only reason to turn it off is as a performance optimization when you are processing a large volume of selectors that are not lists (e.g. the output of certain CSS parsers like Rework)</p>
<p>Traverse all tokens of a (sub)tree:</p>
<pre><code>parsel.walk(node, callback)</code></pre>
<p><code>callback</code> is called with each node as the only argument.</p>
<p>Calculate specificity (returns an array of 3 numbers):</p>
<pre><code>parsel.specificity(selectorOrNode)</code></pre>
<p>To convert the specificity array to a number, you can use <code>parsel.specificityToNumber(specificity [, base])</code>.
If a base is not provided, it will be the max specificity component + 1.</p>
<p><strong>Try it out!</strong> In this page, <code>parsel</code> is assigned to a global so you can experiment with the API in the console!</p>
</section>
<footer>
<p>Made with ❤ by <a href="https://lea.verou.me">Lea Verou</a>
• <a href="test.html">Tests</a>
• <a href="https://github.com/leaverou/parsel">Github</a>
</footer>
<script type="module">
import * as parsel from "./parsel.js";
self.parsel = parsel; // so that people can experiment in the console
let url = new URL(location);
const selector = url.searchParams.get("selector");
if (selector) {
selectorText.value = selector;
}
(tryout.oninput = e => {
url = new URL(location);
url.searchParams.set("selector", selectorText.value);
history.replaceState(null, '', url);
tokens.textContent = JSON.stringify(parsel.tokenize(selectorText.value), null, "\t");
Prism.highlightElement(tokens);
try {
tree.textContent = JSON.stringify(parsel.parse(selectorText.value, {recursive: recursive.checked}), null, "\t");
tree.classList.remove("error");
specificity.textContent = parsel.specificity(selectorText.value);
Prism.highlightElement(tree);
}
catch (e) {
tree.classList.add("error");
tree.innerHTML = `<details><summary>${e}</summary>${e.stack.replace(e, "")}</details>`;
}
})();
</script>
<script src="prism.js"></script>
</body>
</html>