forked from angular/angular
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(render): allow to configure when templates are serialized to strings
Introduces the injectable `TemplateCloner` that can be configured via the new token `MAX_IN_MEMORY_ELEMENTS_PER_TEMPLATE_TOKEN`. Also replaces `document.adoptNode` with `document.importNode` as otherwise custom elements are not triggered in chrome 43. Closes angular#3418
- Loading branch information
Showing
24 changed files
with
310 additions
and
222 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {isString} from 'angular2/src/facade/lang'; | ||
import {Injectable, Inject} from 'angular2/di'; | ||
import {DOM} from 'angular2/src/dom/dom_adapter'; | ||
import {MAX_IN_MEMORY_ELEMENTS_PER_TEMPLATE_TOKEN} from './dom_tokens'; | ||
|
||
@Injectable() | ||
export class TemplateCloner { | ||
maxInMemoryElementsPerTemplate: number; | ||
|
||
constructor(@Inject(MAX_IN_MEMORY_ELEMENTS_PER_TEMPLATE_TOKEN) maxInMemoryElementsPerTemplate) { | ||
this.maxInMemoryElementsPerTemplate = maxInMemoryElementsPerTemplate; | ||
} | ||
|
||
prepareForClone(templateRoot: Element): Element | string { | ||
var elementCount = DOM.querySelectorAll(DOM.content(templateRoot), '*').length; | ||
if (this.maxInMemoryElementsPerTemplate >= 0 && | ||
elementCount >= this.maxInMemoryElementsPerTemplate) { | ||
return DOM.getInnerHTML(templateRoot); | ||
} else { | ||
return templateRoot; | ||
} | ||
} | ||
|
||
cloneContent(preparedTemplateRoot: Element | string, importNode: boolean): Node { | ||
var templateContent; | ||
if (isString(preparedTemplateRoot)) { | ||
templateContent = DOM.content(DOM.createTemplate(preparedTemplateRoot)); | ||
if (importNode) { | ||
// Attention: We can't use document.adoptNode here | ||
// as this does NOT wake up custom elements in Chrome 43 | ||
// TODO: Use div.innerHTML instead of template.innerHTML when we | ||
// have code to support the various special cases and | ||
// don't use importNode additionally (e.g. for <tr>, svg elements, ...) | ||
// see https://github.com/angular/angular/issues/3364 | ||
templateContent = DOM.importIntoDoc(templateContent); | ||
} | ||
} else { | ||
templateContent = DOM.content(<Element>preparedTemplateRoot); | ||
if (importNode) { | ||
templateContent = DOM.importIntoDoc(templateContent); | ||
} else { | ||
templateContent = DOM.clone(templateContent); | ||
} | ||
} | ||
return templateContent; | ||
} | ||
} |
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
Oops, something went wrong.