Skip to content

Commit faeabee

Browse files
committed
feat: abstract clone method
1 parent 4ad66b9 commit faeabee

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

src/evaluators/browserEvaluator.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ export default (): EvaluatorType => {
5252
throw new Error('Unimplemented.');
5353
};
5454

55+
// eslint-disable-next-line no-unused-vars
56+
const clone = (node) => {
57+
throw new Error('Unimplemented.');
58+
};
59+
5560
return {
61+
clone,
5662
getAttributeValue,
5763
getPropertyValue,
5864
isElement,

src/evaluators/cheerioEvaluator.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ export default (): EvaluatorType => {
7979
node.remove();
8080
};
8181

82+
const clone = (node) => {
83+
return node.clone();
84+
};
85+
8286
return {
87+
clone,
8388
getAttributeValue,
8489
getPropertyValue,
8590
isElement,

src/subroutines/removeSubroutine.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,24 @@ import {
99
import type {
1010
SubroutineType
1111
} from '../types';
12+
import {
13+
SurgeonError
14+
} from '../errors';
1215
import selectSubroutine from './selectSubroutine';
1316

1417
const debug = createDebug('subroutine:remove');
1518

16-
const removeSubroutine: SubroutineType = (inputSubject, [cssSelector, quantifierExpression], {evaluator}) => {
19+
const removeSubroutine: SubroutineType = (subject, [cssSelector, quantifierExpression], {evaluator}) => {
1720
debug('selecting "%s" for removal', cssSelector);
1821

22+
if (!evaluator.isElement(subject)) {
23+
throw new SurgeonError('Unexpected value. Value must be an element.');
24+
}
25+
1926
// Ensure that we do not mutate the parent node.
20-
const subject = inputSubject.clone();
27+
const cloneSubject = evaluator.clone(subject);
2128

22-
const matches = selectSubroutine(subject, [cssSelector, quantifierExpression], {evaluator});
29+
const matches = selectSubroutine(cloneSubject, [cssSelector, quantifierExpression], {evaluator});
2330

2431
if (Array.isArray(matches)) {
2532
for (const match of matches) {
@@ -29,7 +36,7 @@ const removeSubroutine: SubroutineType = (inputSubject, [cssSelector, quantifier
2936
evaluator.remove(matches);
3037
}
3138

32-
return subject;
39+
return cloneSubject;
3340
};
3441

3542
export default removeSubroutine;

src/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type ElementType = Object;
77
type BindleType = Object;
88

99
export type EvaluatorType = {|
10+
+clone: (element: ElementType) => ElementType,
1011
+getAttributeValue: (element: ElementType, name: string) => string,
1112
+getPropertyValue: (element: ElementType, name: string) => mixed,
1213
+isElement: (maybeElement: mixed) => boolean,

0 commit comments

Comments
 (0)