MCPcopy Index your code
hub / github.com/codeaashu/claude-code / measureTextNode

Function measureTextNode

src/ink/dom.ts:332–374  ·  view source on GitHub ↗
(
  node: DOMNode,
  width: number,
  widthMode: LayoutMeasureMode,
)

Source from the content-addressed store, hash-verified

330}
331
332const measureTextNode = function (
333 node: DOMNode,
334 width: number,
335 widthMode: LayoutMeasureMode,
336): { width: number; height: number } {
337 const rawText =
338 node.nodeName === '#text' ? node.nodeValue : squashTextNodes(node)
339
340 // Expand tabs for measurement (worst case: 8 spaces each).
341 // Actual tab expansion happens in output.ts based on screen position.
342 const text = expandTabs(rawText)
343
344 const dimensions = measureText(text, width)
345
346 // Text fits into container, no need to wrap
347 if (dimensions.width <= width) {
348 return dimensions
349 }
350
351 // This is happening when <Box> is shrinking child nodes and layout asks
352 // if we can fit this text node in a <1px space, so we just say "no"
353 if (dimensions.width >= 1 && width > 0 && width < 1) {
354 return dimensions
355 }
356
357 // For text with embedded newlines (pre-wrapped content), avoid re-wrapping
358 // at measurement width when layout is asking for intrinsic size (Undefined mode).
359 // This prevents height inflation during min/max size checks.
360 //
361 // However, when layout provides an actual constraint (Exactly or AtMost mode),
362 // we must respect it and measure at that width. Otherwise, if the actual
363 // rendering width is smaller than the natural width, the text will wrap to
364 // more lines than layout expects, causing content to be truncated.
365 if (text.includes('\n') && widthMode === LayoutMeasureMode.Undefined) {
366 const effectiveWidth = Math.max(width, dimensions.width)
367 return measureText(text, effectiveWidth)
368 }
369
370 const textWrap = node.style?.textWrap ?? 'wrap'
371 const wrappedText = wrapText(text, width, textWrap)
372
373 return measureText(wrappedText, width)
374}
375
376// ink-raw-ansi nodes hold pre-rendered ANSI strings with known dimensions.
377// No stringWidth, no wrapping, no tab expansion — the producer (e.g. ColorDiff)

Callers

nothing calls this directly

Calls 5

squashTextNodesFunction · 0.85
expandTabsFunction · 0.85
measureTextFunction · 0.85
maxMethod · 0.80
wrapTextFunction · 0.70

Tested by

no test coverage detected