【问题标题】:HTML Tags Within Internationalized Strings In Polymer.dartPolymer.dart 中国际化字符串中的 HTML 标记
【发布时间】:2014-01-19 00:51:57
【问题描述】:

我在 Polymer 元素中显示国际化字符串,如下所示:

<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>

...并具有以下飞镖代码:

@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});

有没有办法将titlesubtitle1paragraph1subtitle2paragraph2 组合成一个可观察变量,其中包含 &lt;br&gt;&lt;span&gt; 标记的值?

【问题讨论】:

标签: internationalization dart dart-polymer dart-html


【解决方案1】:

更新

Dart Polymer 1.0 的即用型元素是bwu-bind-html


更新

Polymer 现在提供开箱即用的支持

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

这是我正在使用的&lt;safe-html&gt; 标签的代码。

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>

用法:

<safe-html model="{{someField}}></safe-html>

【讨论】:

  • 谢谢,君特。当我将您的元素用作&lt;safe-html model="{{myContent}}"&gt;&lt;safe-html&gt; 时,我收到以下错误并且内容没有采用样式。 Removing disallowed attribute &lt;SPAN class="content"&gt; Removing disallowed attribute &lt;SPAN class="subtitle"&gt; Removing disallowed attribute &lt;SPAN class="content"&gt; Removing disallowed attribute &lt;SPAN class="subtitle"&gt; Removing disallowed attribute &lt;SPAN class="content"&gt;
  • 您可以自定义NodeValidator。我添加了行 `..allowElement('SPAN', attributes: ['class']);` 作为示例(实际上没有尝试过 - 希望它仍然有效)
  • allowHtml5() 显然不包括“脚本元素、样式属性和任何脚本处理程序”(from API dartdoc)。
  • 此元素的功劳归于stackoverflow.com/questions/19316300(刚刚再次偶然发现)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-16
  • 2022-06-22
相关资源
最近更新 更多