From 49a5ad79072791352b2c1db58e4f009fa861a456 Mon Sep 17 00:00:00 2001 From: Rob Eisenberg Date: Mon, 3 Aug 2015 09:26:59 -0400 Subject: [PATCH] fix(html-behavior): remove double compile of template parts --- src/dom.js | 26 ++++++++++++++++++++++++++ src/html-behavior.js | 40 ++++++++++++++++------------------------ 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/dom.js b/src/dom.js index 1fe11cac..8119a374 100644 --- a/src/dom.js +++ b/src/dom.js @@ -1,4 +1,5 @@ let needsTemplateFixup = !('content' in document.createElement('template')); +let shadowPoly = window.ShadowDOMPolyfill || null; export let DOMBoundary = 'aurelia-dom-boundary'; @@ -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); + } +} diff --git a/src/html-behavior.js b/src/html-behavior.js index 8df71647..ab9d17b2 100644 --- a/src/html-behavior.js +++ b/src/html-behavior.js @@ -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 }, @@ -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); @@ -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;