Skip to content

Commit

Permalink
Merge 2485dff into e2cd66a
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed May 3, 2022
2 parents e2cd66a + 2485dff commit 48b6162
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/LICENSE
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
Some of the tests for this project are based on the cheerio-advanced-selectors project.
License information below.

The MIT License (MIT)

Copyright (c) 2015-2018 Thomas Watson Steen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

===

Some of the tests for this project are based on the Sizzle project.
License information below.

Expand Down
82 changes: 82 additions & 0 deletions test/advanced-selectors.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { select } from "../src";
import { parseDocument } from "htmlparser2";
import { innerText } from "domutils";

const testCases: [
name: string,
html: string,
selector: string,
text: string
][] = [
["Non-advanced selector", "<div>foo</div><div>bar</div>", "div", "foobar"],
[
"Simple selector ending in :first()",
"<div>foo</div><div>bar</div>",
"div:first",
"foo",
],
[
"Simple selector ending in :last()",
"<div>foo</div><div>bar</div>",
"div:last",
"bar",
],
[
"Simple selector ending in :first-child()",
"<div><span>foo</span><span>bar</span></div>",
"div span:first-child",
"foo",
],
[
"Simple selector ending in :last-child()",
"<div><span>foo</span><span>bar</span></div>",
"div span:last-child",
"bar",
],
[
"Simple selector ending in :eq()",
"<div>foo</div><div>bar</div>",
"div:eq(1)",
"bar",
],
[
"Simple selector with :eq() in the middle",
"<div><span>foo</span></div><div><span>bar</span></div>",
"div:eq(0) span",
"foo",
],
[
"Complex selector",
"<div><span><h1>foo</h1></span><span><h1>bar</h1></span></div>",
"div:first span:eq(1) h1",
"bar",
],
];

describe("cheerio-advanced-selectors — #find()", () => {
for (const [name, html, selector, text] of testCases) {
it(name, () => {
const document = parseDocument(html);
const result = select(selector, document);
expect(innerText(result)).toBe(text);
});
}

it("Custom context", () => {
const document = parseDocument("<div>foo</div><div>bar</div>");
const result = select("div:eq(1)", document, {
context: document.children[0],
});
expect(innerText(result)).toBe("bar");
});

it("Custom root", () => {
const document = parseDocument(
"<div><span>foo</span></div><div><span>bar</span></div>"
);
const result = select("span:eq(1)", document, {
root: document,
});
expect(innerText(result)).toBe("bar");
});
});

0 comments on commit 48b6162

Please sign in to comment.