Skip to content

Commit 36299df

Browse files
committed
Added new getTranslation method to HTMLCS and scoped translation object into language codes. HTMLCS.process and HTMLCSAuditor.run can accept language arguments specifically. The auditor will read the current documents language via <html lang="{string}">" and set it automatically, or use the default of en if the global translation object doesn't contain a key with the matching language code.
1 parent 66ca6de commit 36299df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+324
-235
lines changed

Auditor/HTMLCSAuditor.js

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ _global.HTMLCSAuditor = new function()
151151
var header = _doc.createElement('div');
152152
header.className = _prefix + 'header';
153153
header.innerHTML = 'HTML_CodeSniffer by Squiz';
154-
header.setAttribute('title', _global.translation["auditor_using_standard"] + standard);
154+
header.setAttribute('title', _global.HTMLCS.getTranslation("auditor_using_standard") + standard);
155155

156156
var dragging = false;
157157
var prevX = 0;
@@ -202,7 +202,7 @@ _global.HTMLCSAuditor = new function()
202202

203203
var closeIcon = _doc.createElement('div');
204204
closeIcon.className = _prefix + 'close';
205-
closeIcon.setAttribute('title', _global.translation["auditor_close"]);
205+
closeIcon.setAttribute('title', _global.HTMLCS.getTranslation("auditor_close"));
206206
closeIcon.onmousedown = function() {
207207
self.close.call(self);
208208
}
@@ -237,25 +237,25 @@ _global.HTMLCSAuditor = new function()
237237
var divider = ', &#160;<span class="' + _prefix + 'divider"></span>';
238238

239239
if (errors > 0) {
240-
var typeName = _global.translation["auditor_errors"];
240+
var typeName = _global.HTMLCS.getTranslation("auditor_errors");
241241
if (errors === 1) {
242-
typeName = _global.translation["auditor_error"];
242+
typeName = _global.HTMLCS.getTranslation("auditor_error");
243243
}
244244
leftContents.push('<strong>' + errors + '</strong> ' + typeName);
245245
}
246246

247247
if (warnings > 0) {
248-
var typeName = _global.translation["auditor_warnings"];
248+
var typeName = _global.HTMLCS.getTranslation("auditor_warnings");
249249
if (warnings === 1) {
250-
typeName = _global.translation["auditor_warning"];
250+
typeName = _global.HTMLCS.getTranslation("auditor_warning");
251251
}
252252
leftContents.push('<strong>' + warnings + '</strong> ' + typeName);
253253
}
254254

255255
if (notices > 0) {
256-
var typeName = _global.translation["auditor_notices"];
256+
var typeName = _global.HTMLCS.getTranslation("auditor_notices");
257257
if (notices === 1) {
258-
typeName = _global.translation["auditor_notice"];
258+
typeName = _global.HTMLCS.getTranslation("auditor_notice");
259259
}
260260
leftContents.push('<strong>' + notices + '</strong> ' + typeName);
261261
}
@@ -325,7 +325,7 @@ _global.HTMLCSAuditor = new function()
325325
lineageHomeLink.href = 'javascript:';
326326

327327
var lineageHomeSpan = _doc.createElement('span');
328-
lineageHomeSpan.innerHTML = _global.translation["auditor_home"];
328+
lineageHomeSpan.innerHTML = _global.HTMLCS.getTranslation("auditor_home");
329329
lineageHomeLink.appendChild(lineageHomeSpan);
330330

331331
lineageHomeLink.onmousedown = function() {
@@ -339,8 +339,8 @@ _global.HTMLCSAuditor = new function()
339339
var lineageReportLink = _doc.createElement('a');
340340
lineageReportLink.className = _prefix + 'lineage-link';
341341
lineageReportLink.href = 'javascript:';
342-
lineageReportLink.innerHTML = _global.translation["auditor_report"];
343-
lineageReportLink.setAttribute('title', _global.translation["auditor_back_to_report"]);
342+
lineageReportLink.innerHTML = _global.HTMLCS.getTranslation("auditor_report");
343+
lineageReportLink.setAttribute('title', _global.HTMLCS.getTranslation("auditor_back_to_report"));
344344

345345
lineageReportLink.onmousedown = function() {
346346
var list = _doc.querySelectorAll('.HTMLCS-inner-wrapper')[0];
@@ -355,7 +355,7 @@ _global.HTMLCSAuditor = new function()
355355
// Issue Count item.
356356
var lineageTotalsItem = _doc.createElement('li');
357357
lineageTotalsItem.className = _prefix + 'lineage-item';
358-
lineageTotalsItem.innerHTML = _global.translation["auditor_issue"] + ' ' + issue + ' ' + _global.translation["auditor_of"] + ' ' + totalIssues;
358+
lineageTotalsItem.innerHTML = _global.HTMLCS.getTranslation("auditor_issue") + ' ' + issue + ' ' + _global.HTMLCS.getTranslation("auditor_of") + ' ' + totalIssues;
359359

360360
lineageHomeItem.appendChild(lineageHomeLink);
361361
lineageReportItem.appendChild(lineageReportLink);
@@ -367,7 +367,7 @@ _global.HTMLCSAuditor = new function()
367367
var buttonGroup = _doc.createElement('div');
368368
buttonGroup.className = _prefix + 'button-group';
369369

370-
var prevButton = buildSummaryButton(_prefix + 'button-previous-issue', 'previous', _global.translation["auditor_previous_issue"], function(target) {
370+
var prevButton = buildSummaryButton(_prefix + 'button-previous-issue', 'previous', _global.HTMLCS.getTranslation("auditor_previous_issue"), function(target) {
371371
var newIssue = Number(issue) - 1;
372372

373373
if (newIssue >= 1) {
@@ -383,7 +383,7 @@ _global.HTMLCSAuditor = new function()
383383
}//end if
384384
});
385385

386-
var nextButton = buildSummaryButton(_prefix + 'button-next-issue', 'next', _global.translation["auditor_next_issue"], function(target) {
386+
var nextButton = buildSummaryButton(_prefix + 'button-next-issue', 'next', _global.HTMLCS.getTranslation("auditor_next_issue"), function(target) {
387387
var newIssue = Number(issue) + 1;
388388

389389
if (newIssue <= _messages.length) {
@@ -481,7 +481,7 @@ _global.HTMLCSAuditor = new function()
481481
useStandardDiv.id = _prefix + 'settings-use-standard';
482482

483483
var useStandardLabel = _doc.createElement('label');
484-
useStandardLabel.innerHTML = _global.translation["auditor_standards"] + ':';
484+
useStandardLabel.innerHTML = _global.HTMLCS.getTranslation("auditor_standards") + ':';
485485
useStandardLabel.setAttribute('for', _prefix + 'settings-use-standard-select');
486486

487487
var useStandardSelect = _doc.createElement('select');
@@ -511,11 +511,11 @@ _global.HTMLCSAuditor = new function()
511511

512512
var issueCountHelpDiv = _doc.createElement('div');
513513
issueCountHelpDiv.id = _prefix + 'settings-issue-count-help';
514-
issueCountHelpDiv.innerHTML = _global.translation["auditor_select_types"];
514+
issueCountHelpDiv.innerHTML = _global.HTMLCS.getTranslation("auditor_select_types");
515515

516516
var viewReportDiv = _doc.createElement('div');
517517
viewReportDiv.id = _prefix + 'settings-view-report';
518-
viewReportDiv.innerHTML = _global.translation["auditor_view_report"];
518+
viewReportDiv.innerHTML = _global.HTMLCS.getTranslation("auditor_view_report");
519519

520520
viewReportDiv.onclick = function() {
521521
if (/disabled/.test(this.className) === false) {
@@ -568,23 +568,23 @@ _global.HTMLCSAuditor = new function()
568568
levelCountDiv.className = 'HTMLCS-tile-text';
569569

570570
if(level == "error") {
571-
levelName = _global.translation['auditor_error'];
571+
levelName = _global.HTMLCS.getTranslation('auditor_error');
572572
if (msgCount !== 1) {
573-
levelName = _global.translation['auditor_errors'];
573+
levelName = _global.HTMLCS.getTranslation('auditor_errors');
574574
}
575575
}
576576

577577
if(level == "warning") {
578-
levelName = _global.translation['auditor_warning'];
578+
levelName = _global.HTMLCS.getTranslation('auditor_warning');
579579
if (msgCount !== 1) {
580-
levelName = _global.translation['auditor_warnings'];
580+
levelName = _global.HTMLCS.getTranslation('auditor_warnings');
581581
}
582582
}
583583

584584
if(level == "notice") {
585-
levelName = _global.translation['auditor_notice'];
585+
levelName = _global.HTMLCS.getTranslation('auditor_notice');
586586
if (msgCount !== 1) {
587-
levelName = _global.translation['auditor_notices'];
587+
levelName = _global.HTMLCS.getTranslation('auditor_notices');
588588
}
589589
}
590590
var content = '<strong>' + msgCount + '</strong> ' + levelName;
@@ -814,18 +814,18 @@ _global.HTMLCSAuditor = new function()
814814

815815
var msgElementSourceInner = _doc.createElement('div');
816816
msgElementSourceInner.className = _prefix + 'issue-source-inner-u2p';
817-
var msg = _global.translation['auditor_unable_to_point'];
817+
var msg = _global.HTMLCS.getTranslation('auditor_unable_to_point');
818818

819819
if (message.element.ownerDocument === null) {
820-
msg = _global.translation['auditor_unable_to_point_removed'];
820+
msg = _global.HTMLCS.getTranslation('auditor_unable_to_point_removed');
821821
} else {
822822
var body = message.element.ownerDocument.getElementsByTagName('body')[0];
823823
if (HTMLCS.util.isInDocument(message.element) === false) {
824-
msg += _global.translation['auditor_unable_to_point_entire'];
824+
msg += _global.HTMLCS.getTranslation('auditor_unable_to_point_entire');
825825
} else if (HTMLCS.util.contains(body, message.element) === false) {
826-
msg = _global.translation['auditor_unable_to_point_outside'];
826+
msg = _global.HTMLCS.getTranslation('auditor_unable_to_point_outside');
827827
} else {
828-
msg += _global.translation['auditor_unable_to_point_outside'];
828+
msg += _global.HTMLCS.getTranslation('auditor_unable_to_point_outside');
829829
}
830830
}
831831

@@ -855,9 +855,9 @@ _global.HTMLCSAuditor = new function()
855855
msgElementSourceHeader.className = _prefix + 'issue-source-header';
856856

857857
var msgSourceHeaderText = _doc.createElement('strong');
858-
msgSourceHeaderText.innerHTML = _global.translation["auditor_code_snippet"];
858+
msgSourceHeaderText.innerHTML = _global.HTMLCS.getTranslation("auditor_code_snippet");
859859

860-
var btnPointTo = buildSummaryButton(_prefix + 'button-point-to-element-' + id, 'pointer', _global.translation["auditor_point_to_element"], function() {
860+
var btnPointTo = buildSummaryButton(_prefix + 'button-point-to-element-' + id, 'pointer', _global.HTMLCS.getTranslation("auditor_point_to_element"), function() {
861861
self.pointToElement(message.element);
862862
});
863863

@@ -977,7 +977,7 @@ _global.HTMLCSAuditor = new function()
977977
var pageNum = _doc.createElement('span');
978978
pageNum.className = _prefix + 'page-number';
979979
// pageNum.innerHTML = 'Page ' + page + ' of ' + totalPages;
980-
pageNum.innerHTML = _global.translation["auditor_issue"] + ' ' + page + ' ' + _global.translation["auditor_of"] + ' ' + totalPages;
980+
pageNum.innerHTML = _global.HTMLCS.getTranslation("auditor_issue") + ' ' + page + ' ' + _global.HTMLCS.getTranslation("auditor_of") + ' ' + totalPages;
981981
navDiv.appendChild(pageNum);
982982

983983
var next = _doc.createElement('span');
@@ -1003,7 +1003,7 @@ _global.HTMLCSAuditor = new function()
10031003
}
10041004

10051005
pageNum.innerHTML = '';
1006-
pageNum.appendChild(document.createTextNode(_global.translation["auditor_issue"] + ' ' + _page + ' ' + _global.translation["auditor_of"] + ' ' + totalPages));
1006+
pageNum.appendChild(document.createTextNode(_global.HTMLCS.getTranslation("auditor_issue") + ' ' + _page + ' ' + _global.HTMLCS.getTranslation("auditor_of") + ' ' + totalPages));
10071007

10081008
var issueList = _doc.querySelectorAll('.HTMLCS-issue-list')[0];
10091009
issueList.style.marginLeft = ((_page - 1) * -300) + 'px';
@@ -1022,7 +1022,7 @@ _global.HTMLCSAuditor = new function()
10221022
}
10231023

10241024
pageNum.innerHTML = '';
1025-
pageNum.appendChild(document.createTextNode(_global.translation["auditor_issue"] + ' ' + _page + ' ' + _global.translation["auditor_of"] + ' ' + totalPages));
1025+
pageNum.appendChild(document.createTextNode(_global.HTMLCS.getTranslation("auditor_issue") + ' ' + _page + ' ' + _global.HTMLCS.getTranslation("auditor_of") + ' ' + totalPages));
10261026

10271027
var issueList = _doc.querySelectorAll('.HTMLCS-issue-list')[0];
10281028
issueList.style.marginLeft = ((_page - 1) * -300) + 'px';
@@ -1373,6 +1373,25 @@ _global.HTMLCSAuditor = new function()
13731373
return _doc;
13741374
};
13751375

1376+
/**
1377+
* Get the current document's language.
1378+
*
1379+
* @return string
1380+
*/
1381+
this.getDocumentLanguage = function() {
1382+
var defaultLang = 'en';
1383+
var doc = this.getOwnerDocument();
1384+
var html = doc.getElementsByTagName('html')[0];
1385+
if (html) {
1386+
var lang = html.getAttribute('lang');
1387+
if (lang) {
1388+
return lang;
1389+
}
1390+
}
1391+
1392+
return defaultLang;
1393+
};
1394+
13761395
/**
13771396
* Run HTML_CodeSniffer and place the results in the auditor.
13781397
*
@@ -1458,6 +1477,10 @@ _global.HTMLCSAuditor = new function()
14581477
_options.path = './';
14591478
}
14601479

1480+
if (!options.lang) {
1481+
_options.lang = this.getDocumentLanguage();
1482+
}
1483+
14611484
if (_options.includeCss === undefined) {
14621485
_options.includeCss = true;
14631486
}
@@ -1559,7 +1582,7 @@ _global.HTMLCSAuditor = new function()
15591582
} else {
15601583
_processSource(standard, sources);
15611584
}
1562-
});
1585+
}, function() {}, options.lang);
15631586
};
15641587

15651588
_processSource(standard, _sources.concat([]));

Contrib/Build/header-bookmarklet.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@
1616
root[prop] = exports[prop];
1717
}
1818
}(this, function () {
19-
var _global = {}
19+
var _global = {
20+
translation: {}
21+
};

Contrib/Build/umd-header.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@
2222
}
2323
}
2424
}(this, function () {
25-
var _global = {}
25+
var _global = {
26+
translation: {}
27+
};

Contrib/PhantomJS/HTMLCS_Run.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ if (system.args.length < 3 || system.args.length > 4) {
9090
}
9191

9292
console.log('');
93-
console.log(_global.translation["auditor_errors"] + ': ' + messages['ERROR'].length + ', ' + _global.translation["auditor_warnings"] + ': ' + messages['WARNING'].length +
94-
', ' + _global.translation["auditor_notices"] + ': ' + messages['NOTICE'].length);
93+
console.log(_global.HTMLCS.getTranslation("auditor_errors") + ': ' + messages['ERROR'].length + ', ' + _global.HTMLCS.getTranslation("auditor_warnings") + ': ' + messages['WARNING'].length +
94+
', ' + _global.HTMLCS.getTranslation("auditor_notices") + ': ' + messages['NOTICE'].length);
9595
cb();
9696
}
9797

Contrib/PhantomJS/runner.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ var HTMLCS_RUNNER = _global.HTMLCS_RUNNER = new function() {
1919
}, function() {
2020
console.log('Something in HTML_CodeSniffer failed to parse. Cannot run.');
2121
console.log('done');
22-
});
22+
}, 'en');
2323
};
2424

2525
this.output = function(msg) {
2626
// Simple output for now.
2727
var typeName = 'UNKNOWN';
2828
switch (msg.type) {
2929
case HTMLCS.ERROR:
30-
typeName = _global.translation["auditor_errors"];
30+
typeName = _global.HTMLCS.getTranslation("auditor_error");
3131
break;
3232

3333
case HTMLCS.WARNING:
34-
typeName = _global.translation["auditor_warnings"];
34+
typeName = _global.HTMLCS.getTranslation("auditor_warning");
3535
break;
3636

3737
case HTMLCS.NOTICE:
38-
typeName = _global.translation["auditor_notices"];
38+
typeName = _global.HTMLCS.getTranslation("auditor_notice");
3939
break;
4040
}//end switch
4141

Contrib/PhantomJS/test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
let data = '';
2+
3+
process.stdin.resume();
4+
process.stdin.setEncoding('utf8');
5+
6+
process.stdin.on('data', function(chunk) {
7+
data += chunk;
8+
});
9+
10+
function toHTML(obj) {
11+
return `<tr>
12+
<td>${obj.code}</td>
13+
<td>${obj.msg}</td>
14+
</tr>`;
15+
}
16+
17+
process.stdin.on('end', function() {
18+
let issues = JSON.parse(data);
19+
let html = `<table>
20+
<thead>
21+
<tr>
22+
<th>Code</th>
23+
<th>Message<th>
24+
</tr>
25+
</thead>
26+
<tbody>
27+
${issues.map(toHTML).join('')}
28+
</tbody>
29+
</table>`;
30+
console.log(html);
31+
});

Contrib/Testing/HTMLCS_Test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ if (system.args.length < 2 || system.args.length > 2) {
9090
assertion = testData.assertions[assert];
9191
for (var i = 0; i < messages.length; i++) {
9292
thisMsg = messages[i];
93-
if (assertion.level === thisMsg[0]) {
93+
if (assertion.level.toLowerCase() === thisMsg[0].toLowerCase()) {
9494
if (assertion.code.test(thisMsg[1]) === true) {
9595
if (assertion.id === thisMsg[3]) {
9696
assertion.triggered = true;

0 commit comments

Comments
 (0)