Skip to content

Commit e6d0e43

Browse files
authoredJul 18, 2022
Merge pull request #137 from krispy1298/master
allow to pass an array of <script>-like tags to parse
2 parents c703a77 + 21ca3c3 commit e6d0e43

File tree

6 files changed

+112
-49
lines changed

6 files changed

+112
-49
lines changed
 

‎README.md

+35-15
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@
66
<p>A <a href="http://eslint.org">ESLint</a> plugin to lint and fix inline scripts contained in HTML files.</p>
77
</div>
88

9-
* [Usage](#usage)
10-
* [Multiple scripts tags in a HTML file](#multiple-scripts-tags-in-a-html-file)
11-
* [XML Support](#xml-support)
12-
* [Settings](#settings)
13-
* [`html/html-extensions`](#htmlhtml-extensions)
14-
* [`html/xml-extensions`](#htmlxml-extensions)
15-
* [`html/indent`](#htmlindent)
16-
* [`html/report-bad-indent`](#htmlreport-bad-indent)
17-
* [`html/javascript-mime-types`](#htmljavascript-mime-types)
18-
* [Troubleshooting](#troubleshooting)
19-
* [No file linted when running `eslint` on a directory](#no-file-linted-when-running-eslint-on-a-directory)
20-
* [Linting templates (or PHP)](#linting-templates-or-php)
21-
* [Linting Vue files](#linting-vue-files)
22-
* [Migration from older versions](#migration-from-older-versions)
23-
* [Credits](#credits)
9+
- [Usage](#usage)
10+
- [Multiple scripts tags in a HTML file](#multiple-scripts-tags-in-a-html-file)
11+
- [History](#history)
12+
- [XML support](#xml-support)
13+
- [Settings](#settings)
14+
- [`html/html-extensions`](#htmlhtml-extensions)
15+
- [`html/xml-extensions`](#htmlxml-extensions)
16+
- [`html/indent`](#htmlindent)
17+
- [`html/report-bad-indent`](#htmlreport-bad-indent)
18+
- [`html/javascript-tag-names`](#htmljavascript-tag-names)
19+
- [`html/javascript-mime-types`](#htmljavascript-mime-types)
20+
- [Troubleshooting](#troubleshooting)
21+
- [No file linted when running `eslint` on a directory](#no-file-linted-when-running-eslint-on-a-directory)
22+
- [Linting templates (or PHP)](#linting-templates-or-php)
23+
- [Linting VUE files](#linting-vue-files)
24+
- [Migration from older versions](#migration-from-older-versions)
25+
- [To v4](#to-v4)
26+
- [To v3](#to-v3)
27+
- [Credits](#credits)
2428

2529
## Usage
2630

@@ -158,6 +162,22 @@ display errors. Example:
158162
}
159163
```
160164

165+
### `html/javascript-tag-names`
166+
167+
By default, the code between `<script>` tags is considered as JavaScript. You can customize which
168+
tags should be considered JavaScript by providing one or multiple tag names.
169+
170+
Example:
171+
172+
```javascript
173+
{
174+
"plugins": [ "html" ],
175+
"settings": {
176+
"html/javascript-tag-names": ["script", "customscript"],
177+
}
178+
}
179+
```
180+
161181
### `html/javascript-mime-types`
162182

163183
By default, the code between `<script>` tags is considered as JavaScript code only if there is no

‎src/__tests__/__snapshots__/extract.js.snap

+9
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ Array [
4646
]
4747
`;
4848

49+
exports[`extract multiple tags types 1`] = `
50+
Array [
51+
"var foo = 1;
52+
",
53+
"var bar = 1;
54+
",
55+
]
56+
`;
57+
4958
exports[`extract script containing 'lower than' characters correctly (#1) 1`] = `
5059
Array [
5160
"if (a < b) { doit(); }

‎src/__tests__/extract.js

+17
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function test(params) {
2525
dedent(params.input),
2626
params.indent,
2727
params.xmlMode,
28+
params.javaScriptTagNames || ["script"],
2829
params.isJavaScriptMIMEType
2930
)
3031
expect(infos.code.map((code) => code.toString())).toMatchSnapshot()
@@ -317,3 +318,19 @@ it("skips script with src attributes", () => {
317318
input: '<script src="foo"></script>',
318319
})
319320
})
321+
322+
it("extract multiple tags types", () => {
323+
test({
324+
input: `
325+
some html
326+
<script>
327+
var foo = 1;
328+
</script>
329+
other
330+
<customscript>
331+
var bar = 1;
332+
</customscript>
333+
`,
334+
javaScriptTagNames: ["script", "customscript"],
335+
})
336+
})

‎src/extract.js

+45-34
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function iterateScripts(code, options, onChunk) {
88

99
const xmlMode = options.xmlMode
1010
const isJavaScriptMIMEType = options.isJavaScriptMIMEType || (() => true)
11+
const javaScriptTagNames = options.javaScriptTagNames || ["script"]
1112
let index = 0
1213
let inScript = false
1314
let cdata = []
@@ -23,7 +24,7 @@ function iterateScripts(code, options, onChunk) {
2324
{
2425
onopentag(name, attrs) {
2526
// Test if current tag is a valid <script> tag.
26-
if (name !== "script") {
27+
if (!javaScriptTagNames.includes(name)) {
2728
return
2829
}
2930

@@ -53,7 +54,7 @@ function iterateScripts(code, options, onChunk) {
5354
},
5455

5556
onclosetag(name) {
56-
if (name !== "script" || !inScript) {
57+
if (!javaScriptTagNames.includes(name) || !inScript) {
5758
return
5859
}
5960

@@ -179,47 +180,57 @@ function* dedent(indent, slice) {
179180
}
180181
}
181182

182-
function extract(code, indentDescriptor, xmlMode, isJavaScriptMIMEType) {
183+
function extract(
184+
code,
185+
indentDescriptor,
186+
xmlMode,
187+
javaScriptTagNames,
188+
isJavaScriptMIMEType
189+
) {
183190
const badIndentationLines = []
184191
const codeParts = []
185192
let lineNumber = 1
186193
let previousHTML = ""
187194

188-
iterateScripts(code, { xmlMode, isJavaScriptMIMEType }, (chunk) => {
189-
const slice = code.slice(chunk.start, chunk.end)
190-
if (chunk.type === "html") {
191-
const match = slice.match(/\r\n|\n|\r/g)
192-
if (match) lineNumber += match.length
193-
previousHTML = slice
194-
} else if (chunk.type === "script") {
195-
const transformedCode = new TransformableString(code)
196-
let indentSlice = slice
197-
for (const cdata of chunk.cdata) {
198-
transformedCode.replace(cdata.start, cdata.end, "")
199-
if (cdata.end === chunk.end) {
200-
indentSlice = code.slice(chunk.start, cdata.start)
195+
iterateScripts(
196+
code,
197+
{ xmlMode, javaScriptTagNames, isJavaScriptMIMEType },
198+
(chunk) => {
199+
const slice = code.slice(chunk.start, chunk.end)
200+
if (chunk.type === "html") {
201+
const match = slice.match(/\r\n|\n|\r/g)
202+
if (match) lineNumber += match.length
203+
previousHTML = slice
204+
} else if (chunk.type === "script") {
205+
const transformedCode = new TransformableString(code)
206+
let indentSlice = slice
207+
for (const cdata of chunk.cdata) {
208+
transformedCode.replace(cdata.start, cdata.end, "")
209+
if (cdata.end === chunk.end) {
210+
indentSlice = code.slice(chunk.start, cdata.start)
211+
}
201212
}
202-
}
203-
transformedCode.replace(0, chunk.start, "")
204-
transformedCode.replace(chunk.end, code.length, "")
205-
for (const action of dedent(
206-
computeIndent(indentDescriptor, previousHTML, indentSlice),
207-
indentSlice
208-
)) {
209-
lineNumber += 1
210-
if (action.type === "dedent") {
211-
transformedCode.replace(
212-
chunk.start + action.from,
213-
chunk.start + action.to,
214-
""
215-
)
216-
} else if (action.type === "bad-indent") {
217-
badIndentationLines.push(lineNumber)
213+
transformedCode.replace(0, chunk.start, "")
214+
transformedCode.replace(chunk.end, code.length, "")
215+
for (const action of dedent(
216+
computeIndent(indentDescriptor, previousHTML, indentSlice),
217+
indentSlice
218+
)) {
219+
lineNumber += 1
220+
if (action.type === "dedent") {
221+
transformedCode.replace(
222+
chunk.start + action.from,
223+
chunk.start + action.to,
224+
""
225+
)
226+
} else if (action.type === "bad-indent") {
227+
badIndentationLines.push(lineNumber)
228+
}
218229
}
230+
codeParts.push(transformedCode)
219231
}
220-
codeParts.push(transformedCode)
221232
}
222-
})
233+
)
223234

224235
return {
225236
code: codeParts,

‎src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ function patch(Linter) {
146146
textOrSourceCode,
147147
pluginSettings.indent,
148148
mode === "xml",
149+
pluginSettings.javaScriptTagNames,
149150
pluginSettings.isJavaScriptMIMEType
150151
)
151152

‎src/settings.js

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ function getSettings(settings) {
5050
getSetting(settings, "xml-extensions") ||
5151
filterOut(defaultXMLExtensions, getSetting(settings, "html-extensions"))
5252

53+
const javaScriptTagNames = getSetting(settings, "javascript-tag-names") || [
54+
"script",
55+
]
56+
5357
let reportBadIndent
5458
switch (getSetting(settings, "report-bad-indent")) {
5559
case undefined:
@@ -102,6 +106,7 @@ function getSettings(settings) {
102106
return {
103107
htmlExtensions,
104108
xmlExtensions,
109+
javaScriptTagNames,
105110
indent,
106111
reportBadIndent,
107112
isJavaScriptMIMEType,

0 commit comments

Comments
 (0)
Please sign in to comment.