Skip to content

Commit

Permalink
feat: add rdtc alias
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed May 8, 2018
1 parent 08ee8dc commit 1fec07d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ The following subroutines are available out of the box.
|CSS selector|CSS selector used to select an element.|N/A|
|Quantifier expression|A [quantifier expression](#quantifier-expression) is used to control the shape of the results (direct result or array of results) and the expected result length.|See [quantifier expression](#quantifier-expression).|


##### Quantifier expression

A *quantifier expression* is used to assert that the query matches a set number of nodes. A quantifier expression is a modifier of the [`select` subroutine](#select-subroutine).
Expand Down Expand Up @@ -344,6 +343,7 @@ Surgeon exports an alias preset is used to reduce verbosity of the queries.
|Name|Description|
|---|---|
|`ra ...`|Reads Element attribute value. Equivalent to `read attribute ...`|
|`rdtc ...`|Removes any descending elements and reads the resulting `textContent` property of an element. Equivalent to `remove * {0,} | read property ... textContent`|
|`rih ...`|Reads `innerHTML` property of an element. Equivalent to `read property ... innerHTML`|
|`roh ...`|Reads `outerHTML` property of an element. Equivalent to `read property ... outerHTML`|
|`rp ...`|Reads Element property value. Equivalent to `read property ...`|
Expand Down
4 changes: 4 additions & 0 deletions src/subroutineAliasPreset.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
nextUntilSubroutine,
readSubroutine,
removeSubroutine,
selectSubroutine,
testSubroutine
} from './subroutines';
Expand All @@ -12,6 +13,9 @@ export default {
ra: (subject, values, bindle) => {
return readSubroutine(subject, ['attribute'].concat(values), bindle);
},
rdtc: (subject, values, bindle) => {
return readSubroutine(removeSubroutine(subject, ['*', '{0,}'], bindle), ['property', 'textContent'], bindle);
},
rih: (subject, values, bindle) => {
return readSubroutine(subject, ['property', 'innerHTML'], bindle);
},
Expand Down
28 changes: 28 additions & 0 deletions test/surgeon/aliases.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,34 @@ import surgeon, {
subroutineAliasPreset
} from '../../src';

test('rdtc: reads direct textNode textContent property', (t): void => {
const x = surgeon({
subroutines: subroutineAliasPreset
});

const subject = `
<div class="foo">bar<div>baz</div></div>
`;

const query = 's .foo | rdtc';

t.deepEqual(x(query, subject), 'bar');
});

test('rtc: reads textContent property', (t): void => {
const x = surgeon({
subroutines: subroutineAliasPreset
});

const subject = `
<div class="foo">bar<div>baz</div></div>
`;

const query = 's .foo | rtc';

t.deepEqual(x(query, subject), 'barbaz');
});

test('ra: reads element attribute', (t): void => {
const x = surgeon({
subroutines: subroutineAliasPreset
Expand Down

0 comments on commit 1fec07d

Please sign in to comment.