Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoFiers committed Jun 30, 2022
1 parent a93fa40 commit c361a0e
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 115 deletions.
23 changes: 13 additions & 10 deletions lib/checks/navigation/meta-refresh-evaluate.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
const separatorRegex = /[;,\s]/
const validRedirectNumRegex = /^[0-9.]+$/
const separatorRegex = /[;,\s]/;
const validRedirectNumRegex = /^[0-9.]+$/;

function metaRefreshEvaluate(node, options, virtualNode) {
export default function metaRefreshEvaluate(node, options, virtualNode) {
const content = (virtualNode.attr('content') || '').trim();
if (!content) {
return true;
}
}
const [redirectStr] = content.split(separatorRegex);
if (!redirectStr.match(validRedirectNumRegex)) {
return true;
}
// Prepend "0" to deal with the ".5" case:
const redirectDelay = parseInt('0' + redirectStr);
const redirectDelay = praseFloat(redirectStr);
this.data({ redirectDelay });
if (typeof options?.minDelay === 'number' && redirectDelay <= options.minDelay) {
if (
typeof options?.minDelay === 'number' &&
redirectDelay <= options.minDelay
) {
return true;
}
if (typeof options?.maxDelay === 'number' && redirectDelay > options.maxDelay) {
if (
typeof options?.maxDelay === 'number' &&
redirectDelay > options.maxDelay
) {
return true;
}
return false;
}

export default metaRefreshEvaluate;
176 changes: 115 additions & 61 deletions test/checks/navigation/meta-refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,132 +3,186 @@ describe('meta-refresh', function() {

var checkContext = axe.testUtils.MockCheckContext();
var checkSetup = axe.testUtils.checkSetup;
var metaRefreshCheck = axe.testUtils.getCheckEvaluate('meta-refresh')
var metaRefreshCheck = axe.testUtils.getCheckEvaluate('meta-refresh');

afterEach(function() {
checkContext.reset();
});

it('returns false if there is a number', function () {
it('returns false if there is a number', function() {
var checkArgs = checkSetup('<meta id="target" name="refresh" content="3">');
assert.isFalse(metaRefreshCheck.apply(checkContext, checkArgs));
});

describe('returns false when valid', function () {
it('there is a decimal', function () {
var checkArgs = checkSetup('<meta id="target" name="refresh" content="3.1">');
describe('returns false when valid', function() {
it('there is a decimal', function() {
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="3.1">'
);
assert.isFalse(metaRefreshCheck.apply(checkContext, checkArgs));
});

it('there is a number followed by a dot', function () {
var checkArgs = checkSetup('<meta id="target" name="refresh" content="3.">');
it('there is a number followed by a dot', function() {
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="3.">'
);
assert.isFalse(metaRefreshCheck.apply(checkContext, checkArgs));
});

it('there is whitespace before the number', function () {
var checkArgs = checkSetup('<meta id="target" name="refresh" content=" \n\t3">');

it('there is a dot followed by a number', function() {
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content=".5">'
);
assert.isFalse(metaRefreshCheck.apply(checkContext, checkArgs));
});

describe('with a valid separator', function () {
it('the number is followed by a semicolon', function () {
var checkArgs = checkSetup('<meta id="target" name="refresh" content="3;">');
it('there is whitespace before the number', function() {
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content=" \n\t3">'
);
assert.isFalse(metaRefreshCheck.apply(checkContext, checkArgs));
});

describe('with a valid separator', function() {
it('the number is followed by a semicolon', function() {
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="3;">'
);
assert.isFalse(metaRefreshCheck.apply(checkContext, checkArgs));
});

it('the number is followed by a comma', function () {
var checkArgs = checkSetup('<meta id="target" name="refresh" content="3;">');

it('the number is followed by a comma', function() {
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="3,">'
);
assert.isFalse(metaRefreshCheck.apply(checkContext, checkArgs));
});

it('the number is followed spaces, and then a separator', function () {
var checkArgs = checkSetup('<meta id="target" name="refresh" content="3 \t\n;">');
it('the number is followed spaces, and then a separator', function() {
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="3 \t\n;">'
);
assert.isFalse(metaRefreshCheck.apply(checkContext, checkArgs));
});

it('the separator is followed by non-separator characters', function () {
var checkArgs = checkSetup('<meta id="target" name="refresh" content="3; https://deque.com/">');
it('the separator is followed by non-separator characters', function() {
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="3; https://deque.com/">'
);
assert.isFalse(metaRefreshCheck.apply(checkContext, checkArgs));
});

it('the separator is a space', function () {
var checkArgs = checkSetup('<meta id="target" name="refresh" content="3 https://deque.com/">');
it('the separator is a space', function() {
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="3 https://deque.com/">'
);
assert.isFalse(metaRefreshCheck.apply(checkContext, checkArgs));
});
});
})
});

describe('returns true when invalid', function () {
it('the number is prefaced with a plus', function () {
var checkArgs = checkSetup('<meta id="target" name="refresh" content="+3">');
describe('returns true when invalid', function() {
it('the number is prefaced with a plus', function() {
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="+3">'
);
assert.isTrue(metaRefreshCheck.apply(checkContext, checkArgs));
});

it('the number is prefaced with a minus', function () {
var checkArgs = checkSetup('<meta id="target" name="refresh" content="a3">');

it('the number is prefaced with a minus', function() {
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="-3">'
);
assert.isTrue(metaRefreshCheck.apply(checkContext, checkArgs));
});

it('the number is prefaced with a letter', function () {
var checkArgs = checkSetup('<meta id="target" name="refresh" content="-3">');

it('the number is prefaced with a letter', function() {
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="a3">'
);
assert.isTrue(metaRefreshCheck.apply(checkContext, checkArgs));
});

it('the number is followed by an invalid separator character', function () {
var checkArgs = checkSetup('<meta id="target" name="refresh" content="3: https://deque.com/">');
it('the number is followed by an invalid separator character', function() {
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="3: https://deque.com/">'
);
assert.isTrue(metaRefreshCheck.apply(checkContext, checkArgs));
});
});


describe('options.minDelay', function () {
it('returns false when the redirect number is greater than minDelay', function () {
var options = { minDelay: 2 }
var checkArgs = checkSetup('<meta id="target" name="refresh" content="3">', options);
describe('options.minDelay', function() {
it('returns false when the redirect number is greater than minDelay', function() {
var options = { minDelay: 2 };
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="3">',
options
);
assert.isFalse(metaRefreshCheck.apply(checkContext, checkArgs));
});

it('returns true when the redirect number equals minDelay', function () {
var options = { minDelay: 3 }
var checkArgs = checkSetup('<meta id="target" name="refresh" content="3">', options);
it('returns true when the redirect number equals minDelay', function() {
var options = { minDelay: 3 };
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="3">',
options
);
assert.isTrue(metaRefreshCheck.apply(checkContext, checkArgs));
});

it('returns true when the redirect number is less than minDelay', function () {
var options = { minDelay: 4 }
var checkArgs = checkSetup('<meta id="target" name="refresh" content="3">', options);
it('returns true when the redirect number is less than minDelay', function() {
var options = { minDelay: 4 };
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="3">',
options
);
assert.isTrue(metaRefreshCheck.apply(checkContext, checkArgs));
});

it('ignores minDelay when set to false', function () {
var options = { minDelay: false }
var checkArgs = checkSetup('<meta id="target" name="refresh" content="0">', options);
it('ignores minDelay when set to false', function() {
var options = { minDelay: false };
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="0">',
options
);
assert.isFalse(metaRefreshCheck.apply(checkContext, checkArgs));
});
});

describe('options.maxDelay', function () {
it('returns true when the redirect number is greater than maxDelay', function () {
var options = { maxDelay: 2 }
var checkArgs = checkSetup('<meta id="target" name="refresh" content="3">', options);
describe('options.maxDelay', function() {
it('returns true when the redirect number is greater than maxDelay', function() {
var options = { maxDelay: 2 };
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="3">',
options
);
assert.isTrue(metaRefreshCheck.apply(checkContext, checkArgs));
});

it('returns false when the redirect number equals maxDelay', function () {
var options = { maxDelay: 3 }
var checkArgs = checkSetup('<meta id="target" name="refresh" content="3">', options);
it('returns false when the redirect number equals maxDelay', function() {
var options = { maxDelay: 3 };
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="3">',
options
);
assert.isFalse(metaRefreshCheck.apply(checkContext, checkArgs));
});

it('returns false when the redirect number is less than maxDelay', function () {
var options = { maxDelay: 4 }
var checkArgs = checkSetup('<meta id="target" name="refresh" content="3">', options);
it('returns false when the redirect number is less than maxDelay', function() {
var options = { maxDelay: 4 };
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="3">',
options
);
assert.isFalse(metaRefreshCheck.apply(checkContext, checkArgs));
});

it('ignores maxDelay when set to false', function () {
var options = { maxDelay: false }
var checkArgs = checkSetup('<meta id="target" name="refresh" content="9999">', options);
it('ignores maxDelay when set to false', function() {
var options = { maxDelay: false };
var checkArgs = checkSetup(
'<meta id="target" name="refresh" content="9999">',
options
);
assert.isFalse(metaRefreshCheck.apply(checkContext, checkArgs));
});
});
Expand Down
27 changes: 12 additions & 15 deletions test/integration/full/meta-refresh/meta-refresh-fail.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
describe('meta-refresh fail', function() {
'use strict';

it('should find no matches', function(done) {
axe.run(
{ runOnly: 'meta-refresh' },
function(err, results) {
try {
assert.isNull(err);
assert.lengthOf(results.violations, 1, 'violations');
assert.lengthOf(results.passes, 0, 'passes');
assert.lengthOf(results.incomplete, 0, 'passes');
assert.lengthOf(results.inapplicable, 0, 'inapplicable');
done();
} catch (e){
done(e);
}
it('should be a violation', function(done) {
axe.run({ runOnly: 'meta-refresh' }, function(err, results) {
try {
assert.isNull(err);
assert.lengthOf(results.violations, 1, 'violations');
assert.lengthOf(results.passes, 0, 'passes');
assert.lengthOf(results.incomplete, 0, 'passes');
assert.lengthOf(results.inapplicable, 0, 'inapplicable');
done();
} catch (e) {
done(e);
}
);
});
});
});
27 changes: 12 additions & 15 deletions test/integration/full/meta-refresh/meta-refresh-inapplicable.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
describe('meta-refresh inapplicable', function() {
'use strict';

it('should find no matches', function(done) {
axe.run(
{ runOnly: 'meta-refresh' },
function(err, results) {
try {
assert.isNull(err);
assert.lengthOf(results.violations, 0, 'violations');
assert.lengthOf(results.passes, 0, 'passes');
assert.lengthOf(results.incomplete, 0, 'passes');
assert.lengthOf(results.inapplicable, 1, 'inapplicable');
done();
} catch (e){
done(e);
}
it('should be inapplicable', function(done) {
axe.run({ runOnly: 'meta-refresh' }, function(err, results) {
try {
assert.isNull(err);
assert.lengthOf(results.violations, 0, 'violations');
assert.lengthOf(results.passes, 0, 'passes');
assert.lengthOf(results.incomplete, 0, 'passes');
assert.lengthOf(results.inapplicable, 1, 'inapplicable');
done();
} catch (e) {
done(e);
}
);
});
});
});
25 changes: 11 additions & 14 deletions test/integration/full/meta-refresh/meta-refresh-pass.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
describe('meta-refresh pass', function() {
'use strict';

it('should find no matches', function(done) {
axe.run(
{ runOnly: 'meta-refresh' },
function(err, results) {
try {
assert.isNull(err);
assert.lengthOf(results.violations, 0, 'violations');
assert.lengthOf(results.passes, 1, 'passes');
assert.lengthOf(results.incomplete, 0, 'passes');
done();
} catch (e){
done(e);
}
it('should pass', function(done) {
axe.run({ runOnly: 'meta-refresh' }, function(err, results) {
try {
assert.isNull(err);
assert.lengthOf(results.violations, 0, 'violations');
assert.lengthOf(results.passes, 1, 'passes');
assert.lengthOf(results.incomplete, 0, 'passes');
done();
} catch (e) {
done(e);
}
);
});
});
});

0 comments on commit c361a0e

Please sign in to comment.