Skip to content

Commit

Permalink
Add example on using Sizzle to run CSS selector queries
Browse files Browse the repository at this point in the history
  • Loading branch information
bwrrp committed Jan 31, 2021
1 parent adaaadd commit 7b1a17c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ This means no `querySelector` / `querySelectorAll` on `ParentNode`, no `closest`

To query a slimdom document using XPath, use [FontoXPath][fontoxpath].

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.

### HTML & browser-specific features and behavior

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

## Contributing
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"rollup": "^2.38.1",
"rollup-plugin-sourcemaps": "^0.6.3",
"rollup-plugin-terser": "^7.0.2",
"sizzle": "^2.3.5",
"ts-jest": "~26.5.0",
"typescript": "^4.1.3"
},
Expand Down
1 change: 1 addition & 0 deletions test/examples/sizzle/sizzle.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'sizzle';
23 changes: 23 additions & 0 deletions test/examples/sizzle/sizzle.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as slimdom from '../../../src/index';

import Sizzle = require('sizzle');

describe('Example: sizzle integration', () => {
it('can use CSS selectors using Sizzle', () => {
const document = new slimdom.Document();
const root = document.appendChild(document.createElement('root'));
const p1 = root.appendChild(document.createElement('p'));
const p2 = root.appendChild(document.createElement('p'));
const p3 = root.appendChild(document.createElement('p'));
const div = root.appendChild(document.createElement('div'));
const p4 = div.appendChild(document.createElement('p'));
p1.setAttribute('id', 'header');
p4.setAttribute('class', 'footer');

expect(Sizzle('p', document)).toEqual([p1, p2, p3, p4]);
expect(Sizzle('div p', document)).toEqual([p4]);
expect(Sizzle('p ~ p', document)).toEqual([p2, p3]);
expect(Sizzle('#header', document)).toEqual([p1]);
expect(Sizzle('.footer', document)).toEqual([p4]);
});
});

0 comments on commit 7b1a17c

Please sign in to comment.