Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose parse5 option scriptingEnabled #1707

Merged
merged 4 commits into from
Feb 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/parsers/parse5.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ var htmlparser2Adapter = require('parse5-htmlparser2-tree-adapter');

exports.parse = function (content, options, isDocument) {
var opts = {
scriptingEnabled:
typeof options.scriptingEnabled === 'boolean'
? options.scriptingEnabled
: true,
treeAdapter: htmlparser2Adapter,
sourceCodeLocationInfo: options.sourceCodeLocationInfo,
};
Expand Down
10 changes: 10 additions & 0 deletions test/__fixtures__/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,13 @@ exports.forms = [
'<form id="textarea"><textarea name="fruits">Apple\nOrange</textarea></form>',
'<form id="spaces"><input type="text" name="fruit" value="Blood orange" /></form>',
].join('');

exports.noscript = [
'</body>',
'<noscript>',
'<!-- anchor linking to external file -->',
'<a href="https://github.com/cheeriojs/cheerio">External Link</a>',
'</noscript>',
'<p>Rocks!</p>',
'</body>',
].join('');
64 changes: 64 additions & 0 deletions test/cheerio.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,68 @@ describe('cheerio', function () {
expect(utils.isHtml('#main')).toBe(false);
});
});

describe('parse5 options', function () {
var noscript = fixtures.noscript;

// should parse noscript tags only with false option value
test('{scriptingEnabled: ???}', function () {
var opt = 'scriptingEnabled';
var options = {};
var result;

// [default] scriptingEnabled: true - tag contains one text element
result = cheerio.load(noscript)('noscript');
expect(result).toHaveLength(1);
expect(result[0].children).toHaveLength(1);
expect(result[0].children[0].type).toBe('text');

// scriptingEnabled: false - content of noscript will parsed
options[opt] = false;
result = cheerio.load(fixtures.noscript, options)('noscript');
expect(result).toHaveLength(1);
expect(result[0].children).toHaveLength(2);
expect(result[0].children[0].type).toBe('comment');
expect(result[0].children[1].type).toBe('tag');
expect(result[0].children[1].name).toBe('a');

// scriptingEnabled: ??? - should acts as true
var values = [undefined, null, 0, ''];
for (var val of values) {
options[opt] = val;
result = cheerio.load(noscript, options)('noscript');
expect(result).toHaveLength(1);
expect(result[0].children).toHaveLength(1);
expect(result[0].children[0].type).toBe('text');
}
});

// should contain location data only with truthful option value
test('{sourceCodeLocationInfo: ???}', function () {
var prop = 'sourceCodeLocation';
var opt = 'sourceCodeLocationInfo';
var options = {};
var result;
var i;

// Location data should not be present
var values = [undefined, null, 0, false, ''];
for (i = 0; i < values.length; i++) {
options[opt] = values[i];
result = cheerio.load(noscript, options)('noscript');
expect(result).toHaveLength(1);
expect(result[0]).not.toHaveProperty(prop);
}

// Location data should be present
values = [true, 1, 'test'];
for (i = 0; i < values.length; i++) {
options[opt] = values[i];
result = cheerio.load(noscript, options)('noscript');
expect(result).toHaveLength(1);
expect(result[0]).toHaveProperty(prop);
expect(typeof result[0][prop]).toBe('object');
}
});
});
});
3 changes: 3 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ declare namespace cheerio {

/** Enable location support for parse5 */
sourceCodeLocationInfo?: boolean;

/** Disable scripting in parse5, so noscript tags would be parsed */
scriptingEnabled?: boolean;
}

interface Selector {
Expand Down
8 changes: 8 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ $ = cheerio.load(html, {
xmlMode: true,
});

$ = cheerio.load(html, {
scriptingEnabled: false,
});

$ = cheerio.load(html, {
sourceCodeLocationInfo: true,
});

$ = cheerio.load(html, {
normalizeWhitespace: true,
withStartIndices: true,
Expand Down