insert html fragment from database

160 views
Skip to first unread message

Marco Stolle

unread,
Jan 27, 2016, 9:39:12 AM1/27/16
to Polymer
Hello,

via ajax i get properties of a page, among those properties there are a few already containing html tags. when i include these in my template the page shows the html code instead of rendering it. 
What step am i missing, i presume there is an element for that?

thanks

Marco

Eric Bidelman

unread,
Jan 27, 2016, 11:19:25 AM1/27/16
to Marco Stolle, Polymer

Can you provide a code snippet of what you're doing?


Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/1d64850a-d018-46a1-8b96-fe570c77a1a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marco Stolle

unread,
Jan 27, 2016, 12:17:45 PM1/27/16
to Polymer, marco....@gmail.com
Hello Eric

this element receives properties i got from a database via a parent element via iron-ajax, the 'product.info' property already contains html markup tags ( a piece of html created in the past with an online wysiwyg editor and stored in the database). When i display my page, for the product.info part it shows raw html on the screen instead of rendered html.



<dom-module id="ws-product">
 
<template>
   
<style include="shared-styles"></style>
   
<style>
     
:host {
        display
: block;
     
}
     
div.addToCart {
        border
: 1px solid grey;
        padding
:5px;
     
}
     
div.clear {
        clear
: both;
     
}
   
</style>
   
<hr>
   
<div class="product">
     
<h3>{{product.name}}</h3>
     
<div class="description">
       
<p>{{product.description}}</p>
     
</div>
      <div class="info">
        {{product.info}}
      </div>
     </div>
  </template>
  <script>
   
(function() {
     
'use strict';
     
Polymer({
       
is: 'ws-product',
       
properties: {
         
product: {
           
name: {
             
type: String
           
},
           
description: {
             
type: String
           
},
           
info: {
             
type: String
           
}
         
}
       
}
     
});
   
})();
 
</script>
</dom-module>

Op woensdag 27 januari 2016 17:19:25 UTC+1 schreef Eric Bidelman:

Karl Tiedt

unread,
Jan 27, 2016, 12:21:09 PM1/27/16
to Marco Stolle, Polymer

Currently polymer has no means of safe html injection but you can use a really dirty hack and set your divs innerHTML property as inner-h-t-m-l="{{producct.info}}" and that should work for now...

Marco Stolle

unread,
Jan 27, 2016, 12:27:08 PM1/27/16
to Polymer, marco....@gmail.com
Ok thank you Karl, that did the trick.

Op woensdag 27 januari 2016 18:21:09 UTC+1 schreef Karl Tiedt:

Eric Bidelman

unread,
Jan 27, 2016, 12:39:28 PM1/27/16
to Marco Stolle, Polymer
Right. Polymer's data binding system prevents common XSS issues. Be cautious with using innerHTML. It opens your app up to XSS attacks.

Chuck Horton

unread,
Feb 1, 2016, 11:14:43 AM2/1/16
to Polymer
If you are using basic HTML tags you could try using the marked-element.

Tomek W

unread,
Feb 3, 2016, 1:53:57 AM2/3/16
to Polymer, marco....@gmail.com
You can also try juicy-html custom element https://github.com/Juicy/juicy-html
It supports direct HTML binding as well as fetching markup from external URL, plus it gives you simple data-binding that makes `dom-bind` work in your HTML fragments, triggers events on stamp, etc. 
It does the thing in a little less hacky way than inner-H-T-M-L.

Tomek
Reply all
Reply to author
Forward
0 new messages