Skip to content

Commit

Permalink
Add test for input containing invalid attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
dbowring committed Oct 21, 2024
1 parent 8bf0a99 commit f48f5e3
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 19 deletions.
54 changes: 46 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"mocha": "10.1.0",
"prettier": "^3.3.2",
"release-it": "17.0.1",
"sinon": "14.0.2"
"sinon": "14.0.2",
"xml-name-validator": "^5.0.0"
}
}
9 changes: 9 additions & 0 deletions test/test-pages/invalid-attributes/expected-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"title": "Lorem Ipsum",
"byline": null,
"dir": null,
"excerpt": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"siteName": null,
"publishedTime": null,
"readerable": false
}
7 changes: 7 additions & 0 deletions test/test-pages/invalid-attributes/expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div id="readability-page-1" class="page">
<div "="">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
</div>
</div>
19 changes: 19 additions & 0 deletions test/test-pages/invalid-attributes/source.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title>Lorem Ipsum</title>
</head>
<body>
<main>
<section>
<div "="">
<div>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</div>
</div>
</section>
</main>
</body>
</html>
24 changes: 14 additions & 10 deletions test/test-readability.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env node, mocha */

var JSDOM = require("jsdom").JSDOM;
var xmlNameValidator = require("xml-name-validator").name;
var chai = require("chai");
var sinon = require("sinon");
chai.config.includeStack = true;
Expand Down Expand Up @@ -121,14 +122,17 @@ function runTestsWithItems(

function attributesForNode(node) {
return Array.from(node.attributes)
.filter(function(attr) {
return xmlNameValidator(attr.name)
})
.map(function (attr) {
return attr.name + "=" + attr.value;
})
.join(",");
}

var actualDOM = domGenerationFn(prettyPrint(result.content));
var expectedDOM = domGenerationFn(prettyPrint(expectedContent));

traverseDOM(
function (actualNode, expectedNode) {
if (actualNode && expectedNode) {
Expand All @@ -152,21 +156,21 @@ function runTestsWithItems(
}
// Compare attributes for element nodes:
} else if (actualNode.nodeType == 1) {
var actualNodeDesc = attributesForNode(actualNode);
var expectedNodeDesc = attributesForNode(expectedNode);
var actualNodeAttributes = attributesForNode(actualNode);
var expectedNodeAttributes = attributesForNode(expectedNode);
var desc =
"node " +
nodeStr(actualNode) +
" attributes (" +
actualNodeDesc +
actualNodeAttributes.join(",") +
") should match (" +
expectedNodeDesc +
") ";
expect(actualNode.attributes.length, desc).eql(
expectedNode.attributes.length
expectedNodeAttributes.join(",") +
") 1";
expect(actualNodeAttributes.length, desc).eql(
expectedNodeAttributes.length
);
for (var i = 0; i < actualNode.attributes.length; i++) {
var attr = actualNode.attributes[i].name;
for (var i = 0; i < actualNodeAttributes.length; i++) {
var attr = actualNodeAttributes[i].name;
var actualValue = actualNode.getAttribute(attr);
var expectedValue = expectedNode.getAttribute(attr);
expect(
Expand Down

0 comments on commit f48f5e3

Please sign in to comment.