-
Notifications
You must be signed in to change notification settings - Fork 30
/
sourceDocument.js
53 lines (44 loc) · 1.74 KB
/
sourceDocument.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
export default class SourceDocument {
constructor({ location, executeClientScripts, contentSelectors, insignificantContentSelectors, filters, content, mimeType }) {
this.location = location;
this.executeClientScripts = executeClientScripts;
this.contentSelectors = contentSelectors;
this.insignificantContentSelectors = insignificantContentSelectors;
this.filters = filters;
this.content = content;
this.mimeType = mimeType;
this.id = new URL(location).pathname.split('/').filter(Boolean).join('-');
}
get cssSelectors() {
const { contentSelectors, insignificantContentSelectors } = this;
const result = [
...SourceDocument.extractCssSelectorsFromProperty(contentSelectors),
...SourceDocument.extractCssSelectorsFromProperty(insignificantContentSelectors),
];
return result.filter(selector => selector);
}
static extractCssSelectorsFromProperty(property) {
if (Array.isArray(property)) {
return []
.concat(property)
.flatMap(selector => SourceDocument.extractCssSelectorsFromSelector(selector));
}
return SourceDocument.extractCssSelectorsFromSelector(property);
}
static extractCssSelectorsFromSelector(selector) {
if (typeof selector === 'object') {
const { startBefore, endBefore, startAfter, endAfter } = selector;
return [ startBefore, endBefore, startAfter, endAfter ].filter(rangeSelector => rangeSelector);
}
return [selector];
}
toPersistence() {
return {
fetch: this.location,
select: this.contentSelectors,
remove: this.insignificantContentSelectors,
filter: this.filters ? this.filters.map(filter => filter.name) : undefined,
executeClientScripts: this.executeClientScripts,
};
}
}