extract.js
4.51 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
"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 nextType = null
let nextEnd = null
function emitChunk(type, end, lastChunk) {
// Ignore empty chunks
if (index !== end) {
// Keep concatenating same type chunks
if (nextType !== null && nextType !== type) {
onChunk({
type: nextType,
start: index,
end: nextEnd,
})
index = nextEnd
}
nextType = type
nextEnd = end
if (lastChunk) {
onChunk({
type: nextType,
start: index,
end: nextEnd,
})
}
}
}
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
}
inScript = true
emitChunk("html", parser.endIndex + 1)
},
oncdatastart () {
if (inScript) {
emitChunk("cdata start", parser.startIndex + 9)
emitChunk("script", parser.endIndex - 2)
emitChunk("cdata end", parser.endIndex + 1)
}
},
onclosetag (name) {
if (name !== "script" || !inScript) {
return
}
inScript = false
const endSpaces = code.slice(index, parser.startIndex).match(/[ \t]*$/)[0].length
emitChunk("script", parser.startIndex - endSpaces)
},
ontext () {
if (!inScript) {
return
}
emitChunk("script", parser.endIndex + 1)
},
}, {
xmlMode: xmlMode === true,
})
parser.parseComplete(code)
emitChunk("html", parser.endIndex + 1, true)
}
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
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) {
yield {
type: "dedent",
from: match.index + newLine.length,
to: match.index + newLine.length + indent.length,
}
}
else if (isEmptyLine) {
yield {
type: "empty",
}
}
else {
yield {
type: "bad-indent",
}
}
if (!isEmptyLine) {
hadNonEmptyLine = true
}
}
}
function extract(code, indentDescriptor, xmlMode, isJavaScriptMIMEType) {
const badIndentationLines = []
const transformedCode = new TransformableString(code)
let lineNumber = 1
let previousHTML = ""
iterateScripts(code, { xmlMode, isJavaScriptMIMEType }, (chunk) => {
const slice = code.slice(chunk.start, chunk.end)
if (chunk.type === "html" || chunk.type === "cdata start" || chunk.type === "cdata end") {
const newLinesRe = /(?:\r\n|\n|\r)([^\r\n])?/g
let lastEmptyLinesLength = 0
while (true) {
const match = newLinesRe.exec(slice)
if (!match) break
lineNumber += 1
lastEmptyLinesLength = !match[1] ? lastEmptyLinesLength + match[0].length : 0
}
transformedCode.replace(chunk.start, chunk.end - lastEmptyLinesLength, "/* HTML */")
if (chunk.type === "html") previousHTML = slice
}
else if (chunk.type === "script") {
for (const action of dedent(computeIndent(indentDescriptor, previousHTML, slice), slice)) {
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)
}
}
}
})
return {
code: transformedCode,
badIndentationLines,
}
}
module.exports = extract