Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate only against problematic tag nesting #3516

Merged
merged 1 commit into from
Mar 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/browser/__tests__/validateDOMNesting-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails react-core
*/

'use strict';

var isTagValidInContext;

// https://html.spec.whatwg.org/multipage/syntax.html#special
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'
];

// https://html.spec.whatwg.org/multipage/syntax.html#formatting
var formattingTags = [
'a', 'b', 'big', 'code', 'em', 'font', 'i', 'nobr', 's', 'small', 'strike',
'strong', 'tt', 'u'
];

function isTagStackValid(stack) {
for (var i = 0; i < stack.length; i++) {
if (!isTagValidInContext(stack[i], stack.slice(0, i))) {
//console.log('invalid', stack[i], stack.slice(0, i));
return false;
}
}
return true;
}

describe('ReactContextValidator', function() {
beforeEach(function() {
require('mock-modules').dumpCache();

isTagValidInContext = require('validateDOMNesting').isTagValidInContext;
});

it('allows any tag with no context', function() {
// With renderToString (for example), we don't know where we're mounting the
// tag so we must err on the side of leniency.
specialTags.concat(formattingTags, ['mysterytag']).forEach(function(tag) {
expect(isTagValidInContext(tag, [])).toBe(true);
});
});

it('allows valid nestings', function() {
expect(isTagStackValid(['table', 'tbody', 'tr', 'td', 'b'])).toBe(true);
expect(isTagStackValid(['body', 'datalist', 'option'])).toBe(true);
expect(isTagStackValid(['div', 'a', 'object', 'a'])).toBe(true);
expect(isTagStackValid(['div', 'p', 'button', 'p'])).toBe(true);
expect(isTagStackValid(['p', 'svg', 'foreignObject', 'p'])).toBe(true);

// Invalid, but not changed by browser parsing so we allow them
expect(isTagStackValid(['div', 'ul', 'ul', 'li'])).toBe(true);
expect(isTagStackValid(['div', 'label', 'div'])).toBe(true);
});

it('prevents problematic nestings', function() {
expect(isTagStackValid(['a', 'a'])).toBe(false);
expect(isTagStackValid(['form', 'form'])).toBe(false);
expect(isTagStackValid(['p', 'p'])).toBe(false);
expect(isTagStackValid(['table', 'tr'])).toBe(false);
});
});
8 changes: 5 additions & 3 deletions src/browser/ui/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ function processChildContext(context, tagName) {
if (__DEV__) {
// Pass down our tag name to child components for validation purposes
context = assign({}, context);
context[validateDOMNesting.parentTagContextKey] = tagName;
var stack = context[validateDOMNesting.tagStackContextKey] || [];
context[validateDOMNesting.tagStackContextKey] = stack.concat([tagName]);
}
return context;
}
Expand Down Expand Up @@ -227,9 +228,9 @@ ReactDOMComponent.Mixin = {

assertValidProps(this, this._currentElement.props);
if (__DEV__) {
if (context[validateDOMNesting.parentTagContextKey]) {
if (context[validateDOMNesting.tagStackContextKey]) {
validateDOMNesting(
context[validateDOMNesting.parentTagContextKey],
context[validateDOMNesting.tagStackContextKey],
this._tag,
this._currentElement
);
Expand Down Expand Up @@ -318,6 +319,7 @@ ReactDOMComponent.Mixin = {
CONTENT_TYPES[typeof props.children] ? props.children : null;
var childrenToUse = contentToUse != null ? null : props.children;
if (contentToUse != null) {
// TODO: Validate that text is allowed as a child of this node
ret = escapeTextContentForBrowser(contentToUse);
} else if (childrenToUse != null) {
var mountImages = this.mountChildren(
Expand Down
4 changes: 2 additions & 2 deletions src/browser/ui/ReactDOMTextComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ assign(ReactDOMTextComponent.prototype, {
*/
mountComponent: function(rootID, transaction, context) {
if (__DEV__) {
if (context[validateDOMNesting.parentTagContextKey]) {
if (context[validateDOMNesting.tagStackContextKey]) {
validateDOMNesting(
context[validateDOMNesting.parentTagContextKey],
context[validateDOMNesting.tagStackContextKey],
'span',
null
);
Expand Down
4 changes: 2 additions & 2 deletions src/browser/ui/ReactMount.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ function mountComponentIntoNode(
var context = emptyObject;
if (__DEV__) {
context = {};
context[validateDOMNesting.parentTagContextKey] =
container.nodeName.toLowerCase();
context[validateDOMNesting.tagStackContextKey] =
[container.nodeName.toLowerCase()];
}
var markup = ReactReconciler.mountComponent(
componentInstance, rootID, transaction, context
Expand Down
Loading