Skip to content

Commit edc10e9

Browse files
committed
Measure text nodes
1 parent e798465 commit edc10e9

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

packages/react-devtools-shared/src/backend/fiber/renderer.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,7 +2251,10 @@ export function attach(
22512251
if (typeof instance !== 'object' || instance === null) {
22522252
return null;
22532253
}
2254-
if (typeof instance.getClientRects === 'function') {
2254+
if (
2255+
typeof instance.getClientRects === 'function' ||
2256+
instance.nodeType === 3
2257+
) {
22552258
// DOM
22562259
const doc = instance.ownerDocument;
22572260
if (instance === doc.documentElement) {
@@ -2273,7 +2276,18 @@ export function attach(
22732276
const win = doc && doc.defaultView;
22742277
const scrollX = win ? win.scrollX : 0;
22752278
const scrollY = win ? win.scrollY : 0;
2276-
const rects = instance.getClientRects();
2279+
let rects;
2280+
if (instance.nodeType === 3) {
2281+
// Text nodes cannot be measured directly but we can measure a Range.
2282+
if (typeof doc.createRange !== 'function') {
2283+
return null;
2284+
}
2285+
const range = doc.createRange();
2286+
range.selectNodeContents(instance);
2287+
rects = range.getClientRects();
2288+
} else {
2289+
rects = instance.getClientRects();
2290+
}
22772291
for (let i = 0; i < rects.length; i++) {
22782292
const rect = rects[i];
22792293
result.push({

packages/react-devtools-shared/src/backend/views/Highlighter/Overlay.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,13 @@ export default class Overlay {
187187
}
188188
}
189189

190-
inspect(nodes: $ReadOnlyArray<HTMLElement>, name?: ?string) {
190+
inspect(nodes: $ReadOnlyArray<HTMLElement | Text>, name?: ?string) {
191191
// We can't get the size of text nodes or comment nodes. React as of v15
192192
// heavily uses comment nodes to delimit text.
193-
const elements = nodes.filter(node => node.nodeType === Node.ELEMENT_NODE);
193+
// TODO: We actually can measure text nodes. We should.
194+
const elements: $ReadOnlyArray<HTMLElement> = (nodes.filter(
195+
node => node.nodeType === Node.ELEMENT_NODE,
196+
): any);
194197

195198
while (this.rects.length > elements.length) {
196199
const rect = this.rects.pop();

0 commit comments

Comments
 (0)