Skip to content

Commit

Permalink
Improve fragmetn identifier checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Munter committed Oct 21, 2016
1 parent 72716b9 commit 22df4d6
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 9 deletions.
11 changes: 11 additions & 0 deletions lib/assetNodeOffsetDescription.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = function assetNodeOffsetDescription(node, asset) {
if (asset.type !== 'Html') {
return '0:0';
}

var htmlString = asset.rawSrc.toString();
var linesBeforeNode = htmlString.split(node.outerHTML)[0].split('\n');
var charsBeforeNode = linesBeforeNode[linesBeforeNode.length - 1];

return [asset.urlOrDescription, linesBeforeNode.length + 2, charsBeforeNode.length + 1].join(':') + ' (' + node.outerHTML + ')';
}
100 changes: 91 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var query = AssetGraph.query;
var async = require('async');
var request = require('request');
var version = require('../package.json').version;
var assetNodeOffsetDescription = require('./assetNodeOffsetDescription');

var TapRender = require('tap-render');

Expand Down Expand Up @@ -38,7 +39,7 @@ module.exports = function (options) {
expected: [200, url].join(' '),
actual: [status, url].join(' '),
at: _.uniq(relations.map(function (relation) {
return relation.from.urlOrDescription.replace(/#.*$/, '');
return relation.from.urlOrDescription;
})).join('\n ')
};

Expand All @@ -52,7 +53,7 @@ module.exports = function (options) {
operator: 'noRedirects',
expected: [200, url].join(' '),
at: _.uniq(relations.map(function (relation) {
return relation.from.urlOrDescription.replace(/#.*$/, '');
return relation.from.urlOrDescription;
})).join('\n ')
};

Expand Down Expand Up @@ -118,7 +119,7 @@ module.exports = function (options) {
expected: insecureLogLine.replace(/\bhttps?:/g, 'https:'),
actual: insecureLogLine,
at: _.uniq(relations.map(function (relation) {
return relation.from.urlOrDescription.replace(/#.*$/, '');
return relation.from.urlOrDescription;
})).join('\n ')
};

Expand Down Expand Up @@ -244,7 +245,7 @@ module.exports = function (options) {
})
.queue(function (assetGraph) {
t.push({
name: 'Checking fragment identifier references'
name: 'Checking document internal fragment identifier references'
});

assetGraph.findAssets({
Expand All @@ -261,17 +262,48 @@ module.exports = function (options) {
return;
}

// Fragment identifiers starting with digits are not allowed
if (selector.match(/#\d/)) {
t.push(null, {
ok: false,
name: 'Fragment check: ' + asset.urlOrDescription + ' --> ' + selector,
operator: 'invalid-fragment',
expected: 'Fragment identifiers are not allowed to start with a digit',
at: assetNodeOffsetDescription(anchor, asset)
});

return;
}

var selectorMatched;

try {
selectorMatched = !!document.querySelector(selector);
} catch (err) {
t.push(null, {
ok: false,
name: 'Fragment check: ' + asset.urlOrDescription + ' --> ' + selector,
operator: 'error',
actual: err
});

return;
}

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

t.push({
name: 'Checking cross document fragment identifier references'
});
assetGraph.findRelations({
type: 'HtmlAnchor',
crossorigin: false,
Expand All @@ -285,11 +317,61 @@ module.exports = function (options) {

t.push(null, {
ok: !!document.querySelector(selector),
name: 'Fragment identifier check: ' + relation.href,
name: 'Fragment check: ' + relation.from.urlOrDescription + ' --> ' + relation.to.urlOrDescription + selector,
operator: 'missing-fragment',
actual: null,
expected: 'id="' + selector.replace('#', '') + '"',
at: assetNodeOffsetDescription(relation.node, relation.from)
});

// Linking to the empty fragment in a different document makes no sense
if (selector === '#') {
t.push(null, {
ok: false,
name: 'Fragment check: ' + relation.from.urlOrDescription + ' --> ' + relation.to.urlOrDescription + selector,
operator: 'empty-fragment',
expected: 'Fragment identifiers linking to different documents should not be empty',
at: assetNodeOffsetDescription(relation.node, relation.from)
});

return;
}

// Fragment identifiers starting with digits are not allowed
if (selector.match(/#\d/)) {
t.push(null, {
ok: false,
name: 'Fragment check: ' + relation.from.urlOrDescription + ' --> ' + relation.to.urlOrDescription + selector,
operator: 'invalid-fragment',
expected: 'Fragment identifiers are not allowed to start with a digit',
at: assetNodeOffsetDescription(relation.node, relation.from)
});

return;
}

var selectorMatched;

try {
selectorMatched = !!document.querySelector(selector);
} catch (err) {
t.push(null, {
ok: false,
name: 'Fragment check: ' + relation.from.urlOrDescription + ' --> ' + relation.to.urlOrDescription + selector,
operator: 'error',
actual: err
});

return;
}

t.push(null, {
ok: selectorMatched,
name: 'Fragment check: ' + relation.from.urlOrDescription + ' --> ' + relation.to.urlOrDescription + selector,
operator: 'missing-fragment',
actual: null,
expected: 'id="' + selector.replace('#', '') + '"',
at: relation.from.urlOrDescription.replace(/#.*$/, '')
at: assetNodeOffsetDescription(relation.node, relation.from)
});
});
})
Expand Down

0 comments on commit 22df4d6

Please sign in to comment.