Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81158 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule getMarkupWrap
10
*/
11
12
var ExecutionEnvironment = require('ExecutionEnvironment');
13
14
var invariant = require('invariant');
15
16
/**
17
* Dummy container used to detect which wraps are necessary.
18
*/
19
var dummyNode =
20
ExecutionEnvironment.canUseDOM ? document.createElement('div') : null;
21
22
/**
23
* Some browsers cannot use `innerHTML` to render certain elements standalone,
24
* so we wrap them, render the wrapped nodes, then extract the desired node.
25
*
26
* In IE8, certain elements cannot render alone, so wrap all elements ('*').
27
*/
28
var shouldWrap = {
29
// Force wrapping for SVG elements because if they get created inside a <div>,
30
// they will be initialized in the wrong namespace (and will not display).
31
'circle': true,
32
'defs': true,
33
'ellipse': true,
34
'g': true,
35
'line': true,
36
'linearGradient': true,
37
'path': true,
38
'polygon': true,
39
'polyline': true,
40
'radialGradient': true,
41
'rect': true,
42
'stop': true,
43
'text': true
44
};
45
46
var selectWrap = [1, '<select multiple="true">', '</select>'];
47
var tableWrap = [1, '<table>', '</table>'];
48
var trWrap = [3, '<table><tbody><tr>', '</tr></tbody></table>'];
49
50
var svgWrap = [1, '<svg>', '</svg>'];
51
52
var markupWrap = {
53
'*': [1, '?<div>', '</div>'],
54
55
'area': [1, '<map>', '</map>'],
56
'col': [2, '<table><tbody></tbody><colgroup>', '</colgroup></table>'],
57
'legend': [1, '<fieldset>', '</fieldset>'],
58
'param': [1, '<object>', '</object>'],
59
'tr': [2, '<table><tbody>', '</tbody></table>'],
60
61
'optgroup': selectWrap,
62
'option': selectWrap,
63
64
'caption': tableWrap,
65
'colgroup': tableWrap,
66
'tbody': tableWrap,
67
'tfoot': tableWrap,
68
'thead': tableWrap,
69
70
'td': trWrap,
71
'th': trWrap,
72
73
'circle': svgWrap,
74
'defs': svgWrap,
75
'ellipse': svgWrap,
76
'g': svgWrap,
77
'line': svgWrap,
78
'linearGradient': svgWrap,
79
'path': svgWrap,
80
'polygon': svgWrap,
81
'polyline': svgWrap,
82
'radialGradient': svgWrap,
83
'rect': svgWrap,
84
'stop': svgWrap,
85
'text': svgWrap
86
};
87
88
/**
89
* Gets the markup wrap configuration for the supplied `nodeName`.
90
*
91
* NOTE: This lazily detects which wraps are necessary for the current browser.
92
*
93
* @param {string} nodeName Lowercase `nodeName`.
94
* @return {?array} Markup wrap configuration, if applicable.
95
*/
96
function getMarkupWrap(nodeName) {
97
invariant(!!dummyNode, 'Markup wrapping node not initialized');
98
if (!markupWrap.hasOwnProperty(nodeName)) {
99
nodeName = '*';
100
}
101
if (!shouldWrap.hasOwnProperty(nodeName)) {
102
if (nodeName === '*') {
103
dummyNode.innerHTML = '<link />';
104
} else {
105
dummyNode.innerHTML = '<' + nodeName + '></' + nodeName + '>';
106
}
107
shouldWrap[nodeName] = !dummyNode.firstChild;
108
}
109
return shouldWrap[nodeName] ? markupWrap[nodeName] : null;
110
}
111
112
113
module.exports = getMarkupWrap;
114
115