mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[DevTools] Measure text nodes (#34851)
We can't measure Text nodes directly but we can measure a Range around them. This is useful since it's common, at least in examples, to use text nodes as children of a Suspense boundary. Especially fallbacks.
This commit is contained in:
committed by
GitHub
parent
6cfc9c1ff3
commit
751edd6e2c
@@ -2251,7 +2251,10 @@ export function attach(
|
||||
if (typeof instance !== 'object' || instance === null) {
|
||||
return null;
|
||||
}
|
||||
if (typeof instance.getClientRects === 'function') {
|
||||
if (
|
||||
typeof instance.getClientRects === 'function' ||
|
||||
instance.nodeType === 3
|
||||
) {
|
||||
// DOM
|
||||
const doc = instance.ownerDocument;
|
||||
if (instance === doc.documentElement) {
|
||||
@@ -2273,7 +2276,21 @@ export function attach(
|
||||
const win = doc && doc.defaultView;
|
||||
const scrollX = win ? win.scrollX : 0;
|
||||
const scrollY = win ? win.scrollY : 0;
|
||||
const rects = instance.getClientRects();
|
||||
let rects;
|
||||
if (instance.nodeType === 3) {
|
||||
// Text nodes cannot be measured directly but we can measure a Range.
|
||||
if (typeof doc.createRange !== 'function') {
|
||||
return null;
|
||||
}
|
||||
const range = doc.createRange();
|
||||
if (typeof range.getClientRects !== 'function') {
|
||||
return null;
|
||||
}
|
||||
range.selectNodeContents(instance);
|
||||
rects = range.getClientRects();
|
||||
} else {
|
||||
rects = instance.getClientRects();
|
||||
}
|
||||
for (let i = 0; i < rects.length; i++) {
|
||||
const rect = rects[i];
|
||||
result.push({
|
||||
|
||||
@@ -187,10 +187,13 @@ export default class Overlay {
|
||||
}
|
||||
}
|
||||
|
||||
inspect(nodes: $ReadOnlyArray<HTMLElement>, name?: ?string) {
|
||||
inspect(nodes: $ReadOnlyArray<HTMLElement | Text>, name?: ?string) {
|
||||
// We can't get the size of text nodes or comment nodes. React as of v15
|
||||
// heavily uses comment nodes to delimit text.
|
||||
const elements = nodes.filter(node => node.nodeType === Node.ELEMENT_NODE);
|
||||
// TODO: We actually can measure text nodes. We should.
|
||||
const elements: $ReadOnlyArray<HTMLElement> = (nodes.filter(
|
||||
node => node.nodeType === Node.ELEMENT_NODE,
|
||||
): any);
|
||||
|
||||
while (this.rects.length > elements.length) {
|
||||
const rect = this.rects.pop();
|
||||
|
||||
Reference in New Issue
Block a user