7

I am displaying internationalized strings within a Polymer element as follows:

<div>
  <span class="title">{{title}}</span>
  <br/><br/>
  <span class="subtitle">{{subtitle1}}</span>
  <br/>
  <span class="content">{{paragraph1}}</span>
  <br/><br/>
  <span class="subtitle">{{subtitle2}}</span>
  <br/>
  <span class="content">{{paragraph2}}</span>
</div>

... and have the following dart code:

@observable String title;
@observable String subtitle1;
@observable String paragraph1;
@observable String subtitle2;
@observable String paragraph2;

//...

void onUpdateLocale(_locale) {
  title = getTitle();
  subtitle1 = getSubtitle1();
  paragraph1 = getParagraph1();
  subtitle2 = getSubtitle2();
  paragraph2 = getParagraph2();
}

//...

getTitle() => Intl.message('MY TITLE', name:'title',
    desc: 'This is my title',
    args: [],
    examples: {'None' : 0});
getSubtitle1() => Intl.message('Subtitle 1', name:'subtitle1',
    desc: 'This is my first subtitle',
    args: [],
    examples: {'None' : 0});
getParagraph1() => Intl.message('This is my first paragraph',
    name:'paragraph1',
    desc: 'This is the my first paragraph',
    args: [],
    examples: {'None' : 0});
getSubtitle2() => Intl.message('Subtitle 2', name:'subtitle1',
    desc: 'This is my second subtitle',
    args: [],
    examples: {'None' : 0});
getParagraph2() => Intl.message('This is my second paragraph',
    name:'paragraph2',
    desc: 'This is the my second paragraph',
    args: [],
    examples: {'None' : 0});

Is there a way to combine title, subtitle1, paragraph1, subtitle2, and paragraph2 into one observable variable that includes the <br> and <span> tags in its value?

2

1 Answer 1

6

Update

A ready-to-use element for Dart Polymer 1.0 is bwu-bind-html


Update

Polymer now provides support for this out of the box

 this.injectBoundHTML('<div>your HTML goes here ${someBoundFieldValue}</div>);

Old

This is the code of the <safe-html> tag I'm using.

library safe_html;

import 'dart:async';
import "dart:html";

import "package:polymer/polymer.dart";


@CustomTag("safe-html")
class SafeHtml extends PolymerElement  {

  @published String model;

  NodeValidator nodeValidator;
  bool get applyAuthorStyles => true;
  bool isInitialized = false;

  SafeHtml.created() : super.created() {
    nodeValidator = new NodeValidatorBuilder()
    ..allowTextElements();
  }

  void modelChanded(old) {
    if(isInitialized) {
      _addFragment();
    }
  }

  void _addFragment() {
    var fragment = new DocumentFragment.html(model, validator: nodeValidator);
    $["container"].nodes
    ..clear()
    ..add(fragment);

  }

  @override
  void attached() {
    super.attached();
    Timer.run(() {
      _addFragment();
      isInitialized = true;
    });
  }
}
<!DOCTYPE html>

<polymer-element name="safe-html"> 
  <template>
    <div id="container"></div>
  </template>

  <script type="application/dart" src='safe_html.dart'></script>

</polymer-element>

usage:

<safe-html model="{{someField}}></safe-html>
4
  • Thanks, Günter. When I use your element as <safe-html model="{{myContent}}"><safe-html> I am getting the following errors and the content doesn't pick up the style. Removing disallowed attribute <SPAN class="content"> Removing disallowed attribute <SPAN class="subtitle"> Removing disallowed attribute <SPAN class="content"> Removing disallowed attribute <SPAN class="subtitle"> Removing disallowed attribute <SPAN class="content">
    – Peter
    Jan 1, 2014 at 13:43
  • 1
    You can customize the NodeValidator. I added the line ` ..allowElement('SPAN', attributes: ['class']);` as an example (not actually tried it - hope it works anyway) Jan 1, 2014 at 13:56
  • allowHtml5() apparently excludes "script elements, style attributes and any script handlers" ( from API dartdoc ).
    – Peter
    Jan 1, 2014 at 14:13
  • Credit for this element goes to stackoverflow.com/questions/19316300 (just stumbled upon again) Jan 15, 2014 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.