Skip to content

Commit

Permalink
feat: add previous css selector parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Dec 20, 2018
1 parent 941c95a commit f78c156
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ The following subroutines are available out of the box.

`previous` subroutine selects the preceding sibling.

|Parameter name|Description|Default|
|---|---|---|
|CSS selector|CSS selector used to select an element.|N/A|

Example:

```html
Expand Down
4 changes: 2 additions & 2 deletions src/evaluators/cheerioEvaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export default (): EvaluatorType => {
.root();
};

const previous = (node) => {
return node.prev();
const previous = (node, selector) => {
return node.prev(selector);
};

const querySelectorAll = (node, selector) => {
Expand Down
26 changes: 26 additions & 0 deletions test/surgeon/evaluators/cheerioEvaluator/previous.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,29 @@ test('returns the previous node', (t) => {

t.true(previousNode.text() === '1');
});

test('returns previous node matching the selector', (t) => {
const {
previous,
parseDocument,
querySelectorAll
} = cheerioEvaluator();

const body = `
<ul>
<li class='foo'>0</li>
<li class='foo'>1</li>
<li class='bar'>2</li>
<li class='baz'>3</li>
</ul>
`;

const document = parseDocument(body);
const nodes = querySelectorAll(document, 'li');

t.true(nodes.length === 4);

const previousNode = previous(nodes[2], '.foo');

t.true(previousNode.text() === '1');
});

0 comments on commit f78c156

Please sign in to comment.