2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2017-02-17 12:01:37 -05:00
|
|
|
import {APP_ID, Inject, Injectable, RenderComponentType, Renderer, RendererFactoryV2, RendererTypeV2, RendererV2, RootRenderer, ViewEncapsulation} from '@angular/core';
|
2016-11-02 11:11:10 -04:00
|
|
|
|
2017-02-15 00:03:18 -05:00
|
|
|
import {isPresent, stringify} from '../facade/lang';
|
2016-12-21 17:14:45 -05:00
|
|
|
import {AnimationKeyframe, AnimationPlayer, AnimationStyles, DirectRenderer, NoOpAnimationPlayer, RenderDebugInfo} from '../private_import_core';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
feat(browser): use AppModules for bootstrap in the browser
This introduces the `BrowserModule` to be used for long form
bootstrap and offline compile bootstrap:
```
@AppModule({
modules: [BrowserModule],
precompile: [MainComponent],
providers: […], // additional providers
directives: […], // additional platform directives
pipes: […] // additional platform pipes
})
class MyModule {
constructor(appRef: ApplicationRef) {
appRef.bootstrap(MainComponent);
}
}
// offline compile
import {bootstrapModuleFactory} from ‘@angular/platform-browser’;
bootstrapModuleFactory(MyModuleNgFactory);
// runtime compile long form
import {bootstrapModule} from ‘@angular/platform-browser-dynamic’;
bootstrapModule(MyModule);
```
The short form, `bootstrap(...)`, can now creates a module on the fly,
given `directives`, `pipes, `providers`, `precompile` and `modules`
properties.
Related changes:
- make `SanitizationService`, `SecurityContext` public in `@angular/core` so that the offline compiler can resolve the token
- move `AnimationDriver` to `platform-browser` and make it
public so that the offline compiler can resolve the token
BREAKING CHANGES:
- short form bootstrap does no longer allow
to inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead.
To provide custom providers for the compiler,
create a custom compiler via `browserCompiler({providers: [...]})`
and pass that into the `bootstrap` method.
2016-06-30 16:07:17 -04:00
|
|
|
import {AnimationDriver} from './animation_driver';
|
2016-08-02 18:53:34 -04:00
|
|
|
import {DOCUMENT} from './dom_tokens';
|
|
|
|
import {EventManager} from './events/event_manager';
|
|
|
|
import {DomSharedStylesHost} from './shared_styles_host';
|
2015-11-17 18:24:36 -05:00
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
export const NAMESPACE_URIS: {[ns: string]: string} = {
|
2016-06-08 19:38:52 -04:00
|
|
|
'xlink': 'http://www.w3.org/1999/xlink',
|
2016-06-27 11:26:45 -04:00
|
|
|
'svg': 'http://www.w3.org/2000/svg',
|
2017-02-20 17:34:15 -05:00
|
|
|
'xhtml': 'http://www.w3.org/1999/xhtml',
|
|
|
|
'xml': 'http://www.w3.org/XML/1998/namespace'
|
2016-06-08 19:38:52 -04:00
|
|
|
};
|
2015-11-19 14:14:44 -05:00
|
|
|
const TEMPLATE_COMMENT_TEXT = 'template bindings={}';
|
2016-09-06 13:25:16 -04:00
|
|
|
const TEMPLATE_BINDINGS_EXP = /^template bindings=(.*)$/;
|
2015-10-27 16:16:27 -04:00
|
|
|
|
2015-12-02 13:35:51 -05:00
|
|
|
export abstract class DomRootRenderer implements RootRenderer {
|
2016-02-01 18:51:00 -05:00
|
|
|
protected registeredComponents: Map<string, DomRenderer> = new Map<string, DomRenderer>();
|
2015-10-06 09:53:39 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
constructor(
|
2016-11-02 19:59:24 -04:00
|
|
|
public document: Document, public eventManager: EventManager,
|
2016-11-02 11:11:10 -04:00
|
|
|
public sharedStylesHost: DomSharedStylesHost, public animationDriver: AnimationDriver,
|
|
|
|
public appId: string) {}
|
2015-10-06 09:53:39 -04:00
|
|
|
|
2015-12-02 13:35:51 -05:00
|
|
|
renderComponent(componentProto: RenderComponentType): Renderer {
|
2016-11-02 19:59:24 -04:00
|
|
|
let renderer = this.registeredComponents.get(componentProto.id);
|
2016-09-30 12:26:53 -04:00
|
|
|
if (!renderer) {
|
2016-11-02 11:11:10 -04:00
|
|
|
renderer = new DomRenderer(
|
|
|
|
this, componentProto, this.animationDriver, `${this.appId}-${componentProto.id}`);
|
2016-02-01 18:51:00 -05:00
|
|
|
this.registeredComponents.set(componentProto.id, renderer);
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
2015-12-02 13:35:51 -05:00
|
|
|
return renderer;
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
2015-12-02 13:35:51 -05:00
|
|
|
}
|
2015-10-06 09:53:39 -04:00
|
|
|
|
2015-12-02 13:35:51 -05:00
|
|
|
@Injectable()
|
|
|
|
export class DomRootRenderer_ extends DomRootRenderer {
|
2016-06-08 19:38:52 -04:00
|
|
|
constructor(
|
|
|
|
@Inject(DOCUMENT) _document: any, _eventManager: EventManager,
|
2016-11-02 11:11:10 -04:00
|
|
|
sharedStylesHost: DomSharedStylesHost, animationDriver: AnimationDriver,
|
|
|
|
@Inject(APP_ID) appId: string) {
|
|
|
|
super(_document, _eventManager, sharedStylesHost, animationDriver, appId);
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
2015-12-02 13:35:51 -05:00
|
|
|
}
|
2015-10-06 09:53:39 -04:00
|
|
|
|
2016-11-02 20:54:05 -04:00
|
|
|
export const DIRECT_DOM_RENDERER: DirectRenderer = {
|
2016-11-03 14:16:28 -04:00
|
|
|
remove(node: Text | Comment | Element) {
|
|
|
|
if (node.parentNode) {
|
|
|
|
node.parentNode.removeChild(node);
|
|
|
|
}
|
|
|
|
},
|
2016-11-02 20:54:05 -04:00
|
|
|
appendChild(node: Node, parent: Element) { parent.appendChild(node);},
|
2016-11-03 14:16:28 -04:00
|
|
|
insertBefore(node: Node, refNode: Node) { refNode.parentNode.insertBefore(node, refNode);},
|
2016-11-02 20:54:05 -04:00
|
|
|
nextSibling(node: Node) { return node.nextSibling;},
|
2016-11-03 14:16:28 -04:00
|
|
|
parentElement(node: Node): Element{return node.parentNode as Element;}
|
2016-11-02 20:54:05 -04:00
|
|
|
};
|
|
|
|
|
2015-12-02 13:35:51 -05:00
|
|
|
export class DomRenderer implements Renderer {
|
|
|
|
private _contentAttr: string;
|
|
|
|
private _hostAttr: string;
|
|
|
|
private _styles: string[];
|
2015-10-06 09:53:39 -04:00
|
|
|
|
2016-11-02 20:54:05 -04:00
|
|
|
directRenderer: DirectRenderer = DIRECT_DOM_RENDERER;
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
constructor(
|
|
|
|
private _rootRenderer: DomRootRenderer, private componentProto: RenderComponentType,
|
2016-11-02 11:11:10 -04:00
|
|
|
private _animationDriver: AnimationDriver, styleShimId: string) {
|
2016-11-02 19:59:24 -04:00
|
|
|
this._styles = flattenStyles(styleShimId, componentProto.styles, []);
|
2015-12-02 13:35:51 -05:00
|
|
|
if (componentProto.encapsulation !== ViewEncapsulation.Native) {
|
|
|
|
this._rootRenderer.sharedStylesHost.addStyles(this._styles);
|
|
|
|
}
|
|
|
|
if (this.componentProto.encapsulation === ViewEncapsulation.Emulated) {
|
2016-11-02 19:59:24 -04:00
|
|
|
this._contentAttr = shimContentAttribute(styleShimId);
|
|
|
|
this._hostAttr = shimHostAttribute(styleShimId);
|
2015-12-02 13:35:51 -05:00
|
|
|
} else {
|
|
|
|
this._contentAttr = null;
|
|
|
|
this._hostAttr = null;
|
|
|
|
}
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
selectRootElement(selectorOrNode: string|Element, debugInfo: RenderDebugInfo): Element {
|
|
|
|
let el: Element;
|
2016-10-19 16:42:39 -04:00
|
|
|
if (typeof selectorOrNode === 'string') {
|
2016-11-02 19:59:24 -04:00
|
|
|
el = this._rootRenderer.document.querySelector(selectorOrNode);
|
|
|
|
if (!el) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error(`The selector "${selectorOrNode}" did not match any elements`);
|
2016-04-13 20:05:17 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
el = selectorOrNode;
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
2016-11-02 19:59:24 -04:00
|
|
|
while (el.firstChild) {
|
|
|
|
el.removeChild(el.firstChild);
|
|
|
|
}
|
2015-12-02 13:35:51 -05:00
|
|
|
return el;
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
createElement(parent: Element|DocumentFragment, name: string, debugInfo: RenderDebugInfo):
|
|
|
|
Element {
|
|
|
|
let el: Element;
|
|
|
|
if (isNamespaced(name)) {
|
|
|
|
const nsAndName = splitNamespace(name);
|
|
|
|
el = document.createElementNS((NAMESPACE_URIS)[nsAndName[0]], nsAndName[1]);
|
|
|
|
} else {
|
|
|
|
el = document.createElement(name);
|
2015-12-02 13:35:51 -05:00
|
|
|
}
|
2016-11-02 19:59:24 -04:00
|
|
|
if (this._contentAttr) {
|
|
|
|
el.setAttribute(this._contentAttr, '');
|
|
|
|
}
|
|
|
|
if (parent) {
|
|
|
|
parent.appendChild(el);
|
2015-11-19 14:14:44 -05:00
|
|
|
}
|
2015-12-02 13:35:51 -05:00
|
|
|
return el;
|
2015-11-19 14:14:44 -05:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
createViewRoot(hostElement: Element): Element|DocumentFragment {
|
|
|
|
let nodesParent: Element|DocumentFragment;
|
2015-12-02 13:35:51 -05:00
|
|
|
if (this.componentProto.encapsulation === ViewEncapsulation.Native) {
|
2016-11-02 19:59:24 -04:00
|
|
|
nodesParent = (hostElement as any).createShadowRoot();
|
2017-02-16 14:56:17 -05:00
|
|
|
this._rootRenderer.sharedStylesHost.addHost(nodesParent);
|
2016-11-02 19:59:24 -04:00
|
|
|
for (let i = 0; i < this._styles.length; i++) {
|
|
|
|
const styleEl = document.createElement('style');
|
|
|
|
styleEl.textContent = this._styles[i];
|
|
|
|
nodesParent.appendChild(styleEl);
|
2015-12-02 13:35:51 -05:00
|
|
|
}
|
2015-10-06 09:53:39 -04:00
|
|
|
} else {
|
2016-11-02 19:59:24 -04:00
|
|
|
if (this._hostAttr) {
|
|
|
|
hostElement.setAttribute(this._hostAttr, '');
|
2015-12-02 13:35:51 -05:00
|
|
|
}
|
|
|
|
nodesParent = hostElement;
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
2015-12-02 13:35:51 -05:00
|
|
|
return nodesParent;
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
createTemplateAnchor(parentElement: Element|DocumentFragment, debugInfo: RenderDebugInfo):
|
|
|
|
Comment {
|
|
|
|
const comment = document.createComment(TEMPLATE_COMMENT_TEXT);
|
|
|
|
if (parentElement) {
|
|
|
|
parentElement.appendChild(comment);
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
2015-12-02 13:35:51 -05:00
|
|
|
return comment;
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
createText(parentElement: Element|DocumentFragment, value: string, debugInfo: RenderDebugInfo):
|
|
|
|
any {
|
|
|
|
const node = document.createTextNode(value);
|
|
|
|
if (parentElement) {
|
|
|
|
parentElement.appendChild(node);
|
2015-12-02 13:35:51 -05:00
|
|
|
}
|
|
|
|
return node;
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
projectNodes(parentElement: Element|DocumentFragment, nodes: Node[]) {
|
|
|
|
if (!parentElement) return;
|
2015-12-02 13:35:51 -05:00
|
|
|
appendNodes(parentElement, nodes);
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
attachViewAfter(node: Node, viewRootNodes: Node[]) { moveNodesAfterSibling(node, viewRootNodes); }
|
2015-10-06 09:53:39 -04:00
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
detachView(viewRootNodes: (Element|Text|Comment)[]) {
|
|
|
|
for (let i = 0; i < viewRootNodes.length; i++) {
|
2016-11-03 14:16:28 -04:00
|
|
|
const node = viewRootNodes[i];
|
|
|
|
if (node.parentNode) {
|
|
|
|
node.parentNode.removeChild(node);
|
|
|
|
}
|
2015-12-02 13:35:51 -05:00
|
|
|
}
|
2015-05-06 13:49:42 -04:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
destroyView(hostElement: Element|DocumentFragment, viewAllNodes: Node[]) {
|
|
|
|
if (this.componentProto.encapsulation === ViewEncapsulation.Native && hostElement) {
|
|
|
|
this._rootRenderer.sharedStylesHost.removeHost((hostElement as any).shadowRoot);
|
2015-10-05 12:43:00 -04:00
|
|
|
}
|
2015-10-01 13:07:49 -04:00
|
|
|
}
|
|
|
|
|
2016-01-25 17:47:25 -05:00
|
|
|
listen(renderElement: any, name: string, callback: Function): Function {
|
2016-06-08 19:38:52 -04:00
|
|
|
return this._rootRenderer.eventManager.addEventListener(
|
|
|
|
renderElement, name, decoratePreventDefault(callback));
|
2015-12-02 13:35:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
listenGlobal(target: string, name: string, callback: Function): Function {
|
2016-06-08 19:38:52 -04:00
|
|
|
return this._rootRenderer.eventManager.addGlobalEventListener(
|
|
|
|
target, name, decoratePreventDefault(callback));
|
2015-11-02 11:39:14 -05:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
setElementProperty(
|
|
|
|
renderElement: Element|DocumentFragment, propertyName: string, propertyValue: any): void {
|
|
|
|
(renderElement as any)[propertyName] = propertyValue;
|
2015-10-01 13:07:49 -04:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
setElementAttribute(renderElement: Element, attributeName: string, attributeValue: string): void {
|
|
|
|
let attrNs: string;
|
|
|
|
let attrNameWithoutNs = attributeName;
|
|
|
|
if (isNamespaced(attributeName)) {
|
|
|
|
const nsAndName = splitNamespace(attributeName);
|
|
|
|
attrNameWithoutNs = nsAndName[1];
|
2016-02-11 16:44:16 -05:00
|
|
|
attributeName = nsAndName[0] + ':' + nsAndName[1];
|
2016-11-02 19:59:24 -04:00
|
|
|
attrNs = NAMESPACE_URIS[nsAndName[0]];
|
2015-12-02 13:35:51 -05:00
|
|
|
}
|
|
|
|
if (isPresent(attributeValue)) {
|
2016-11-02 19:59:24 -04:00
|
|
|
if (attrNs) {
|
|
|
|
renderElement.setAttributeNS(attrNs, attributeName, attributeValue);
|
2015-12-02 13:35:51 -05:00
|
|
|
} else {
|
2016-11-02 19:59:24 -04:00
|
|
|
renderElement.setAttribute(attributeName, attributeValue);
|
2015-12-02 13:35:51 -05:00
|
|
|
}
|
|
|
|
} else {
|
2016-01-08 15:01:29 -05:00
|
|
|
if (isPresent(attrNs)) {
|
2016-11-02 19:59:24 -04:00
|
|
|
renderElement.removeAttributeNS(attrNs, attrNameWithoutNs);
|
2016-01-08 15:01:29 -05:00
|
|
|
} else {
|
2016-11-02 19:59:24 -04:00
|
|
|
renderElement.removeAttribute(attributeName);
|
2016-01-08 15:01:29 -05:00
|
|
|
}
|
2015-05-06 13:49:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
setBindingDebugInfo(renderElement: Element, propertyName: string, propertyValue: string): void {
|
|
|
|
if (renderElement.nodeType === Node.COMMENT_NODE) {
|
2016-10-06 18:10:27 -04:00
|
|
|
const existingBindings =
|
2016-11-02 19:59:24 -04:00
|
|
|
renderElement.nodeValue.replace(/\n/g, '').match(TEMPLATE_BINDINGS_EXP);
|
|
|
|
const parsedBindings = JSON.parse(existingBindings[1]);
|
|
|
|
parsedBindings[propertyName] = propertyValue;
|
|
|
|
renderElement.nodeValue =
|
|
|
|
TEMPLATE_COMMENT_TEXT.replace('{}', JSON.stringify(parsedBindings, null, 2));
|
2015-12-02 13:35:51 -05:00
|
|
|
} else {
|
2017-02-08 06:36:43 -05:00
|
|
|
// Attribute names with `$` (eg `x-y$`) are valid per spec, but unsupported by some browsers
|
2017-02-16 16:55:55 -05:00
|
|
|
propertyName = propertyName.replace(/\$/g, '_');
|
|
|
|
this.setElementAttribute(renderElement, propertyName, propertyValue);
|
2015-12-02 13:35:51 -05:00
|
|
|
}
|
2015-10-01 13:07:49 -04:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
setElementClass(renderElement: Element, className: string, isAdd: boolean): void {
|
2015-12-02 13:35:51 -05:00
|
|
|
if (isAdd) {
|
2016-11-02 19:59:24 -04:00
|
|
|
renderElement.classList.add(className);
|
2015-12-02 13:35:51 -05:00
|
|
|
} else {
|
2016-11-02 19:59:24 -04:00
|
|
|
renderElement.classList.remove(className);
|
2015-10-01 13:07:49 -04:00
|
|
|
}
|
2015-05-06 13:49:42 -04:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
setElementStyle(renderElement: HTMLElement, styleName: string, styleValue: string): void {
|
2015-12-02 13:35:51 -05:00
|
|
|
if (isPresent(styleValue)) {
|
2016-11-02 19:59:24 -04:00
|
|
|
(renderElement.style as any)[styleName] = stringify(styleValue);
|
2015-12-02 13:35:51 -05:00
|
|
|
} else {
|
2016-11-02 19:59:24 -04:00
|
|
|
// IE requires '' instead of null
|
|
|
|
// see https://github.com/angular/angular/issues/7916
|
|
|
|
(renderElement.style as any)[styleName] = '';
|
2015-07-24 18:28:44 -04:00
|
|
|
}
|
2015-05-06 13:49:42 -04:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
invokeElementMethod(renderElement: Element, methodName: string, args: any[]): void {
|
|
|
|
(renderElement as any)[methodName].apply(renderElement, args);
|
2015-12-02 13:35:51 -05:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
setText(renderNode: Text, text: string): void { renderNode.nodeValue = text; }
|
2015-12-02 13:35:51 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
animate(
|
|
|
|
element: any, startingStyles: AnimationStyles, keyframes: AnimationKeyframe[],
|
2016-11-14 19:59:06 -05:00
|
|
|
duration: number, delay: number, easing: string,
|
|
|
|
previousPlayers: AnimationPlayer[] = []): AnimationPlayer {
|
2017-01-05 14:33:40 -05:00
|
|
|
if (this._rootRenderer.document.body.contains(element)) {
|
2016-12-21 17:14:45 -05:00
|
|
|
return this._animationDriver.animate(
|
|
|
|
element, startingStyles, keyframes, duration, delay, easing, previousPlayers);
|
|
|
|
}
|
2017-01-05 14:33:40 -05:00
|
|
|
return new NoOpAnimationPlayer();
|
2015-05-06 13:49:42 -04:00
|
|
|
}
|
2015-06-24 16:46:39 -04:00
|
|
|
}
|
2015-05-06 13:49:42 -04:00
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
function moveNodesAfterSibling(sibling: Node, nodes: Node[]) {
|
2016-11-03 14:16:28 -04:00
|
|
|
const parent = sibling.parentNode;
|
2016-11-02 19:59:24 -04:00
|
|
|
if (nodes.length > 0 && parent) {
|
|
|
|
const nextSibling = sibling.nextSibling;
|
|
|
|
if (nextSibling) {
|
|
|
|
for (let i = 0; i < nodes.length; i++) {
|
|
|
|
parent.insertBefore(nodes[i], nextSibling);
|
2015-12-09 17:44:18 -05:00
|
|
|
}
|
|
|
|
} else {
|
2016-11-02 19:59:24 -04:00
|
|
|
for (let i = 0; i < nodes.length; i++) {
|
|
|
|
parent.appendChild(nodes[i]);
|
2015-12-09 17:44:18 -05:00
|
|
|
}
|
2015-05-06 13:49:42 -04:00
|
|
|
}
|
2015-06-02 13:15:16 -04:00
|
|
|
}
|
2015-06-24 16:46:39 -04:00
|
|
|
}
|
2015-06-02 13:15:16 -04:00
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
function appendNodes(parent: Element | DocumentFragment, nodes: Node[]) {
|
|
|
|
for (let i = 0; i < nodes.length; i++) {
|
|
|
|
parent.appendChild(nodes[i]);
|
2015-12-02 13:35:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 13:07:49 -04:00
|
|
|
function decoratePreventDefault(eventHandler: Function): Function {
|
2016-11-02 19:59:24 -04:00
|
|
|
return (event: any) => {
|
|
|
|
const allowDefaultBehavior = eventHandler(event);
|
2015-12-02 13:35:51 -05:00
|
|
|
if (allowDefaultBehavior === false) {
|
2015-10-01 13:07:49 -04:00
|
|
|
// TODO(tbosch): move preventDefault into event plugins...
|
2016-11-02 19:59:24 -04:00
|
|
|
event.preventDefault();
|
|
|
|
event.returnValue = false;
|
2015-10-01 13:07:49 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2015-11-10 18:56:25 -05:00
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
const COMPONENT_REGEX = /%COMP%/g;
|
2015-12-02 13:35:51 -05:00
|
|
|
export const COMPONENT_VARIABLE = '%COMP%';
|
2016-05-20 19:11:49 -04:00
|
|
|
export const HOST_ATTR = `_nghost-${COMPONENT_VARIABLE}`;
|
|
|
|
export const CONTENT_ATTR = `_ngcontent-${COMPONENT_VARIABLE}`;
|
2015-12-02 13:35:51 -05:00
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
export function shimContentAttribute(componentShortId: string): string {
|
2016-10-06 18:10:27 -04:00
|
|
|
return CONTENT_ATTR.replace(COMPONENT_REGEX, componentShortId);
|
2015-12-02 13:35:51 -05:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
export function shimHostAttribute(componentShortId: string): string {
|
2016-10-06 18:10:27 -04:00
|
|
|
return HOST_ATTR.replace(COMPONENT_REGEX, componentShortId);
|
2015-12-02 13:35:51 -05:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
export function flattenStyles(
|
|
|
|
compId: string, styles: Array<any|any[]>, target: string[]): string[] {
|
2016-10-19 16:42:39 -04:00
|
|
|
for (let i = 0; i < styles.length; i++) {
|
|
|
|
let style = styles[i];
|
|
|
|
|
|
|
|
if (Array.isArray(style)) {
|
2016-11-02 19:59:24 -04:00
|
|
|
flattenStyles(compId, style, target);
|
2015-12-02 13:35:51 -05:00
|
|
|
} else {
|
2016-10-06 18:10:27 -04:00
|
|
|
style = style.replace(COMPONENT_REGEX, compId);
|
2015-12-02 13:35:51 -05:00
|
|
|
target.push(style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2016-08-05 12:50:49 -04:00
|
|
|
const NS_PREFIX_RE = /^:([^:]+):(.+)$/;
|
2015-11-10 18:56:25 -05:00
|
|
|
|
2016-11-02 19:59:24 -04:00
|
|
|
export function isNamespaced(name: string) {
|
|
|
|
return name[0] === ':';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function splitNamespace(name: string): string[] {
|
2016-08-05 12:50:49 -04:00
|
|
|
const match = name.match(NS_PREFIX_RE);
|
2015-11-10 18:56:25 -05:00
|
|
|
return [match[1], match[2]];
|
2016-01-08 15:01:29 -05:00
|
|
|
}
|
2017-02-08 06:36:43 -05:00
|
|
|
|
2017-02-15 00:03:18 -05:00
|
|
|
|
|
|
|
@Injectable()
|
2017-02-16 16:55:55 -05:00
|
|
|
export class DomRendererFactoryV2 implements RendererFactoryV2 {
|
|
|
|
private rendererByCompId = new Map<string, RendererV2>();
|
|
|
|
private defaultRenderer: RendererV2;
|
|
|
|
|
|
|
|
constructor(private eventManager: EventManager, private sharedStylesHost: DomSharedStylesHost) {
|
|
|
|
this.defaultRenderer = new DefaultDomRendererV2(eventManager);
|
|
|
|
};
|
|
|
|
|
2017-02-17 12:01:37 -05:00
|
|
|
createRenderer(element: any, type: RendererTypeV2): RendererV2 {
|
2017-02-16 16:55:55 -05:00
|
|
|
if (!element || !type) {
|
|
|
|
return this.defaultRenderer;
|
|
|
|
}
|
|
|
|
switch (type.encapsulation) {
|
|
|
|
case ViewEncapsulation.Emulated: {
|
|
|
|
let renderer = this.rendererByCompId.get(type.id);
|
|
|
|
if (!renderer) {
|
|
|
|
renderer = new EmulatedEncapsulationDomRendererV2(
|
|
|
|
this.eventManager, this.sharedStylesHost, type);
|
|
|
|
this.rendererByCompId.set(type.id, renderer);
|
|
|
|
}
|
|
|
|
(<EmulatedEncapsulationDomRendererV2>renderer).applyToHost(element);
|
|
|
|
return renderer;
|
|
|
|
}
|
|
|
|
case ViewEncapsulation.Native:
|
|
|
|
return new ShadowDomRenderer(this.eventManager, this.sharedStylesHost, element, type);
|
|
|
|
default: {
|
|
|
|
if (!this.rendererByCompId.has(type.id)) {
|
|
|
|
const styles = flattenStyles(type.id, type.styles, []);
|
|
|
|
this.sharedStylesHost.addStyles(styles);
|
|
|
|
this.rendererByCompId.set(type.id, this.defaultRenderer);
|
|
|
|
}
|
|
|
|
return this.defaultRenderer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DefaultDomRendererV2 implements RendererV2 {
|
|
|
|
constructor(private eventManager: EventManager) {}
|
|
|
|
|
|
|
|
destroy(): void {}
|
|
|
|
|
|
|
|
destroyNode: null;
|
2017-02-15 00:03:18 -05:00
|
|
|
|
2017-02-16 16:55:55 -05:00
|
|
|
createElement(name: string, namespace?: string): any {
|
2017-02-15 00:03:18 -05:00
|
|
|
if (namespace) {
|
|
|
|
return document.createElementNS(NAMESPACE_URIS[namespace], name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return document.createElement(name);
|
|
|
|
}
|
|
|
|
|
2017-02-16 16:55:55 -05:00
|
|
|
createComment(value: string): any { return document.createComment(value); }
|
2017-02-15 00:03:18 -05:00
|
|
|
|
2017-02-16 16:55:55 -05:00
|
|
|
createText(value: string): any { return document.createTextNode(value); }
|
2017-02-15 00:03:18 -05:00
|
|
|
|
|
|
|
appendChild(parent: any, newChild: any): void { parent.appendChild(newChild); }
|
|
|
|
|
|
|
|
insertBefore(parent: any, newChild: any, refChild: any): void {
|
|
|
|
if (parent) {
|
|
|
|
parent.insertBefore(newChild, refChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 11:36:49 -05:00
|
|
|
removeChild(parent: any, oldChild: any): void {
|
|
|
|
if (parent) {
|
|
|
|
parent.removeChild(oldChild);
|
|
|
|
}
|
|
|
|
}
|
2017-02-15 00:03:18 -05:00
|
|
|
|
2017-02-16 16:55:55 -05:00
|
|
|
selectRootElement(selectorOrNode: string|any): any {
|
2017-02-15 00:03:18 -05:00
|
|
|
let el: any = typeof selectorOrNode === 'string' ? document.querySelector(selectorOrNode) :
|
|
|
|
selectorOrNode;
|
2017-02-17 11:56:36 -05:00
|
|
|
if (!el) {
|
|
|
|
throw new Error(`The selector "${selectorOrNode}" did not match any elements`);
|
|
|
|
}
|
2017-02-15 00:03:18 -05:00
|
|
|
el.textContent = '';
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
|
|
|
|
parentNode(node: any): any { return node.parentNode; }
|
|
|
|
|
|
|
|
nextSibling(node: any): any { return node.nextSibling; }
|
|
|
|
|
|
|
|
setAttribute(el: any, name: string, value: string, namespace?: string): void {
|
|
|
|
if (namespace) {
|
|
|
|
el.setAttributeNS(NAMESPACE_URIS[namespace], namespace + ':' + name, value);
|
|
|
|
} else {
|
|
|
|
el.setAttribute(name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeAttribute(el: any, name: string, namespace?: string): void {
|
|
|
|
if (namespace) {
|
|
|
|
el.removeAttributeNS(NAMESPACE_URIS[namespace], name);
|
|
|
|
} else {
|
|
|
|
el.removeAttribute(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addClass(el: any, name: string): void { el.classList.add(name); }
|
|
|
|
|
|
|
|
removeClass(el: any, name: string): void { el.classList.remove(name); }
|
|
|
|
|
|
|
|
setStyle(el: any, style: string, value: any, hasVendorPrefix: boolean, hasImportant: boolean):
|
|
|
|
void {
|
2017-02-17 11:56:36 -05:00
|
|
|
if (hasVendorPrefix || hasImportant) {
|
|
|
|
el.style.setProperty(style, value, hasImportant ? 'important' : '');
|
|
|
|
} else {
|
|
|
|
el.style[style] = value;
|
|
|
|
}
|
2017-02-15 00:03:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
removeStyle(el: any, style: string, hasVendorPrefix: boolean): void {
|
2017-02-17 11:56:36 -05:00
|
|
|
if (hasVendorPrefix) {
|
|
|
|
el.style.removeProperty(style);
|
|
|
|
} else {
|
|
|
|
// IE requires '' instead of null
|
|
|
|
// see https://github.com/angular/angular/issues/7916
|
|
|
|
el.style[style] = '';
|
|
|
|
}
|
2017-02-15 00:03:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
setProperty(el: any, name: string, value: any): void { el[name] = value; }
|
|
|
|
|
2017-02-16 16:55:55 -05:00
|
|
|
setValue(node: any, value: string): void { node.nodeValue = value; }
|
2017-02-15 00:03:18 -05:00
|
|
|
|
|
|
|
listen(target: 'window'|'document'|'body'|any, event: string, callback: (event: any) => boolean):
|
|
|
|
() => void {
|
|
|
|
if (typeof target === 'string') {
|
|
|
|
return <() => void>this.eventManager.addGlobalEventListener(
|
|
|
|
target, event, decoratePreventDefault(callback));
|
|
|
|
}
|
|
|
|
return <() => void>this.eventManager.addEventListener(
|
|
|
|
target, event, decoratePreventDefault(callback)) as() => void;
|
2017-02-08 06:36:43 -05:00
|
|
|
}
|
|
|
|
}
|
2017-02-16 16:55:55 -05:00
|
|
|
|
|
|
|
class EmulatedEncapsulationDomRendererV2 extends DefaultDomRendererV2 {
|
|
|
|
private contentAttr: string;
|
|
|
|
private hostAttr: string;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
eventManager: EventManager, sharedStylesHost: DomSharedStylesHost,
|
2017-02-17 12:01:37 -05:00
|
|
|
private component: RendererTypeV2) {
|
2017-02-16 16:55:55 -05:00
|
|
|
super(eventManager);
|
|
|
|
const styles = flattenStyles(component.id, component.styles, []);
|
|
|
|
sharedStylesHost.addStyles(styles);
|
|
|
|
|
|
|
|
this.contentAttr = shimContentAttribute(component.id);
|
|
|
|
this.hostAttr = shimHostAttribute(component.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
applyToHost(element: any) { super.setAttribute(element, this.hostAttr, ''); }
|
|
|
|
|
|
|
|
createElement(parent: any, name: string): Element {
|
|
|
|
const el = super.createElement(parent, name);
|
|
|
|
super.setAttribute(el, this.contentAttr, '');
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ShadowDomRenderer extends DefaultDomRendererV2 {
|
|
|
|
private shadowRoot: any;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
eventManager: EventManager, private sharedStylesHost: DomSharedStylesHost,
|
2017-02-17 12:01:37 -05:00
|
|
|
private hostEl: any, private component: RendererTypeV2) {
|
2017-02-16 16:55:55 -05:00
|
|
|
super(eventManager);
|
|
|
|
this.shadowRoot = (hostEl as any).createShadowRoot();
|
|
|
|
this.sharedStylesHost.addHost(this.shadowRoot);
|
|
|
|
const styles = flattenStyles(component.id, component.styles, []);
|
|
|
|
for (let i = 0; i < styles.length; i++) {
|
|
|
|
const styleEl = document.createElement('style');
|
|
|
|
styleEl.textContent = styles[i];
|
|
|
|
this.shadowRoot.appendChild(styleEl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private nodeOrShadowRoot(node: any): any { return node === this.hostEl ? this.shadowRoot : node; }
|
|
|
|
|
|
|
|
destroy() { this.sharedStylesHost.removeHost(this.shadowRoot); }
|
|
|
|
|
|
|
|
appendChild(parent: any, newChild: any): void {
|
|
|
|
return super.appendChild(this.nodeOrShadowRoot(parent), newChild);
|
|
|
|
}
|
|
|
|
insertBefore(parent: any, newChild: any, refChild: any): void {
|
|
|
|
return super.insertBefore(this.nodeOrShadowRoot(parent), newChild, refChild);
|
|
|
|
}
|
|
|
|
removeChild(parent: any, oldChild: any): void {
|
|
|
|
return super.removeChild(this.nodeOrShadowRoot(parent), oldChild);
|
|
|
|
}
|
|
|
|
parentNode(node: any): any {
|
|
|
|
return this.nodeOrShadowRoot(super.parentNode(this.nodeOrShadowRoot(node)));
|
|
|
|
}
|
|
|
|
}
|