Skip to content

Commit

Permalink
perf(htmlcs): remove duplicate lookup on maps
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Feb 29, 2024
1 parent b636226 commit 051cfe6
Show file tree
Hide file tree
Showing 99 changed files with 378 additions and 224 deletions.
2 changes: 1 addition & 1 deletion fast_htmlcs/HTMLCS.Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ _global.HTMLCS.util = {
*
* @returns {Window}
*/
getElementWindow: function (element) {
getElementWindow: (element) => {
const doc = element.ownerDocument || element;

// @ts-ignore check for element window
Expand Down
58 changes: 25 additions & 33 deletions fast_htmlcs/HTMLCS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@ _global.HTMLCS = new (function () {
this.addMessage = (type, element, msg, code, data) => {
const ccode = _getMessageCode(code);
const textId = ccode + element.outerHTML;
const pos = _duplicates.get(textId);

if (!_duplicates.has(textId)) {
if (typeof pos === "undefined") {
// track the position to use to update the prior message on duplicates.
_duplicates.set(textId, this.messages.length);
this.messages.push({
Expand All @@ -232,7 +233,6 @@ _global.HTMLCS = new (function () {
runner: "htmlcs",
});
} else {
const pos = _duplicates.get(textId);
this.messages[pos].recurrence = this.messages[pos].recurrence + 1;
}
};
Expand All @@ -259,26 +259,25 @@ _global.HTMLCS = new (function () {
const element = elements.shift();
const tagName =
element === topElement ? "_top" : element.tagName.toLowerCase();
const tag = _tags.get(tagName);

if (_tags.has(tagName)) {
const tag = _tags.get(tagName);

if (tag.length > 0) {
// do not pass in callback
_processSniffs(element, tag, topElement, undefined);
}
if (tag && tag.length > 0) {
_processSniffs(element, tag, topElement, undefined);
}
}

_currentSniff = HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_3_1_3_1;

// Due to filtering of presentation roles for general sniffing these need to be handled
// separately. The 1.3.1 sniff needs to run to detect any incorrect usage of the presentation
// role.
for (const element of topElement.querySelectorAll(
const presentationElements = topElement.querySelectorAll(
'[role="presentation"]'
)) {
_currentSniff.testSemanticPresentationRole(element);
);

if (presentationElements) {
_currentSniff = HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_3_1_3_1;
for (const element of presentationElements) {
_currentSniff.testSemanticPresentationRole(element);
}
}

if (callback instanceof Function === true) {
Expand Down Expand Up @@ -347,10 +346,8 @@ _global.HTMLCS = new (function () {
const ruleSet = _getRuleset(part);

if (ruleSet) {
// Already included.
this.registerStandard(standard, part, callback, failCallback, options);
} else {
// TODO: remove _include script callback standard always included
_includeScript(
_standard,
function () {
Expand Down Expand Up @@ -380,7 +377,7 @@ _global.HTMLCS = new (function () {
const oldRuleSet = _getRuleset(part);

const ruleSet = {
sniffs: undefined,
sniffs: [],
};

for (const x in oldRuleSet) {
Expand All @@ -396,7 +393,6 @@ _global.HTMLCS = new (function () {
if (options) {
if (options.include && options.include.length > 0) {
// Included sniffs.
// @ts-ignore
ruleSet.sniffs = options.include;
} else if (options.exclude) {
// Excluded sniffs.
Expand All @@ -414,7 +410,6 @@ _global.HTMLCS = new (function () {
// Register the sniffs for this standard.
_registerSniffs(
standard,
// @ts-ignore
ruleSet.sniffs.slice(0, ruleSet.sniffs.length),
callback,
failCallback
Expand All @@ -429,7 +424,7 @@ _global.HTMLCS = new (function () {
* @param {Function} callback The function to call once the sniffs are registered.
*/
const _registerSniffs = (standard, sniffs, callback, failCallback) => {
if (sniffs.length === 0) {
if (!sniffs || sniffs.length === 0) {
return callback.call(this);
}

Expand All @@ -438,7 +433,7 @@ _global.HTMLCS = new (function () {
standard,
sniffs.shift(),
function () {
_registerSniffs(standard, sniffs, callback, failCallback);
return _registerSniffs(standard, sniffs, callback, failCallback);
},
failCallback
);
Expand Down Expand Up @@ -491,21 +486,18 @@ _global.HTMLCS = new (function () {
* @param {String} sniff The name of the sniff.
*/
this.registerSniff = (standard: string, sniff: string) => {
// Get the sniff object.
const sniffObj = _getSniff(standard, sniff);

if (!sniffObj) {
return false;
}

// Call the register method of the sniff, it should return an array of tags.
if (sniffObj.register) {
const watchedTags = sniffObj.register();

for (const wtag of watchedTags) {
!_tags.has(wtag)
? _tags.set(wtag, [sniffObj])
: _tags.get(wtag).push(sniffObj);
if (sniffObj && "register" in sniffObj) {
for (const wtag of sniffObj.register) {
const tag = _tags.get(wtag);

if (!tag) {
_tags.set(wtag, [sniffObj]);
} else {
tag.push(sniffObj);
}
}
}
};
Expand Down
4 changes: 3 additions & 1 deletion fast_htmlcs/Standards/Section508/Sniffs/A.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ _global.HTMLCS_Section508_Sniffs_A = {
*
* @returns {Array} The list of elements.
*/
register: () => ["_top", "img", "object", "bgsound", "audio"],
get register() {
return ["_top", "img", "object", "bgsound", "audio"];
},

/**
* Process the registered element.
Expand Down
6 changes: 4 additions & 2 deletions fast_htmlcs/Standards/Section508/Sniffs/B.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ _global.HTMLCS_Section508_Sniffs_B = {
*
* @returns {Array} The list of elements.
*/
register: () => ["object", "applet", "embed", "video"],
get register() {
return ["object", "applet", "embed", "video"];
},

/**
* Process the registered element.
*
* @param {DOMNode} element The element registered.
* @param {DOMNode} top The top element of the tested code.
*/
process: function (element, _) {
process: (element, _) => {
HTMLCS.addMessage(
HTMLCS.NOTICE,
element,
Expand Down
4 changes: 3 additions & 1 deletion fast_htmlcs/Standards/Section508/Sniffs/C.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ _global.HTMLCS_Section508_Sniffs_C = {
*
* @returns {Array} The list of elements.
*/
register: () => ["_top"],
get register() {
return ["_top"];
},

/**
* Process the registered element.
Expand Down
28 changes: 10 additions & 18 deletions fast_htmlcs/Standards/Section508/Sniffs/D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ _global.HTMLCS_Section508_Sniffs_D = {
*
* @returns {Array} The list of elements.
*/
register: () => ["_top"],
get register() {
return ["_top"];
},

/**
* Process the registered element.
Expand All @@ -23,18 +25,19 @@ _global.HTMLCS_Section508_Sniffs_D = {
"Ensure that content is ordered in a meaningful sequence when linearised, such as when style sheets are disabled.",
"Linearised"
);
this.testPresentationMarkup(top);

_global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_3_1_3_1.testPresentationMarkup(
top
)
this.testHeadingOrder(top);

// Look for any script elements, and fire off another notice regarding
// potentially hidden text (eg. "click to expand" sections). For instance,
// such text should be stored semantically in the page, not loaded into
// a container through AJAX (and thus not accessible with scripting off).
const hasScript = HTMLCS.util.getAllElements(
top,
'script, link[rel="stylesheet"]'
);
if (hasScript.length > 0) {
if (
HTMLCS.util.getAllElements(top, 'script, link[rel="stylesheet"]').length
) {
HTMLCS.addMessage(
HTMLCS.NOTICE,
top,
Expand All @@ -45,17 +48,6 @@ _global.HTMLCS_Section508_Sniffs_D = {
}
},

/**
* Test for the use of presentational elements.
*
* @param [DOMNode] top The top element of the tested code.
*/
testPresentationMarkup: function (top) {
_global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_3_1_3_1.testPresentationMarkup(
top
);
},

testHeadingOrder: function (top) {
let lastHeading = 0;

Expand Down
4 changes: 3 additions & 1 deletion fast_htmlcs/Standards/Section508/Sniffs/G.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ _global.HTMLCS_Section508_Sniffs_G = {
*
* @returns {Array} The list of elements.
*/
register: () => ["table"],
get register() {
return ["table"];
},

/**
* Process the registered element.
Expand Down
6 changes: 4 additions & 2 deletions fast_htmlcs/Standards/Section508/Sniffs/H.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ _global.HTMLCS_Section508_Sniffs_H = {
*
* @returns {Array} The list of elements.
*/
register: () => ["table"],
get register() {
return ["table"];
},

/**
* Process the registered element.
*
* @param {DOMNode} element The element registered.
* @param {DOMNode} top The top element of the tested code.
*/
process: function (table, _) {
process: (table, _) => {
const headersAttr = HTMLCS.util.testTableHeaders(table);

// Incorrect usage of headers - error; emit always.
Expand Down
4 changes: 3 additions & 1 deletion fast_htmlcs/Standards/Section508/Sniffs/I.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ _global.HTMLCS_Section508_Sniffs_I = {
*
* @returns {Array} The list of elements.
*/
register: () => ["frame", "iframe", "object"],
get register() {
return ["frame", "iframe", "object"];
},

/**
* Process the registered element.
Expand Down
4 changes: 3 additions & 1 deletion fast_htmlcs/Standards/Section508/Sniffs/J.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ _global.HTMLCS_Section508_Sniffs_J = {
*
* @returns {Array} The list of elements.
*/
register: () => ["_top"],
get register() {
return ["_top"];
},

/**
* Process the registered element.
Expand Down
4 changes: 3 additions & 1 deletion fast_htmlcs/Standards/Section508/Sniffs/K.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ _global.HTMLCS_Section508_Sniffs_K = {
*
* @returns {Array} The list of elements.
*/
register: () => ["_top"],
get register() {
return ["_top"];
},

/**
* Process the registered element.
Expand Down
4 changes: 3 additions & 1 deletion fast_htmlcs/Standards/Section508/Sniffs/L.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ _global.HTMLCS_Section508_Sniffs_L = {
*
* @returns {Array} The list of elements.
*/
register: () => ["_top"],
get register() {
return ["_top"];
},

/**
* Process the registered element.
Expand Down
6 changes: 4 additions & 2 deletions fast_htmlcs/Standards/Section508/Sniffs/M.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ _global.HTMLCS_Section508_Sniffs_M = {
*
* @returns {Array} The list of elements.
*/
register: () => ["object", "applet", "bgsound", "embed", "audio", "video"],
get register() {
return ["object", "applet", "bgsound", "embed", "audio", "video"];
},

/**
* Process the registered element.
*
* @param {DOMNode} element The element registered.
* @param {DOMNode} top The top element of the tested code.
*/
process: function (element, _) {
process: (element, _) => {
HTMLCS.addMessage(
HTMLCS.NOTICE,
element,
Expand Down
6 changes: 4 additions & 2 deletions fast_htmlcs/Standards/Section508/Sniffs/N.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ _global.HTMLCS_Section508_Sniffs_N = {
*
* @returns {Array} The list of elements.
*/
register: () => ["form"],
get register() {
return ["form"];
},

/**
* Process the registered element.
*
* @param {DOMNode} element The element registered.
* @param {DOMNode} top The top element of the tested code.
*/
process: function (element, _) {
process: (element, _) => {
if (element.nodeName === "FORM") {
HTMLCS.addMessage(
HTMLCS.NOTICE,
Expand Down
4 changes: 3 additions & 1 deletion fast_htmlcs/Standards/Section508/Sniffs/O.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ _global.HTMLCS_Section508_Sniffs_O = {
*
* @returns {Array} The list of elements.
*/
register: () => ["_top", "a", "area"],
get register() {
return ["_top", "a", "area"];
},

/**
* Process the registered element.
Expand Down
4 changes: 3 additions & 1 deletion fast_htmlcs/Standards/Section508/Sniffs/P.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ _global.HTMLCS_Section508_Sniffs_P = {
*
* @returns {Array} The list of elements.
*/
register: () => ["_top", "meta"],
get register() {
return ["_top", "meta"];
},

/**
* Process the registered element.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_1_1_1_1 = {
*
* @returns {Array} The list of elements.
*/
register: () => ["_top", "img"],
get register() {
return ["_top", "img"];
},

/**
* Process the registered element.
Expand Down
Loading

0 comments on commit 051cfe6

Please sign in to comment.