diff --git a/Readability.js b/Readability.js index 45048d5..27c494b 100644 --- a/Readability.js +++ b/Readability.js @@ -48,6 +48,7 @@ function Readability(doc, options) { this._nbTopCandidates = options.nbTopCandidates || this.DEFAULT_N_TOP_CANDIDATES; this._charThreshold = options.charThreshold || this.DEFAULT_CHAR_THRESHOLD; this._classesToPreserve = this.CLASSES_TO_PRESERVE.concat(options.classesToPreserve || []); + this._keepClasses = !!options.keepClasses; // Start with all flags set this._flags = this.FLAG_STRIP_UNLIKELYS | @@ -163,8 +164,10 @@ Readability.prototype = { // Readability cannot open relative uris so we convert them to absolute uris. this._fixRelativeUris(articleContent); - // Remove classes. - this._cleanClasses(articleContent); + if (!this._keepClasses) { + // Remove classes. + this._cleanClasses(articleContent); + } }, /** diff --git a/package.json b/package.json index dd158fb..4504e91 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "js-beautify": "^1.5.5", "jsdom": "^13.1", "matcha": "^0.6.0", - "mocha": "^2.2.*" + "mocha": "^2.2.*", + "sinon": "^7.3.2" } } diff --git a/test/test-readability.js b/test/test-readability.js index be3579e..c92ae8f 100644 --- a/test/test-readability.js +++ b/test/test-readability.js @@ -1,5 +1,6 @@ var JSDOM = require("jsdom").JSDOM; var chai = require("chai"); +var sinon = require("sinon"); chai.config.includeStack = true; var expect = chai.expect; @@ -201,15 +202,57 @@ describe("Readability API", function() { expect(new Readability(doc)._maxElemsToParse).eql(0); expect(new Readability(doc, {maxElemsToParse: 42})._maxElemsToParse).eql(42); }); + + it("should accept a keepClasses option", function() { + expect(new Readability(doc)._keepClasses).eql(false); + expect(new Readability(doc, {keepClasses: true})._keepClasses).eql(true); + expect(new Readability(doc, {keepClasses: false})._keepClasses).eql(false); + }); }); describe("#parse", function() { + var exampleSource = testPages[0].source; + it("shouldn't parse oversized documents as per configuration", function() { var doc = new JSDOMParser().parse("
yo
"); expect(function() { new Readability(doc, {maxElemsToParse: 1}).parse(); }).to.Throw("Aborting parsing document; 2 elements found"); }); + + it("should run _cleanClasses with default configuration", function() { + var doc = new JSDOMParser().parse(exampleSource); + var parser = new Readability(doc); + + parser._cleanClasses = sinon.fake(); + + parser.parse(); + + expect(parser._cleanClasses.called).eql(true); + }); + + it("should run _cleanClasses when option keepClasses = false", function() { + var doc = new JSDOMParser().parse(exampleSource); + var parser = new Readability(doc, {keepClasses: false}); + + parser._cleanClasses = sinon.fake(); + + parser.parse(); + + expect(parser._cleanClasses.called).eql(true); + }); + + it("shouldn't run _cleanClasses when option keepClasses = true", function() { + var doc = new JSDOMParser().parse(exampleSource); + var parser = new Readability(doc, {keepClasses: true}); + + parser._cleanClasses = sinon.fake(); + + parser.parse(); + + expect(parser._cleanClasses.called).eql(false); + }); + }); });