Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/light-candles-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lit-labs/ssr': minor
---

Adds HTMLElement.shadowRoot property to dom-shim.
12 changes: 10 additions & 2 deletions packages/labs/ssr/src/lib/dom-shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export const getWindow = ({
value,
}));
}
private _shadowRoot: null | ShadowRoot = null;
get shadowRoot() {
return this._shadowRoot;
}
abstract attributeChangedCallback?(
name: string,
old: string | null,
Expand All @@ -60,8 +64,12 @@ export const getWindow = ({
hasAttribute(name: string) {
return attributesForElement(this).has(name);
}
attachShadow() {
return {host: this};
attachShadow(init: ShadowRootInit) {
const shadowRoot = {host: this};
if (init && init.mode === 'open') {
this._shadowRoot = shadowRoot;
}
return shadowRoot;
}
getAttribute(name: string) {
const value = attributesForElement(this).get(name);
Expand Down
1 change: 1 addition & 0 deletions packages/labs/ssr/src/test/all-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

import './lib/dom-shim_test.js';
import './lib/render-lit_test.js';
import './lib/module-loader_test.js';
33 changes: 33 additions & 0 deletions packages/labs/ssr/src/test/lib/dom-shim_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/

import {getWindow} from '../../lib/dom-shim.js';
import {test} from 'uvu';
import * as assert from 'uvu/assert';

const window = getWindow({}) as unknown as Window;
const {HTMLElement} = window.window;

test('elements without an attached shadow root should expose a null value from the "shadowRoot" property', () => {
class UnattachedShadowRoot extends HTMLElement {}
const element = new UnattachedShadowRoot();
assert.is(element.shadowRoot, null);
});
test('elements defined with an open shadow root should expose it\'s shadow root from the "shadowRoot" property', () => {
class OpenShadowRoot extends HTMLElement {}
const element = new OpenShadowRoot();
const shadow = element.attachShadow({mode: 'open'});
assert.is(shadow, element.shadowRoot);
assert.is(shadow.host, element);
});
test('elements defined with a closed shadow root should expose a null value from the "shadowRoot" property', () => {
class ClosedShadowRoot extends HTMLElement {}
const element = new ClosedShadowRoot();
element.attachShadow({mode: 'closed'});
assert.is(element.shadowRoot, null);
});

test.run();