Skip to content

Commit

Permalink
feat(parse): Expose parse5 option scriptingEnabled (#1707)
Browse files Browse the repository at this point in the history
* expose parse5 option scriptingEnabled

* parse5 options test

* add option scriptingEnabled into types

* typo in comment

Co-authored-by: 5saviahv <5saviahv@users.noreply.github.com>
  • Loading branch information
5saviahv and 5saviahv committed Feb 7, 2021
1 parent 6d94fce commit 7eb4cc4
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/parsers/parse5.js
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
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
Expand Up @@ -415,4 +415,68 @@ describe('cheerio', function () {
expect(utils.isHtml('<123>')).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
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
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

0 comments on commit 7eb4cc4

Please sign in to comment.