1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var _assign = require('object-assign');
|
12 |
|
13 | var emptyFunction = require('fbjs/lib/emptyFunction');
|
14 | var warning = require('fbjs/lib/warning');
|
15 |
|
16 | var validateDOMNesting = emptyFunction;
|
17 |
|
18 | if (process.env.NODE_ENV !== 'production') {
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | var specialTags = ['address', 'applet', 'area', 'article', 'aside', 'base', 'basefont', 'bgsound', 'blockquote', 'body', 'br', 'button', 'caption', 'center', 'col', 'colgroup', 'dd', 'details', 'dir', 'div', 'dl', 'dt', 'embed', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'frame', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hgroup', 'hr', 'html', 'iframe', 'img', 'input', 'isindex', 'li', 'link', 'listing', 'main', 'marquee', 'menu', 'menuitem', 'meta', 'nav', 'noembed', 'noframes', 'noscript', 'object', 'ol', 'p', 'param', 'plaintext', 'pre', 'script', 'section', 'select', 'source', 'style', 'summary', 'table', 'tbody', 'td', 'template', 'textarea', 'tfoot', 'th', 'thead', 'title', 'tr', 'track', 'ul', 'wbr', 'xmp'];
|
32 |
|
33 |
|
34 | var inScopeTags = ['applet', 'caption', 'html', 'table', 'td', 'th', 'marquee', 'object', 'template',
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | 'foreignObject', 'desc', 'title'];
|
40 |
|
41 |
|
42 | var buttonScopeTags = inScopeTags.concat(['button']);
|
43 |
|
44 |
|
45 | var impliedEndTags = ['dd', 'dt', 'li', 'option', 'optgroup', 'p', 'rp', 'rt'];
|
46 |
|
47 | var emptyAncestorInfo = {
|
48 | current: null,
|
49 |
|
50 | formTag: null,
|
51 | aTagInScope: null,
|
52 | buttonTagInScope: null,
|
53 | nobrTagInScope: null,
|
54 | pTagInButtonScope: null,
|
55 |
|
56 | listItemTagAutoclosing: null,
|
57 | dlItemTagAutoclosing: null
|
58 | };
|
59 |
|
60 | var updatedAncestorInfo = function (oldInfo, tag, instance) {
|
61 | var ancestorInfo = _assign({}, oldInfo || emptyAncestorInfo);
|
62 | var info = { tag: tag, instance: instance };
|
63 |
|
64 | if (inScopeTags.indexOf(tag) !== -1) {
|
65 | ancestorInfo.aTagInScope = null;
|
66 | ancestorInfo.buttonTagInScope = null;
|
67 | ancestorInfo.nobrTagInScope = null;
|
68 | }
|
69 | if (buttonScopeTags.indexOf(tag) !== -1) {
|
70 | ancestorInfo.pTagInButtonScope = null;
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 | if (specialTags.indexOf(tag) !== -1 && tag !== 'address' && tag !== 'div' && tag !== 'p') {
|
76 | ancestorInfo.listItemTagAutoclosing = null;
|
77 | ancestorInfo.dlItemTagAutoclosing = null;
|
78 | }
|
79 |
|
80 | ancestorInfo.current = info;
|
81 |
|
82 | if (tag === 'form') {
|
83 | ancestorInfo.formTag = info;
|
84 | }
|
85 | if (tag === 'a') {
|
86 | ancestorInfo.aTagInScope = info;
|
87 | }
|
88 | if (tag === 'button') {
|
89 | ancestorInfo.buttonTagInScope = info;
|
90 | }
|
91 | if (tag === 'nobr') {
|
92 | ancestorInfo.nobrTagInScope = info;
|
93 | }
|
94 | if (tag === 'p') {
|
95 | ancestorInfo.pTagInButtonScope = info;
|
96 | }
|
97 | if (tag === 'li') {
|
98 | ancestorInfo.listItemTagAutoclosing = info;
|
99 | }
|
100 | if (tag === 'dd' || tag === 'dt') {
|
101 | ancestorInfo.dlItemTagAutoclosing = info;
|
102 | }
|
103 |
|
104 | return ancestorInfo;
|
105 | };
|
106 |
|
107 | |
108 |
|
109 |
|
110 | var isTagValidWithParent = function (tag, parentTag) {
|
111 |
|
112 | switch (parentTag) {
|
113 |
|
114 | case 'select':
|
115 | return tag === 'option' || tag === 'optgroup' || tag === '#text';
|
116 | case 'optgroup':
|
117 | return tag === 'option' || tag === '#text';
|
118 |
|
119 |
|
120 | case 'option':
|
121 | return tag === '#text';
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | case 'tr':
|
129 | return tag === 'th' || tag === 'td' || tag === 'style' || tag === 'script' || tag === 'template';
|
130 |
|
131 | case 'tbody':
|
132 | case 'thead':
|
133 | case 'tfoot':
|
134 | return tag === 'tr' || tag === 'style' || tag === 'script' || tag === 'template';
|
135 |
|
136 | case 'colgroup':
|
137 | return tag === 'col' || tag === 'template';
|
138 |
|
139 | case 'table':
|
140 | return tag === 'caption' || tag === 'colgroup' || tag === 'tbody' || tag === 'tfoot' || tag === 'thead' || tag === 'style' || tag === 'script' || tag === 'template';
|
141 |
|
142 | case 'head':
|
143 | return tag === 'base' || tag === 'basefont' || tag === 'bgsound' || tag === 'link' || tag === 'meta' || tag === 'title' || tag === 'noscript' || tag === 'noframes' || tag === 'style' || tag === 'script' || tag === 'template';
|
144 |
|
145 | case 'html':
|
146 | return tag === 'head' || tag === 'body';
|
147 | case '#document':
|
148 | return tag === 'html';
|
149 | }
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | switch (tag) {
|
155 | case 'h1':
|
156 | case 'h2':
|
157 | case 'h3':
|
158 | case 'h4':
|
159 | case 'h5':
|
160 | case 'h6':
|
161 | return parentTag !== 'h1' && parentTag !== 'h2' && parentTag !== 'h3' && parentTag !== 'h4' && parentTag !== 'h5' && parentTag !== 'h6';
|
162 |
|
163 | case 'rp':
|
164 | case 'rt':
|
165 | return impliedEndTags.indexOf(parentTag) === -1;
|
166 |
|
167 | case 'body':
|
168 | case 'caption':
|
169 | case 'col':
|
170 | case 'colgroup':
|
171 | case 'frame':
|
172 | case 'head':
|
173 | case 'html':
|
174 | case 'tbody':
|
175 | case 'td':
|
176 | case 'tfoot':
|
177 | case 'th':
|
178 | case 'thead':
|
179 | case 'tr':
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | return parentTag == null;
|
185 | }
|
186 |
|
187 | return true;
|
188 | };
|
189 |
|
190 | |
191 |
|
192 |
|
193 | var findInvalidAncestorForTag = function (tag, ancestorInfo) {
|
194 | switch (tag) {
|
195 | case 'address':
|
196 | case 'article':
|
197 | case 'aside':
|
198 | case 'blockquote':
|
199 | case 'center':
|
200 | case 'details':
|
201 | case 'dialog':
|
202 | case 'dir':
|
203 | case 'div':
|
204 | case 'dl':
|
205 | case 'fieldset':
|
206 | case 'figcaption':
|
207 | case 'figure':
|
208 | case 'footer':
|
209 | case 'header':
|
210 | case 'hgroup':
|
211 | case 'main':
|
212 | case 'menu':
|
213 | case 'nav':
|
214 | case 'ol':
|
215 | case 'p':
|
216 | case 'section':
|
217 | case 'summary':
|
218 | case 'ul':
|
219 | case 'pre':
|
220 | case 'listing':
|
221 | case 'table':
|
222 | case 'hr':
|
223 | case 'xmp':
|
224 | case 'h1':
|
225 | case 'h2':
|
226 | case 'h3':
|
227 | case 'h4':
|
228 | case 'h5':
|
229 | case 'h6':
|
230 | return ancestorInfo.pTagInButtonScope;
|
231 |
|
232 | case 'form':
|
233 | return ancestorInfo.formTag || ancestorInfo.pTagInButtonScope;
|
234 |
|
235 | case 'li':
|
236 | return ancestorInfo.listItemTagAutoclosing;
|
237 |
|
238 | case 'dd':
|
239 | case 'dt':
|
240 | return ancestorInfo.dlItemTagAutoclosing;
|
241 |
|
242 | case 'button':
|
243 | return ancestorInfo.buttonTagInScope;
|
244 |
|
245 | case 'a':
|
246 |
|
247 |
|
248 | return ancestorInfo.aTagInScope;
|
249 |
|
250 | case 'nobr':
|
251 | return ancestorInfo.nobrTagInScope;
|
252 | }
|
253 |
|
254 | return null;
|
255 | };
|
256 |
|
257 | |
258 |
|
259 |
|
260 |
|
261 | var findOwnerStack = function (instance) {
|
262 | if (!instance) {
|
263 | return [];
|
264 | }
|
265 |
|
266 | var stack = [];
|
267 | do {
|
268 | stack.push(instance);
|
269 | } while (instance = instance._currentElement._owner);
|
270 | stack.reverse();
|
271 | return stack;
|
272 | };
|
273 |
|
274 | var didWarn = {};
|
275 |
|
276 | validateDOMNesting = function (childTag, childText, childInstance, ancestorInfo) {
|
277 | ancestorInfo = ancestorInfo || emptyAncestorInfo;
|
278 | var parentInfo = ancestorInfo.current;
|
279 | var parentTag = parentInfo && parentInfo.tag;
|
280 |
|
281 | if (childText != null) {
|
282 | process.env.NODE_ENV !== 'production' ? warning(childTag == null, 'validateDOMNesting: when childText is passed, childTag should be null') : void 0;
|
283 | childTag = '#text';
|
284 | }
|
285 |
|
286 | var invalidParent = isTagValidWithParent(childTag, parentTag) ? null : parentInfo;
|
287 | var invalidAncestor = invalidParent ? null : findInvalidAncestorForTag(childTag, ancestorInfo);
|
288 | var problematic = invalidParent || invalidAncestor;
|
289 |
|
290 | if (problematic) {
|
291 | var ancestorTag = problematic.tag;
|
292 | var ancestorInstance = problematic.instance;
|
293 |
|
294 | var childOwner = childInstance && childInstance._currentElement._owner;
|
295 | var ancestorOwner = ancestorInstance && ancestorInstance._currentElement._owner;
|
296 |
|
297 | var childOwners = findOwnerStack(childOwner);
|
298 | var ancestorOwners = findOwnerStack(ancestorOwner);
|
299 |
|
300 | var minStackLen = Math.min(childOwners.length, ancestorOwners.length);
|
301 | var i;
|
302 |
|
303 | var deepestCommon = -1;
|
304 | for (i = 0; i < minStackLen; i++) {
|
305 | if (childOwners[i] === ancestorOwners[i]) {
|
306 | deepestCommon = i;
|
307 | } else {
|
308 | break;
|
309 | }
|
310 | }
|
311 |
|
312 | var UNKNOWN = '(unknown)';
|
313 | var childOwnerNames = childOwners.slice(deepestCommon + 1).map(function (inst) {
|
314 | return inst.getName() || UNKNOWN;
|
315 | });
|
316 | var ancestorOwnerNames = ancestorOwners.slice(deepestCommon + 1).map(function (inst) {
|
317 | return inst.getName() || UNKNOWN;
|
318 | });
|
319 | var ownerInfo = [].concat(
|
320 |
|
321 |
|
322 | deepestCommon !== -1 ? childOwners[deepestCommon].getName() || UNKNOWN : [], ancestorOwnerNames, ancestorTag,
|
323 |
|
324 | invalidAncestor ? ['...'] : [], childOwnerNames, childTag).join(' > ');
|
325 |
|
326 | var warnKey = !!invalidParent + '|' + childTag + '|' + ancestorTag + '|' + ownerInfo;
|
327 | if (didWarn[warnKey]) {
|
328 | return;
|
329 | }
|
330 | didWarn[warnKey] = true;
|
331 |
|
332 | var tagDisplayName = childTag;
|
333 | var whitespaceInfo = '';
|
334 | if (childTag === '#text') {
|
335 | if (/\S/.test(childText)) {
|
336 | tagDisplayName = 'Text nodes';
|
337 | } else {
|
338 | tagDisplayName = 'Whitespace text nodes';
|
339 | whitespaceInfo = " Make sure you don't have any extra whitespace between tags on " + 'each line of your source code.';
|
340 | }
|
341 | } else {
|
342 | tagDisplayName = '<' + childTag + '>';
|
343 | }
|
344 |
|
345 | if (invalidParent) {
|
346 | var info = '';
|
347 | if (ancestorTag === 'table' && childTag === 'tr') {
|
348 | info += ' Add a <tbody> to your code to match the DOM tree generated by ' + 'the browser.';
|
349 | }
|
350 | process.env.NODE_ENV !== 'production' ? warning(false, 'validateDOMNesting(...): %s cannot appear as a child of <%s>.%s ' + 'See %s.%s', tagDisplayName, ancestorTag, whitespaceInfo, ownerInfo, info) : void 0;
|
351 | } else {
|
352 | process.env.NODE_ENV !== 'production' ? warning(false, 'validateDOMNesting(...): %s cannot appear as a descendant of ' + '<%s>. See %s.', tagDisplayName, ancestorTag, ownerInfo) : void 0;
|
353 | }
|
354 | }
|
355 | };
|
356 |
|
357 | validateDOMNesting.updatedAncestorInfo = updatedAncestorInfo;
|
358 |
|
359 |
|
360 | validateDOMNesting.isTagValidInContext = function (tag, ancestorInfo) {
|
361 | ancestorInfo = ancestorInfo || emptyAncestorInfo;
|
362 | var parentInfo = ancestorInfo.current;
|
363 | var parentTag = parentInfo && parentInfo.tag;
|
364 | return isTagValidWithParent(tag, parentTag) && !findInvalidAncestorForTag(tag, ancestorInfo);
|
365 | };
|
366 | }
|
367 |
|
368 | module.exports = validateDOMNesting; |
\ | No newline at end of file |