From 2d1c8a6ad399684a54343073254e225fb949bbb1 Mon Sep 17 00:00:00 2001 From: Steven Lambert Date: Thu, 5 Sep 2019 14:13:26 -0600 Subject: [PATCH] feat(audit): add lang query paramenter to help urls (#1794) * feat(audit): add lang query paramenter to help urls * fix test * encode uri --- lib/core/base/audit.js | 13 ++++++++++--- test/core/base/audit.js | 28 ++++++++++++++++++++++++++++ test/core/public/configure.js | 16 ++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/lib/core/base/audit.js b/lib/core/base/audit.js index c46f6ffa69..e5a8d6db11 100644 --- a/lib/core/base/audit.js +++ b/lib/core/base/audit.js @@ -37,6 +37,7 @@ function Audit(audit) { this.brand = 'axe'; this.application = 'axeAPI'; this.tagExclude = ['experimental']; + this.lang = 'en'; this.defaultConfig = audit; this._init(); @@ -60,7 +61,8 @@ Audit.prototype._setDefaultLocale = function() { const locale = { checks: {}, - rules: {} + rules: {}, + lang: this.lang }; // XXX: unable to use `for-of` here, as doing so would @@ -223,6 +225,10 @@ Audit.prototype.applyLocale = function(locale) { if (locale.rules) { this._applyRuleLocale(locale.rules); } + + if (locale.lang) { + this.lang = locale.lang; + } }; /** @@ -642,7 +648,7 @@ Audit.prototype.setBranding = function(branding) { /** * For all the rules, create the helpUrl and add it to the data for that rule */ -function getHelpUrl({ brand, application }, ruleId, version) { +function getHelpUrl({ brand, application, lang }, ruleId, version) { return ( axe.constants.helpUrlBase + brand + @@ -651,7 +657,8 @@ function getHelpUrl({ brand, application }, ruleId, version) { '/' + ruleId + '?application=' + - application + encodeURIComponent(application) + + (lang && lang !== 'en' ? '&lang=' + encodeURIComponent(lang) : '') ); } diff --git a/test/core/base/audit.js b/test/core/base/audit.js index 3ebc8e9d0a..4404e7bfaa 100644 --- a/test/core/base/audit.js +++ b/test/core/base/audit.js @@ -236,6 +236,24 @@ describe('Audit', function() { 'https://dequeuniversity.com/rules/axe/1.0/target?application=axeAPI' ); }); + it('sets the lang query if locale has been set', function() { + var audit = new Audit(); + audit.addRule({ + id: 'target', + matches: 'function () {return "hello";}', + selector: 'bob' + }); + audit.applyLocale({ + lang: 'de' + }); + assert.lengthOf(audit.rules, 1); + assert.equal(audit.data.rules.target, undefined); + audit._constructHelpUrls(); + assert.deepEqual(audit.data.rules.target, { + helpUrl: + 'https://dequeuniversity.com/rules/axe/x.y/target?application=axeAPI&lang=de' + }); + }); }); describe('Audit#setBranding', function() { @@ -368,6 +386,16 @@ describe('Audit', function() { audit.resetRulesAndChecks(); assert.equal(audit.checks.target, undefined); }); + it('should reset locale', function() { + var audit = new Audit(); + assert.equal(audit.lang, 'en'); + audit.applyLocale({ + lang: 'de' + }); + assert.equal(audit.lang, 'de'); + audit.resetRulesAndChecks(); + assert.equal(audit.lang, 'en'); + }); }); describe('Audit#addCheck', function() { diff --git a/test/core/public/configure.js b/test/core/public/configure.js index 8b684d4daa..ed499eee2a 100644 --- a/test/core/public/configure.js +++ b/test/core/public/configure.js @@ -343,6 +343,22 @@ describe('axe.configure', function() { }); }); + it('sets the lang property', function() { + axe.configure({ + locale: { + lang: 'lol', + rules: { greeting: { description: 'hello' } }, + checks: { + banana: { + fail: 'icecream' + } + } + } + }); + + assert.equal(axe._audit.lang, 'lol'); + }); + describe('only given checks', function() { it('should not error', function() { assert.doesNotThrow(function() {