UNPKG

3.27 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const adapter_1 = require("./adapter");
4const constant_1 = require("./constant");
5const options_1 = require("./options");
6const lodash_1 = require("lodash");
7const env_1 = require("./env");
8const voidHtmlTags = new Set([
9 // 'image',
10 'img',
11 'input',
12 'import'
13]);
14if (env_1.isTestEnv) {
15 voidHtmlTags.add('image');
16}
17exports.capitalized = (name) => name.charAt(0).toUpperCase() + name.slice(1);
18function stringifyAttributes(input, componentName) {
19 const attributes = [];
20 for (const key of Object.keys(input)) {
21 let value = input[key];
22 if (value === false) {
23 continue;
24 }
25 if (Array.isArray(value)) {
26 value = value.join(' ');
27 }
28 let attribute = key;
29 if ("quickapp" /* quickapp */ === adapter_1.Adapter.type && key === 'style') {
30 const nameCapitalized = exports.capitalized(componentName);
31 if (!['div', 'text'].includes(componentName) &&
32 (constant_1.quickappComponentName.has(nameCapitalized) || constant_1.DEFAULT_Component_SET_COPY.has(nameCapitalized))) {
33 attribute = 'customstyle';
34 }
35 }
36 if (process.env.NODE_ENV !== 'test' &&
37 ("weapp" /* weapp */ === adapter_1.Adapter.type || "qq" /* qq */ === adapter_1.Adapter.type) &&
38 key === adapter_1.Adapter.key &&
39 typeof value === 'string') {
40 value = value.split(`${constant_1.LOOP_ORIGINAL}.`).join('');
41 }
42 if (value !== true) {
43 attribute += `="${String(value)}"`;
44 }
45 attributes.push(attribute);
46 }
47 return attributes.length > 0 ? ' ' + attributes.join(' ') : '';
48}
49exports.createHTMLElement = (options, isFirstEmit = false) => {
50 options = Object.assign({
51 name: 'div',
52 attributes: {},
53 value: ''
54 }, options);
55 const name = options.name;
56 if ("quickapp" /* quickapp */ === adapter_1.Adapter.type) {
57 const nameCapitalized = exports.capitalized(name);
58 if (constant_1.quickappComponentName.has(nameCapitalized)) {
59 options.name = `taro-${name}`;
60 if (options.attributes['className']) {
61 options.attributes['class'] = options.attributes['className'];
62 delete options.attributes['className'];
63 }
64 }
65 if (isFirstEmit && name === 'div' && options_1.transformOptions.isRoot) {
66 options.name = 'taro-page';
67 for (const key in options.attributes) {
68 if (options.attributes.hasOwnProperty(key)) {
69 const attr = options.attributes[key];
70 options.attributes[lodash_1.camelCase(key)] = attr;
71 delete options.attributes[key];
72 }
73 }
74 }
75 if (name === 'view') {
76 options.name = 'div';
77 }
78 }
79 const isVoidTag = voidHtmlTags.has(options.name);
80 let ret = `<${options.name}${stringifyAttributes(options.attributes, name)}${isVoidTag ? `/` : ''}>`;
81 if (!isVoidTag) {
82 ret += `${options.value}</${options.name}>`;
83 }
84 return ret;
85};
86//# sourceMappingURL=create-html-element.js.map
\No newline at end of file