As you probably know - Polymer prohibits the unescapedHTML binding for many reasonable reasons.
How to inject HTML into a template with polymer
How to display html inside template?
However in my project it was a requirement to apply HTML from external source.
Therefore I've implemented a component named <echo-html>:
<dom-module id="echo-html">
<template>
<style>
</style>
</template>
</dom-module>
<script>
(function() {
Polymer({
is: 'echo-html',
properties: {
html: {
type: Object,
value: '',
observer: '_refreshHtml'
},
},
_refreshHtml: function() {
if (this.html) {
var value = '';
var container = Polymer.dom(this).parentNode;
if ((this.html.constructor == Array) && (this.html.length == 1)) {
value = this.html[0];
} else {
value = this.html;
}
if (this.parentNode) {
this.outerHTML = value;
}
this.scopeSubtree(container, true);
$(this).removeClass('echo-html');
}
},
});
})();
</script>
The problem I have is this component binds exactly the pure HTML, so when I want to use <p> or <strong> or any other element, I can't simply use parent's component styling and therefore needs to use global styles.
Is there any way to apply styles of parent element, so for example:
<parent-element>
<echo-html html$="{{some-nasty-html}}"></echo-html>
</parent-element>
The "some-nasty-html" will include .parent-element
class on it?
Is there any way I can remove the .echo-html
class as well? Actually this is my question :)