mirror of
https://github.com/facebook/react.git
synced 2026-11-18 20:15:56 +00:00
Use strings as the type for DOM elements
This makes ReactDOM a simple helper for creating ReactElements with the string tag as the type. The actual class is internal and created by instantiateReactComponent. Configurable using injection. There's not a separate class for each tag. There's just a generic ReactDOMComponent which could take any tag name. Invididual tags can be wrapped. When wrapping happens you can return the same tag again. If the wrapper returns the same string, then we fall back to the generic component. This avoids recursion in a single level wrapper.
This commit is contained in:
1 parent
35764c5ffb
commit
3aaccd2dc9
16 files changed
+328
-215
No files matched your search
@@ -101,18 +101,51 @@ function putListener(id, registrationName, listener, transaction) {
|
||||
);
|
||||
}
|
||||
|
||||
// For HTML, certain tags should omit their close tag. We keep a whitelist for
|
||||
// those special cased tags.
|
||||
|
||||
var omittedCloseTags = {
|
||||
'area': true,
|
||||
'base': true,
|
||||
'br': true,
|
||||
'col': true,
|
||||
'embed': true,
|
||||
'hr': true,
|
||||
'img': true,
|
||||
'input': true,
|
||||
'keygen': true,
|
||||
'link': true,
|
||||
'meta': true,
|
||||
'param': true,
|
||||
'source': true,
|
||||
'track': true,
|
||||
'wbr': true
|
||||
// NOTE: menuitem's close tag should be omitted, but that causes problems.
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new React class that is idempotent and capable of containing other
|
||||
* React components. It accepts event listeners and DOM properties that are
|
||||
* valid according to `DOMProperty`.
|
||||
*
|
||||
* - Event listeners: `onClick`, `onMouseDown`, etc.
|
||||
* - DOM properties: `className`, `name`, `title`, etc.
|
||||
*
|
||||
* The `style` property functions differently from the DOM API. It accepts an
|
||||
* object mapping of style properties to values.
|
||||
*
|
||||
* @constructor ReactDOMComponent
|
||||
* @extends ReactComponent
|
||||
* @extends ReactMultiChild
|
||||
*/
|
||||
function ReactDOMComponent(tag, omitClose) {
|
||||
this._tagOpen = '<' + tag;
|
||||
this._tagClose = omitClose ? '' : '</' + tag + '>';
|
||||
function ReactDOMComponent(tag) {
|
||||
// TODO: DANGEROUS this tag should be sanitized.
|
||||
this._tag = tag;
|
||||
this.tagName = tag.toUpperCase();
|
||||
}
|
||||
|
||||
ReactDOMComponent.displayName = 'ReactDOMComponent';
|
||||
|
||||
ReactDOMComponent.Mixin = {
|
||||
|
||||
/**
|
||||
@@ -136,10 +169,11 @@ ReactDOMComponent.Mixin = {
|
||||
mountDepth
|
||||
);
|
||||
assertValidProps(this.props);
|
||||
var closeTag = omittedCloseTags[this._tag] ? '' : '</' + this._tag + '>';
|
||||
return (
|
||||
this._createOpenTagMarkupAndPutListeners(transaction) +
|
||||
this._createContentMarkup(transaction) +
|
||||
this._tagClose
|
||||
closeTag
|
||||
);
|
||||
}
|
||||
),
|
||||
@@ -158,7 +192,7 @@ ReactDOMComponent.Mixin = {
|
||||
*/
|
||||
_createOpenTagMarkupAndPutListeners: function(transaction) {
|
||||
var props = this.props;
|
||||
var ret = this._tagOpen;
|
||||
var ret = '<' + this._tag;
|
||||
|
||||
for (var propKey in props) {
|
||||
if (!props.hasOwnProperty(propKey)) {
|
||||
|
||||
@@ -31,7 +31,7 @@ var ReactBrowserComponentMixin = require('ReactBrowserComponentMixin');
|
||||
var ReactComponentBrowserEnvironment =
|
||||
require('ReactComponentBrowserEnvironment');
|
||||
var ReactDefaultBatchingStrategy = require('ReactDefaultBatchingStrategy');
|
||||
var ReactDOM = require('ReactDOM');
|
||||
var ReactDOMComponent = require('ReactDOMComponent');
|
||||
var ReactDOMButton = require('ReactDOMButton');
|
||||
var ReactDOMForm = require('ReactDOMForm');
|
||||
var ReactDOMImg = require('ReactDOMImg');
|
||||
@@ -76,18 +76,22 @@ function inject() {
|
||||
BeforeInputEventPlugin: BeforeInputEventPlugin
|
||||
});
|
||||
|
||||
ReactInjection.DOM.injectComponentClasses({
|
||||
button: ReactDOMButton,
|
||||
form: ReactDOMForm,
|
||||
img: ReactDOMImg,
|
||||
input: ReactDOMInput,
|
||||
option: ReactDOMOption,
|
||||
select: ReactDOMSelect,
|
||||
textarea: ReactDOMTextarea,
|
||||
ReactInjection.NativeComponent.injectGenericComponentClass(
|
||||
ReactDOMComponent
|
||||
);
|
||||
|
||||
html: createFullPageComponent(ReactDOM.html),
|
||||
head: createFullPageComponent(ReactDOM.head),
|
||||
body: createFullPageComponent(ReactDOM.body)
|
||||
ReactInjection.NativeComponent.injectComponentClasses({
|
||||
'button': ReactDOMButton,
|
||||
'form': ReactDOMForm,
|
||||
'img': ReactDOMImg,
|
||||
'input': ReactDOMInput,
|
||||
'option': ReactDOMOption,
|
||||
'select': ReactDOMSelect,
|
||||
'textarea': ReactDOMTextarea,
|
||||
|
||||
'html': createFullPageComponent('html'),
|
||||
'head': createFullPageComponent('head'),
|
||||
'body': createFullPageComponent('body')
|
||||
});
|
||||
|
||||
// This needs to happen after createFullPageComponent() otherwise the mixin
|
||||
@@ -97,7 +101,7 @@ function inject() {
|
||||
ReactInjection.DOMProperty.injectDOMPropertyConfig(HTMLDOMPropertyConfig);
|
||||
ReactInjection.DOMProperty.injectDOMPropertyConfig(SVGDOMPropertyConfig);
|
||||
|
||||
ReactInjection.EmptyComponent.injectEmptyComponent(ReactDOM.noscript);
|
||||
ReactInjection.EmptyComponent.injectEmptyComponent('noscript');
|
||||
|
||||
ReactInjection.Updates.injectReconcileTransaction(
|
||||
ReactComponentBrowserEnvironment.ReactReconcileTransaction
|
||||
|
||||
@@ -22,9 +22,9 @@ var DOMProperty = require('DOMProperty');
|
||||
var EventPluginHub = require('EventPluginHub');
|
||||
var ReactComponent = require('ReactComponent');
|
||||
var ReactCompositeComponent = require('ReactCompositeComponent');
|
||||
var ReactDOM = require('ReactDOM');
|
||||
var ReactEmptyComponent = require('ReactEmptyComponent');
|
||||
var ReactBrowserEventEmitter = require('ReactBrowserEventEmitter');
|
||||
var ReactNativeComponent = require('ReactNativeComponent');
|
||||
var ReactPerf = require('ReactPerf');
|
||||
var ReactRootIndex = require('ReactRootIndex');
|
||||
var ReactUpdates = require('ReactUpdates');
|
||||
@@ -35,8 +35,8 @@ var ReactInjection = {
|
||||
DOMProperty: DOMProperty.injection,
|
||||
EmptyComponent: ReactEmptyComponent.injection,
|
||||
EventPluginHub: EventPluginHub.injection,
|
||||
DOM: ReactDOM.injection,
|
||||
EventEmitter: ReactBrowserEventEmitter.injection,
|
||||
NativeComponent: ReactNativeComponent.injection,
|
||||
Perf: ReactPerf.injection,
|
||||
RootIndex: ReactRootIndex.injection,
|
||||
Updates: ReactUpdates.injection
|
||||
|
||||
@@ -307,7 +307,7 @@ var ReactMount = {
|
||||
'componentDidUpdate.'
|
||||
);
|
||||
|
||||
var componentInstance = instantiateReactComponent(nextComponent);
|
||||
var componentInstance = instantiateReactComponent(nextComponent, null);
|
||||
var reactRootID = ReactMount._registerComponent(
|
||||
componentInstance,
|
||||
container
|
||||
|
||||
@@ -33,16 +33,14 @@ var invariant = require('invariant');
|
||||
* take advantage of React's reconciliation for styling and <title>
|
||||
* management. So we just document it and throw in dangerous cases.
|
||||
*
|
||||
* @param {function} componentClass convenience constructor to wrap
|
||||
* @param {string} tag The tag to wrap
|
||||
* @return {function} convenience constructor of new component
|
||||
*/
|
||||
function createFullPageComponent(componentClass) {
|
||||
var elementFactory = ReactDescriptor.createFactory(componentClass.type);
|
||||
function createFullPageComponent(tag) {
|
||||
var elementFactory = ReactDescriptor.createFactory(tag);
|
||||
|
||||
var FullPageComponent = ReactCompositeComponent.createClass({
|
||||
displayName: 'ReactFullPageComponent' + (
|
||||
componentClass.type.displayName || ''
|
||||
),
|
||||
displayName: 'ReactFullPageComponent' + tag,
|
||||
|
||||
componentWillUnmount: function() {
|
||||
invariant(
|
||||
|
||||
Reference in new issue
Block a user