Skip to content

Commit 6ed9f9c

Browse files
authored
feat: added the scriptingEnabled option (#448)
1 parent b492065 commit 6ed9f9c

13 files changed

+273
-60
lines changed

README.md

+38
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ type sources =
9090
value: string,
9191
resourcePath: string
9292
) => boolean;
93+
scriptingEnabled?: boolean;
9394
};
9495
```
9596

@@ -447,6 +448,43 @@ module.exports = {
447448
};
448449
```
449450

451+
#### `scriptingEnabled`
452+
453+
Type:
454+
455+
```ts
456+
type scriptingEnabled = boolean;
457+
```
458+
459+
Default: `true`
460+
461+
By default, the parser in `html-loader` interprets content inside `<noscript>` tags as `#text`, so processing of content inside this tag will be ignored.
462+
463+
In order to enable processing inside `<noscript>` for content recognition by the parser as `#AST`, set this parameter to: `false`
464+
465+
Additional information: [scriptingEnabled](https://parse5.js.org/interfaces/parse5.ParserOptions.html#scriptingEnabled)
466+
467+
**webpack.config.js**
468+
469+
```js
470+
module.exports = {
471+
module: {
472+
rules: [
473+
{
474+
test: /\.html$/i,
475+
loader: "html-loader",
476+
options: {
477+
sources: {
478+
// Enables processing inside the <noscript> tag
479+
scriptingEnabled: false,
480+
},
481+
},
482+
},
483+
],
484+
},
485+
};
486+
```
487+
450488
### `preprocessor`
451489

452490
Type:

src/options.json

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
},
5757
"urlFilter": {
5858
"instanceof": "Function"
59+
},
60+
"scriptingEnabled": {
61+
"type": "boolean"
5962
}
6063
},
6164
"additionalProperties": false

src/plugins/sources-plugin.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import {
1010
export default (options) =>
1111
function process(html) {
1212
const sources = [];
13-
const document = parse(html, { sourceCodeLocationInfo: true });
13+
const document = parse(html, {
14+
sourceCodeLocationInfo: true,
15+
scriptingEnabled: options.sources.scriptingEnabled,
16+
});
1417

1518
let needIgnore = false;
1619

src/utils.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,14 @@ function getSourcesOption(rawOptions) {
11221122

11231123
const sources = normalizeSourcesList(rawOptions.sources.list);
11241124

1125-
return { list: sources, urlFilter: rawOptions.sources.urlFilter };
1125+
return {
1126+
list: sources,
1127+
urlFilter: rawOptions.sources.urlFilter,
1128+
scriptingEnabled:
1129+
typeof rawOptions.sources.scriptingEnabled === "undefined"
1130+
? true
1131+
: rawOptions.sources.scriptingEnabled,
1132+
};
11261133
}
11271134

11281135
export function normalizeOptions(rawOptions, loaderContext) {

test/__snapshots__/esModule-option.test.js.snap

+27-6
Large diffs are not rendered by default.

test/__snapshots__/loader.test.js.snap

+9-2
Large diffs are not rendered by default.

test/__snapshots__/minimize-option.test.js.snap

+39-12
Large diffs are not rendered by default.

test/__snapshots__/sources-option.test.js.snap

+126-32
Large diffs are not rendered by default.

test/__snapshots__/validate-options.test.js.snap

+10-5
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ exports[`validate options should throw an error on the "preprocessor" option wit
2929
exports[`validate options should throw an error on the "sources" option with "[]" value 1`] = `
3030
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
3131
- options.sources should be one of these:
32-
boolean | object { list?, urlFilter? }
32+
boolean | object { list?, urlFilter?, scriptingEnabled? }
3333
-> By default every loadable attributes (for example - <img src='image.png'>) is imported (const img = require('./image.png'). You may need to specify loaders for images in your configuration.
3434
-> Read more at https://github.com/webpack-contrib/html-loader#sources
3535
Details:
3636
* options.sources should be a boolean.
3737
* options.sources should be an object:
38-
object { list?, urlFilter? }"
38+
object { list?, urlFilter?, scriptingEnabled? }"
3939
`;
4040
4141
exports[`validate options should throw an error on the "sources" option with "{"list":[]}" value 1`] = `
@@ -64,10 +64,15 @@ exports[`validate options should throw an error on the "sources" option with "{"
6464
\\"src\\" | \\"srcset\\""
6565
`;
6666
67+
exports[`validate options should throw an error on the "sources" option with "{"scriptingEnabled":"true"}" value 1`] = `
68+
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
69+
- options.sources.scriptingEnabled should be a boolean."
70+
`;
71+
6772
exports[`validate options should throw an error on the "sources" option with "{"unknown":true}" value 1`] = `
6873
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
6974
- options.sources has an unknown property 'unknown'. These properties are valid:
70-
object { list?, urlFilter? }"
75+
object { list?, urlFilter?, scriptingEnabled? }"
7176
`;
7277
7378
exports[`validate options should throw an error on the "sources" option with "{"urlFilter":false}" value 1`] = `
@@ -78,13 +83,13 @@ exports[`validate options should throw an error on the "sources" option with "{"
7883
exports[`validate options should throw an error on the "sources" option with "true" value 1`] = `
7984
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
8085
- options.sources should be one of these:
81-
boolean | object { list?, urlFilter? }
86+
boolean | object { list?, urlFilter?, scriptingEnabled? }
8287
-> By default every loadable attributes (for example - <img src='image.png'>) is imported (const img = require('./image.png'). You may need to specify loaders for images in your configuration.
8388
-> Read more at https://github.com/webpack-contrib/html-loader#sources
8489
Details:
8590
* options.sources should be a boolean.
8691
* options.sources should be an object:
87-
object { list?, urlFilter? }"
92+
object { list?, urlFilter?, scriptingEnabled? }"
8893
`;
8994
9095
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `

test/fixtures/noscript.png

70 Bytes
Loading

test/fixtures/simple.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -447,4 +447,8 @@ <h2>An Ordered HTML List</h2>
447447
<template type="some_type">
448448
<div class="will-not-be-parse"></div>
449449
</template>
450-
</div>
450+
</div>
451+
452+
<noscript>
453+
<img src="./noscript.png" alt="noscript content" />
454+
</noscript>

test/sources-option.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ describe("'sources' option", () => {
297297

298298
return true;
299299
},
300+
scriptingEnabled: false,
300301
},
301302
});
302303
const stats = await compile(compiler);

test/validate-options.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ describe("validate options", () => {
5858
],
5959
},
6060
{ urlFilter: () => true },
61+
{ scriptingEnabled: true },
62+
{ scriptingEnabled: false },
6163
{
6264
list: [
6365
{
@@ -122,6 +124,7 @@ describe("validate options", () => {
122124
],
123125
},
124126
{ urlFilter: false },
127+
{ scriptingEnabled: "true" },
125128
{ unknown: true },
126129
],
127130
},

0 commit comments

Comments
 (0)