fix(elements): detect matchesSelector prototype without IIFE (#37799)

Although in SSR we patch the global prototypes with DOM globals
like Element and Node, this patch does not occur before the
matches function is called in Angular Elements. This is similar
to the behavior in @angular/upgrade.

Fixes #24551

PR Close #37799
This commit is contained in:
Adam Plumer 2020-10-07 20:38:46 -05:00 committed by atscott
parent 9f3388e491
commit bf717b1a31
1 changed files with 11 additions and 8 deletions

View File

@ -7,12 +7,6 @@
*/
import {ComponentFactoryResolver, Injector, Type} from '@angular/core';
const matches = (() => {
const elProto = Element.prototype as any;
return elProto.matches || elProto.matchesSelector || elProto.mozMatchesSelector ||
elProto.msMatchesSelector || elProto.oMatchesSelector || elProto.webkitMatchesSelector;
})();
/**
* Provide methods for scheduling the execution of a callback.
*/
@ -96,11 +90,20 @@ export function kebabToCamelCase(input: string): string {
return input.replace(/-([a-z\d])/g, (_, char) => char.toUpperCase());
}
let _matches: (this: any, selector: string) => boolean;
/**
* Check whether an `Element` matches a CSS selector.
* NOTE: this is duplicated from @angular/upgrade, and can
* be consolidated in the future
*/
export function matchesSelector(element: Element, selector: string): boolean {
return matches.call(element, selector);
export function matchesSelector(el: any, selector: string): boolean {
if (!_matches) {
const elProto = <any>Element.prototype;
_matches = elProto.matches || elProto.matchesSelector || elProto.mozMatchesSelector ||
elProto.msMatchesSelector || elProto.oMatchesSelector || elProto.webkitMatchesSelector;
}
return el.nodeType === Node.ELEMENT_NODE ? _matches.call(el, selector) : false;
}
/**