From 170634bfa1e3eb0862c0b383493b0364bc95a2b7 Mon Sep 17 00:00:00 2001 From: 5saviahv <5saviahv@users.noreply.github.com> Date: Mon, 1 Feb 2021 04:27:38 +0200 Subject: [PATCH 1/4] expose parse5 option scriptingEnabled --- lib/parsers/parse5.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/parsers/parse5.js b/lib/parsers/parse5.js index 870103c3f5..e20bc84419 100644 --- a/lib/parsers/parse5.js +++ b/lib/parsers/parse5.js @@ -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, }; From 324ab2e671d4fbfe0ef0cd55a128573b043d05fc Mon Sep 17 00:00:00 2001 From: 5saviahv <5saviahv@users.noreply.github.com> Date: Sat, 6 Feb 2021 22:45:46 +0200 Subject: [PATCH 2/4] parse5 options test --- test/__fixtures__/fixtures.js | 10 ++++++ test/cheerio.js | 64 +++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/test/__fixtures__/fixtures.js b/test/__fixtures__/fixtures.js index 79f85ca3f0..e232515c1c 100644 --- a/test/__fixtures__/fixtures.js +++ b/test/__fixtures__/fixtures.js @@ -84,3 +84,13 @@ exports.forms = [ '
', '
', ].join(''); + +exports.noscript = [ + '', + '', + '

Rocks!

', + '', +].join(''); diff --git a/test/cheerio.js b/test/cheerio.js index bbf23528f7..30a94bdc35 100644 --- a/test/cheerio.js +++ b/test/cheerio.js @@ -409,4 +409,68 @@ describe('cheerio', function () { expect(utils.isHtml('#main')).toBe(false); }); }); + + describe('parse5 options', function () { + var noscript = fixtures.noscript; + + // should parse nodata 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'); + } + }); + }); }); From b3301e511aa3ef67adbb05f84d48416c4881fb3e Mon Sep 17 00:00:00 2001 From: 5saviahv <5saviahv@users.noreply.github.com> Date: Sat, 6 Feb 2021 22:54:58 +0200 Subject: [PATCH 3/4] add option scriptingEnabled into types --- types/index.d.ts | 3 +++ types/index.test-d.ts | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/types/index.d.ts b/types/index.d.ts index 3d7cd34ce7..536efa0310 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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 { diff --git a/types/index.test-d.ts b/types/index.test-d.ts index c86fcbfe0c..196b945c2c 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -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, From 6cdb0168a3ccd0eade61f081d2e499a857de393e Mon Sep 17 00:00:00 2001 From: 5saviahv <5saviahv@users.noreply.github.com> Date: Sat, 6 Feb 2021 23:00:10 +0200 Subject: [PATCH 4/4] typo in comment --- test/cheerio.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cheerio.js b/test/cheerio.js index 30a94bdc35..8f3d1ba4bc 100644 --- a/test/cheerio.js +++ b/test/cheerio.js @@ -413,7 +413,7 @@ describe('cheerio', function () { describe('parse5 options', function () { var noscript = fixtures.noscript; - // should parse nodata only with false option value + // should parse noscript tags only with false option value test('{scriptingEnabled: ???}', function () { var opt = 'scriptingEnabled'; var options = {};