@tarojs/components
Version:
Taro 组件库。
69 lines (64 loc) • 2.15 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
const index = require('./index-dd4b7ba3.js');
let RichText = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
this.renderNode = (node) => {
if ('type' in node && node.type === 'text') {
// unsupport Html Entries
const content = (node.text || '').replace(/ /g, '\u00A0');
return content;
}
else if ('name' in node && node.name) {
const { name, attrs, children } = node;
const attributes = {};
let childList = [];
if (attrs && typeof attrs === 'object') {
for (const key in attrs) {
const val = attrs[key];
if (key === 'style' && typeof val === 'string') {
// stencil JSX style props only support object
const styles = val
.split(';')
.map(item => item.trim())
.filter(item => item);
const styleObj = {};
styles.forEach(item => {
if (!item)
return;
const res = /(.+): *(.+)/g.exec(item);
if (!res)
return;
const [, name, value] = res;
const styleName = name.replace(/-([a-z])/g, (...args) => args[1].toUpperCase());
styleObj[styleName] = value;
});
if (Object.keys(styleObj).length) {
attributes.style = styleObj;
}
continue;
}
attributes[key] = val;
}
}
if (children && children.length) {
childList = children.map(node => this.renderNode(node));
}
// @ts-ignore
return index.h(name, attributes, childList);
}
return null;
};
}
render() {
const { nodes, renderNode } = this;
if (Array.isArray(nodes)) {
return (index.h(index.Host, null, nodes.map(node => renderNode(node))));
}
else {
return index.h(index.Host, { innerHTML: nodes });
}
}
};
exports.taro_rich_text_core = RichText;