1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var _prodInvariant = require('./reactProdInvariant'),
|
12 | _assign = require('object-assign');
|
13 |
|
14 | var DOMChildrenOperations = require('./DOMChildrenOperations');
|
15 | var DOMLazyTree = require('./DOMLazyTree');
|
16 | var ReactDOMComponentTree = require('./ReactDOMComponentTree');
|
17 |
|
18 | var escapeTextContentForBrowser = require('./escapeTextContentForBrowser');
|
19 | var invariant = require('fbjs/lib/invariant');
|
20 | var validateDOMNesting = require('./validateDOMNesting');
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | var ReactDOMTextComponent = function (text) {
|
38 |
|
39 | this._currentElement = text;
|
40 | this._stringText = '' + text;
|
41 |
|
42 | this._hostNode = null;
|
43 | this._hostParent = null;
|
44 |
|
45 |
|
46 | this._domID = 0;
|
47 | this._mountIndex = 0;
|
48 | this._closingComment = null;
|
49 | this._commentNodes = null;
|
50 | };
|
51 |
|
52 | _assign(ReactDOMTextComponent.prototype, {
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | mountComponent: function (transaction, hostParent, hostContainerInfo, context) {
|
62 | if (process.env.NODE_ENV !== 'production') {
|
63 | var parentInfo;
|
64 | if (hostParent != null) {
|
65 | parentInfo = hostParent._ancestorInfo;
|
66 | } else if (hostContainerInfo != null) {
|
67 | parentInfo = hostContainerInfo._ancestorInfo;
|
68 | }
|
69 | if (parentInfo) {
|
70 |
|
71 |
|
72 | validateDOMNesting(null, this._stringText, this, parentInfo);
|
73 | }
|
74 | }
|
75 |
|
76 | var domID = hostContainerInfo._idCounter++;
|
77 | var openingValue = ' react-text: ' + domID + ' ';
|
78 | var closingValue = ' /react-text ';
|
79 | this._domID = domID;
|
80 | this._hostParent = hostParent;
|
81 | if (transaction.useCreateElement) {
|
82 | var ownerDocument = hostContainerInfo._ownerDocument;
|
83 | var openingComment = ownerDocument.createComment(openingValue);
|
84 | var closingComment = ownerDocument.createComment(closingValue);
|
85 | var lazyTree = DOMLazyTree(ownerDocument.createDocumentFragment());
|
86 | DOMLazyTree.queueChild(lazyTree, DOMLazyTree(openingComment));
|
87 | if (this._stringText) {
|
88 | DOMLazyTree.queueChild(lazyTree, DOMLazyTree(ownerDocument.createTextNode(this._stringText)));
|
89 | }
|
90 | DOMLazyTree.queueChild(lazyTree, DOMLazyTree(closingComment));
|
91 | ReactDOMComponentTree.precacheNode(this, openingComment);
|
92 | this._closingComment = closingComment;
|
93 | return lazyTree;
|
94 | } else {
|
95 | var escapedText = escapeTextContentForBrowser(this._stringText);
|
96 |
|
97 | if (transaction.renderToStaticMarkup) {
|
98 |
|
99 |
|
100 |
|
101 | return escapedText;
|
102 | }
|
103 |
|
104 | return '<!--' + openingValue + '-->' + escapedText + '<!--' + closingValue + '-->';
|
105 | }
|
106 | },
|
107 |
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | receiveComponent: function (nextText, transaction) {
|
116 | if (nextText !== this._currentElement) {
|
117 | this._currentElement = nextText;
|
118 | var nextStringText = '' + nextText;
|
119 | if (nextStringText !== this._stringText) {
|
120 |
|
121 |
|
122 |
|
123 | this._stringText = nextStringText;
|
124 | var commentNodes = this.getHostNode();
|
125 | DOMChildrenOperations.replaceDelimitedText(commentNodes[0], commentNodes[1], nextStringText);
|
126 | }
|
127 | }
|
128 | },
|
129 |
|
130 | getHostNode: function () {
|
131 | var hostNode = this._commentNodes;
|
132 | if (hostNode) {
|
133 | return hostNode;
|
134 | }
|
135 | if (!this._closingComment) {
|
136 | var openingComment = ReactDOMComponentTree.getNodeFromInstance(this);
|
137 | var node = openingComment.nextSibling;
|
138 | while (true) {
|
139 | !(node != null) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Missing closing comment for text component %s', this._domID) : _prodInvariant('67', this._domID) : void 0;
|
140 | if (node.nodeType === 8 && node.nodeValue === ' /react-text ') {
|
141 | this._closingComment = node;
|
142 | break;
|
143 | }
|
144 | node = node.nextSibling;
|
145 | }
|
146 | }
|
147 | hostNode = [this._hostNode, this._closingComment];
|
148 | this._commentNodes = hostNode;
|
149 | return hostNode;
|
150 | },
|
151 |
|
152 | unmountComponent: function () {
|
153 | this._closingComment = null;
|
154 | this._commentNodes = null;
|
155 | ReactDOMComponentTree.uncacheNode(this);
|
156 | }
|
157 | });
|
158 |
|
159 | module.exports = ReactDOMTextComponent; |
\ | No newline at end of file |