From b374669b773a22181cfd81f869fd5c1cb29fe1f4 Mon Sep 17 00:00:00 2001 From: Jey Date: Sun, 17 Feb 2019 10:34:34 -0500 Subject: [PATCH] feat: Pass context argument to rule matches (#1370) --- build/templates.js | 2 +- lib/core/base/rule.js | 2 +- test/core/base/rule.js | 27 +++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/build/templates.js b/build/templates.js index 92f4bab687..bb568cf348 100644 --- a/build/templates.js +++ b/build/templates.js @@ -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%>' }; diff --git a/lib/core/base/rule.js b/lib/core/base/rule.js index b4169c45b5..db6bc6b88b 100644 --- a/lib/core/base/rule.js +++ b/lib/core/base/rule.js @@ -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 diff --git a/test/core/base/rule.js b/test/core/base/rule.js index 70a371c75f..1234c1548c 100644 --- a/test/core/base/rule.js +++ b/test/core/base/rule.js @@ -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');