Skip to content

Commit

Permalink
Fix issue causing markup to be pruned from tangy-template templates
Browse files Browse the repository at this point in the history
  • Loading branch information
rjcorwin committed Jan 8, 2020
1 parent 1f64a87 commit 0618ebf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
3 changes: 2 additions & 1 deletion tangy-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export class TangyTemplate extends PolymerElement {
connectedCallback() {
super.connectedCallback()
// <tangy-form-item> will evaluate this template in the scope it lives.
this.template = this.textContent
// Note we're not using innerHTML because that will result in expressions like greater than becoming HTML encoded. See note on MDN docs https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML
this.template = this.innerHTML.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>')
}

}
Expand Down
19 changes: 12 additions & 7 deletions test/tangy-template_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
<tangy-form-item id="item1">
<tangy-input name="input1" label="Number"></tangy-input>
<tangy-template name="foo">
${getValue('input1')}
is
${parseInt(getValue('input1')) >= 42
? `greater than or equal to`
: `less than`
}
42.
<h1>
${getValue('input1')}
is
${parseInt(getValue('input1')) >= 42 && true
? `greater than or equal to`
: `less than`
}
42.
</h1>
</tangy-template>
</tangy-form-item>
</tangy-form>
Expand All @@ -59,7 +61,10 @@
form.newResponse()
form.querySelector('#item1').querySelector('[name=input1]').value = '43'
form.querySelector('#item1').querySelector('[name=input1]').dispatchEvent(new Event('change', {bubbles:true}))
// Make sure greater than, less than, and ampersands are converted correctly into code to create the resulting string.
assert.equal(form.querySelector('#item1').querySelector('tangy-template').shadowRoot.querySelector('#container').innerText, '43 is greater than or equal to 42.')
// Ensure that markup did not get pruned either.
assert.equal(!!form.querySelector('#item1').querySelector('tangy-template').shadowRoot.querySelector('#container').querySelector('h1'), true)
})
})
</script>
Expand Down

0 comments on commit 0618ebf

Please sign in to comment.