Skip to content

Commit

Permalink
chore(htmlcs): remove script fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Feb 9, 2024
1 parent d74c63a commit 3fe62f7
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 98 deletions.
80 changes: 5 additions & 75 deletions fast_htmlcs/HTMLCS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,22 +345,8 @@ _global.HTMLCS = new (function () {
// See if the ruleset object is already included (eg. if minified).
const parts = _standard.split("/");
const part = parts[parts.length - 2];
const ruleSet = _getRuleset(part);

if (ruleSet) {
// Already included.
_registerStandard(_standard, part, callback, failCallback, options);
} else {
// TODO: remove _include script callback standard always included
_includeScript(
_standard,
function () {
// Script is included now register the standard.
_registerStandard(_standard, part, callback, failCallback, options);
},
failCallback
);
}
_registerStandard(_standard, part, callback, failCallback, options);
};

/**
Expand Down Expand Up @@ -464,8 +450,6 @@ _global.HTMLCS = new (function () {
// Already loaded.
if (sniffObj) {
cb();
} else {
_includeScript(_getSniffPath(standard, sniff), cb, failCallback);
}
} else {
// Including a whole other standard.
Expand Down Expand Up @@ -509,20 +493,6 @@ _global.HTMLCS = new (function () {
}
};

/**
* Returns the path to the sniff file.
*
* @param {String} standard The name of the standard.
* @param {String} sniff The name of the sniff.
*
* @returns {String} The path to the JS file of the sniff.
*/
const _getSniffPath = (standard, sniff) => {
const parts = standard.split("/");
parts.pop();
return parts.join("/") + "/Sniffs/" + sniff.replace(/\./g, "/") + ".js";
};

/**
* Returns the path to a local standard.
*
Expand All @@ -545,8 +515,10 @@ _global.HTMLCS = new (function () {
const cstandard = _standards.has(standard) && _standards.get(standard); // standard should always exist
let name = "HTMLCS_";

name += ((cstandard && cstandard.name) || "") + "_Sniffs_";
name += sniff.split(".").join("_");
name +=
((cstandard && cstandard.name) || "") +
"_Sniffs_" +
sniff.split(".").join("_");

if (!_global[name]) {
return null;
Expand All @@ -567,46 +539,4 @@ _global.HTMLCS = new (function () {
*/
const _getMessageCode = (code) =>
_standard + "." + _currentSniff._name + "." + code;

/**
* Includes the specified JS file.
*
* @param {String} src The URL to the JS file.
* @param {Function} callback The function to call once the script is loaded.
*/
const _includeScript = (src, callback, failCallback) => {
const script = document.createElement("script");

script.onload = function () {
script.onload = null;
// @ts-ignore
script.onreadystatechange = null;
callback.call(this);
};

script.onerror = function () {
script.onload = null;
// @ts-ignore
script.onreadystatechange = null;
if (failCallback) {
failCallback.call(this);
}
};

// @ts-ignore
script.onreadystatechange = function () {
if (/^(complete|loaded)$/.test(this.readyState) === true) {
// @ts-ignore
script.onreadystatechange = null;
// @ts-ignore
script.onload();
}
};

script.src = src;

document.head
? document.head.appendChild(script)
: document.getElementsByTagName("head")[0].appendChild(script);
};
})();
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_4_1_4_3 = {
const required = failure.required;
const recommend = failure.recommendation;
const hasBgImg = failure.hasBgImage || false;
const hasBgGradient = failure.hasBgGradient || false;
const hasBgGradient = failure.hasBgGradient || false;
const isAbsolute = failure.isAbsolute || false;
const hasAlpha = failure.hasAlpha || false;

Expand All @@ -47,7 +47,6 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_4_1_4_3 = {
Math.pow(10, decimals);
}


if (required === 4.5) {
code = "G18";
} else if (required === 3.0) {
Expand Down Expand Up @@ -90,10 +89,9 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_4_1_4_3 = {
HTMLCS.addMessage(
HTMLCS.WARNING,
element,
_global.HTMLCS.getTranslation("1_4_3_G18_or_G145.BgGradient").replace(
/\{\{required\}\}/g,
required + ""
),
_global.HTMLCS.getTranslation(
"1_4_3_G18_or_G145.BgGradient"
).replace(/\{\{required\}\}/g, required + ""),
code
);
} else if (isAbsolute === true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_4_1_4_3_Contrast = {

bgColour = parentStyle.backgroundColor;

if (parentStyle.background && parentStyle.background.includes("gradient(")) {
if (
parentStyle.background &&
parentStyle.background.includes("gradient(")
) {
hasBgGradient = true;
break;
}
Expand All @@ -126,8 +129,8 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_4_1_4_3_Contrast = {
//Make sure it is trying to cover the entire content area
beforeStyle.width == parentStyle.width &&
parseInt(beforeStyle.height, 10) <=
parseInt(parentStyle.height, 10) &&
beforeStyle.backgroundImage !== "none"
parseInt(parentStyle.height, 10) &&
beforeStyle.backgroundImage !== "none"
) {
//And finally it needs a background image
hasBgImg = true;
Expand All @@ -147,7 +150,7 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_4_1_4_3_Contrast = {
value: undefined,
required: reqRatio,
hasAlpha: false,
hasBgGradient: true
hasBgGradient: true,
});
continue;
} else if (bgColour && bgAlpha < 1.0 && bgAlpha > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,17 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_4_1_4_6 = {
".";
}

if (hasBgGradient === true) {
if (hasBgGradient) {
code += ".BgGradient";
HTMLCS.addMessage(
HTMLCS.WARNING,
element,
_global.HTMLCS.getTranslation("1_4_6_G18_or_G145.BgGradient").replace(
/\{\{required\}\}/g,
required + ""
),
_global.HTMLCS.getTranslation(
"1_4_6_G18_or_G145.BgGradient"
).replace(/\{\{required\}\}/g, required + ""),
code
);
} else if (isAbsolute === true) {
} else if (isAbsolute) {
code += ".Abs";
HTMLCS.addMessage(
HTMLCS.WARNING,
Expand All @@ -104,7 +103,7 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_4_1_4_6 = {
),
code
);
} else if (hasBgImg === true) {
} else if (hasBgImg) {
code += ".BgImage";
HTMLCS.addMessage(
HTMLCS.WARNING,
Expand Down
2 changes: 1 addition & 1 deletion fast_htmlcs/Translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ _global.translation["en"] = {
"This element is absolutely positioned and the background color can not be determined. Ensure the contrast ratio between the text and all covered parts of the background are at least {{required}}:1.",
"1_4_3_G18_or_G145.BgImage":
"This element's text is placed on a background image. Ensure the contrast ratio between the text and all covered parts of the image are at least {{required}}:1.",
"1_4_3_G18_or_G145.BgGradient":
"1_4_3_G18_or_G145.BgGradient":
"This element's text is placed on a gradient. Ensure the contrast ratio between the text and all covered parts of the gradient are at least {{required}}:1.",
"1_4_3_G18_or_G145.Alpha":
"This element's text or background contains transparency. Ensure the contrast ratio between the text and background are at least {{required}}:1.",
Expand Down
2 changes: 1 addition & 1 deletion fast_htmlcs/Translations/nl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ _global.translation["nl"] = {
//1_4_3.js
"1_4_3_G18_or_G145.Abs":
"Dit element is absoluut gepositioneerd en de achtergrondkleur kan niet bepaald worden. Zorg ervoor dat de contrastverhouding tussen de tekst en alle bedekte delen van de achtergond minstens {{required}}:1 is.",
"1_4_3_G18_or_G145.BgImage":
"1_4_3_G18_or_G145.BgImage":
"De tekst van dit element werd geplaatst op een achtergrondafbeelding. Zorg ervoor dat de contrastverhouding tussen de tekst en alle bedekte delen van de achtergrondafbeelding minstens {{required}}:1 is.",
"1_4_3_G18_or_G145.BgGradient":
"De tekst van dit element is op een achtergrondverloop geplaatst. Zorg ervoor dat de contrastverhouding tussen de tekst en alle bedekte delen van het achtergrondverloop minimaal {{vereist}}:1 is.",
Expand Down
4 changes: 2 additions & 2 deletions fast_htmlcs/Translations/pl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ _global.translation["pl"] = {
//1_4_3.js
"1_4_3_G18_or_G145.Abs":
"Ten element jest pozycjonowany absolutnie i nie jest możliwe zweryfikowanie jego kontrastu. Sprawdź, czy kontrast jest na wystarczającym poziomie: {{required}}:1.",
"1_4_3_G18_or_G145.BgImage":
"1_4_3_G18_or_G145.BgImage":
"Tekst elementu wyświetlany jest na obrazku. Należy upewnić się, że stosunek kontrastu tekstu do tła wynosi co najmniej {{required}}:1.",
"1_4_3_G18_or_G145.BgGradient":
"Tekst tego elementu jest umieszczony na gradiencie. Upewnij się, że współczynnik kontrastu między tekstem a wszystkimi zakrytymi częściami gradientu wynosi co najmniej {{required}}:1.",
"Tekst tego elementu jest umieszczony na gradiencie. Upewnij się, że współczynnik kontrastu między tekstem a wszystkimi zakrytymi częściami gradientu wynosi co najmniej {{required}}:1.",
"1_4_3_G18_or_G145.Alpha":
"Tekst lub tło tego elementu jest przezroczyste. Upewnij się, że współczynnik kontrastu między tekstem a tłem wynosi co najmniej {{required}}:1.",
"1_4_3_G18_or_G145.Fail":
Expand Down
2 changes: 1 addition & 1 deletion fast_htmlcs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fast_htmlcs",
"version": "0.0.69",
"version": "0.0.70",
"description": "A high performance fork of HTML_CodeSniffer.",
"license": "BSD-3-Clause",
"main": "index.js",
Expand Down
2 changes: 1 addition & 1 deletion kayle/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kayle",
"version": "0.8.6",
"version": "0.8.7",
"description": "Extremely fast and accurate accessibility engine built for any headless tool like playwright or puppeteer.",
"main": "./build/index.js",
"keywords": [
Expand Down

0 comments on commit 3fe62f7

Please sign in to comment.