This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
164 lines (136 loc) · 6.84 KB
/
index.js
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
const HTML_ATTRIBUTES = [
'accept', 'acceptCharset', 'accessKey', 'action', 'allowFullScreen', 'allowTransparency',
'alt', 'async', 'autoComplete', 'autoFocus', 'autoPlay', 'capture', 'cellPadding',
'cellSpacing', 'challenge', 'charSet', 'checked', 'cite', 'classID', 'className',
'colSpan', 'cols', 'content', 'contentEditable', 'contextMenu', 'controls', 'coords',
'crossOrigin', 'data', 'dateTime', 'default', 'defer', 'dir', 'disabled', 'download',
'draggable', 'encType', 'form', 'formAction', 'formEncType', 'formMethod', 'formNoValidate',
'formTarget', 'frameBorder', 'headers', 'height', 'hidden', 'high', 'href', 'hrefLang',
'htmlFor', 'httpEquiv', 'icon', 'id', 'inputMode', 'integrity', 'is', 'keyParams', 'keyType',
'kind', 'label', 'lang', 'list', 'loop', 'low', 'manifest', 'marginHeight', 'marginWidth',
'max', 'maxLength', 'media', 'mediaGroup', 'method', 'min', 'minLength', 'multiple', 'muted',
'name', 'noValidate', 'nonce', 'open', 'optimum', 'pattern', 'placeholder', 'poster',
'preload', 'profile', 'radioGroup', 'readOnly', 'rel', 'required', 'reversed', 'role',
'rowSpan', 'rows', 'sandbox', 'scope', 'scoped', 'scrolling', 'seamless', 'selected',
'shape', 'size', 'sizes', 'span', 'spellCheck', 'src', 'srcDoc', 'srcLang', 'srcSet', 'start',
'step', 'style', 'summary', 'tabIndex', 'target', 'title', 'type', 'useMap', 'value', 'width',
'wmode', 'wrap'
]
const NON_STANDARD_ATTRIBUTES = [
'autoCapitalize', 'autoCorrect', 'color', 'itemProp', 'itemScope', 'itemType', 'itemRef',
'itemID', 'security', 'unselectable', 'results', 'autoSave'
]
const SVG_ATTRIBUTES = [
'accentHeight', 'accumulate', 'additive', 'alignmentBaseline', 'allowReorder', 'alphabetic',
'amplitude', 'arabicForm', 'ascent', 'attributeName', 'attributeType', 'autoReverse',
'azimuth', 'baseFrequency', 'baseProfile', 'baselineShift', 'bbox', 'begin', 'bias', 'by',
'calcMode', 'capHeight', 'clip', 'clipPath', 'clipPathUnits', 'clipRule', 'colorInterpolation',
'colorInterpolationFilters', 'colorProfile', 'colorRendering', 'contentScriptType',
'contentStyleType', 'cursor', 'cx', 'cy', 'd', 'decelerate', 'descent', 'diffuseConstant',
'direction', 'display', 'divisor', 'dominantBaseline', 'dur', 'dx', 'dy', 'edgeMode',
'elevation', 'enableBackground', 'end', 'exponent', 'externalResourcesRequired', 'fill',
'fillOpacity', 'fillRule', 'filter', 'filterRes', 'filterUnits', 'floodColor', 'floodOpacity',
'focusable', 'fontFamily', 'fontSize', 'fontSizeAdjust', 'fontStretch', 'fontStyle',
'fontVariant', 'fontWeight', 'format', 'from', 'fx', 'fy', 'g1', 'g2', 'glyphName',
'glyphOrientationHorizontal', 'glyphOrientationVertical', 'glyphRef', 'gradientTransform',
'gradientUnits', 'hanging', 'horizAdvX', 'horizOriginX', 'ideographic', 'imageRendering',
'in', 'in2', 'intercept', 'k', 'k1', 'k2', 'k3', 'k4', 'kernelMatrix', 'kernelUnitLength',
'kerning', 'keyPoints', 'keySplines', 'keyTimes', 'lengthAdjust', 'letterSpacing',
'lightingColor', 'limitingConeAngle', 'local', 'markerEnd', 'markerHeight', 'markerMid',
'markerStart', 'markerUnits', 'markerWidth', 'mask', 'maskContentUnits', 'maskUnits',
'mathematical', 'mode', 'numOctaves', 'offset', 'opacity', 'operator', 'order',
'orient', 'orientation', 'origin', 'overflow', 'overlinePosition', 'overlineThickness',
'paintOrder', 'panose1', 'pathLength', 'patternContentUnits', 'patternTransform',
'patternUnits', 'pointerEvents', 'points', 'pointsAtX', 'pointsAtY', 'pointsAtZ',
'preserveAlpha', 'preserveAspectRatio', 'primitiveUnits', 'r', 'radius', 'refX', 'refY',
'renderingIntent', 'repeatCount', 'repeatDur', 'requiredExtensions', 'requiredFeatures',
'restart', 'result', 'rotate', 'rx', 'ry', 'scale', 'seed', 'shapeRendering', 'slope',
'spacing', 'specularConstant', 'specularExponent', 'speed', 'spreadMethod', 'startOffset',
'stdDeviation', 'stemh', 'stemv', 'stitchTiles', 'stopColor', 'stopOpacity',
'strikethroughPosition', 'strikethroughThickness', 'string', 'stroke', 'strokeDasharray',
'strokeDashoffset', 'strokeLinecap', 'strokeLinejoin', 'strokeMiterlimit',
'strokeOpacity', 'strokeWidth', 'surfaceScale', 'systemLanguage', 'tableValues', 'targetX',
'targetY', 'textAnchor', 'textDecoration', 'textLength', 'textRendering', 'to', 'transform',
'u1', 'u2', 'underlinePosition', 'underlineThickness', 'unicode', 'unicodeBidi',
'unicodeRange', 'unitsPerEm', 'vAlphabetic', 'vHanging', 'vIdeographic', 'vMathematical',
'values', 'vectorEffect', 'version', 'vertAdvY', 'vertOriginX', 'vertOriginY', 'viewBox',
'viewTarget', 'visibility', 'widths', 'wordSpacing', 'writingMode', 'x', 'x1', 'x2',
'xChannelSelector', 'xHeight', 'xlinkActuate', 'xlinkArcrole', 'xlinkHref', 'xlinkRole',
'xlinkShow', 'xlinkTitle', 'xlinkType', 'xmlBase', 'xmlLang', 'xmlSpace', 'y', 'y1', 'y2',
'yChannelSelector', 'z', 'zoomAndPan'
]
const attributeMap = [
...HTML_ATTRIBUTES,
...NON_STANDARD_ATTRIBUTES,
...SVG_ATTRIBUTES
].reduce((accumulator, attribute) => {
const lowerCase = attribute.toLowerCase()
if (attribute !== lowerCase) {
accumulator[ lowerCase ] = attribute
}
return accumulator
}, {})
attributeMap['class'] = 'className'
function camelCase (string) {
return string.toLowerCase().replace(/-([a-z])/g, (match, $1) => $1.toUpperCase())
}
function styleStringToJSON (string = '') {
return string.split(';').reduce((accumulator, piece) => {
const pair = piece.split(':')
const key = camelCase(pair[0] || '').trim()
const value = (pair[1] || '').trim()
if (key && value) {
accumulator[key] = value
}
return accumulator
}, {})
}
export function attributeListToReact (attributeList) {
return [...attributeList].reduce((accumulator, { name, value }) => {
let key = attributeMap[name.replace(/[-:]/, '')] || name
if (key === 'style') {
value = styleStringToJSON(value)
}
accumulator[key] = value
return accumulator
}, {})
}
let keyCounter = 0
export function nodeListToReact (nodeList, createElement) {
return [...nodeList].reduce((accumulator, node) => {
if (!node._domReactKey) {
node._domReactKey = '_domReact' + String(keyCounter++)
}
const child = nodeToReact(node, createElement)
if (Array.isArray(child)) {
accumulator.push(...child)
} else {
accumulator.push(child)
}
return accumulator
}, [])
}
export function nodeToReact (node, createElement) {
if (!node) {
return null
}
if (node.nodeType === 3) {
return node.nodeValue
}
if (node.nodeType !== 1) {
return null
}
const type = node.nodeName.toLowerCase()
let props = {}
let children = []
if (node.hasAttributes()) {
props = attributeListToReact(node.attributes)
}
if (node._domReactKey) {
props.key = node._domReactKey
}
if (node.hasChildNodes()) {
children = nodeListToReact(node.childNodes, createElement)
}
return createElement(type, props, ...children)
}