Skip to content

Commit 509ba42

Browse files
🎨 [PANA-4378] Eliminate need for getTextContent's ignoreWhiteSpace option (#3889)
1 parent 8395461 commit 509ba42

File tree

5 files changed

+12
-16
lines changed

5 files changed

+12
-16
lines changed

‎packages/rum-core/src/domain/privacy.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,14 @@ function isFormElement(node: Node | null): boolean {
173173
*/
174174
const censorText = (text: string) => text.replace(/\S/g, TEXT_MASKING_CHAR)
175175

176-
export function getTextContent(
177-
textNode: Node,
178-
ignoreWhiteSpace: boolean,
179-
parentNodePrivacyLevel: NodePrivacyLevel
180-
): string | undefined {
176+
export function getTextContent(textNode: Node, parentNodePrivacyLevel: NodePrivacyLevel): string | undefined {
181177
// The parent node may not be a html element which has a tagName attribute.
182178
// So just let it be undefined which is ok in this use case.
183179
const parentTagName = textNode.parentElement?.tagName
184180
let textContent = textNode.textContent || ''
185181

186-
if (ignoreWhiteSpace && !textContent.trim()) {
182+
const shouldIgnoreWhiteSpace = parentTagName === 'HEAD'
183+
if (shouldIgnoreWhiteSpace && !textContent.trim()) {
187184
return
188185
}
189186

‎packages/rum/src/domain/record/serialization/serialization.types.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export type SerializationContext =
3939

4040
export interface SerializeOptions {
4141
serializedNodeIds?: Set<number>
42-
ignoreWhiteSpace?: boolean
4342
parentNodePrivacyLevel: ParentNodePrivacyLevel
4443
serializationContext: SerializationContext
4544
configuration: RumConfiguration

‎packages/rum/src/domain/record/serialization/serializeNode.spec.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -777,10 +777,12 @@ describe('serializeNodeWithId', () => {
777777
})
778778
})
779779

780-
it('does not serialize text nodes with only white space if the ignoreWhiteSpace option is specified', () => {
781-
expect(
782-
serializeNodeWithId(document.createTextNode(' '), { ...getDefaultOptions(), ignoreWhiteSpace: true })
783-
).toEqual(null)
780+
it('does not serialize text nodes with only white space if the parent is a HEAD element', () => {
781+
const head = document.getElementsByTagName('head')[0]
782+
const textNode = document.createTextNode(' ')
783+
head.appendChild(textNode)
784+
expect(serializeNodeWithId(textNode, getDefaultOptions())).toEqual(null)
785+
head.removeChild(textNode)
784786
})
785787
})
786788

‎packages/rum/src/domain/record/serialization/serializeNode.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,12 @@ function serializeElementNode(element: Element, options: SerializeOptions): Elem
167167
// We should not create a new object systematically as it could impact performances. Try to reuse
168168
// the same object as much as possible, and clone it only if we need to.
169169
let childNodesSerializationOptions
170-
if (options.parentNodePrivacyLevel === nodePrivacyLevel && options.ignoreWhiteSpace === (tagName === 'head')) {
170+
if (options.parentNodePrivacyLevel === nodePrivacyLevel) {
171171
childNodesSerializationOptions = options
172172
} else {
173173
childNodesSerializationOptions = {
174174
...options,
175175
parentNodePrivacyLevel: nodePrivacyLevel,
176-
ignoreWhiteSpace: tagName === 'head',
177176
}
178177
}
179178
childNodes = serializeChildNodes(element, childNodesSerializationOptions)
@@ -199,7 +198,7 @@ function isSVGElement(el: Element): boolean {
199198
*/
200199

201200
function serializeTextNode(textNode: Text, options: SerializeOptions): TextNode | undefined {
202-
const textContent = getTextContent(textNode, options.ignoreWhiteSpace || false, options.parentNodePrivacyLevel)
201+
const textContent = getTextContent(textNode, options.parentNodePrivacyLevel)
203202
if (textContent === undefined) {
204203
return
205204
}

‎packages/rum/src/domain/record/trackers/trackMutation.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,7 @@ function processCharacterDataMutations(
318318

319319
textMutations.push({
320320
id: getSerializedNodeId(mutation.target),
321-
// TODO: pass a valid "ignoreWhiteSpace" argument
322-
value: getTextContent(mutation.target, false, parentNodePrivacyLevel) ?? null,
321+
value: getTextContent(mutation.target, parentNodePrivacyLevel) ?? null,
323322
})
324323
}
325324

0 commit comments

Comments
 (0)