Skip to content

Commit

Permalink
🐛[RUMF-1493] Avoid infinite loop on form > input[name="host"] eleme…
Browse files Browse the repository at this point in the history
…nt (#2017)

* [REPLAY] Fix freeze

Make sure to not count form element with child input as name host as a shadow root

* [REPLAY] Refactor unit tests for clarity

* [REPLAY] Update test labels for clarity

Co-authored-by: Bastien Caudan <1331991+bcaudan@users.noreply.github.com>

---------

Co-authored-by: Bastien Caudan <1331991+bcaudan@users.noreply.github.com>
  • Loading branch information
ThibautGeriz and bcaudan authored Feb 16, 2023
1 parent f83a99c commit 4e1793e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
43 changes: 32 additions & 11 deletions packages/rum-core/src/browser/htmlDomUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,42 @@ describe('isElementNode', () => {

if (!isIE()) {
describe('isShadowRoot', () => {
const parent = document.createElement('div')
parent.attachShadow({ mode: 'open' })
const parameters: Array<[Node, boolean]> = [
[parent.shadowRoot!, true],
[parent, false],
[document.body, false],
[document.createTextNode('hello'), false],
[document.createComment('hello'), false],
const notShadowDomNodes: Node[] = [
document,
document.head,
document.body,
document.createElement('div'),
document.createTextNode('hello'),
document.createComment('hello'),
]

parameters.forEach(([element, result]) => {
it(`should return ${String(result)} for "${String(element)}"`, () => {
expect(isNodeShadowRoot(element)).toBe(result)
notShadowDomNodes.forEach((element) => {
it(`should return false for "${String(element.nodeName)}"`, () => {
expect(isNodeShadowRoot(element)).toBe(false)
})
})

it('should return true for shadow root but not its host', () => {
const parent = document.createElement('div')
const shadowRoot = parent.attachShadow({ mode: 'open' })
expect(isNodeShadowRoot(parent)).toBe(false)
expect(isNodeShadowRoot(shadowRoot)).toBe(true)
})

it('should return false for a[href] despite it has a host property', () => {
const link = document.createElement('a')
link.setAttribute('href', 'http://localhost/some/path')
expect(link.host).toBeTruthy()
expect(isNodeShadowRoot(link)).toBe(false)
})

it('should return false for a form with an input[name="host"] despite it has a host property', () => {
const form = document.createElement('form')
const input = document.createElement('input')
input.setAttribute('name', 'host')
form.appendChild(input)
expect(isNodeShadowRoot(form)).toBe(false)
})
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/rum-core/src/browser/htmlDomUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function isNodeShadowHost(node: Node): node is Element & { shadowRoot: Sh

export function isNodeShadowRoot(node: Node): node is ShadowRoot {
const shadowRoot = node as ShadowRoot
return !!shadowRoot.host && isElementNode(shadowRoot.host)
return !!shadowRoot.host && shadowRoot.nodeType === Node.DOCUMENT_FRAGMENT_NODE && isElementNode(shadowRoot.host)
}

export function getChildNodes(node: Node) {
Expand Down

0 comments on commit 4e1793e

Please sign in to comment.