import { isIE } from '../../../../core/test/specHelper' import { InputPrivacyMode, PRIVACY_ATTR_NAME, PRIVACY_ATTR_VALUE_HIDDEN, PRIVACY_ATTR_VALUE_INPUT_IGNORED, PRIVACY_ATTR_VALUE_INPUT_MASKED, } from '../../constants' import { hasSerializedNode } from './serializationUtils' import { serializeDocument, serializeNodeWithId, SerializeOptions } from './serialize' import { ElementNode, NodeType } from './types' const DEFAULT_OPTIONS: SerializeOptions = { document, ancestorInputPrivacyMode: InputPrivacyMode.NONE, } describe('serializeNodeWithId', () => { let sandbox: HTMLElement beforeEach(() => { if (isIE()) { pending('IE not supported') } sandbox = document.createElement('div') sandbox.id = 'sandbox' document.body.appendChild(sandbox) }) afterEach(() => { sandbox.remove() }) describe('document serialization', () => { it('serializes a document', () => { const document = new DOMParser().parseFromString(`foo`, 'text/html') expect(serializeDocument(document)).toEqual({ type: NodeType.Document, childNodes: [ jasmine.objectContaining({ type: NodeType.DocumentType, name: 'html', publicId: '', systemId: '' }), jasmine.objectContaining({ type: NodeType.Element, tagName: 'html' }), ], id: (jasmine.any(Number) as unknown) as number, }) }) }) describe('elements serialization', () => { it('serializes a div', () => { expect(serializeNodeWithId(document.createElement('div'), DEFAULT_OPTIONS)).toEqual({ type: NodeType.Element, tagName: 'div', attributes: {}, isSVG: undefined, childNodes: [], id: (jasmine.any(Number) as unknown) as number, }) }) it('serializes hidden elements', () => { const element = document.createElement('div') element.setAttribute(PRIVACY_ATTR_NAME, PRIVACY_ATTR_VALUE_HIDDEN) expect(serializeNodeWithId(element, DEFAULT_OPTIONS)).toEqual({ type: NodeType.Element, tagName: 'div', attributes: { id: '', class: '', rr_width: '0px', rr_height: '0px', [PRIVACY_ATTR_NAME]: PRIVACY_ATTR_VALUE_HIDDEN, }, isSVG: undefined, childNodes: [], id: (jasmine.any(Number) as unknown) as number, }) }) it('does not serialize hidden element children', () => { const element = document.createElement('div') element.setAttribute(PRIVACY_ATTR_NAME, PRIVACY_ATTR_VALUE_HIDDEN) element.appendChild(document.createElement('hr')) expect((serializeNodeWithId(element, DEFAULT_OPTIONS)! as ElementNode).childNodes).toEqual([]) }) it('serializes attributes', () => { const element = document.createElement('div') element.setAttribute('foo', 'bar') element.setAttribute('data-foo', 'data-bar') element.className = 'zog' element.style.width = '10px' expect((serializeNodeWithId(element, DEFAULT_OPTIONS)! as ElementNode).attributes).toEqual({ foo: 'bar', 'data-foo': 'data-bar', class: 'zog', style: 'width: 10px;', }) }) it('serializes scroll position', () => { const element = document.createElement('div') Object.assign(element.style, { width: '100px', height: '100px', overflow: 'scroll' }) const inner = document.createElement('div') Object.assign(inner.style, { width: '200px', height: '200px' }) element.appendChild(inner) sandbox.appendChild(element) element.scrollBy(10, 20) expect((serializeNodeWithId(element, DEFAULT_OPTIONS)! as ElementNode).attributes).toEqual( jasmine.objectContaining({ rr_scrollTop: 20, rr_scrollLeft: 10, }) ) }) it('ignores white space in
', () => { const head = document.createElement('head') head.innerHTML = `