-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(avoid-inline-spacing): add spacing threshold (#3533)
* fix(avoid-inline-spacing): add spacing threshold * Fix integration tests * Improve options for inline-style-property * IE can't count * Add is-visible-matches method * nudge circle * Tweak failure messages * Lint things * Apply suggestions from code review Co-authored-by: Steven Lambert <2433219+straker@users.noreply.github.com> * Address feedback * Move isMultiline to commons.dom * pull tests that do not pass in IE Co-authored-by: Steven Lambert <2433219+straker@users.noreply.github.com>
- Loading branch information
1 parent
3618f50
commit 92add05
Showing
17 changed files
with
787 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"id": "important-letter-spacing", | ||
"evaluate": "inline-style-property-evaluate", | ||
"options": { | ||
"cssProperty": "letter-spacing", | ||
"minValue": 0.12 | ||
}, | ||
"metadata": { | ||
"impact": "serious", | ||
"messages": { | ||
"pass": "Letter-spacing in the style attribute is not set to !important, or meets the minimum", | ||
"fail": "letter-spacing in the style attribute must not use !important, or be at ${data.minValue}em (current ${data.value}em)" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"id": "important-line-height", | ||
"evaluate": "inline-style-property-evaluate", | ||
"options": { | ||
"multiLineOnly": true, | ||
"cssProperty": "line-height", | ||
"minValue": 1.5, | ||
"normalValue": 1 | ||
}, | ||
"metadata": { | ||
"impact": "serious", | ||
"messages": { | ||
"pass": "line-height in the style attribute is not set to !important, or meets the minimum", | ||
"fail": "line-height in the style attribute must not use !important, or be at ${data.minValue}em (current ${data.value}em)" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"id": "important-word-spacing", | ||
"evaluate": "inline-style-property-evaluate", | ||
"options": { | ||
"cssProperty": "word-spacing", | ||
"minValue": 0.16 | ||
}, | ||
"metadata": { | ||
"impact": "serious", | ||
"messages": { | ||
"pass": "word-spacing in the style attribute is not set to !important, or meets the minimum", | ||
"fail": "word-spacing in the style attribute must not use !important, or be at ${data.minValue}em (current ${data.value}em)" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { isMultiline } from '../../commons/dom'; | ||
|
||
/** | ||
* Check if a CSS property, !important or not is within an allowed range | ||
*/ | ||
export default function inlineStyleProperty(node, options) { | ||
const { | ||
cssProperty, | ||
absoluteValues, | ||
minValue, | ||
maxValue, | ||
normalValue = 0, | ||
noImportant, | ||
multiLineOnly | ||
} = options; | ||
if ( | ||
(!noImportant && | ||
node.style.getPropertyPriority(cssProperty) !== `important`) || | ||
(multiLineOnly && !isMultiline(node)) | ||
) { | ||
return true; | ||
} | ||
|
||
const data = {}; | ||
if (typeof minValue === 'number') { | ||
data.minValue = minValue; | ||
} | ||
if (typeof maxValue === 'number') { | ||
data.maxValue = maxValue; | ||
} | ||
|
||
// These do not set the actual value to important, instead they | ||
// say that it is important to use the inherited / root value. | ||
// The actual value can still be modified | ||
const declaredPropValue = node.style.getPropertyValue(cssProperty); | ||
if ( | ||
['inherit', 'unset', 'revert', 'revert-layer'].includes(declaredPropValue) | ||
) { | ||
this.data({ value: declaredPropValue, ...data }); | ||
return true; | ||
} | ||
|
||
const value = getNumberValue(node, { | ||
absoluteValues, | ||
cssProperty, | ||
normalValue | ||
}); | ||
this.data({ value, ...data }); | ||
if (typeof value !== 'number') { | ||
return undefined; // Renderer did something it shouldn't | ||
} | ||
|
||
if ( | ||
(typeof minValue !== 'number' || value >= minValue) && | ||
(typeof maxValue !== 'number' || value <= maxValue) | ||
) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
function getNumberValue(domNode, { cssProperty, absoluteValues, normalValue }) { | ||
const computedStyle = window.getComputedStyle(domNode); | ||
const cssPropValue = computedStyle.getPropertyValue(cssProperty); | ||
if (cssPropValue === 'normal') { | ||
return normalValue; | ||
} | ||
const parsedValue = parseFloat(cssPropValue); | ||
if (absoluteValues) { | ||
return parsedValue; | ||
} | ||
|
||
const fontSize = parseFloat(computedStyle.getPropertyValue('font-size')); | ||
// Make the value relative to the font-size | ||
const value = Math.round((parsedValue / fontSize) * 100) / 100; | ||
if (isNaN(value)) { | ||
return cssPropValue; // Something went wrong, return the string instead | ||
} | ||
return value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Returns true if content has client rects that have no vertical overlap. | ||
* I.e. they are rendered on different "lines". | ||
* @param {Element} domNode | ||
* @param {number} margin (default: 2) | ||
* @returns {number} | ||
*/ | ||
export default function isMultiline(domNode, margin = 2) { | ||
const range = domNode.ownerDocument.createRange(); | ||
range.setStart(domNode, 0); | ||
range.setEnd(domNode, domNode.childNodes.length); | ||
let lastLineEnd = 0; | ||
let lineCount = 0; | ||
for (const rect of range.getClientRects()) { | ||
if (rect.height <= margin) { | ||
continue; | ||
} | ||
if (lastLineEnd > rect.top + margin) { | ||
lastLineEnd = Math.max(lastLineEnd, rect.bottom); | ||
} else if (lineCount === 0) { | ||
lastLineEnd = rect.bottom; | ||
lineCount++; | ||
} else { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
{ | ||
"id": "avoid-inline-spacing", | ||
"selector": "[style]", | ||
"matches": "is-visible-matches", | ||
"tags": ["cat.structure", "wcag21aa", "wcag1412", "ACT"], | ||
"actIds": ["24afc2", "9e45ec", "78fd32"], | ||
"metadata": { | ||
"description": "Ensure that text spacing set through style attributes can be adjusted with custom stylesheets", | ||
"help": "Inline text spacing must be adjustable with custom stylesheets" | ||
}, | ||
"all": ["avoid-inline-spacing"], | ||
"all": [ | ||
"important-letter-spacing", | ||
"important-word-spacing", | ||
"important-line-height" | ||
], | ||
"any": [], | ||
"none": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { isVisible } from '../commons/dom'; | ||
|
||
export default function hasVisibleTextMatches(node) { | ||
return isVisible(node, false); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"id": "24afc2", | ||
"title": "Letter spacing in style attributes is not !important", | ||
"axeRules": ["avoid-inline-spacing"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"id": "78fd32", | ||
"title": "Line height in style attributes is not !important", | ||
"axeRules": ["avoid-inline-spacing"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"id": "9e45ec", | ||
"title": "Word spacing in style attributes is not !important", | ||
"axeRules": ["avoid-inline-spacing"] | ||
} |
Oops, something went wrong.