MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / appendTextFromElementCommon

Function appendTextFromElementCommon

javascript/atoms/typescript/get-text.ts:161–238  ·  view source on GitHub ↗
(
  elem: Element,
  lines: string[],
  isShownFn: (e: Element) => boolean,
  childNodeFn: (
    node: Node,
    lines: string[],
    shown: boolean,
    whitespace: string | null,
    textTransform: string | null,
  ) => void,
)

Source from the content-addressed store, hash-verified

159// isShownFn and childNodeFn are injected so each path plugs in its own
160// visibility check and child dispatch. (dom.js lines 1049-1119)
161function appendTextFromElementCommon(
162 elem: Element,
163 lines: string[],
164 isShownFn: (e: Element) => boolean,
165 childNodeFn: (
166 node: Node,
167 lines: string[],
168 shown: boolean,
169 whitespace: string | null,
170 textTransform: string | null,
171 ) => void,
172): void {
173 function currLine(): string {
174 return lines[lines.length - 1] ?? ''
175 }
176
177 // Guarded through a Node-typed alias: isElement returns `node is Element`,
178 // so testing elem directly would narrow it to never for the whole of the
179 // early-return's else path. isElement rather than a nodeName comparison
180 // because it uppercases for XML documents and reads tagName through its
181 // descriptor, which a clobbering `name="tagName"` cannot shadow.
182 const node: Node = elem
183 if (isElement(node, 'BR')) {
184 lines.push('')
185 return
186 }
187
188 const isTD = isElement(elem, 'TD')
189 const display = getEffectiveStyle(elem, 'display')
190 // <td> sometimes reports a block display value; treat it as inline so it
191 // behaves like a table cell for newline purposes. (dom.js 1063)
192 const isBlock = !isTD && !INLINE_DISPLAY_BOXES.includes(display || '')
193
194 // Insert a newline before a block element when the current line is non-empty,
195 // unless the previous sibling uses display:run-in (which flows into this
196 // element). A floated element is never run into. (dom.js 1070-1081)
197 // Dropped: styleFloat (IE-only). getPropertyValue('float') is the CSS name.
198 const previousElementSibling = elem.previousElementSibling
199 const prevDisplay = previousElementSibling
200 ? getEffectiveStyle(previousElementSibling, 'display')
201 : ''
202 const thisFloat = getEffectiveStyle(elem, 'float')
203 const runIntoThis = prevDisplay === 'run-in' && thisFloat === 'none'
204 if (isBlock && !runIntoThis && !isEmptyOrWhitespace(currLine())) {
205 lines.push('')
206 }
207
208 // A non-shown element still traverses its children so that a child with an
209 // explicit visibility:visible can contribute its text even when the parent
210 // has visibility:hidden. Text nodes that are direct children of a non-shown
211 // element are silenced via shown=false. (dom.js 1084-1088)
212 const shown = isShownFn(elem)
213
214 var whitespace: string | null = null
215 var textTransform: string | null = null
216 if (shown) {
217 whitespace = getEffectiveStyle(elem, 'white-space')
218 textTransform = getEffectiveStyle(elem, 'text-transform')

Callers 2

appendTextFromElementFunction · 0.85

Calls 5

isEmptyOrWhitespaceFunction · 0.85
includesMethod · 0.80
currLineFunction · 0.70
isElementFunction · 0.50
getEffectiveStyleFunction · 0.50

Tested by

no test coverage detected