This repository was archived by the owner on Aug 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlToArray.js
More file actions
91 lines (77 loc) · 2.33 KB
/
htmlToArray.js
File metadata and controls
91 lines (77 loc) · 2.33 KB
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
/* @flow */
const has = require('lodash/has')
const map = require('lodash/map')
const trim = require('lodash/trim')
const split = require('lodash/split')
const reject = require('lodash/reject')
const toLower = require('lodash/toLower')
const memoize = require('lodash/memoize')
const isString = require('lodash/isString')
const camelCase = require('lodash/camelCase')
const transform = require('lodash/transform')
const { load } = require('cheerio')
const template = require('./template')
const msPattern = /^-ms-/
const parseHTML = memoize(
html => load(html, { xmlMode: true }).root().toArray()[0].children
)
const toJSXKey = memoize(key => {
const lower = toLower(key)
return camelCase(msPattern.test(lower) ? lower.substr(1) : lower)
})
const transformStyle = object => {
if (isString(object.style)) {
object.style = transform(split(object.style, ';'), (result, style) => {
const firstColon = style.indexOf(':')
const key = trim(style.substr(0, firstColon))
if (key) {
result[toJSXKey(key)] = trim(style.substr(firstColon + 1))
}
}, {})
}
}
const rename = (object, fromKey, toKey) => {
if (has(object, fromKey)) {
object[toKey] = object[fromKey]
delete object[fromKey]
}
}
const transformElement = ({
name: type,
attribs: props,
children: childElements
}) => {
transformStyle(props)
rename(props, 'for', 'htmlFor')
rename(props, 'class', 'className')
if (type === 'input') {
rename(props, 'checked', 'defaultChecked')
rename(props, 'value', 'defaultValue')
}
let children = transformElements(childElements)
if (type === 'textarea' && children.length) {
props.defaultValue = children[0]
children = []
}
return { type, props, children }
}
const transformElements = elements =>
map(
reject(elements, ['type', 'comment']),
el => el.type === 'text' ? el.data : transformElement(el)
)
/**
* converts an html string to an array of plain objects and strings
*
* @example
* import htmlToArray from 'secondwheel/htmlToArray'
*
* // stringify to transmit over the network, cache, etc.
* const str = JSON.stringify(htmlToArray('<b>hello <%= name %></b>', { name: 'foo' }))
*/
const htmlToArray = (
tpl/*: string */ = '',
data/*: Object */ = {}
)/*: Array<string | Object> */ =>
transformElements(parseHTML(template(tpl, data)))
module.exports = htmlToArray