Skip to content
This repository was archived by the owner on Feb 14, 2019. It is now read-only.

Commit 81a812a

Browse files
committed
IE11 support
1 parent ea3e8bf commit 81a812a

File tree

3 files changed

+74
-11
lines changed

3 files changed

+74
-11
lines changed

src/lit-html.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -337,17 +337,23 @@ export class Template {
337337
// We have a part for each match found
338338
partIndex += lastIndex;
339339

340-
// We keep this current node, but reset its content to the last
341-
// literal part. We insert new literal nodes before this so that the
342-
// tree walker keeps its position correctly.
343-
node.textContent = strings[lastIndex];
344-
345340
// Generate a new text node for each literal section
346341
// These nodes are also used as the markers for node parts
347342
for (let i = 0; i < lastIndex; i++) {
348-
parent.insertBefore(document.createTextNode(strings[i]), node);
343+
console.log(parent.nodeName);
344+
parent.insertBefore(
345+
(strings[i] === '')
346+
? document.createComment('')
347+
: document.createTextNode(strings[i]),
348+
node);
349349
this.parts.push(new TemplatePart('node', index++));
350350
}
351+
parent.insertBefore(
352+
strings[lastIndex] === '' ?
353+
document.createComment('') :
354+
document.createTextNode(strings[lastIndex]),
355+
node);
356+
nodesToRemove.push(node);
351357
} else if (
352358
node.nodeType === 8 /* Node.COMMENT_NODE */ &&
353359
node.nodeValue === marker) {
@@ -365,7 +371,7 @@ export class Template {
365371
const previousSibling = node.previousSibling;
366372
if (previousSibling === null || previousSibling !== previousNode ||
367373
previousSibling.nodeType !== Node.TEXT_NODE) {
368-
parent.insertBefore(document.createTextNode(''), node);
374+
parent.insertBefore(document.createComment(''), node);
369375
} else {
370376
index--;
371377
}
@@ -375,7 +381,7 @@ export class Template {
375381
// We don't have to check if the next node is going to be removed,
376382
// because that node will induce a new marker if so.
377383
if (node.nextSibling === null) {
378-
parent.insertBefore(document.createTextNode(''), node);
384+
parent.insertBefore(document.createComment(''), node);
379385
} else {
380386
index--;
381387
}
@@ -738,7 +744,19 @@ export class TemplateInstance {
738744
index++;
739745
walker.nextNode();
740746
}
741-
this._parts.push(this._partCallback(this, part, walker.currentNode));
747+
let node = walker.currentNode;
748+
// Swap the comment placeholders for empty text nodes
749+
// The nextSibling of the part placeholder is the part endNode
750+
if (node.nextSibling !== null && node.nextSibling!.nodeType === 8) {
751+
node.parentNode!.replaceChild(document.createTextNode(''), node.nextSibling!);
752+
}
753+
// The part placeholder is the part startNode
754+
if (node.nodeType === 8) {
755+
node = document.createTextNode('');
756+
walker.currentNode.parentNode!.replaceChild(node, walker.currentNode);
757+
walker.currentNode = node;
758+
}
759+
this._parts.push(this._partCallback(this, part, node));
742760
}
743761
}
744762
return fragment;

src/test/lit-html_test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,37 @@ suite('lit-html', () => {
525525
</div>`);
526526
});
527527

528+
test('render style tags with expressions correctly', () => {
529+
const color = 'red';
530+
const t = html`
531+
<style>
532+
h1: {
533+
color: ${color};
534+
}
535+
</style>`;
536+
render(t, container);
537+
assert.equal(container.innerHTML, `
538+
<style>
539+
h1: {
540+
color: red;
541+
}
542+
</style>`);
543+
});
544+
545+
test('renders an attribute after empty style node binding', () => {
546+
// This test is sensitive to the exact binding in the style tag.
547+
// Make sure the binding takes up the whole element with no text
548+
// on either side of it
549+
render(html`
550+
<style>${'bar'}</style>
551+
<a href="/buy/${'foo'}"></a>
552+
`, container);
553+
assert.equal(container.innerHTML, `
554+
<style>bar</style>
555+
<a href="/buy/foo"></a>
556+
`);
557+
});
558+
528559
test('renders expressions with preceding elements', () => {
529560
render(html`<a>${'foo'}</a>${html`<h1>${'bar'}</h1>`}`, container);
530561
assert.equal(container.innerHTML, '<a>foo</a><h1>bar</h1>');
@@ -563,6 +594,20 @@ suite('lit-html', () => {
563594
container = document.createElement('div');
564595
});
565596

597+
test('sanity check one', () => {
598+
// bump line numbers
599+
const foo = 'aaa';
600+
601+
const t = () => html`<div>${foo}</div>`;
602+
603+
render(t(), container);
604+
assert.equal(container.innerHTML, '<div>aaa</div>');
605+
const text = container.firstChild!.childNodes[1] as Text;
606+
text.textContent = 'bbb';
607+
render(t(), container);
608+
assert.equal(container.innerHTML, '<div>bbb</div>');
609+
});
610+
566611
test('dirty checks simple values', () => {
567612
const foo = 'aaa';
568613

@@ -573,7 +618,7 @@ suite('lit-html', () => {
573618
const text = container.firstChild!.childNodes[1] as Text;
574619
assert.equal(text.textContent, 'aaa');
575620

576-
// Set textContent manually. Since lit-html doesn't dirty checks against
621+
// Set textContent manually. Since lit-html doesn't dirty check against
577622
// actual DOM, but again previous part values, this modification should
578623
// persist through the next render with the same value.
579624
text.textContent = 'bbb';

test/shady.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
<script>
55
window.ShadyDOM = {force: true};
66
</script>
7+
<script src="../node_modules/babel-polyfill/dist/polyfill.min.js"></script>
78
<script src="../node_modules/wct-browser-legacy/browser.js"></script>
89
<script src="../node_modules/@webcomponents/shadydom/shadydom.min.js"></script>
910
<script src="../node_modules/@webcomponents/shadycss/scoping-shim.min.js"></script>
1011
<script src="../node_modules/@webcomponents/template/template.js"></script>
11-
<script src="../node_modules/babel-polyfill/dist/polyfill.min.js"></script>
1212
</head>
1313
<body>
1414
<script type="module" src="./lib/shady-render_test.js"></script>

0 commit comments

Comments
 (0)