forked from gajus/surgeon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoveSubroutine.js
42 lines (33 loc) · 1.02 KB
/
removeSubroutine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// @flow
import {
FinalResultSentinel,
} from 'pianola';
import type {
SubroutineType,
} from '../types';
import {
SurgeonError,
} from '../errors';
import Logger from '../Logger';
import selectSubroutine from './selectSubroutine';
const log = Logger.child({
namespace: 'subroutine:remove',
});
const removeSubroutine: SubroutineType = (subject, [cssSelector, quantifierExpression], {evaluator}) => {
log.debug('selecting "%s" for removal', cssSelector);
if (!evaluator.isElement(subject)) {
throw new SurgeonError('Unexpected value. Value must be an element.');
}
// Ensure that we do not mutate the parent node.
const cloneSubject = evaluator.clone(subject);
const matches = selectSubroutine(cloneSubject, [cssSelector, quantifierExpression], {evaluator});
if (Array.isArray(matches)) {
for (const match of matches) {
evaluator.remove(match);
}
} else if (!(matches instanceof FinalResultSentinel)) {
evaluator.remove(matches);
}
return cloneSubject;
};
export default removeSubroutine;