snapshot and rebuild shadow DOM
https://github.com/rrweb-io/rrweb/issues/38
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,3 +5,4 @@ build
|
||||
dist
|
||||
es
|
||||
lib
|
||||
temp
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
idNodeMap,
|
||||
INode,
|
||||
} from './types';
|
||||
import { isElement } from './utils';
|
||||
|
||||
const tagMap: tagMap = {
|
||||
script: 'noscript',
|
||||
@@ -177,6 +178,25 @@ function buildNode(
|
||||
}
|
||||
}
|
||||
}
|
||||
if (n.isShadowHost) {
|
||||
/**
|
||||
* Since node is newly rebuilt, it should be a normal element
|
||||
* without shadowRoot.
|
||||
* But if there are some weird situations that has defined
|
||||
* custom element in the scope before we rebuild node, it may
|
||||
* register the shadowRoot earlier.
|
||||
* The logic in the 'else' block is just a try-my-best solution
|
||||
* for the corner case, please let we know if it is wrong and
|
||||
* we can remove it.
|
||||
*/
|
||||
if (!node.shadowRoot) {
|
||||
node.attachShadow({ mode: 'open' });
|
||||
} else {
|
||||
while (node.shadowRoot.firstChild) {
|
||||
node.shadowRoot.removeChild(node.shadowRoot.firstChild);
|
||||
}
|
||||
}
|
||||
}
|
||||
return node;
|
||||
case NodeType.Text:
|
||||
return doc.createTextNode(
|
||||
@@ -240,7 +260,11 @@ export function buildNodeWithSN(
|
||||
continue;
|
||||
}
|
||||
|
||||
node.appendChild(childNode);
|
||||
if (childN.isShadow && isElement(node) && node.shadowRoot) {
|
||||
node.shadowRoot.appendChild(childNode);
|
||||
} else {
|
||||
node.appendChild(childNode);
|
||||
}
|
||||
if (afterAppend) {
|
||||
afterAppend(childNode);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
MaskInputOptions,
|
||||
SlimDOMOptions,
|
||||
} from './types';
|
||||
import { isElement } from './utils';
|
||||
|
||||
let _id = 1;
|
||||
const tagNameRegex = RegExp('[^a-z0-9-_]');
|
||||
@@ -622,26 +623,38 @@ export function serializeNodeWithId(
|
||||
) {
|
||||
preserveWhiteSpace = false;
|
||||
}
|
||||
const bypassOptions = {
|
||||
doc,
|
||||
map,
|
||||
blockClass,
|
||||
blockSelector,
|
||||
skipChild,
|
||||
inlineStylesheet,
|
||||
maskInputOptions,
|
||||
slimDOMOptions,
|
||||
recordCanvas,
|
||||
preserveWhiteSpace,
|
||||
onSerialize,
|
||||
onIframeLoad,
|
||||
iframeLoadTimeout,
|
||||
};
|
||||
for (const childN of Array.from(n.childNodes)) {
|
||||
const serializedChildNode = serializeNodeWithId(childN, {
|
||||
doc,
|
||||
map,
|
||||
blockClass,
|
||||
blockSelector,
|
||||
skipChild,
|
||||
inlineStylesheet,
|
||||
maskInputOptions,
|
||||
slimDOMOptions,
|
||||
recordCanvas,
|
||||
preserveWhiteSpace,
|
||||
onSerialize,
|
||||
onIframeLoad,
|
||||
iframeLoadTimeout,
|
||||
});
|
||||
const serializedChildNode = serializeNodeWithId(childN, bypassOptions);
|
||||
if (serializedChildNode) {
|
||||
serializedNode.childNodes.push(serializedChildNode);
|
||||
}
|
||||
}
|
||||
|
||||
if (isElement(n) && n.shadowRoot) {
|
||||
serializedNode.isShadowHost = true;
|
||||
for (const childN of Array.from(n.shadowRoot.childNodes)) {
|
||||
const serializedChildNode = serializeNodeWithId(childN, bypassOptions);
|
||||
if (serializedChildNode) {
|
||||
serializedChildNode.isShadow = true;
|
||||
serializedNode.childNodes.push(serializedChildNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
|
||||
@@ -56,6 +56,8 @@ export type serializedNode = (
|
||||
| commentNode
|
||||
) & {
|
||||
rootId?: number;
|
||||
isShadowHost?: boolean;
|
||||
isShadow?: boolean;
|
||||
};
|
||||
|
||||
export type serializedNodeWithId = serializedNode & { id: number };
|
||||
|
||||
5
src/utils.ts
Normal file
5
src/utils.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { INode } from './types';
|
||||
|
||||
export function isElement(n: Node | INode): n is Element {
|
||||
return n.nodeType === n.ELEMENT_NODE;
|
||||
}
|
||||
@@ -211,6 +211,25 @@ exports[`[html file]: picture.html 1`] = `
|
||||
</body></html>"
|
||||
`;
|
||||
|
||||
exports[`[html file]: shadow-dom.html 1`] = `
|
||||
"<!DOCTYPE html><html xmlns=\\"http://www.w3.org/1999/xhtml\\" lang=\\"en\\"><head>
|
||||
<meta charset=\\"UTF-8\\" />
|
||||
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\" />
|
||||
<title>shadow DOM</title>
|
||||
</head>
|
||||
<body>
|
||||
<fancy-tabs background=\\"\\" role=\\"tablist\\" selected=\\"1\\">
|
||||
<button slot=\\"title\\" role=\\"tab\\" tabindex=\\"-1\\" aria-selected=\\"false\\">Tab 1</button>
|
||||
<button slot=\\"title\\" selected=\\"\\" role=\\"tab\\" tabindex=\\"0\\" aria-selected=\\"true\\">Tab 2</button>
|
||||
<button slot=\\"title\\" role=\\"tab\\" tabindex=\\"-1\\" aria-selected=\\"false\\">Tab 3</button>
|
||||
<section role=\\"tabpanel\\" tabindex=\\"0\\" aria-hidden=\\"true\\">content panel 1</section>
|
||||
<section role=\\"tabpanel\\" tabindex=\\"0\\" aria-hidden=\\"false\\">content panel 2</section>
|
||||
<section role=\\"tabpanel\\" tabindex=\\"0\\" aria-hidden=\\"true\\">content panel 3</section>
|
||||
</fancy-tabs>
|
||||
<noscript>SCRIPT_PLACEHOLDER</noscript>
|
||||
</body></html>"
|
||||
`;
|
||||
|
||||
exports[`[html file]: video.html 1`] = `
|
||||
"<!DOCTYPE html><html xmlns=\\"http://www.w3.org/1999/xhtml\\" lang=\\"en\\"><head>
|
||||
<meta charset=\\"UTF-8\\" />
|
||||
@@ -409,3 +428,384 @@ exports[`iframe integration tests 1`] = `
|
||||
\\"id\\": 1
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`shadown DOM integration tests 1`] = `
|
||||
"{
|
||||
\\"type\\": 0,
|
||||
\\"childNodes\\": [
|
||||
{
|
||||
\\"type\\": 1,
|
||||
\\"name\\": \\"html\\",
|
||||
\\"publicId\\": \\"\\",
|
||||
\\"systemId\\": \\"\\",
|
||||
\\"id\\": 2
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"html\\",
|
||||
\\"attributes\\": {
|
||||
\\"lang\\": \\"en\\"
|
||||
},
|
||||
\\"childNodes\\": [
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"head\\",
|
||||
\\"attributes\\": {},
|
||||
\\"childNodes\\": [
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 5
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"meta\\",
|
||||
\\"attributes\\": {
|
||||
\\"charset\\": \\"UTF-8\\"
|
||||
},
|
||||
\\"childNodes\\": [],
|
||||
\\"id\\": 6
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 7
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"meta\\",
|
||||
\\"attributes\\": {
|
||||
\\"name\\": \\"viewport\\",
|
||||
\\"content\\": \\"width=device-width, initial-scale=1.0\\"
|
||||
},
|
||||
\\"childNodes\\": [],
|
||||
\\"id\\": 8
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 9
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"title\\",
|
||||
\\"attributes\\": {},
|
||||
\\"childNodes\\": [
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"shadow DOM\\",
|
||||
\\"id\\": 11
|
||||
}
|
||||
],
|
||||
\\"id\\": 10
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 12
|
||||
}
|
||||
],
|
||||
\\"id\\": 4
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 13
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"body\\",
|
||||
\\"attributes\\": {},
|
||||
\\"childNodes\\": [
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 15
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"fancy-tabs\\",
|
||||
\\"attributes\\": {
|
||||
\\"background\\": \\"\\",
|
||||
\\"role\\": \\"tablist\\",
|
||||
\\"selected\\": \\"1\\"
|
||||
},
|
||||
\\"childNodes\\": [
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 17
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"button\\",
|
||||
\\"attributes\\": {
|
||||
\\"slot\\": \\"title\\",
|
||||
\\"role\\": \\"tab\\",
|
||||
\\"tabindex\\": \\"-1\\",
|
||||
\\"aria-selected\\": \\"false\\"
|
||||
},
|
||||
\\"childNodes\\": [
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"Tab 1\\",
|
||||
\\"id\\": 19
|
||||
}
|
||||
],
|
||||
\\"id\\": 18
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 20
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"button\\",
|
||||
\\"attributes\\": {
|
||||
\\"slot\\": \\"title\\",
|
||||
\\"selected\\": \\"\\",
|
||||
\\"role\\": \\"tab\\",
|
||||
\\"tabindex\\": \\"0\\",
|
||||
\\"aria-selected\\": \\"true\\"
|
||||
},
|
||||
\\"childNodes\\": [
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"Tab 2\\",
|
||||
\\"id\\": 22
|
||||
}
|
||||
],
|
||||
\\"id\\": 21
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 23
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"button\\",
|
||||
\\"attributes\\": {
|
||||
\\"slot\\": \\"title\\",
|
||||
\\"role\\": \\"tab\\",
|
||||
\\"tabindex\\": \\"-1\\",
|
||||
\\"aria-selected\\": \\"false\\"
|
||||
},
|
||||
\\"childNodes\\": [
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"Tab 3\\",
|
||||
\\"id\\": 25
|
||||
}
|
||||
],
|
||||
\\"id\\": 24
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 26
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"section\\",
|
||||
\\"attributes\\": {
|
||||
\\"role\\": \\"tabpanel\\",
|
||||
\\"tabindex\\": \\"0\\",
|
||||
\\"aria-hidden\\": \\"true\\"
|
||||
},
|
||||
\\"childNodes\\": [
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"content panel 1\\",
|
||||
\\"id\\": 28
|
||||
}
|
||||
],
|
||||
\\"id\\": 27
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 29
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"section\\",
|
||||
\\"attributes\\": {
|
||||
\\"role\\": \\"tabpanel\\",
|
||||
\\"tabindex\\": \\"0\\",
|
||||
\\"aria-hidden\\": \\"false\\"
|
||||
},
|
||||
\\"childNodes\\": [
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"content panel 2\\",
|
||||
\\"id\\": 31
|
||||
}
|
||||
],
|
||||
\\"id\\": 30
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 32
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"section\\",
|
||||
\\"attributes\\": {
|
||||
\\"role\\": \\"tabpanel\\",
|
||||
\\"tabindex\\": \\"0\\",
|
||||
\\"aria-hidden\\": \\"true\\"
|
||||
},
|
||||
\\"childNodes\\": [
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"content panel 3\\",
|
||||
\\"id\\": 34
|
||||
}
|
||||
],
|
||||
\\"id\\": 33
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 35
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 36,
|
||||
\\"isShadow\\": true
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"style\\",
|
||||
\\"attributes\\": {},
|
||||
\\"childNodes\\": [
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n :host {\\\\n display: inline-block;\\\\n width: 650px;\\\\n font-family: 'Roboto Slab';\\\\n contain: content;\\\\n }\\\\n :host([background]) {\\\\n background: var(--background-color, #9E9E9E);\\\\n border-radius: 10px;\\\\n padding: 10px;\\\\n }\\\\n #panels {\\\\n box-shadow: 0 2px 2px rgba(0, 0, 0, .3);\\\\n background: white;\\\\n border-radius: 3px;\\\\n padding: 16px;\\\\n height: 250px;\\\\n overflow: auto;\\\\n }\\\\n #tabs {\\\\n display: inline-flex;\\\\n -webkit-user-select: none;\\\\n user-select: none;\\\\n }\\\\n #tabs slot {\\\\n display: inline-flex; /* Safari bug. Treats <slot> as a parent */\\\\n }\\\\n /* Safari does not support #id prefixes on ::slotted\\\\n See https://bugs.webkit.org/show_bug.cgi?id=160538 */\\\\n #tabs ::slotted(*) {\\\\n font: 400 16px/22px 'Roboto';\\\\n padding: 16px 8px;\\\\n margin: 0;\\\\n text-align: center;\\\\n width: 100px;\\\\n text-overflow: ellipsis;\\\\n white-space: nowrap;\\\\n overflow: hidden;\\\\n cursor: pointer;\\\\n border-top-left-radius: 3px;\\\\n border-top-right-radius: 3px;\\\\n background: linear-gradient(#fafafa, #eee);\\\\n border: none; /* if the user users a <button> */\\\\n }\\\\n #tabs ::slotted([aria-selected=\\\\\\"true\\\\\\"]) {\\\\n font-weight: 600;\\\\n background: white;\\\\n box-shadow: none;\\\\n }\\\\n #tabs ::slotted(:focus) {\\\\n z-index: 1; /* make sure focus ring doesn't get buried */\\\\n }\\\\n #panels ::slotted([aria-hidden=\\\\\\"true\\\\\\"]) {\\\\n display: none;\\\\n }\\\\n \\",
|
||||
\\"isStyle\\": true,
|
||||
\\"id\\": 38
|
||||
}
|
||||
],
|
||||
\\"id\\": 37,
|
||||
\\"isShadow\\": true
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 39,
|
||||
\\"isShadow\\": true
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"div\\",
|
||||
\\"attributes\\": {
|
||||
\\"id\\": \\"tabs\\"
|
||||
},
|
||||
\\"childNodes\\": [
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 41
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"slot\\",
|
||||
\\"attributes\\": {
|
||||
\\"id\\": \\"tabsSlot\\",
|
||||
\\"name\\": \\"title\\"
|
||||
},
|
||||
\\"childNodes\\": [],
|
||||
\\"id\\": 42
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 43
|
||||
}
|
||||
],
|
||||
\\"id\\": 40,
|
||||
\\"isShadow\\": true
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 44,
|
||||
\\"isShadow\\": true
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"div\\",
|
||||
\\"attributes\\": {
|
||||
\\"id\\": \\"panels\\"
|
||||
},
|
||||
\\"childNodes\\": [
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 46
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"slot\\",
|
||||
\\"attributes\\": {
|
||||
\\"id\\": \\"panelsSlot\\"
|
||||
},
|
||||
\\"childNodes\\": [],
|
||||
\\"id\\": 47
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 48
|
||||
}
|
||||
],
|
||||
\\"id\\": 45,
|
||||
\\"isShadow\\": true
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 49,
|
||||
\\"isShadow\\": true
|
||||
}
|
||||
],
|
||||
\\"id\\": 16,
|
||||
\\"isShadowHost\\": true
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\",
|
||||
\\"id\\": 50
|
||||
},
|
||||
{
|
||||
\\"type\\": 2,
|
||||
\\"tagName\\": \\"script\\",
|
||||
\\"attributes\\": {},
|
||||
\\"childNodes\\": [
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"SCRIPT_PLACEHOLDER\\",
|
||||
\\"id\\": 52
|
||||
}
|
||||
],
|
||||
\\"id\\": 51
|
||||
},
|
||||
{
|
||||
\\"type\\": 3,
|
||||
\\"textContent\\": \\"\\\\n \\\\n\\\\n\\",
|
||||
\\"id\\": 53
|
||||
}
|
||||
],
|
||||
\\"id\\": 14
|
||||
}
|
||||
],
|
||||
\\"id\\": 3
|
||||
}
|
||||
],
|
||||
\\"id\\": 1
|
||||
}"
|
||||
`;
|
||||
|
||||
209
test/html/shadow-dom.html
Normal file
209
test/html/shadow-dom.html
Normal file
@@ -0,0 +1,209 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>shadow DOM</title>
|
||||
</head>
|
||||
<body>
|
||||
<fancy-tabs background>
|
||||
<button slot="title">Tab 1</button>
|
||||
<button slot="title" selected>Tab 2</button>
|
||||
<button slot="title">Tab 3</button>
|
||||
<section>content panel 1</section>
|
||||
<section>content panel 2</section>
|
||||
<section>content panel 3</section>
|
||||
</fancy-tabs>
|
||||
<script>
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
// Feature detect
|
||||
if (!(window.customElements && document.body.attachShadow)) {
|
||||
document.querySelector('fancy-tabs').innerHTML =
|
||||
"<b>Your browser doesn't support Shadow DOM and Custom Elements v1.</b>";
|
||||
return;
|
||||
}
|
||||
|
||||
let selected_ = null;
|
||||
|
||||
// See https://www.w3.org/TR/wai-aria-practices-1.1/#tabpanel
|
||||
|
||||
customElements.define(
|
||||
'fancy-tabs',
|
||||
class extends HTMLElement {
|
||||
constructor() {
|
||||
super(); // always call super() first in the ctor.
|
||||
|
||||
// Create shadow DOM for the component.
|
||||
let shadowRoot = this.attachShadow({ mode: 'open' });
|
||||
shadowRoot.innerHTML = `
|
||||
<style>
|
||||
:host {
|
||||
display: inline-block;
|
||||
width: 650px;
|
||||
font-family: 'Roboto Slab';
|
||||
contain: content;
|
||||
}
|
||||
:host([background]) {
|
||||
background: var(--background-color, #9E9E9E);
|
||||
border-radius: 10px;
|
||||
padding: 10px;
|
||||
}
|
||||
#panels {
|
||||
box-shadow: 0 2px 2px rgba(0, 0, 0, .3);
|
||||
background: white;
|
||||
border-radius: 3px;
|
||||
padding: 16px;
|
||||
height: 250px;
|
||||
overflow: auto;
|
||||
}
|
||||
#tabs {
|
||||
display: inline-flex;
|
||||
-webkit-user-select: none;
|
||||
user-select: none;
|
||||
}
|
||||
#tabs slot {
|
||||
display: inline-flex; /* Safari bug. Treats <slot> as a parent */
|
||||
}
|
||||
/* Safari does not support #id prefixes on ::slotted
|
||||
See https://bugs.webkit.org/show_bug.cgi?id=160538 */
|
||||
#tabs ::slotted(*) {
|
||||
font: 400 16px/22px 'Roboto';
|
||||
padding: 16px 8px;
|
||||
margin: 0;
|
||||
text-align: center;
|
||||
width: 100px;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
cursor: pointer;
|
||||
border-top-left-radius: 3px;
|
||||
border-top-right-radius: 3px;
|
||||
background: linear-gradient(#fafafa, #eee);
|
||||
border: none; /* if the user users a <button> */
|
||||
}
|
||||
#tabs ::slotted([aria-selected="true"]) {
|
||||
font-weight: 600;
|
||||
background: white;
|
||||
box-shadow: none;
|
||||
}
|
||||
#tabs ::slotted(:focus) {
|
||||
z-index: 1; /* make sure focus ring doesn't get buried */
|
||||
}
|
||||
#panels ::slotted([aria-hidden="true"]) {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
<div id="tabs">
|
||||
<slot id="tabsSlot" name="title"></slot>
|
||||
</div>
|
||||
<div id="panels">
|
||||
<slot id="panelsSlot"></slot>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
get selected() {
|
||||
return selected_;
|
||||
}
|
||||
|
||||
set selected(idx) {
|
||||
selected_ = idx;
|
||||
this._selectTab(idx);
|
||||
|
||||
// Updated the element's selected attribute value when
|
||||
// backing property changes.
|
||||
this.setAttribute('selected', idx);
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.setAttribute('role', 'tablist');
|
||||
|
||||
const tabsSlot = this.shadowRoot.querySelector('#tabsSlot');
|
||||
const panelsSlot = this.shadowRoot.querySelector('#panelsSlot');
|
||||
|
||||
this.tabs = tabsSlot.assignedNodes({ flatten: true });
|
||||
this.panels = panelsSlot
|
||||
.assignedNodes({ flatten: true })
|
||||
.filter((el) => {
|
||||
return el.nodeType === Node.ELEMENT_NODE;
|
||||
});
|
||||
|
||||
// Add aria role="tabpanel" to each content panel.
|
||||
for (let [i, panel] of this.panels.entries()) {
|
||||
panel.setAttribute('role', 'tabpanel');
|
||||
panel.setAttribute('tabindex', 0);
|
||||
}
|
||||
|
||||
// Save refer to we can remove listeners later.
|
||||
this._boundOnTitleClick = this._onTitleClick.bind(this);
|
||||
this._boundOnKeyDown = this._onKeyDown.bind(this);
|
||||
|
||||
tabsSlot.addEventListener('click', this._boundOnTitleClick);
|
||||
tabsSlot.addEventListener('keydown', this._boundOnKeyDown);
|
||||
|
||||
this.selected = this._findFirstSelectedTab() || 0;
|
||||
}
|
||||
|
||||
disconnectedCallback() {
|
||||
const tabsSlot = this.shadowRoot.querySelector('#tabsSlot');
|
||||
tabsSlot.removeEventListener('click', this._boundOnTitleClick);
|
||||
tabsSlot.removeEventListener('keydown', this._boundOnKeyDown);
|
||||
}
|
||||
|
||||
_onTitleClick(e) {
|
||||
if (e.target.slot === 'title') {
|
||||
this.selected = this.tabs.indexOf(e.target);
|
||||
e.target.focus();
|
||||
}
|
||||
}
|
||||
|
||||
_onKeyDown(e) {
|
||||
switch (e.code) {
|
||||
case 'ArrowUp':
|
||||
case 'ArrowLeft':
|
||||
e.preventDefault();
|
||||
var idx = this.selected - 1;
|
||||
idx = idx < 0 ? this.tabs.length - 1 : idx;
|
||||
this.tabs[idx].click();
|
||||
break;
|
||||
case 'ArrowDown':
|
||||
case 'ArrowRight':
|
||||
e.preventDefault();
|
||||
var idx = this.selected + 1;
|
||||
this.tabs[idx % this.tabs.length].click();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_findFirstSelectedTab() {
|
||||
let selectedIdx;
|
||||
for (let [i, tab] of this.tabs.entries()) {
|
||||
tab.setAttribute('role', 'tab');
|
||||
|
||||
// Allow users to declaratively select a tab
|
||||
// Highlight last tab which has the selected attribute.
|
||||
if (tab.hasAttribute('selected')) {
|
||||
selectedIdx = i;
|
||||
}
|
||||
}
|
||||
return selectedIdx;
|
||||
}
|
||||
|
||||
_selectTab(idx = null) {
|
||||
for (let i = 0, tab; (tab = this.tabs[i]); ++i) {
|
||||
let select = i === idx;
|
||||
tab.setAttribute('tabindex', select ? 0 : -1);
|
||||
tab.setAttribute('aria-selected', select);
|
||||
this.panels[i].setAttribute('aria-hidden', !select);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -166,3 +166,50 @@ describe('iframe integration tests', function (this: ISuite) {
|
||||
assert(result.pass, result.pass ? '' : result.report());
|
||||
}).timeout(5000);
|
||||
});
|
||||
|
||||
describe('shadown DOM integration tests', function (this: ISuite) {
|
||||
const shadowDomHtml = path.join(__dirname, 'html/shadow-dom.html');
|
||||
const raw = fs.readFileSync(shadowDomHtml, 'utf-8');
|
||||
|
||||
before(async () => {
|
||||
this.server = await server();
|
||||
this.browser = await puppeteer.launch({
|
||||
// headless: false,
|
||||
});
|
||||
|
||||
const bundle = await rollup.rollup({
|
||||
input: path.resolve(__dirname, '../src/index.ts'),
|
||||
plugins: [typescript()],
|
||||
});
|
||||
const { code } = await bundle.generate({
|
||||
name: 'rrweb',
|
||||
format: 'iife',
|
||||
});
|
||||
this.code = code;
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await this.browser.close();
|
||||
await this.server.close();
|
||||
});
|
||||
|
||||
it('snapshot shadow DOM', async () => {
|
||||
const page: puppeteer.Page = await this.browser.newPage();
|
||||
// console for debug
|
||||
// tslint:disable-next-line: no-console
|
||||
page.on('console', (msg) => console.log(msg.text()));
|
||||
await page.goto(`http://localhost:3030/html`);
|
||||
await page.setContent(raw, {
|
||||
waitUntil: 'load',
|
||||
});
|
||||
const snapshotResult = JSON.stringify(
|
||||
await page.evaluate(`${this.code};
|
||||
rrweb.snapshot(document)[0];
|
||||
`),
|
||||
null,
|
||||
2,
|
||||
);
|
||||
const result = matchSnapshot(snapshotResult, __filename, this.title);
|
||||
assert(result.pass, result.pass ? '' : result.report());
|
||||
}).timeout(5000);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user