Skip to content

Commit

Permalink
Add option.serializer, fixes mozilla#605 (mozilla#607)
Browse files Browse the repository at this point in the history
  • Loading branch information
danburzo authored Aug 4, 2020
1 parent 52ab9b5 commit b1d15c0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,4 @@ module.exports = {
// Only check typeof against valid results
"valid-typeof": 2,
},
}
};
5 changes: 4 additions & 1 deletion Readability.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ function Readability(doc, options) {
this._charThreshold = options.charThreshold || this.DEFAULT_CHAR_THRESHOLD;
this._classesToPreserve = this.CLASSES_TO_PRESERVE.concat(options.classesToPreserve || []);
this._keepClasses = !!options.keepClasses;
this._serializer = options.serializer || function(el) {
return el.innerHTML;
};

// Start with all flags set
this._flags = this.FLAG_STRIP_UNLIKELYS |
Expand Down Expand Up @@ -2057,7 +2060,7 @@ Readability.prototype = {
title: this._articleTitle,
byline: metadata.byline || this._articleByline,
dir: this._articleDir,
content: articleContent.innerHTML,
content: this._serializer(articleContent),
textContent: textContent,
length: textContent.length,
excerpt: metadata.excerpt,
Expand Down
11 changes: 11 additions & 0 deletions test/test-readability.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,17 @@ describe("Readability API", function() {
expect(parser._cleanClasses.called).eql(false);
});

it("should use custom content serializer sent as option", function() {
var dom = new JSDOM("My cat: <img src=''>");
var expected_xhtml = "<div xmlns=\"http://www.w3.org/1999/xhtml\" id=\"readability-page-1\" class=\"page\">My cat: <img src=\"\" /></div>";
var xml = new dom.window.XMLSerializer();
var content = new Readability(dom.window.document, {
serializer: function(el) {
return xml.serializeToString(el.firstChild);
}
}).parse().content;
expect(content).eql(expected_xhtml);
});
});
});

Expand Down

0 comments on commit b1d15c0

Please sign in to comment.