-
Notifications
You must be signed in to change notification settings - Fork 781
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: put isContext methods on axe.utils
- Loading branch information
1 parent
135898b
commit f379804
Showing
11 changed files
with
325 additions
and
60 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
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
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 @@ | ||
/** | ||
* Checks if a value is array-like. | ||
* | ||
* @param {any} arr - The value to check. | ||
* @returns {boolean} - Returns true if the value is array-like, false otherwise. | ||
*/ | ||
export default function isArrayLike(arr) { | ||
return ( | ||
!!arr && | ||
typeof arr === 'object' && | ||
typeof arr.length === 'number' && | ||
// Avoid DOM weirdness | ||
arr instanceof window.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,53 @@ | ||
import objectHasOwn from './object-has-own'; | ||
import isArrayLike from './is-array-like'; | ||
|
||
/** | ||
* Determine if some value can be parsed as a context | ||
* @private | ||
* @param {Mixed} contextSpec The configuration object passed to `Context` | ||
* @return {boolea} | ||
*/ | ||
export function isContextSpec(contextSpec) { | ||
return isContextObject(contextSpec) || isContextProp(contextSpec); | ||
} | ||
|
||
/** | ||
* Checks if the given context specification is a valid context object. | ||
* | ||
* @param {Object} contextSpec - The context specification object to check. | ||
* @returns {boolean} - Returns true if the context specification is a valid context object, otherwise returns false. | ||
*/ | ||
export function isContextObject(contextSpec) { | ||
return ['include', 'exclude'].some( | ||
prop => objectHasOwn(contextSpec, prop) && isContextProp(contextSpec[prop]) | ||
); | ||
} | ||
|
||
/** | ||
* Checks if the given contextList is a valid context property. | ||
* @param {string|Node|Array} contextList - The contextList to check. | ||
* @returns {boolean} - Returns true if the contextList is a valid context property, otherwise false. | ||
*/ | ||
export function isContextProp(contextList) { | ||
return ( | ||
typeof contextList === 'string' || | ||
contextList instanceof window.Node || | ||
isLabelledFramesSelector(contextList) || | ||
isLabelledShadowDomSelector(contextList) || | ||
isArrayLike(contextList) | ||
); | ||
} | ||
|
||
export function isLabelledFramesSelector(selector) { | ||
// This doesn't guarantee the selector is valid. | ||
// Just that this isn't a runOptions object | ||
// Normalization will ignore invalid selectors | ||
return objectHasOwn(selector, 'fromFrames'); | ||
} | ||
|
||
export function isLabelledShadowDomSelector(selector) { | ||
// This doesn't guarantee the selector is valid. | ||
// Just that this isn't a runOptions object | ||
// Normalization will ignore invalid selectors | ||
return objectHasOwn(selector, 'fromShadowDom'); | ||
} |
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,7 @@ | ||
// Wrapper to prevent throwing for non-objects & null | ||
export default function objectHasOwn(obj, prop) { | ||
if (!obj || typeof obj !== 'object') { | ||
return false; | ||
} | ||
return Object.prototype.hasOwnProperty.call(obj, prop); | ||
} |
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,30 @@ | ||
describe('axe.utils.isArrayLike', () => { | ||
const isArrayLike = axe.utils.isArrayLike; | ||
|
||
it('is true for an array', () => { | ||
assert.isTrue(isArrayLike([])); | ||
}); | ||
|
||
it('is true for an array-like object', () => { | ||
assert.isTrue(isArrayLike({ length: 1 })); | ||
}); | ||
|
||
it('is false for strings (which also have .length)', () => { | ||
assert.isFalse(isArrayLike('string')); | ||
}); | ||
|
||
it('is false for a Node with .length', () => { | ||
const div = document.createElement('div'); | ||
div.length = 123; | ||
assert.isFalse(isArrayLike(div)); | ||
}); | ||
|
||
it('is false for non-array-like objects', () => { | ||
assert.isFalse(isArrayLike({})); | ||
assert.isFalse(isArrayLike(null)); | ||
assert.isFalse(isArrayLike(undefined)); | ||
assert.isFalse(isArrayLike(1)); | ||
assert.isFalse(isArrayLike(true)); | ||
assert.isFalse(isArrayLike(false)); | ||
}); | ||
}); |
Oops, something went wrong.