Skip to content

Commit 6f6c6d3

Browse files
committed
Add example on using Sizzle to run CSS selector queries
1 parent 7d6a84c commit 6f6c6d3

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ This library does not implement CSS selectors, which means no `querySelector` /
7272

7373
To query a slimdom document using XPath or XQuery, use [FontoXPath][fontoxpath].
7474

75+
To query a slimdom document using CSS, see [this example][sizzle-example] which shows how to use [sizzle][sizzle] to run queries using CSS selectors.
76+
7577
### HTML & browser-specific features and behavior
7678

7779
Emulating a full browser environment is not the goal of this library. Consider using [jsdom][jsdom] instead if you need that.
@@ -100,6 +102,8 @@ The following features are missing simply because I have not yet had a need for
100102
[parse5-example]: https://github.com/bwrrp/slimdom.js/tree/master/test/examples/parse5
101103
[parse5]: https://github.com/inikulin/parse5
102104
[dom-treeadapter]: https://github.com/RReverser/dom-treeadapter
105+
[sizzle-example]: https://github.com/bwrrp/slimdom.js/tree/master/test/examples/sizzle
106+
[sizzle]: https://github.com/jquery/sizzle
103107
[jsdom]: https://github.com/jsdom/jsdom
104108

105109
## Contributing

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"rollup": "^2.38.1",
4646
"rollup-plugin-sourcemaps": "^0.6.3",
4747
"rollup-plugin-terser": "^7.0.2",
48+
"sizzle": "^2.3.5",
4849
"ts-jest": "~26.5.0",
4950
"typescript": "^4.1.3"
5051
},

test/examples/sizzle/sizzle.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'sizzle';

test/examples/sizzle/sizzle.tests.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as slimdom from '../../../src/index';
2+
3+
import Sizzle = require('sizzle');
4+
5+
describe('Example: sizzle integration', () => {
6+
it('can use CSS selectors using Sizzle', () => {
7+
const document = new slimdom.Document();
8+
const root = document.appendChild(document.createElement('root'));
9+
const p1 = root.appendChild(document.createElement('p'));
10+
const p2 = root.appendChild(document.createElement('p'));
11+
const p3 = root.appendChild(document.createElement('p'));
12+
const div = root.appendChild(document.createElement('div'));
13+
const p4 = div.appendChild(document.createElement('p'));
14+
p1.setAttribute('id', 'header');
15+
p4.setAttribute('class', 'footer');
16+
17+
expect(Sizzle('p', document)).toEqual([p1, p2, p3, p4]);
18+
expect(Sizzle('div p', document)).toEqual([p4]);
19+
expect(Sizzle('p ~ p', document)).toEqual([p2, p3]);
20+
expect(Sizzle('#header', document)).toEqual([p1]);
21+
expect(Sizzle('.footer', document)).toEqual([p4]);
22+
});
23+
});

0 commit comments

Comments
 (0)