Added "keepClasses" option to prevent cleaning of classes (#552)

pull/554/head
Jordy van den Aardweg 5 years ago committed by Gijs
parent f33a6c2a23
commit 2982216913

@ -48,6 +48,7 @@ function Readability(doc, options) {
this._nbTopCandidates = options.nbTopCandidates || this.DEFAULT_N_TOP_CANDIDATES; this._nbTopCandidates = options.nbTopCandidates || this.DEFAULT_N_TOP_CANDIDATES;
this._charThreshold = options.charThreshold || this.DEFAULT_CHAR_THRESHOLD; this._charThreshold = options.charThreshold || this.DEFAULT_CHAR_THRESHOLD;
this._classesToPreserve = this.CLASSES_TO_PRESERVE.concat(options.classesToPreserve || []); this._classesToPreserve = this.CLASSES_TO_PRESERVE.concat(options.classesToPreserve || []);
this._keepClasses = !!options.keepClasses;
// Start with all flags set // Start with all flags set
this._flags = this.FLAG_STRIP_UNLIKELYS | this._flags = this.FLAG_STRIP_UNLIKELYS |
@ -163,8 +164,10 @@ Readability.prototype = {
// Readability cannot open relative uris so we convert them to absolute uris. // Readability cannot open relative uris so we convert them to absolute uris.
this._fixRelativeUris(articleContent); this._fixRelativeUris(articleContent);
// Remove classes. if (!this._keepClasses) {
this._cleanClasses(articleContent); // Remove classes.
this._cleanClasses(articleContent);
}
}, },
/** /**

@ -30,6 +30,7 @@
"js-beautify": "^1.5.5", "js-beautify": "^1.5.5",
"jsdom": "^13.1", "jsdom": "^13.1",
"matcha": "^0.6.0", "matcha": "^0.6.0",
"mocha": "^2.2.*" "mocha": "^2.2.*",
"sinon": "^7.3.2"
} }
} }

@ -1,5 +1,6 @@
var JSDOM = require("jsdom").JSDOM; var JSDOM = require("jsdom").JSDOM;
var chai = require("chai"); var chai = require("chai");
var sinon = require("sinon");
chai.config.includeStack = true; chai.config.includeStack = true;
var expect = chai.expect; var expect = chai.expect;
@ -201,15 +202,57 @@ describe("Readability API", function() {
expect(new Readability(doc)._maxElemsToParse).eql(0); expect(new Readability(doc)._maxElemsToParse).eql(0);
expect(new Readability(doc, {maxElemsToParse: 42})._maxElemsToParse).eql(42); 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() { describe("#parse", function() {
var exampleSource = testPages[0].source;
it("shouldn't parse oversized documents as per configuration", function() { it("shouldn't parse oversized documents as per configuration", function() {
var doc = new JSDOMParser().parse("<html><div>yo</div></html>"); var doc = new JSDOMParser().parse("<html><div>yo</div></html>");
expect(function() { expect(function() {
new Readability(doc, {maxElemsToParse: 1}).parse(); new Readability(doc, {maxElemsToParse: 1}).parse();
}).to.Throw("Aborting parsing document; 2 elements found"); }).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);
});
}); });
}); });

Loading…
Cancel
Save