Skip to content

Commit

Permalink
fix(@lit-labs/ssr-dom-shim): patch localName and tagName with customE…
Browse files Browse the repository at this point in the history
…lements.define call

This change extends the CustomElementRegistryShim to patch localName and tagName into the web component, when calling define.
This allows instances to call `this.localName` and `this.tagName` accordingly.

Fixes lit#3375
  • Loading branch information
kyubisation committed Feb 20, 2024
1 parent b779807 commit e1dffa7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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();

0 comments on commit e1dffa7

Please sign in to comment.