Skip to content

Commit 7eb4cc4

Browse files
authored
feat(parse): Expose parse5 option scriptingEnabled (#1707)
* expose parse5 option scriptingEnabled * parse5 options test * add option scriptingEnabled into types * typo in comment Co-authored-by: 5saviahv <[email protected]>
1 parent 6d94fce commit 7eb4cc4

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

lib/parsers/parse5.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ var htmlparser2Adapter = require('parse5-htmlparser2-tree-adapter');
44

55
exports.parse = function (content, options, isDocument) {
66
var opts = {
7+
scriptingEnabled:
8+
typeof options.scriptingEnabled === 'boolean'
9+
? options.scriptingEnabled
10+
: true,
711
treeAdapter: htmlparser2Adapter,
812
sourceCodeLocationInfo: options.sourceCodeLocationInfo,
913
};

test/__fixtures__/fixtures.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,13 @@ exports.forms = [
8484
'<form id="textarea"><textarea name="fruits">Apple\nOrange</textarea></form>',
8585
'<form id="spaces"><input type="text" name="fruit" value="Blood orange" /></form>',
8686
].join('');
87+
88+
exports.noscript = [
89+
'</body>',
90+
'<noscript>',
91+
'<!-- anchor linking to external file -->',
92+
'<a href="https://github.com/cheeriojs/cheerio">External Link</a>',
93+
'</noscript>',
94+
'<p>Rocks!</p>',
95+
'</body>',
96+
].join('');

test/cheerio.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,68 @@ describe('cheerio', function () {
415415
expect(utils.isHtml('<123>')).toBe(false);
416416
});
417417
});
418+
419+
describe('parse5 options', function () {
420+
var noscript = fixtures.noscript;
421+
422+
// should parse noscript tags only with false option value
423+
test('{scriptingEnabled: ???}', function () {
424+
var opt = 'scriptingEnabled';
425+
var options = {};
426+
var result;
427+
428+
// [default] scriptingEnabled: true - tag contains one text element
429+
result = cheerio.load(noscript)('noscript');
430+
expect(result).toHaveLength(1);
431+
expect(result[0].children).toHaveLength(1);
432+
expect(result[0].children[0].type).toBe('text');
433+
434+
// scriptingEnabled: false - content of noscript will parsed
435+
options[opt] = false;
436+
result = cheerio.load(fixtures.noscript, options)('noscript');
437+
expect(result).toHaveLength(1);
438+
expect(result[0].children).toHaveLength(2);
439+
expect(result[0].children[0].type).toBe('comment');
440+
expect(result[0].children[1].type).toBe('tag');
441+
expect(result[0].children[1].name).toBe('a');
442+
443+
// scriptingEnabled: ??? - should acts as true
444+
var values = [undefined, null, 0, ''];
445+
for (var val of values) {
446+
options[opt] = val;
447+
result = cheerio.load(noscript, options)('noscript');
448+
expect(result).toHaveLength(1);
449+
expect(result[0].children).toHaveLength(1);
450+
expect(result[0].children[0].type).toBe('text');
451+
}
452+
});
453+
454+
// should contain location data only with truthful option value
455+
test('{sourceCodeLocationInfo: ???}', function () {
456+
var prop = 'sourceCodeLocation';
457+
var opt = 'sourceCodeLocationInfo';
458+
var options = {};
459+
var result;
460+
var i;
461+
462+
// Location data should not be present
463+
var values = [undefined, null, 0, false, ''];
464+
for (i = 0; i < values.length; i++) {
465+
options[opt] = values[i];
466+
result = cheerio.load(noscript, options)('noscript');
467+
expect(result).toHaveLength(1);
468+
expect(result[0]).not.toHaveProperty(prop);
469+
}
470+
471+
// Location data should be present
472+
values = [true, 1, 'test'];
473+
for (i = 0; i < values.length; i++) {
474+
options[opt] = values[i];
475+
result = cheerio.load(noscript, options)('noscript');
476+
expect(result).toHaveLength(1);
477+
expect(result[0]).toHaveProperty(prop);
478+
expect(typeof result[0][prop]).toBe('object');
479+
}
480+
});
481+
});
418482
});

types/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ declare namespace cheerio {
228228

229229
/** Enable location support for parse5 */
230230
sourceCodeLocationInfo?: boolean;
231+
232+
/** Disable scripting in parse5, so noscript tags would be parsed */
233+
scriptingEnabled?: boolean;
231234
}
232235

233236
interface Selector {

types/index.test-d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ $ = cheerio.load(html, {
3434
xmlMode: true,
3535
});
3636

37+
$ = cheerio.load(html, {
38+
scriptingEnabled: false,
39+
});
40+
41+
$ = cheerio.load(html, {
42+
sourceCodeLocationInfo: true,
43+
});
44+
3745
$ = cheerio.load(html, {
3846
normalizeWhitespace: true,
3947
withStartIndices: true,

0 commit comments

Comments
 (0)