Files
Angular-Docs-Cn/modules/@angular/platform-browser/src/browser/generic_browser_adapter.ts
T

72 lines
2.4 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {DomAdapter} from '../dom/dom_adapter';
2016-09-27 17:30:26 -07:00
import {isPresent} from '../facade/lang';
2016-06-08 16:38:52 -07:00
/**
* Provides DOM operations in any browser environment.
*
* @security Tread carefully! Interacting with the DOM directly is dangerous and
* can introduce XSS risks.
*/
export abstract class GenericBrowserDomAdapter extends DomAdapter {
2015-09-18 00:49:56 +02:00
private _animationPrefix: string = null;
private _transitionEnd: string = null;
constructor() {
super();
try {
2016-09-27 17:30:26 -07:00
const element = this.createElement('div', this.defaultDoc());
2015-09-18 00:49:56 +02:00
if (isPresent(this.getStyle(element, 'animationName'))) {
this._animationPrefix = '';
} else {
2016-09-27 17:30:26 -07:00
const domPrefixes = ['Webkit', 'Moz', 'O', 'ms'];
for (let i = 0; i < domPrefixes.length; i++) {
2015-09-18 00:49:56 +02:00
if (isPresent(this.getStyle(element, domPrefixes[i] + 'AnimationName'))) {
this._animationPrefix = '-' + domPrefixes[i].toLowerCase() + '-';
2015-09-18 00:49:56 +02:00
break;
}
}
}
2016-09-27 17:30:26 -07:00
const transEndEventNames: {[key: string]: string} = {
2015-09-18 00:49:56 +02:00
WebkitTransition: 'webkitTransitionEnd',
MozTransition: 'transitionend',
OTransition: 'oTransitionEnd otransitionend',
transition: 'transitionend'
};
2016-09-27 17:30:26 -07:00
Object.keys(transEndEventNames).forEach((key: string) => {
2015-09-18 00:49:56 +02:00
if (isPresent(this.getStyle(element, key))) {
2016-09-27 17:30:26 -07:00
this._transitionEnd = transEndEventNames[key];
2015-09-18 00:49:56 +02:00
}
});
} catch (e) {
this._animationPrefix = null;
this._transitionEnd = null;
}
}
2015-08-28 11:29:19 -07:00
getDistributedNodes(el: HTMLElement): Node[] { return (<any>el).getDistributedNodes(); }
2015-07-07 20:03:00 -07:00
resolveAndSetHref(el: HTMLAnchorElement, baseUrl: string, href: string) {
el.href = href == null ? baseUrl : baseUrl + '/../' + href;
}
2015-05-12 11:19:47 -07:00
supportsDOMEvents(): boolean { return true; }
supportsNativeShadowDOM(): boolean {
2016-09-27 17:30:26 -07:00
return typeof(<any>this.defaultDoc().body).createShadowRoot === 'function';
}
2016-09-27 17:30:26 -07:00
getAnimationPrefix(): string { return this._animationPrefix ? this._animationPrefix : ''; }
getTransitionEnd(): string { return this._transitionEnd ? this._transitionEnd : ''; }
2015-09-18 00:49:56 +02:00
supportsAnimation(): boolean {
return isPresent(this._animationPrefix) && isPresent(this._transitionEnd);
}
}