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
|
|
|
|
*/
|
|
|
|
|
2016-11-02 11:11:10 -04:00
|
|
|
import {APP_ID, Inject, Injectable, RenderComponentType, Renderer, RootRenderer, ViewEncapsulation} from '@angular/core';
|
|
|
|
|
2016-10-19 16:42:39 -04:00
|
|
|
import {isBlank, isPresent, stringify} from '../facade/lang';
|
2016-11-02 20:54:05 -04:00
|
|
|
import {AnimationKeyframe, AnimationPlayer, AnimationStyles, DirectRenderer, 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-12-10 13:11:39 -05:00
|
|
|
import {camelCaseToDashCase} from './util';
|
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',
|
|
|
|
'xhtml': 'http://www.w3.org/1999/xhtml'
|
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();
|
2015-12-02 13:35:51 -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 {
|
|
|
|
this.setElementAttribute(renderElement, propertyName, propertyValue);
|
|
|
|
}
|
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 {
|
2016-06-08 19:38:52 -04:00
|
|
|
return this._animationDriver.animate(
|
2016-11-14 19:59:06 -05:00
|
|
|
element, startingStyles, keyframes, duration, delay, easing, previousPlayers);
|
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
|
|
|
}
|