目录

rich-text

Represents the rich text container. Text content is directly written in the label content area.

Since: 1.0.0

Constraints:

Attributes

Name Type Default Value Mandatory Description
nodes array/string [] No Node list/HTML string.
space string - No

How consecutive spaces are rendered. Valid values:

- ensp: half-width Chinese space.

- emsp: full-width Chinese space.

- nbsp: non-breaking space with the font-dependent width.

user-select boolean false No Whether the text is selectable.

nodes:

Two types of nodes are supported: element node and text node. The default type is element node, which is the HTML node displayed in the rich text container.

Trusted HTML Nodes and Attributes

  • The class and style attributes are supported globally, but the id attribute is not supported.

  • If an untrusted HTML node is used, the node and all its child nodes will be removed.

  • The name attribute is case insensitive. Lowercase letters are recommended.

  • JavaScript code written in the rich-text component does not support event execution.

  • The attrs attribute supports class, but not id.

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

Example:

HXML file:

<view>
  <view>Rendering using HTML strings</view>
  <view>
    <scroll-view scroll-y>{{htmlSnip}}</scroll-view>
    <button type="primary" bindtap="renderHtml">Rendering HTML</button>
    <block has:if="{{renderedByHtml}}">
      <rich-text nodes="{{htmlSnip}}" user-select="{{true}}"></rich-text>
    </block>
  </view>
</view>
<view>
  <view>Rendering by node</view>
  <view>
    <scroll-view scroll-y>{{nodeSnip}}</scroll-view>
    <button type="primary" bindtap="renderNode">Rendering node</button>
    <block has:if="{{renderedByNode}}">
      <rich-text nodes="{{nodes}}"></rich-text>
    </block>
  </view>
</view>

JavaScript file:

Page({
  data: {
    htmlSnip: `<div class="div_class">
      <h1>Title</h1>
      <p class="p">
      Life is&nbsp;<i>like</i>&nbsp;a box of
      <b>&nbsp;chocolates I can copy</b>.
      </p>
      </div>`,
    nodeSnip: `Page({
      data: {
        nodes: [{
          name: 'div',
          attrs: {
            class: 'div_class',
            style: 'line-height: 30px; color: orange;'
          },
          children: [{
            type: 'text',
            text: 'You never know what you're gonna get.'
          }]
        }]
      }
    })`,
    nodes: [{
      name: "div",
      attrs: {
        class: "div_class",
        style: "line-height: 60px; color:#FFA500;"
      },
      children: [
        {
          type: "text",
          text: "You never know what you're gonna get."
        }
      ]
    }],
    renderedByHtml: false,
    renderedByNode: false
  },
  renderHtml() {
    this.setData({
      renderedByHtml: true
    });
  },
  renderNode() {
    this.setData({
      renderedByNode: true
    });
  }
});