-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…h-of-undefined-ref into staging-13 Co-authored-by: Aymeric Mortemousque <[email protected]>
- Loading branch information
Showing
11 changed files
with
158 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { appendElement } from '../../test' | ||
import { cssEscape, elementMatches, getClassList, getParentElement } from './polyfills' | ||
|
||
describe('cssEscape', () => { | ||
it('should escape a string', () => { | ||
expect(cssEscape('.foo#bar')).toEqual('\\.foo\\#bar') | ||
expect(cssEscape('()[]{}')).toEqual('\\(\\)\\[\\]\\{\\}') | ||
expect(cssEscape('--a')).toEqual('--a') | ||
expect(cssEscape('\0')).toEqual('\ufffd') | ||
}) | ||
}) | ||
|
||
describe('elementMatches', () => { | ||
it('should return true if the element matches the selector', () => { | ||
const element = document.createElement('div') | ||
element.classList.add('foo') | ||
expect(elementMatches(element, '.foo')).toEqual(true) | ||
}) | ||
|
||
it('should return false if the element does not match the selector', () => { | ||
const element = document.createElement('div') | ||
element.classList.add('bar') | ||
expect(elementMatches(element, '.foo')).toEqual(false) | ||
}) | ||
}) | ||
|
||
describe('getParentElement', () => { | ||
it('should return the parentElement of a element that supports the parentElement property', () => { | ||
const divElement = appendElement('<div class="parent"><div target></div></div>') | ||
const parenElement = getParentElement(divElement) | ||
expect(parenElement?.className).toEqual('parent') | ||
}) | ||
|
||
it('should return the parentElement of a element that does not support the parentElement property (e.g.: svg on IE)', () => { | ||
const svgElement = appendElement('<div class="parent"><svg target></svg></div>') | ||
Object.defineProperty(svgElement, 'parenElement', { get: () => undefined }) | ||
const parenElement = getParentElement(svgElement) | ||
expect(parenElement?.className).toEqual('parent') | ||
}) | ||
}) | ||
|
||
describe('getClassList', () => { | ||
it('should return the classList of an element that supports the classList property', () => { | ||
const divElement = appendElement('<div class="foo bar"></div>') | ||
const classList = getClassList(divElement) | ||
expect(classList[0]).toEqual('foo') | ||
expect(classList[1]).toEqual('bar') | ||
}) | ||
|
||
it('should return the classList of an element that does not support the classList property (e.g.: svg on IE)', () => { | ||
const svgElement = appendElement('<svg class="foo bar"></svg>') | ||
Object.defineProperty(svgElement, 'classList', { get: () => undefined }) | ||
const classList = getClassList(svgElement) | ||
expect(classList[0]).toEqual('foo') | ||
expect(classList[1]).toEqual('bar') | ||
}) | ||
}) |
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,64 @@ | ||
// https://github.com/jquery/jquery/blob/a684e6ba836f7c553968d7d026ed7941e1a612d8/src/selector/escapeSelector.js | ||
export function cssEscape(str: string) { | ||
if (window.CSS && window.CSS.escape) { | ||
return window.CSS.escape(str) | ||
} | ||
|
||
// eslint-disable-next-line no-control-regex | ||
return str.replace(/([\0-\x1f\x7f]|^-?\d)|^-$|[^\x80-\uFFFF\w-]/g, function (ch, asCodePoint) { | ||
if (asCodePoint) { | ||
// U+0000 NULL becomes U+FFFD REPLACEMENT CHARACTER | ||
if (ch === '\0') { | ||
return '\uFFFD' | ||
} | ||
// Control characters and (dependent upon position) numbers get escaped as code points | ||
return `${ch.slice(0, -1)}\\${ch.charCodeAt(ch.length - 1).toString(16)} ` | ||
} | ||
// Other potentially-special ASCII characters get backslash-escaped | ||
return `\\${ch}` | ||
}) | ||
} | ||
|
||
export function elementMatches(element: Element & { msMatchesSelector?(selector: string): boolean }, selector: string) { | ||
if (element.matches) { | ||
return element.matches(selector) | ||
} | ||
// IE11 support | ||
if (element.msMatchesSelector) { | ||
return element.msMatchesSelector(selector) | ||
} | ||
return false | ||
} | ||
|
||
/** | ||
* Return the parentElement of an node | ||
* | ||
* In cases where parentElement is not supported, such as in IE11 for SVG nodes, we fallback to parentNode | ||
*/ | ||
export function getParentElement(node: Node): HTMLElement | null { | ||
if (node.parentElement) { | ||
return node.parentElement | ||
} | ||
|
||
let parentNode = node.parentNode | ||
while (parentNode !== null && parentNode.nodeType !== Node.ELEMENT_NODE) { | ||
parentNode = node.parentNode | ||
} | ||
|
||
return parentNode as HTMLElement | null | ||
} | ||
|
||
/** | ||
* Return the classList of an element or an array of classes if classList is not supported | ||
* | ||
* In cases where classList is not supported, such as in IE11 for SVG and MathML elements, | ||
* we fallback to using element.getAttribute('class'). | ||
* We opt for element.getAttribute('class') over element.className because className returns an SVGAnimatedString for SVG elements. | ||
*/ | ||
export function getClassList(element: Element): DOMTokenList | string[] { | ||
if (element.classList) { | ||
return element.classList | ||
} | ||
|
||
return (element.getAttribute('class') || '').split(/\s+/) | ||
} |
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
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