extract.js
5.43 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"use strict"
const htmlparser = require("htmlparser2")
const TransformableString = require("./TransformableString")
function iterateScripts(code, options, onChunk) {
if (!code) return
const xmlMode = options.xmlMode
const isJavaScriptMIMEType = options.isJavaScriptMIMEType || (() => true)
let index = 0
let inScript = false
let cdata = []
const chunks = []
function pushChunk(type, end) {
chunks.push({ type, start: index, end, cdata })
cdata = []
index = end
}
const parser = new htmlparser.Parser(
{
onopentag(name, attrs) {
// Test if current tag is a valid <script> tag.
if (name !== "script") {
return
}
if (attrs.type && !isJavaScriptMIMEType(attrs.type)) {
return
}
if (attrs.src) {
return
}
inScript = true
pushChunk("html", parser.endIndex + 1)
},
oncdatastart() {
cdata.push(
{
start: parser.startIndex,
end: parser.startIndex + 9,
},
{
start: parser.endIndex - 2,
end: parser.endIndex + 1,
}
)
},
onclosetag(name) {
if (name !== "script" || !inScript) {
return
}
inScript = false
if (parser.startIndex < chunks[chunks.length - 1].end) {
// The parser didn't move its index after the previous chunk emited. It occurs on
// self-closing tags (xml mode). Just ignore this script.
return
}
pushChunk("script", parser.startIndex)
},
ontext() {
if (!inScript) {
return
}
pushChunk("script", parser.endIndex + 1)
},
},
{
xmlMode: xmlMode === true,
}
)
parser.parseComplete(code)
pushChunk("html", parser.endIndex + 1)
{
const emitChunk = () => {
const cdata = []
for (let i = startChunkIndex; i < index; i += 1) {
cdata.push.apply(cdata, chunks[i].cdata)
}
onChunk({
type: chunks[startChunkIndex].type,
start: chunks[startChunkIndex].start,
end: chunks[index - 1].end,
cdata,
})
}
let startChunkIndex = 0
let index
for (index = 1; index < chunks.length; index += 1) {
if (chunks[startChunkIndex].type === chunks[index].type) continue
emitChunk()
startChunkIndex = index
}
emitChunk()
}
}
function computeIndent(descriptor, previousHTML, slice) {
if (!descriptor) {
const indentMatch = /[\n\r]+([ \t]*)/.exec(slice)
return indentMatch ? indentMatch[1] : ""
}
if (descriptor.relative) {
return previousHTML.match(/([^\n\r]*)<[^<]*$/)[1] + descriptor.spaces
}
return descriptor.spaces
}
function* dedent(indent, slice) {
let hadNonEmptyLine = false
const re = /(\r\n|\n|\r)([ \t]*)(.*)/g
let lastIndex = 0
while (true) {
const match = re.exec(slice)
if (!match) break
const newLine = match[1]
const lineIndent = match[2]
const lineText = match[3]
const isEmptyLine = !lineText
const isFirstNonEmptyLine = !isEmptyLine && !hadNonEmptyLine
const badIndentation =
// Be stricter on the first line
isFirstNonEmptyLine
? indent !== lineIndent
: lineIndent.indexOf(indent) !== 0
if (!badIndentation) {
lastIndex = match.index + newLine.length + indent.length
// Remove the first line if it is empty
const fromIndex = match.index === 0 ? 0 : match.index + newLine.length
yield {
type: "dedent",
from: fromIndex,
to: lastIndex,
}
} else if (isEmptyLine) {
yield {
type: "empty",
}
} else {
yield {
type: "bad-indent",
}
}
if (!isEmptyLine) {
hadNonEmptyLine = true
}
}
const endSpaces = slice.slice(lastIndex).match(/[ \t]*$/)[0].length
if (endSpaces) {
yield {
type: "dedent",
from: slice.length - endSpaces,
to: slice.length,
}
}
}
function extract(code, indentDescriptor, xmlMode, isJavaScriptMIMEType) {
const badIndentationLines = []
const codeParts = []
let lineNumber = 1
let previousHTML = ""
iterateScripts(code, { xmlMode, isJavaScriptMIMEType }, chunk => {
const slice = code.slice(chunk.start, chunk.end)
if (chunk.type === "html") {
const match = slice.match(/\r\n|\n|\r/g)
if (match) lineNumber += match.length
previousHTML = slice
} else if (chunk.type === "script") {
const transformedCode = new TransformableString(code)
let indentSlice = slice
for (const cdata of chunk.cdata) {
transformedCode.replace(cdata.start, cdata.end, "")
if (cdata.end === chunk.end) {
indentSlice = code.slice(chunk.start, cdata.start)
}
}
transformedCode.replace(0, chunk.start, "")
transformedCode.replace(chunk.end, code.length, "")
for (const action of dedent(
computeIndent(indentDescriptor, previousHTML, indentSlice),
indentSlice
)) {
lineNumber += 1
if (action.type === "dedent") {
transformedCode.replace(
chunk.start + action.from,
chunk.start + action.to,
""
)
} else if (action.type === "bad-indent") {
badIndentationLines.push(lineNumber)
}
}
codeParts.push(transformedCode)
}
})
return {
code: codeParts,
badIndentationLines,
}
}
module.exports = extract