From ffa5dee7219f4172da921a68a6f2f10043396812 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Tue, 10 May 2022 18:40:52 +0800 Subject: [PATCH] Use linkedom in readability tests --- .../readabilityjs/test/test-readability.js | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/packages/readabilityjs/test/test-readability.js b/packages/readabilityjs/test/test-readability.js index 0c8154a3e..17b287b6d 100644 --- a/packages/readabilityjs/test/test-readability.js +++ b/packages/readabilityjs/test/test-readability.js @@ -1,6 +1,8 @@ var JSDOM = require("jsdom").JSDOM; var chai = require("chai"); var sinon = require("sinon"); +const { parseHTML } = require("linkedom"); + chai.config.includeStack = true; var expect = chai.expect; @@ -52,7 +54,7 @@ function htmlTransform(str) { return str.replace(/\s+/g, " "); } -function runTestsWithItems(label, domGenerationFn, source, expectedContent, expectedMetadata) { +function runTestsWithItems(label, domGenerationFn, source, expectedContent, expectedMetadata, uri) { describe(label, function() { this.timeout(30000); @@ -63,7 +65,7 @@ function runTestsWithItems(label, domGenerationFn, source, expectedContent, expe var doc = domGenerationFn(source); // Provide one class name to preserve, which we know appears in a few // of the test documents. - var myReader = new Readability(doc, { classesToPreserve: ["caption"] }); + var myReader = new Readability(doc, { classesToPreserve: ["caption"], url: uri }); result = myReader.parse(); } catch (err) { throw reformatError(err); @@ -227,7 +229,7 @@ describe("Readability API", function() { }); it("should run _cleanClasses with default configuration", function() { - var doc = new JSDOM(exampleSource).window.document; + var doc = parseHTML(exampleSource).document; var parser = new Readability(doc); parser._cleanClasses = sinon.fake(); @@ -238,7 +240,7 @@ describe("Readability API", function() { }); it("should run _cleanClasses when option keepClasses = false", function() { - var doc = new JSDOM(exampleSource).window.document; + var doc = parseHTML(exampleSource).document; var parser = new Readability(doc, {keepClasses: false}); parser._cleanClasses = sinon.fake(); @@ -249,7 +251,7 @@ describe("Readability API", function() { }); it("shouldn't run _cleanClasses when option keepClasses = true", function() { - var doc = new JSDOM(exampleSource).window.document; + var doc = parseHTML(exampleSource).document; var parser = new Readability(doc, {keepClasses: true}); parser._cleanClasses = sinon.fake(); @@ -272,30 +274,30 @@ describe("Readability API", function() { }); it("should not proxy image with data uri", function() { - var dom = new JSDOM("My cat: My cat: \"Red"); - var expected_xhtml = "
My cat: \"Red"); + var expected_xhtml = "
My cat: \"Red
"; - var content = new Readability(dom.window.document).parse().content; + "Y4OHwAAAABJRU5ErkJggg==\" alt=\"Red dot\">
"; + var content = new Readability(dom.document).parse().content; expect(content).eql(expected_xhtml); }); it("should handle srcset elements with density descriptors", function() { - var dom = new JSDOM('My image: My image: ' - ); - var expected_xhtml = '
My image: ' + + 'https://webkit.org/demos/srcset/image-4x.png 4x">' + + ''); + var expected_xhtml = '
My image: ' + '
'; - var content = new Readability(dom.window.document, { + 'https://webkit.org/demos/srcset/image-4x.png 4x,">
'; + var content = new Readability(dom.document, { createImageProxyUrl: function(url) { return url; } @@ -304,11 +306,11 @@ describe("Readability API", function() { }); it("should remove srcset elements that are lazy loading placeholders", function() { - var dom = new JSDOM('My image: '); - var expected_xhtml = '
' + - 'My image: ' + - '
' - var content = new Readability(dom.window.document, { + var dom = parseHTML('My image: '); + var expected_xhtml = '
' + + 'My image: ' + + '
'; + var content = new Readability(dom.document, { createImageProxyUrl: function(url) { return url; } @@ -324,12 +326,10 @@ describe("Test pages", function() { var uri = "http://fakehost/test/page.html"; runTestsWithItems("jsdom", function(source) { - var doc = new JSDOM(source, { - url: uri, - }).window.document; + var doc =parseHTML(source).document; removeCommentNodesRecursively(doc); return doc; - }, testPage.source, testPage.expectedContent, testPage.expectedMetadata); + }, testPage.source, testPage.expectedContent, testPage.expectedMetadata, uri); // runTestsWithItems("JSDOMParser", function(source) { // var parser = new JSDOMParser();