UNPKG

2.07 kBJavaScriptView Raw
1import { r as registerInstance, h, H as Host } from './index-8809c729.js';
2
3let RichText = class {
4 constructor(hostRef) {
5 registerInstance(this, hostRef);
6 this.renderNode = (node) => {
7 if ('type' in node && node.type === 'text') {
8 // nonsupport Html Entries
9 const content = (node.text || '').replace(/ /g, '\u00A0');
10 return content;
11 }
12 else if ('name' in node && node.name) {
13 const { name, attrs, children } = node;
14 const attributes = {};
15 let childList = [];
16 if (attrs && typeof attrs === 'object') {
17 for (const key in attrs) {
18 const val = attrs[key];
19 if (key === 'style' && typeof val === 'string') {
20 // stencil JSX style props only support object
21 const styles = val
22 .split(';')
23 .map(item => item.trim())
24 .filter(item => item);
25 const styleObj = {};
26 styles.forEach(item => {
27 if (!item)
28 return;
29 const res = /(.+): *(.+)/g.exec(item);
30 if (!res)
31 return;
32 const [, name, value] = res;
33 const styleName = name.replace(/-([a-z])/g, (...args) => args[1].toUpperCase());
34 styleObj[styleName] = value;
35 });
36 if (Object.keys(styleObj).length) {
37 attributes.style = styleObj;
38 }
39 continue;
40 }
41 attributes[key] = val;
42 }
43 }
44 if (children && children.length) {
45 childList = children.map(node => this.renderNode(node));
46 }
47 // @ts-ignore
48 return h(name, attributes, childList);
49 }
50 return null;
51 };
52 }
53 render() {
54 const { nodes, renderNode } = this;
55 if (Array.isArray(nodes)) {
56 return (h(Host, null, nodes.map(node => renderNode(node))));
57 }
58 else {
59 return h(Host, { innerHTML: nodes });
60 }
61 }
62};
63
64export { RichText as taro_rich_text_core };