@brightspace-ui-labs/s-html
Version:
Element wrapper of `.innerHTML` for data binding with HTML elements.
104 lines (78 loc) • 1.8 kB
JavaScript
/**
@license
Copyright (c) 2016 The StartPolymer Project Authors. All rights reserved.
This code may only be used under the MIT License found at https://github.com/StartPolymer/license
The complete set of authors may be found at https://github.com/StartPolymer/authors
The complete set of contributors may be found at https://github.com/StartPolymer/contributors
*/
import '@polymer/polymer/polymer-legacy.js';
import { Polymer } from '@polymer/polymer/lib/legacy/polymer-fn.js';
import { dom } from '@polymer/polymer/lib/legacy/polymer.dom.js';
const $_documentContainer = document.createElement('template');
$_documentContainer.innerHTML = `<dom-module id="s-html">
<template strip-whitespace="">
<style>
:host {
display: inline-block;
}
:host > style:first-of-type + * {
--shtml-firstchild;
}
h1 {
--shtml-h1;
}
h2 {
--shtml-h2;
}
h3 {
--shtml-h3;
}
h4 {
--shtml-h4;
}
h5 {
--shtml-h5;
}
h6 {
--shtml-h6;
}
</style>
<slot></slot>
</template>
</dom-module>`;
document.head.appendChild($_documentContainer.content);
Polymer({
is: 's-html',
properties: {
/**
* Text with HTML elements.
*/
html: {
type: String,
observer: '_htmlChanged'
},
/**
* Set to true for unescape escaped HTML (e.g. "<br>"), we get ("<br>").
*/
unescape: {
type: Boolean,
value: false
}
},
_htmlChanged: function(html) {
var child = dom(this).queryDistributedElements('span')[0];
if (this.unescape) {
html = this._unescapeHtml(html);
}
if (child) {
dom(child).innerHTML = html;
} else {
dom(this.root).innerHTML = html;
}
},
_unescapeHtml: function(html) {
var textarea = document.createElement('textarea');
textarea.innerHTML = html;
return textarea.textContent;
}
});