fix: can't record shadow root's children except the last one (#956)

* fix: can't record shadow root's children except the last one

* remove unnecessary type cast
This commit is contained in:
Yun Feng
2022-09-07 13:35:55 +08:00
committed by GitHub
parent f1b5cb2738
commit e7fdf53366
4 changed files with 42 additions and 22 deletions

View File

@@ -370,15 +370,31 @@ export default class MutationBuffer {
} }
if (!node) { if (!node) {
for (let index = addList.length - 1; index >= 0; index--) { for (let index = addList.length - 1; index >= 0; index--) {
const _node = addList.get(index)!; const _node = addList.get(index);
// ensure _node is defined before attempting to find value // ensure _node is defined before attempting to find value
if (_node) { if (_node) {
const parentId = this.mirror.getId(_node.value.parentNode); const parentId = this.mirror.getId(_node.value.parentNode);
const nextId = getNextId(_node.value); const nextId = getNextId(_node.value);
if (parentId !== -1 && nextId !== -1) {
if (nextId === -1) continue;
// nextId !== -1 && parentId !== -1
else if (parentId !== -1) {
node = _node; node = _node;
break; break;
} }
// nextId !== -1 && parentId === -1 This branch can happen if the node is the child of shadow root
else {
const nodeInShadowDom = _node.value;
// Get the host of the shadow dom and treat it as parent node.
const shadowHost: Element | null = nodeInShadowDom.getRootNode
? (nodeInShadowDom.getRootNode() as ShadowRoot)?.host
: null;
const parentId = this.mirror.getId(shadowHost);
if (parentId !== -1) {
node = _node;
break;
}
}
} }
} }
} }

View File

@@ -12053,6 +12053,18 @@ exports[`record integration tests should record shadow DOM 1`] = `
\\"id\\": 43, \\"id\\": 43,
\\"isShadow\\": true \\"isShadow\\": true
} }
},
{
\\"parentId\\": 22,
\\"nextId\\": 43,
\\"node\\": {
\\"type\\": 2,
\\"tagName\\": \\"span\\",
\\"attributes\\": {},
\\"childNodes\\": [],
\\"id\\": 44,
\\"isShadow\\": true
}
} }
] ]
} }
@@ -12073,7 +12085,7 @@ exports[`record integration tests should record shadow DOM 1`] = `
\\"tagName\\": \\"p\\", \\"tagName\\": \\"p\\",
\\"attributes\\": {}, \\"attributes\\": {},
\\"childNodes\\": [], \\"childNodes\\": [],
\\"id\\": 44 \\"id\\": 45
} }
} }
] ]
@@ -12104,12 +12116,12 @@ exports[`record integration tests should record shadow DOM 1`] = `
\\"removes\\": [], \\"removes\\": [],
\\"adds\\": [ \\"adds\\": [
{ {
\\"parentId\\": 44, \\"parentId\\": 45,
\\"nextId\\": null, \\"nextId\\": null,
\\"node\\": { \\"node\\": {
\\"type\\": 3, \\"type\\": 3,
\\"textContent\\": \\"hi\\", \\"textContent\\": \\"hi\\",
\\"id\\": 45 \\"id\\": 46
} }
} }
] ]
@@ -12123,18 +12135,18 @@ exports[`record integration tests should record shadow DOM 1`] = `
\\"attributes\\": [], \\"attributes\\": [],
\\"removes\\": [ \\"removes\\": [
{ {
\\"parentId\\": 44, \\"parentId\\": 45,
\\"id\\": 45 \\"id\\": 46
} }
], ],
\\"adds\\": [ \\"adds\\": [
{ {
\\"parentId\\": 44, \\"parentId\\": 45,
\\"nextId\\": null, \\"nextId\\": null,
\\"node\\": { \\"node\\": {
\\"type\\": 3, \\"type\\": 3,
\\"textContent\\": \\"123\\", \\"textContent\\": \\"123\\",
\\"id\\": 46 \\"id\\": 47
} }
} }
] ]
@@ -12149,24 +12161,24 @@ exports[`record integration tests should record shadow DOM 1`] = `
\\"removes\\": [], \\"removes\\": [],
\\"adds\\": [ \\"adds\\": [
{ {
\\"parentId\\": 44, \\"parentId\\": 45,
\\"nextId\\": null, \\"nextId\\": null,
\\"node\\": { \\"node\\": {
\\"type\\": 2, \\"type\\": 2,
\\"tagName\\": \\"span\\", \\"tagName\\": \\"span\\",
\\"attributes\\": {}, \\"attributes\\": {},
\\"childNodes\\": [], \\"childNodes\\": [],
\\"id\\": 47, \\"id\\": 48,
\\"isShadow\\": true \\"isShadow\\": true
} }
}, },
{ {
\\"parentId\\": 47, \\"parentId\\": 48,
\\"nextId\\": null, \\"nextId\\": null,
\\"node\\": { \\"node\\": {
\\"type\\": 3, \\"type\\": 3,
\\"textContent\\": \\"nested shadow dom\\", \\"textContent\\": \\"nested shadow dom\\",
\\"id\\": 48 \\"id\\": 49
} }
} }
] ]

View File

@@ -561,6 +561,7 @@ describe('record integration tests', function (this: ISuite) {
const el = document.querySelector('.my-element') as HTMLDivElement; const el = document.querySelector('.my-element') as HTMLDivElement;
const shadowRoot = el.shadowRoot as ShadowRoot; const shadowRoot = el.shadowRoot as ShadowRoot;
shadowRoot.appendChild(document.createElement('span'));
shadowRoot.appendChild(document.createElement('p')); shadowRoot.appendChild(document.createElement('p'));
sleep(1) sleep(1)
.then(() => { .then(() => {

View File

@@ -81,7 +81,6 @@ export const startServer = (defaultPort: number = 3030) =>
resolve(s); resolve(s);
}) })
.on('error', (e) => { .on('error', (e) => {
console.log('port in use, trying next one');
s.listen().on('listening', () => { s.listen().on('listening', () => {
resolve(s); resolve(s);
}); });
@@ -152,14 +151,6 @@ function stringifySnapshots(snapshots: eventWithTime[]): string {
coordinatesReg.lastIndex = 0; // wow, a real wart in ECMAScript coordinatesReg.lastIndex = 0; // wow, a real wart in ECMAScript
} }
} }
// strip blob:urls as they are different every time
console.log(
a.attributes.src,
'src' in a.attributes &&
a.attributes.src &&
typeof a.attributes.src === 'string',
);
}); });
s.data.adds.forEach((add) => { s.data.adds.forEach((add) => {
if (add.node.type === NodeType.Element) { if (add.node.type === NodeType.Element) {