Skip to content

Commit 6168b77

Browse files
committed
Add unescape method and span for content styled with CSS
1 parent 773a334 commit 6168b77

File tree

3 files changed

+87
-11
lines changed

3 files changed

+87
-11
lines changed

README.md

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,64 @@
1-
# `<s-html>`
1+
# &lt;s-html&gt;
22

3-
Element wrapper for html data binding.
3+
Element wrapper of `.innerHTML` for data binding with HTML elements.
44

55
## Usage
66

7-
`locales.json`
7+
### Example 1
8+
9+
Localize text with HTML elements using [app-localize-behavior](https://github.com/PolymerElements/app-localize-behavior).
10+
11+
locales.json
812
```json
913
{
10-
"text": "<br />Test<br />"
14+
"text": "<a href=\"/settings\">Settings</a>"
1115
}
1216
```
1317

14-
`<div><s-html html="[[localize('text')]]"></s-html></div>`
18+
HTML
19+
```html
20+
<s-html html="[[localize('text')]]"></s-html>
21+
```
22+
23+
### Example 2
24+
25+
Using `span` element for content styled with CSS.
26+
27+
locales.json
28+
```json
29+
{
30+
"text": "<a class=\"red\" href=\"/settings\">Settings</a>"
31+
}
32+
```
1533

16-
## Install
34+
CSS
35+
```css
36+
.red {
37+
color: red;
38+
}
39+
```
40+
41+
HTML
42+
```html
43+
<s-html html="[[localize('text')]]"><span></span></s-html>
44+
```
45+
46+
### Example 3
47+
48+
Unescape escaped HTML elements.
49+
50+
locales.json
51+
```json
52+
{
53+
"text": "polymer &lt;br&gt;"
54+
}
55+
```
56+
57+
HTML
58+
```html
59+
<s-html unescape html="[[localize('text')]]"></s-html>
60+
```
1761

18-
`bower i StartPolymer/s-html -S`
62+
## Installation
1963

64+
`bower i s-html -S`

bower.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
{
22
"name": "s-html",
3-
"version": "0.1.2",
4-
"description": "Element wrapper for html data binding.",
3+
"version": "0.1.3",
4+
"description": "Element wrapper of `.innerHTML` for data binding with HTML elements.",
55
"authors": [
66
"The StartPolymer Project Authors (https://github.com/StartPolymer/authors)"
77
],
88
"keywords": [
99
"web-components",
1010
"polymer",
1111
"html",
12-
"data-binding"
12+
"data-binding",
13+
"unescape",
14+
"innerHTML"
1315
],
1416
"dependencies": {
1517
"polymer": "Polymer/polymer#^1.1.0"

s-html.html

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
</style>
2121

22+
<content select="span"></content>
23+
2224
</template>
2325
<script>
2426

@@ -27,14 +29,41 @@
2729
is: 's-html',
2830

2931
properties: {
32+
/**
33+
* Text with HTML elements.
34+
*/
3035
html: {
3136
type: String,
3237
observer: '_htmlChanged'
38+
},
39+
40+
/**
41+
* Set to true for unescape escaped HTML (e.g. "&lt;br&gt;"), we get ("<br>").
42+
*/
43+
unescape: {
44+
type: Boolean,
45+
value: false
3346
}
3447
},
3548

3649
_htmlChanged: function(html) {
37-
Polymer.dom(this.root).innerHTML = html;
50+
var child = Polymer.dom(this).queryDistributedElements('span')[0];
51+
52+
if (this.unescape) {
53+
html = this._unescapeHtml(html);
54+
}
55+
56+
if (child) {
57+
Polymer.dom(child).innerHTML = html;
58+
} else {
59+
Polymer.dom(this.root).innerHTML = html;
60+
}
61+
},
62+
63+
_unescapeHtml: function(html) {
64+
var textarea = document.createElement('textarea');
65+
textarea.innerHTML = html;
66+
return textarea.textContent;
3867
}
3968

4069
});

0 commit comments

Comments
 (0)