:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import type { Replayer } from 'posthog-js/rrweb'
import type { eventWithTime } from 'posthog-js/rrweb-types'
import { UNRECORDED_ATTRIBUTE, UnrecordedContentPlugin } from '../unrecorded-content'
// posthog-js/* ships ESM the test transform can't load; these mirror rrweb's enum values.
jest.mock('posthog-js/rrweb-types', () => ({
EventType: { IncrementalSnapshot: 3 },
IncrementalSource: { Mutation: 0, CanvasMutation: 9 },
}))
const NODE_ID = 7
function replayerWith(tagName: string, attributes: Record<string, string>, element: Element): Replayer {
const meta = { type: 2, tagName, attributes, childNodes: [], id: NODE_ID }
return {
getMirror: () => ({ getMeta: () => meta, getNode: (id: number) => (id === NODE_ID ? element : null) }),
} as unknown as Replayer
}
function build(tagName: string, attributes: Record<string, string>, events: eventWithTime[]): string | null {
const element = document.createElement(tagName)
// rrweb's rebuild sets ordinary attributes on the element and keeps `rr_*` ones in the mirror only.
for (const [name, value] of Object.entries(attributes)) {
if (!name.startsWith('rr_')) {
element.setAttribute(name, value)
}
}
UnrecordedContentPlugin(events).onBuild?.(element, {
id: NODE_ID,
replayer: replayerWith(tagName, attributes, element),
})
return element.getAttribute(UNRECORDED_ATTRIBUTE)
}
function attributeChange(attributes: Record<string, string | null>): eventWithTime {
return {
type: 3,
timestamp: 2000,
data: { source: 0, texts: [], removes: [], adds: [], attributes: [{ id: NODE_ID, attributes }] },
} as unknown as eventWithTime
}
const drawn = {
type: 3,
timestamp: 2000,
data: { source: 9, id: NODE_ID, type: 0, commands: [] },
} as unknown as eventWithTime
const imageSetLater = attributeChange({ rr_dataURL: 'data:image/png;base64,AA==' })
const iframePageRecorded = {
type: 3,
timestamp: 2000,
data: {
source: 0,
texts: [],
removes: [],
attributes: [],
adds: [{ parentId: NODE_ID, nextId: null, node: { type: 0, childNodes: [], id: 99 } }],
isAttachIframe: true,
},
} as unknown as eventWithTime
describe('UnrecordedContentPlugin', () => {
it.each([
['a cross-origin iframe', 'iframe', { rr_src: 'https://embed.example.com/' }, [], 'embed'],
['a recorded iframe', 'iframe', {}, [], null],
[
'a cross-origin iframe whose page was recorded',
'iframe',
{ rr_src: 'https://embed.example.com/' },
[iframePageRecorded],
null,
],
['a canvas with no pixels recorded', 'canvas', {}, [], 'canvas'],
['a canvas with recorded drawing', 'canvas', {}, [drawn], null],
['a canvas with a snapshot image', 'canvas', { rr_dataURL: 'data:image/png;base64,AA==' }, [], null],
['a canvas whose image arrives later', 'canvas', {}, [imageSetLater], null],
['a canvas whose image is removed', 'canvas', {}, [attributeChange({ rr_dataURL: null })], 'canvas'],
['a video', 'video', { src: 'https://cdn.example.com/clip.mp4' }, [], 'video'],
['an embedded document', 'embed', { src: 'https://cdn.example.com/file.pdf' }, [], 'document'],
['an object', 'object', { data: 'https://cdn.example.com/file.pdf' }, [], 'document'],
['an object holding an image', 'object', { data: 'https://cdn.example.com/logo.svg' }, [], null],
['an embed typed as an image', 'embed', { src: 'https://cdn.example.com/logo', type: 'image/png' }, [], null],
])('labels %s correctly', (_label, tagName, attributes, events, expected) => {
expect(build(tagName, attributes, events)).toBe(expected)
})
it('drops the video poster so the label shows', () => {
const element = document.createElement('video')
element.setAttribute('poster', 'https://cdn.example.com/poster.jpg')
UnrecordedContentPlugin([]).onBuild?.(element, { id: NODE_ID, replayer: replayerWith('video', {}, element) })
expect(element.hasAttribute('poster')).toBe(false)
})
it('labels an iframe whose cross-origin src arrives later', () => {
const element = document.createElement('iframe')
const change = attributeChange({ rr_src: 'https://embed.example.com/' })
UnrecordedContentPlugin([change]).handler?.(change, false, { replayer: replayerWith('iframe', {}, element) })
expect(element.getAttribute(UNRECORDED_ATTRIBUTE)).toBe('embed')
})
})