文档管理中心
您当前正在浏览新版开发者文档中心,目录分类和层级有所调整。点击左侧当前文档分类名称前的“☰”图标,可切换文档分类。 了解新版目录
开发与测试元服务开发ASCF框架API参考组件基础内容rich-text

rich-text

富文本容器。文本内容直接写在标签内容区。

起始版本: 1.0.0

约束与限制:

  • 文本内容只支持静态内容。

  • 由于实时编译,文本内容尽量不要频繁改变,否则可能导致性能问题。

属性:

展开
名称 类型 默认值 必填 描述
nodes array/string [] 否 节点列表/HTMLString。
space string - 否

显示连续空格,取值为ensp、emsp、nbsp:

- ensp:中文字符空格一半大小。

- emsp:中文字符空格大小。

- nbsp:根据字体设置的空格大小。

user-select boolean false 否 文本是否可选。

nodes:

现支持两种节点,通过type来区分,分别是元素节点和文本节点,默认是元素节点,在富文本区域里显示的HTML节点。

  • 元素节点:type = node

    展开
    属性 说明 类型 必填 描述
    name 标签名 string 是 支持部分受信任的HTML节点。
    attrs 属性 Object 否 支持部分受信任的属性,遵循Pascal命名法。
    children 子节点列表 Array 否 结构和nodes一致。
  • 文本节点:type = text

    展开
    属性 说明 类型 必填 描述
    text 文本 string 是 支持entities。

受信任的HTML节点及属性:

说明
  • 全局支持class和style属性,不支持id属性。

  • 如果使用了不受信任的HTML节点,该节点及其所有子节点将会被移除。

  • name属性大小写不敏感,建议使用小写。

  • rich-text里面写js不支持事件执行。

  • attrs属性不支持id支持class。

展开
节点 属性
a -
abbr -
address -
article -
aside -
b -
bdi -
bdo dir
big -
blockquote -
br -
caption -
center -
cite -
code -
col span, width
colgroup span, width
dd -
del -
div -
dl -
dt -
em -
fieldset -
font -
footer -
h1 -
h2 -
h3 -
h4 -
h5 -
h6 -
header -
hr -
i -
img alt, src, height, width
ins -
label -
legend -
li -
mark -
nav -
ol start, type
p -
pre -
q -
rt -
ruby -
s -
section -
small -
span -
strong -
sub -
sup -
table width
tbody -
td colspan, rowspan, height, width
tfoot -
th colspan, rowspan, height, width
thead -
tr -
tt -
u -
ul -

示例:

hxml文件:

收起
自动换行
深色代码主题
复制
  1. <view>
  2. <view>通过HTML String渲染</view>
  3. <view>
  4. <scroll-view scroll-y>{{htmlSnip}}</scroll-view>
  5. <button type="primary" bindtap="renderHtml">渲染HTML</button>
  6. <block has:if="{{renderedByHtml}}">
  7. <rich-text nodes="{{htmlSnip}}" user-select="{{true}}"></rich-text>
  8. </block>
  9. </view>
  10. </view>
  11. <view>
  12. <view>通过节点渲染</view>
  13. <view>
  14. <scroll-view scroll-y>{{nodeSnip}}</scroll-view>
  15. <button type="primary" bindtap="renderNode">渲染Node</button>
  16. <block has:if="{{renderedByNode}}">
  17. <rich-text nodes="{{nodes}}"></rich-text>
  18. </block>
  19. </view>
  20. </view>

js文件:

收起
自动换行
深色代码主题
复制
  1. Page({
  2. data: {
  3. htmlSnip: `<div class="div_class">
  4. <h1>Title</h1>
  5. <p class="p">
  6. Life is&nbsp;<i>like</i>&nbsp;a box of
  7. <b>&nbsp;chocolates我可以复制</b>.
  8. </p>
  9. </div>`,
  10. nodeSnip: `Page({
  11. data: {
  12. nodes: [{
  13. name: 'div',
  14. attrs: {
  15. class: 'div_class',
  16. style: 'line-height: 30px; color: orange;'
  17. },
  18. children: [{
  19. type: 'text',
  20. text: 'You never know what you're gonna get.'
  21. }]
  22. }]
  23. }
  24. })`,
  25. nodes: [{
  26. name: "div",
  27. attrs: {
  28. class: "div_class",
  29. style: "line-height: 60px; color:#FFA500;"
  30. },
  31. children: [
  32. {
  33. type: "text",
  34. text: "You never know what you're gonna get."
  35. }
  36. ]
  37. }],
  38. renderedByHtml: false,
  39. renderedByNode: false
  40. },
  41. renderHtml() {
  42. this.setData({
  43. renderedByHtml: true
  44. });
  45. },
  46. renderNode() {
  47. this.setData({
  48. renderedByNode: true
  49. });
  50. }
  51. });