Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81152 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 ReactTextComponent
10
* @typechecks static-only
11
*/
12
13
"use strict";
14
15
var DOMPropertyOperations = require('DOMPropertyOperations');
16
var ReactComponent = require('ReactComponent');
17
var ReactElement = require('ReactElement');
18
19
var assign = require('Object.assign');
20
var escapeTextForBrowser = require('escapeTextForBrowser');
21
22
/**
23
* Text nodes violate a couple assumptions that React makes about components:
24
*
25
* - When mounting text into the DOM, adjacent text nodes are merged.
26
* - Text nodes cannot be assigned a React root ID.
27
*
28
* This component is used to wrap strings in elements so that they can undergo
29
* the same reconciliation that is applied to elements.
30
*
31
* TODO: Investigate representing React components in the DOM with text nodes.
32
*
33
* @class ReactTextComponent
34
* @extends ReactComponent
35
* @internal
36
*/
37
var ReactTextComponent = function(props) {
38
// This constructor and it's argument is currently used by mocks.
39
};
40
41
assign(ReactTextComponent.prototype, ReactComponent.Mixin, {
42
43
/**
44
* Creates the markup for this text node. This node is not intended to have
45
* any features besides containing text content.
46
*
47
* @param {string} rootID DOM ID of the root node.
48
* @param {ReactReconcileTransaction|ReactServerRenderingTransaction} transaction
49
* @param {number} mountDepth number of components in the owner hierarchy
50
* @return {string} Markup for this text node.
51
* @internal
52
*/
53
mountComponent: function(rootID, transaction, mountDepth) {
54
ReactComponent.Mixin.mountComponent.call(
55
this,
56
rootID,
57
transaction,
58
mountDepth
59
);
60
61
var escapedText = escapeTextForBrowser(this.props);
62
63
if (transaction.renderToStaticMarkup) {
64
// Normally we'd wrap this in a `span` for the reasons stated above, but
65
// since this is a situation where React won't take over (static pages),
66
// we can simply return the text as it is.
67
return escapedText;
68
}
69
70
return (
71
'<span ' + DOMPropertyOperations.createMarkupForID(rootID) + '>' +
72
escapedText +
73
'</span>'
74
);
75
},
76
77
/**
78
* Updates this component by updating the text content.
79
*
80
* @param {object} nextComponent Contains the next text content.
81
* @param {ReactReconcileTransaction} transaction
82
* @internal
83
*/
84
receiveComponent: function(nextComponent, transaction) {
85
var nextProps = nextComponent.props;
86
if (nextProps !== this.props) {
87
this.props = nextProps;
88
ReactComponent.BackendIDOperations.updateTextContentByID(
89
this._rootNodeID,
90
nextProps
91
);
92
}
93
}
94
95
});
96
97
var ReactTextComponentFactory = function(text) {
98
// Bypass validation and configuration
99
return new ReactElement(ReactTextComponent, null, null, null, null, text);
100
};
101
102
ReactTextComponentFactory.type = ReactTextComponent;
103
104
module.exports = ReactTextComponentFactory;
105
106