99
1010'use strict' ;
1111
12- var DOMProperty = require ( 'DOMProperty' ) ;
13- var ReactDOMComponentFlags = require ( 'ReactDOMComponentFlags' ) ;
1412var { HostComponent, HostText} = require ( 'ReactTypeOfWork' ) ;
15- var { ELEMENT_NODE , COMMENT_NODE } = require ( 'HTMLNodeType' ) ;
1613
1714var invariant = require ( 'fbjs/lib/invariant' ) ;
1815
19- var ATTR_NAME = DOMProperty . ID_ATTRIBUTE_NAME ;
20- var Flags = ReactDOMComponentFlags ;
21-
2216var randomKey = Math . random ( ) . toString ( 36 ) . slice ( 2 ) ;
23-
2417var internalInstanceKey = '__reactInternalInstance$' + randomKey ;
25-
2618var internalEventHandlersKey = '__reactEventHandlers$' + randomKey ;
2719
28- /**
29- * Check if a given node should be cached.
30- */
31- function shouldPrecacheNode ( node , nodeID ) {
32- return (
33- ( node . nodeType === ELEMENT_NODE &&
34- node . getAttribute ( ATTR_NAME ) === '' + nodeID ) ||
35- ( node . nodeType === COMMENT_NODE &&
36- node . nodeValue === ' react-text: ' + nodeID + ' ' ) ||
37- ( node . nodeType === COMMENT_NODE &&
38- node . nodeValue === ' react-empty: ' + nodeID + ' ' )
39- ) ;
40- }
41-
42- /**
43- * Drill down (through composites and empty components) until we get a host or
44- * host text component.
45- *
46- * This is pretty polymorphic but unavoidable with the current structure we have
47- * for `_renderedChildren`.
48- */
49- function getRenderedHostOrTextFromComponent ( component ) {
50- var rendered ;
51- while ( ( rendered = component . _renderedComponent ) ) {
52- component = rendered ;
53- }
54- return component ;
55- }
56-
57- /**
58- * Populate `_hostNode` on the rendered host/text component with the given
59- * DOM node. The passed `inst` can be a composite.
60- */
61- function precacheNode ( inst , node ) {
62- var hostInst = getRenderedHostOrTextFromComponent ( inst ) ;
63- hostInst . _hostNode = node ;
64- node [ internalInstanceKey ] = hostInst ;
65- }
66-
6720function precacheFiberNode ( hostInst , node ) {
6821 node [ internalInstanceKey ] = hostInst ;
6922}
7023
71- function uncacheNode ( inst ) {
72- var node = inst . _hostNode ;
73- if ( node ) {
74- delete node [ internalInstanceKey ] ;
75- inst . _hostNode = null ;
76- }
77- }
78-
79- /**
80- * Populate `_hostNode` on each child of `inst`, assuming that the children
81- * match up with the DOM (element) children of `node`.
82- *
83- * We cache entire levels at once to avoid an n^2 problem where we access the
84- * children of a node sequentially and have to walk from the start to our target
85- * node every time.
86- *
87- * Since we update `_renderedChildren` and the actual DOM at (slightly)
88- * different times, we could race here and see a newer `_renderedChildren` than
89- * the DOM nodes we see. To avoid this, ReactMultiChild calls
90- * `prepareToManageChildren` before we change `_renderedChildren`, at which
91- * time the container's child nodes are always cached (until it unmounts).
92- */
93- function precacheChildNodes ( inst , node ) {
94- if ( inst . _flags & Flags . hasCachedChildNodes ) {
95- return ;
96- }
97- var children = inst . _renderedChildren ;
98- var childNode = node . firstChild ;
99- outer: for ( var name in children ) {
100- if ( ! children . hasOwnProperty ( name ) ) {
101- continue ;
102- }
103- var childInst = children [ name ] ;
104- var childID = getRenderedHostOrTextFromComponent ( childInst ) . _domID ;
105- if ( childID === 0 ) {
106- // We're currently unmounting this child in ReactMultiChild; skip it.
107- continue ;
108- }
109- // We assume the child nodes are in the same order as the child instances.
110- for ( ; childNode !== null ; childNode = childNode . nextSibling ) {
111- if ( shouldPrecacheNode ( childNode , childID ) ) {
112- precacheNode ( childInst , childNode ) ;
113- continue outer;
114- }
115- }
116- // We reached the end of the DOM children without finding an ID match.
117- invariant ( false , 'Unable to find element with ID %s.' , childID ) ;
118- }
119- inst . _flags |= Flags . hasCachedChildNodes ;
120- }
121-
12224/**
12325 * Given a DOM node, return the closest ReactDOMComponent or
12426 * ReactDOMTextComponent instance ancestor.
@@ -149,9 +51,6 @@ function getClosestInstanceFromNode(node) {
14951 }
15052 for ( ; node && ( inst = node [ internalInstanceKey ] ) ; node = parents . pop ( ) ) {
15153 closest = inst ;
152- if ( parents . length ) {
153- precacheChildNodes ( inst , node ) ;
154- }
15554 }
15655
15756 return closest ;
@@ -166,18 +65,11 @@ function getInstanceFromNode(node) {
16665 if ( inst ) {
16766 if ( inst . tag === HostComponent || inst . tag === HostText ) {
16867 return inst ;
169- } else if ( inst . _hostNode === node ) {
170- return inst ;
17168 } else {
17269 return null ;
17370 }
17471 }
175- inst = getClosestInstanceFromNode ( node ) ;
176- if ( inst != null && inst . _hostNode === node ) {
177- return inst ;
178- } else {
179- return null ;
180- }
72+ return null ;
18173}
18274
18375/**
@@ -193,33 +85,7 @@ function getNodeFromInstance(inst) {
19385
19486 // Without this first invariant, passing a non-DOM-component triggers the next
19587 // invariant for a missing parent, which is super confusing.
196- invariant (
197- inst . _hostNode !== undefined ,
198- 'getNodeFromInstance: Invalid argument.' ,
199- ) ;
200-
201- if ( inst . _hostNode ) {
202- return inst . _hostNode ;
203- }
204-
205- // Walk up the tree until we find an ancestor whose DOM node we have cached.
206- var parents = [ ] ;
207- while ( ! inst . _hostNode ) {
208- parents . push ( inst ) ;
209- invariant (
210- inst . _hostParent ,
211- 'React DOM tree root should always have a node reference.' ,
212- ) ;
213- inst = inst . _hostParent ;
214- }
215-
216- // Now parents contains each ancestor that does *not* have a cached native
217- // node, and `inst` is the deepest ancestor that does.
218- for ( ; parents . length ; inst = parents . pop ( ) ) {
219- precacheChildNodes ( inst , inst . _hostNode ) ;
220- }
221-
222- return inst . _hostNode ;
88+ invariant ( false , 'getNodeFromInstance: Invalid argument.' ) ;
22389}
22490
22591function getFiberCurrentPropsFromNode ( node ) {
@@ -234,9 +100,6 @@ var ReactDOMComponentTree = {
234100 getClosestInstanceFromNode : getClosestInstanceFromNode ,
235101 getInstanceFromNode : getInstanceFromNode ,
236102 getNodeFromInstance : getNodeFromInstance ,
237- precacheChildNodes : precacheChildNodes ,
238- precacheNode : precacheNode ,
239- uncacheNode : uncacheNode ,
240103 precacheFiberNode : precacheFiberNode ,
241104 getFiberCurrentPropsFromNode,
242105 updateFiberProps,
0 commit comments