forked from gajus/surgeon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cheerioEvaluator.js
113 lines (94 loc) · 2.44 KB
/
cheerioEvaluator.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// @flow
import cheerio from 'cheerio';
import type {
EvaluatorType,
} from '../types';
import {
ReadSubroutineNotFoundError,
} from '../errors';
export default (): EvaluatorType => {
const getAttributeValue = (node, name) => {
const attributeValue = node.attr(name);
if (typeof attributeValue === 'string') {
return attributeValue;
}
throw new ReadSubroutineNotFoundError();
};
const getPropertyValue = (node, name) => {
if (name === 'href') {
// URLs might include spaces, which a brower would encode when
// the attribute value is retrieved through a DOM property.
return encodeURI(node.attr('href'));
}
if (name === 'src') {
// URLs might include spaces, which a brower would encode when
// the attribute value is retrieved through a DOM property.
return encodeURI(node.attr('src'));
}
if (name === 'textContent') {
return node.text();
}
// @see https://github.com/cheeriojs/cheerio/issues/944
if (name === 'outerHTML') {
return node.clone().wrap('<div>').parent().html();
}
// @see https://github.com/cheeriojs/cheerio/issues/1099
if (name === 'innerHTML') {
return node.html();
}
// @see https://github.com/cheeriojs/cheerio/issues/993
if (name === 'childNodes') {
return node[0].childNodes;
}
return node.prop(name);
};
/**
* @see https://github.com/cheeriojs/cheerio/issues/765
*/
const isElement = (maybeElement) => {
return typeof maybeElement === 'object' && maybeElement !== null && typeof maybeElement.cheerio !== 'undefined';
};
const parseDocument = (subject) => {
return cheerio
.load(subject, {
xmlMode: false,
})
.root();
};
const previous = (node, selector) => {
return node.prev(selector);
};
const querySelectorAll = (node, selector) => {
return node
.find(selector)
.toArray()
.map((element) => {
return cheerio(element);
});
};
const nextUntil = (node, selector, filter) => {
return node
.nextUntil(selector, filter)
.toArray()
.map((element) => {
return cheerio(element);
});
};
const remove = (node) => {
node.remove();
};
const clone = (node) => {
return node.clone();
};
return {
clone,
getAttributeValue,
getPropertyValue,
isElement,
nextUntil,
parseDocument,
previous,
querySelectorAll,
remove,
};
};