Skip to content

Commit

Permalink
fix(html-behavior): remove double compile of template parts
Browse files Browse the repository at this point in the history
  • Loading branch information
EisenbergEffect committed Aug 3, 2015
1 parent e807868 commit 49a5ad7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
26 changes: 26 additions & 0 deletions src/dom.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let needsTemplateFixup = !('content' in document.createElement('template'));
let shadowPoly = window.ShadowDOMPolyfill || null;

export let DOMBoundary = 'aurelia-dom-boundary';

Expand All @@ -15,3 +16,28 @@ export function createTemplateFromMarkup(markup){

return temp;
}

export function replaceNode(newNode, node, parentNode){
if(node.parentNode){
node.parentNode.replaceChild(newNode, node);
}else if(shadowPoly){ //HACK: IE template element and shadow dom polyfills not quite right...
shadowPoly.unwrap(parentNode).replaceChild(
shadowPoly.unwrap(newNode),
shadowPoly.unwrap(node)
);
}else{ //HACK: same as above
parentNode.replaceChild(newNode, node);
}
}

export function removeNode(node, parentNode) {
if(node.parentNode){
node.parentNode.removeChild(node);
}else if(shadowPoly){ //HACK: IE template element and shadow dom polyfills not quite right...
shadowPoly.unwrap(parentNode).removeChild(
shadowPoly.unwrap(node)
);
}else{ //HACK: same as above
parentNode.removeChild(node);
}
}
40 changes: 16 additions & 24 deletions src/html-behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {hyphenate} from './util';
import {BindableProperty} from './bindable-property';
import {BehaviorInstance} from './behavior-instance';
import {ResourceRegistry} from './resource-registry';
import {DOMBoundary} from './dom';
import {DOMBoundary, replaceNode, removeNode} from './dom';

var defaultInstruction = { suppressBind:false },
contentSelectorFactoryOptions = { suppressBind:true, enhance:false },
Expand Down Expand Up @@ -162,18 +162,7 @@ export class HtmlBehaviorResource {
part = node.getAttribute('part');

node.removeAttribute(instruction.originalAttrName);

if(node.parentNode){
node.parentNode.replaceChild(template, node);
}else if(window.ShadowDOMPolyfill){ //HACK: IE template element and shadow dom polyfills not quite right...
ShadowDOMPolyfill.unwrap(parentNode).replaceChild(
ShadowDOMPolyfill.unwrap(template),
ShadowDOMPolyfill.unwrap(node)
);
}else{ //HACK: same as above
parentNode.replaceChild(template, node);
}

replaceNode(template, node, parentNode);
fragment.appendChild(node);
instruction.viewFactory = compiler.compile(fragment, resources);

Expand All @@ -188,39 +177,42 @@ export class HtmlBehaviorResource {
var partReplacements = instruction.partReplacements = {};

if(this.processContent(compiler, resources, node, instruction) && node.hasChildNodes()){
instruction.skipContentProcessing = false;

if(!this.usesShadowDOM){
var fragment = document.createDocumentFragment(),
currentChild = node.firstChild,
nextSibling;
if(this.usesShadowDOM){
var currentChild = node.firstChild,
nextSibling, toReplace;

while (currentChild) {
nextSibling = currentChild.nextSibling;

if(currentChild.tagName === 'TEMPLATE' && (toReplace = currentChild.getAttribute('replace-part'))){
partReplacements[toReplace] = compiler.compile(currentChild, resources);
}else{
fragment.appendChild(currentChild);
removeNode(currentChild, parentNode);
}

currentChild = nextSibling;
}

instruction.contentFactory = compiler.compile(fragment, resources);
instruction.skipContentProcessing = false;
}else{
var currentChild = node.firstChild,
nextSibling, toReplace;
var fragment = document.createDocumentFragment(),
currentChild = node.firstChild,
nextSibling;

while (currentChild) {
nextSibling = currentChild.nextSibling;

if(currentChild.tagName === 'TEMPLATE' && (toReplace = currentChild.getAttribute('replace-part'))){
partReplacements[toReplace] = compiler.compile(currentChild, resources);
removeNode(currentChild, parentNode);
}else{
fragment.appendChild(currentChild);
}

currentChild = nextSibling;
}

instruction.contentFactory = compiler.compile(fragment, resources);
instruction.skipContentProcessing = true;
}
}else{
instruction.skipContentProcessing = true;
Expand Down

0 comments on commit 49a5ad7

Please sign in to comment.