Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[labs/ssr-dom-shim] Patch localName and tagName with customElements.define call #4553

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/labs/ssr-dom-shim/src/index.ts
Expand Up @@ -148,6 +148,17 @@ const CustomElementRegistryShim = class CustomElementRegistry {
);
}
}
// Polyfill tagName and localName for the component.
Object.defineProperties(ctor.prototype, {
localName: {
value: name,
writable: false,
},
tagName: {
value: name.toUpperCase(),
writable: false,
},
});
this.__definitions.set(name, {
ctor,
// Note it's important we read `observedAttributes` in case it is a getter
Expand Down
11 changes: 10 additions & 1 deletion packages/labs/ssr-dom-shim/src/test/element-shim_test.ts
Expand Up @@ -7,7 +7,7 @@
import {suite} from 'uvu';
// eslint-disable-next-line import/extensions
import * as assert from 'uvu/assert';
import {HTMLElement} from '@lit-labs/ssr-dom-shim';
import {customElements, HTMLElement} from '@lit-labs/ssr-dom-shim';

const test = suite('Element Shim');

Expand Down Expand Up @@ -45,4 +45,13 @@ test('setAttribute silently casts values to a string', () => {
assert.equal(shimmedEl.getAttribute('tomato'), '[object Object]');
});

test('localName and tagName should be available', () => {
const elementName = 'lit-test';
customElements.define(elementName, class extends HTMLElement {});
const LitTest = customElements.get(elementName)!;
const shimmedEl = new LitTest();
assert.equal(shimmedEl.localName, elementName);
assert.equal(shimmedEl.tagName, elementName.toUpperCase());
});

test.run();