Skip to content

Commit

Permalink
Check fragment identifier integrity. Closes #105
Browse files Browse the repository at this point in the history
  • Loading branch information
Munter committed Oct 17, 2016
1 parent 184539a commit d972ff4
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,53 @@ module.exports = function (options) {
followRelations: relationsQuery,
concurrency: options.concurrency || 100
})
.queue(function (assetGraph) {
t.push({
name: 'Checking fragment identifier references'
});

assetGraph.findAssets({
type: 'Html',
isLoaded: true
}).forEach(function (asset) {
var document = asset.parseTree;
var fragmentLinks = [].slice.call(document.querySelectorAll('a[href^="#"]'));

fragmentLinks.forEach(function (anchor) {
var selector = anchor.href.replace('about:blank', '');

t.push(null, {
ok: !!document.querySelector(selector),
name: 'Fragment identifier check: ' + asset.fileName + selector,
operator: 'missing-fragment',
actual: null,
expected: 'id="' + selector.replace('#', '') + '"',
at: asset.urlOrDescription.replace(/#.*$/, '')
});
});
});

assetGraph.findRelations({
type: 'HtmlAnchor',
crossorigin: false,
href: /#.*/,
to: {
type: 'Html'
}
}).forEach(function (relation) {
var document = relation.to.parseTree;
var selector = relation.href.replace(/^[^#]*/, '');

t.push(null, {
ok: !!document.querySelector(selector),
name: 'Fragment identifier check: ' + relation.href,
operator: 'missing-fragment',
actual: null,
expected: 'id="' + selector.replace('#', '') + '"',
at: relation.from.urlOrDescription.replace(/#.*$/, '')
});
});
})
.queue(function (assetGraph, callback) {
var hrefMap = {};

Expand Down
30 changes: 30 additions & 0 deletions testdata/fragmentIdentifier/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

<div id="one"></div>
<div id="two"></div>
<div id="three"></div>

<a href="#one"></a>
<a href="#two"></a>
<a href="#three"></a>

<a href="#broken-one"></a>
<a href="#broken-two"></a>
<a href="#broken-three"></a>

<a href="page.html#one"></a>
<a href="page.html#two"></a>
<a href="page.html#three"></a>

<a href="page.html#broken-one"></a>
<a href="page.html#broken-two"></a>
<a href="page.html#broken-three"></a>

</body>
</html>
14 changes: 14 additions & 0 deletions testdata/fragmentIdentifier/page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

<div id="one"></div>
<div id="two"></div>
<div id="three"></div>

</body>
</html>

0 comments on commit d972ff4

Please sign in to comment.