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

Function appendTextFromTextNode

javascript/atoms/typescript/get-text.ts:100–156  ·  view source on GitHub ↗
(
  textNode: Text,
  lines: string[],
  whitespace: string | null,
  textTransform: string | null,
)

Source from the content-addressed store, hash-verified

98// applying white-space collapsing rules and text-transform.
99// (dom.js line 1203: bot.dom.appendVisibleTextLinesFromTextNode_)
100function appendTextFromTextNode(
101 textNode: Text,
102 lines: string[],
103 whitespace: string | null,
104 textTransform: string | null,
105): void {
106 // Strip zero-width non-printing characters before regularising spaces, so
107 // the zero-width space, which is technically also a space, is not
108 // promoted to a regular space. (dom.js 1213)
109 // Stripped: U+200B (zero-width space), U+200E (LTR mark), U+200F (RTL mark).
110 var text = (textNode.nodeValue || '').replace(/[\u200b\u200e\u200f]/g, '')
111
112 // goog.string.canonicalizeNewlines: normalise \r\n and bare \r to \n.
113 // (dom.js 1218)
114 text = text.replace(/(\r\n|\r|\n)/g, '\n')
115
116 // white-space: normal and nowrap collapse newlines into spaces.
117 if (whitespace === 'normal' || whitespace === 'nowrap') {
118 text = text.replace(/\n/g, ' ')
119 }
120
121 // white-space: pre and pre-wrap protect internal spacing by converting
122 // breaking spaces to NBSP so they survive the later collapsing pass.
123 // All other white-space values collapse runs of breaking space to one space.
124 // PRE_SPACE_RE and BREAKING_SPACE_RE cover space, \f, \t, \v, U+2028, U+2029
125 // (dom.js 1226-1230; constructed as RegExp to avoid literal line terminators)
126 if (whitespace === 'pre' || whitespace === 'pre-wrap') {
127 text = text.replace(PRE_SPACE_RE, '\xa0')
128 } else {
129 text = text.replace(BREAKING_SPACE_RE, ' ')
130 }
131
132 // text-transform. The capitalize path applies both patterns in sequence:
133 // pattern 0 handles word-initial letters, pattern 1 handles letters
134 // immediately after a '_' or '*' that opens a run. (dom.js 1232-1247)
135 if (textTransform === 'capitalize') {
136 text = text.replace(CAPITALIZE_PATTERNS[0], function () {
137 return arguments[1] + (arguments[2] as string).toUpperCase()
138 })
139 text = text.replace(CAPITALIZE_PATTERNS[1], function () {
140 return arguments[1] + arguments[2] + (arguments[3] as string).toUpperCase()
141 })
142 } else if (textTransform === 'uppercase') {
143 text = text.toUpperCase()
144 } else if (textTransform === 'lowercase') {
145 text = text.toLowerCase()
146 }
147
148 // Merge into the last accumulated line. If the current line already ends
149 // with a space and the new text starts with one, drop the duplicate.
150 // (dom.js 1249-1254)
151 var currLine = lines.pop() || ''
152 if (currLine.endsWith(' ') && text.startsWith(' ')) {
153 text = text.substring(1)
154 }
155 lines.push(currLine + text)
156}
157

Callers 2

appendTextFromElementFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected