Skip to content

Commit

Permalink
feat: Pass context argument to rule matches (#1370)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeeyyy authored and WilcoFiers committed Feb 17, 2019
1 parent 8e85ae2 commit b374669
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
evaluate: 'function (node, options, virtualNode, context) {\n<%=source%>\n}',
after: 'function (results, options) {\n<%=source%>\n}',
gather: 'function (context) {\n<%=source%>\n}',
matches: 'function (node, virtualNode) {\n<%=source%>\n}',
matches: 'function (node, virtualNode, context) {\n<%=source%>\n}',
source: '(function () {\n<%=source%>\n}())',
commons: '<%=source%>'
};
2 changes: 1 addition & 1 deletion lib/core/base/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Rule.prototype.run = function(context, options, resolve, reject) {
try {
// Matches throws an error when it lacks support for document methods
nodes = this.gather(context).filter(node =>
this.matches(node.actualNode, node)
this.matches(node.actualNode, node, context)
);
} catch (error) {
// Exit the rule execution if matches fails
Expand Down
27 changes: 27 additions & 0 deletions test/core/base/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,33 @@ describe('Rule', function() {
);
});

it('should pass a context to #matches', function(done) {
var div = document.createElement('div');
fixture.appendChild(div);
var success = false,
rule = new Rule({
matches: function(node, virtualNode, context) {
assert.isDefined(context);
assert.hasAnyKeys(context, ['cssom', 'include', 'exclude']);
assert.lengthOf(context.include, 1);
success = true;
return [];
}
});

rule.run(
{
include: [axe.utils.getFlattenedTree(div)[0]]
},
{},
function() {
assert.isTrue(success);
done();
},
isNotCalled
);
});

it('should handle an error in #matches', function(done) {
var div = document.createElement('div');
div.setAttribute('style', '#fff');
Expand Down

0 comments on commit b374669

Please sign in to comment.