zhangjian
2023-06-05 0976d2d0f90cff460cedfdc8bd74e98c2c31a58c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const SINGLE_TAGS = [
  'area',
  'base',
  'br',
  'col',
  'command',
  'embed',
  'hr',
  'img',
  'input',
  'keygen',
  'link',
  'menuitem',
  'meta',
  'param',
  'source',
  'track',
  'wbr'
];
 
const ATTRIBUTE_QUOTES_REQUIRED = /[\t\n\f\r "'`=<>]/;
 
/** Render PostHTML Tree to HTML
 *
 * @param  {Array|Object} tree PostHTML Tree @param  {Object} options Options
 *
 * @return {String} HTML
 */
function render(tree, options) {
  /** Options
   *
   * @type {Object}
   *
   * @prop {Array<String|RegExp>} singleTags  Custom single tags (selfClosing)
   * @prop {String} closingSingleTag Closing format for single tag @prop
   * @prop {Boolean} quoteAllAttributes If all attributes should be quoted.
   * Otherwise attributes will be unquoted when allowed.
   * @prop {Boolean} replaceQuote Replaces quotes in attribute values with `&quote;`
   *
   * Formats:
   *
   * ``` tag: `<br></br>` ```, slash: `<br />` ```, ```default: `<br>` ```
   */
  options = options || {};
 
  const singleTags = options.singleTags ? SINGLE_TAGS.concat(options.singleTags) : SINGLE_TAGS;
  const singleRegExp = singleTags.filter(tag => {
    return tag instanceof RegExp;
  });
 
  const {closingSingleTag} = options;
  let {quoteAllAttributes} = options;
  if (quoteAllAttributes === undefined) {
    quoteAllAttributes = true;
  }
 
  let {replaceQuote} = options;
  if (replaceQuote === undefined) {
    replaceQuote = true;
  }
 
  let {quoteStyle} = options;
  if (quoteStyle === undefined) {
    quoteStyle = 2;
  }
 
  return html(tree);
 
  /** @private */
  function isSingleTag(tag) {
    if (singleRegExp.length > 0) {
      return singleRegExp.some(reg => reg.test(tag));
    }
 
    if (!singleTags.includes(tag)) {
      return false;
    }
 
    return true;
  }
 
  /** @private */
  function attrs(object) {
    let attr = '';
 
    for (const key in object) {
      if (typeof object[key] === 'string') {
        if (quoteAllAttributes || object[key].match(ATTRIBUTE_QUOTES_REQUIRED)) {
          let attrValue = object[key];
 
          if (replaceQuote) {
            attrValue = object[key].replace(/"/g, '&quot;');
          }
 
          attr += makeAttr(key, attrValue, quoteStyle);
        } else if (object[key] === '') {
          attr += ' ' + key;
        } else {
          attr += ' ' + key + '=' + object[key];
        }
      } else if (object[key] === true) {
        attr += ' ' + key;
      } else if (typeof object[key] === 'number') {
        attr += makeAttr(key, object[key], quoteStyle);
      }
    }
 
    return attr;
  }
 
  /** @private */
  function traverse(tree, cb) {
    if (tree !== undefined) {
      for (let i = 0, {length} = tree; i < length; i++) {
        traverse(cb(tree[i]), cb);
      }
    }
  }
 
  /** @private */
  function makeAttr(key, attrValue, quoteStyle = 1) {
    if (quoteStyle === 1) {
      // Single Quote
      return ` ${key}='${attrValue}'`;
    }
 
    if (quoteStyle === 2) {
      // Double Quote
      return ` ${key}="${attrValue}"`;
    }
 
    // Smart Quote
    if (attrValue.includes('"')) {
      return ` ${key}='${attrValue}'`;
    }
 
    return ` ${key}="${attrValue}"`;
  }
 
  /**
   * HTML Stringifier
   *
   * @param  {Array|Object} tree PostHTML Tree
   *
   * @return {String} result HTML
   */
  function html(tree) {
    let result = '';
 
    if (!Array.isArray(tree)) {
      tree = [tree];
    }
 
    traverse(tree, node => {
      // Undefined, null, '', [], NaN
      if (node === undefined ||
        node === null ||
        node === false ||
        node.length === 0 ||
        Number.isNaN(node)) {
        return;
      }
 
      // Treat as new root tree if node is an array
      if (Array.isArray(node)) {
        result += html(node);
 
        return;
      }
 
      if (typeof node === 'string' || typeof node === 'number') {
        result += node;
 
        return;
      }
 
      // Skip node
      if (node.tag === false) {
        result += html(node.content);
 
        return;
      }
 
      const tag = node.tag || 'div';
 
      result += '<' + tag;
 
      if (node.attrs) {
        result += attrs(node.attrs);
      }
 
      if (isSingleTag(tag)) {
        switch (closingSingleTag) {
          case 'tag':
            result += '></' + tag + '>';
 
            break;
          case 'slash':
            result += ' />';
 
            break;
          default:
            result += '>';
        }
 
        result += html(node.content);
      } else {
        result += '>' + html(node.content) + '</' + tag + '>';
      }
    });
 
    return result;
  }
}
 
/**
 * @module posthtml-render
 *
 * @version 1.1.5
 * @license MIT
 */
module.exports = render;