-
Notifications
You must be signed in to change notification settings - Fork 1
/
DocumentPreProcessor.js
176 lines (144 loc) · 4.4 KB
/
DocumentPreProcessor.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import find from 'lodash/find'
import { mediaSmall, mediaLarge } from './multi-column-styles'
import { MediaStyleSheet, Script } from '../models'
import { MEDIA_QUERY_LARGE, MEDIA_QUERY_SMALL } from '../constants'
const state = {
root: null, // App context, used for styles
document: null, // content context, used for (removing) scripts
styleSheets: [], // MediaStyleSheet list
scripts: [], // Scripts
requestURI: '', // Original domain of the request
}
class DocumentPreProcessor {
static setContextDocument(document) {
state.document = document
}
static setRootDocument(document) {
state.root = document
}
static setRequestURI(requestURI) {
state.requestURI = requestURI
}
static createStyleSheets({ paddingLeft, columnGap }) {
state.styleSheets = [
...state.styleSheets,
new MediaStyleSheet({
media: MEDIA_QUERY_LARGE,
rules: [...mediaSmall({ paddingLeft, columnGap })],
}),
new MediaStyleSheet({
media: MEDIA_QUERY_SMALL,
rules: [...mediaLarge({ paddingLeft, columnGap })],
}),
]
}
static appendStyleSheets() {
state.styleSheets.forEach(
sheet =>
state.root.querySelector(`#${sheet.id}`) === null &&
sheet.appendSheet(state.root)
)
}
static appendScripts() {
state.scripts.forEach(
script =>
/(?:text|application)\/(?:(x-)?java|ecma)script/.test(script.type) &&
script.appendScript(state.root)
)
}
static createScriptElements() {
const scriptElements = Array.from(
state.document.querySelectorAll('script') || []
)
const { requestURI, scripts } = state
if (!scriptElements) return scripts
state.scripts = scriptElements.map(node => new Script({ node, requestURI }))
return state.scripts
}
static removeScriptElements() {
const scripts = state.document.querySelectorAll('script')
for (let i = 0; i < scripts.length; i++) {
scripts[i].parentNode.removeChild(scripts[i])
}
}
static getStyleSheetByMediaOrId({ id, media }) {
if (!id && !media) {
return console.warn(
"DocumentPreProcessor#updateStyleSheet requires either and 'id' or a 'media' parameter"
)
}
let styleSheetId
if (id) {
styleSheetId = id
} else if (media) {
const _styleSheet = find(this.styleSheets, { media })
if (!_styleSheet) {
return console.warn(
"No styleSheet exists for provided 'id' or 'media'",
id,
media
)
}
styleSheetId = _styleSheet.id
}
const styleSheetElement = state.root.querySelector(`#${styleSheetId}`)
if (!styleSheetElement) {
return console.warn(
"No styleSheet exists for provided 'id' or 'media'",
id,
media
)
}
return { styleSheetElement, styleSheetId }
}
static removeStyleSheet({ id, media }) {
const {
styleSheetElement,
} = DocumentPreProcessor.getStyleSheetByMediaOrId({ id, media })
styleSheetElement.parentNode.removeChild(styleSheetElement)
state.styleSheets = [...state.styleSheets.filter(a => a.id !== id)]
}
static removeStyleSheets() {
let sheet
while ((sheet = state.styleSheets.pop())) {
DocumentPreProcessor.removeStyleSheet({ id: sheet.id })
}
}
static removeScript({ id }) {
const script = state.root.querySelector(`#${id}`)
if (script) script.parentNode.removeChild(script)
state.scripts = [...state.scripts.filter(a => a.id !== id)]
}
static removeScripts() {
let script
while ((script = state.scripts.pop())) {
DocumentPreProcessor.removeScript({ id: script.id })
}
}
// exchange an existing media stylesheet for a new one that targets the
// same media
static swapStyleSheet(/* media */) {}
static swapStyleSheets() {}
static getStyleSheets() {
return state.styleSheets
}
static getContextDocument() {
return state.document
}
static getRootDocument() {
return state.root
}
static parseXML(callback) {
// TODO errors should be handled
// @issue: https://github.com/triplecanopy/b-ber/issues/220
const err = null
DocumentPreProcessor.removeScriptElements()
DocumentPreProcessor.appendStyleSheets()
DocumentPreProcessor.appendScripts()
if (callback && typeof callback === 'function') {
return callback(err, state.document)
}
return state.document
}
}
export default DocumentPreProcessor