Skip to content

Commit

Permalink
Merged in wilcofiers/axe-core/WWD412-helpUri (pull request #87)
Browse files Browse the repository at this point in the history
fix (audit): don't override custom helpUrl
  • Loading branch information
WilcoFiers committed Sep 10, 2016
2 parents a2a9f97 + 02442fe commit 3ff9740
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 22 deletions.
6 changes: 3 additions & 3 deletions lib/checks/navigation/p-as-heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ options = options || {};
let margins = options.margins || [];

let nextSibling = siblings.slice(currentIndex+1)
.find(elm => elm.nodeName.toUpperCase() === 'P')
.find(elm => elm.nodeName.toUpperCase() === 'P');

let prevSibling = siblings.slice(0, currentIndex).reverse()
.find(elm => elm.nodeName.toUpperCase() === 'P')
.find(elm => elm.nodeName.toUpperCase() === 'P');

function getTextContainer(elm) {
let nextNode = elm;
Expand Down Expand Up @@ -52,7 +52,7 @@ function getStyleValues(node) {
};
}

function isHeaderStyle(styleA, styleB, margin) {
function isHeaderStyle(styleA, styleB, margins) {
return margins.reduce((out, margin) => {
return (out || (
(!margin.size || styleA.fontSize / margin.size > styleB.fontSize) &&
Expand Down
30 changes: 21 additions & 9 deletions lib/core/base/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ Audit.prototype.validateOptions = function (options) {

Audit.prototype.setBranding = function (branding) {
'use strict';
let previous = {
brand: this.brand,
application: this.application
};
if (branding && branding.hasOwnProperty('brand') &&
branding.brand && typeof branding.brand === 'string') {
this.brand = branding.brand;
Expand All @@ -268,23 +272,31 @@ Audit.prototype.setBranding = function (branding) {
branding.application && typeof branding.application === 'string') {
this.application = branding.application;
}
this._constructHelpUrls();
this._constructHelpUrls(previous);
};

/**
* For all the rules, create the helpUrl and add it to the data for that rule
*/
function getHelpUrl ({brand, application}, ruleId, version) {
return axe.constants.helpUrlBase + brand +
'/' + ( version || axe.version.substring(0, axe.version.lastIndexOf('.'))) +
'/' + ruleId + '?application=' + application;
}

Audit.prototype._constructHelpUrls = function () {
Audit.prototype._constructHelpUrls = function (previous = null) {
var version = axe.version.substring(0, axe.version.lastIndexOf('.'));

this.rules.forEach(rule => {
this.data.rules[rule.id] = this.data.rules[rule.id] || {};
this.data.rules[rule.id].helpUrl = 'https://dequeuniversity.com/rules/' +
this.brand + '/' +
version + '/' +
rule.id + '?' +
'application=' + this.application;
if (!this.data.rules[rule.id]) {
this.data.rules[rule.id] = {};
}
let metaData = this.data.rules[rule.id];
if (
typeof metaData.helpUrl !== 'string' ||
(previous && metaData.helpUrl === getHelpUrl(previous, rule.id, version))
) {
metaData.helpUrl = getHelpUrl(this, rule.id, version);
}
});
};

Expand Down
1 change: 1 addition & 0 deletions lib/core/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var definitions = [{
}];

var constants = {
helpUrlBase: 'https://dequeuniversity.com/rules/',
results: [],
resultGroups: [],
resultGroupMap: {},
Expand Down
1 change: 0 additions & 1 deletion lib/rules/window-is-top.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/* global window */
return node.ownerDocument.defaultView.self === node.ownerDocument.defaultView.top;
18 changes: 11 additions & 7 deletions test/checks/navigation/p-as-heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,25 @@ describe('p-as-heading', function () {

it('returns false if any of the margins is passed', function () {
var options = {
margins: [{ size: 1.2, weight: 100 }],
margins: [{ size: 1.5 }],
margins: [{ italic: true }]
margins: [
{ size: 1.2, weight: 100 },
{ size: 1.5 },
{ italic: true }
],
};

fixture.innerHTML = '<p style="font-style:italic">elm 1</p> <p>elm 2</p>';
var node = fixture.querySelector('p');
assert.isFalse(checks['p-as-heading'].evaluate.call(checkContext, node, options));
})
});

it('returns true if none of the set margins is passed', function () {
var options = {
margins: [{ size: 1.2, weight: 100 }],
margins: [{ size: 1.5 }],
margins: [{ size: 1.2, italic: true }]
margins: [
{ size: 1.2, weight: 100 },
{ size: 1.5 },
{ size: 1.2, italic: true }
]
};

fixture.innerHTML = '<p style="font-size:1.5em">elm 1</p> <p>elm 2</p>';
Expand Down
56 changes: 56 additions & 0 deletions test/core/base/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,42 @@ describe('Audit', function () {
helpUrl: 'https://dequeuniversity.com/rules/axe/x.y/target?application=thing'
});
});

it('does not override helpUrls of different products', function () {
var audit = new Audit();
audit.addRule({
id: 'target1',
matches: 'function () {return "hello";}',
selector: 'bob',
metadata: {
helpUrl: 'https://dequeuniversity.com/rules/myproject/x.y/target1?application=axeAPI'
}
});
audit.addRule({
id: 'target2',
matches: 'function () {return "hello";}',
selector: 'bob'
});

assert.equal(
audit.data.rules.target1.helpUrl,
'https://dequeuniversity.com/rules/myproject/x.y/target1?application=axeAPI'
);
assert.isUndefined(audit.data.rules.target2);

assert.lengthOf(audit.rules, 2);
audit.brand = 'thing';
audit._constructHelpUrls();

assert.equal(
audit.data.rules.target1.helpUrl,
'https://dequeuniversity.com/rules/myproject/x.y/target1?application=axeAPI'
);
assert.equal(
audit.data.rules.target2.helpUrl,
'https://dequeuniversity.com/rules/thing/x.y/target2?application=axeAPI'
);
});
});

describe('Audit#setBranding', function () {
Expand Down Expand Up @@ -166,6 +202,26 @@ describe('Audit', function () {
helpUrl: 'https://dequeuniversity.com/rules/axe/x.y/target?application=axeAPI'
});
});
it('should not replace custom set branding', function () {
var audit = new Audit();
audit.addRule({
id: 'target',
matches: 'function () {return "hello";}',
selector: 'bob',
metadata: {
helpUrl: 'https://dequeuniversity.com/rules/customer-x/x.y/target?application=axeAPI'
}
});
audit.setBranding({
application: 'thing',
brand: 'other'
});
assert.equal(
audit.data.rules.target.helpUrl,
'https://dequeuniversity.com/rules/customer-x/x.y/target?application=axeAPI'
);

});
});


Expand Down
4 changes: 2 additions & 2 deletions test/rule-matches/p-as-heading-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ describe('p-as-heading-matches', function () {
'use strict';

var rule;
var fixture = document.getElementById('fixture')
var fixture = document.getElementById('fixture');

beforeEach(function () {
rule = axe._audit.rules.find(function (rule) {
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('p-as-heading-matches', function () {
assert.isTrue(rule.matches(target));

fixture.innerHTML = '<p id="target">some text</p><div></div>';
var target = fixture.querySelector('#target');
target = fixture.querySelector('#target');

assert.isFalse(rule.matches(target));
});
Expand Down

0 comments on commit 3ff9740

Please sign in to comment.