Skip to content

Commit

Permalink
Merge pull request #4 from luwes/slot
Browse files Browse the repository at this point in the history
Improve <slot> element support
  • Loading branch information
ttraenkler authored Jul 5, 2023
2 parents f806d0d + bf211e7 commit 0aade2c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
55 changes: 53 additions & 2 deletions esm/html/slot-element.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
import {HTMLElement} from './element.js';
import {registerHTMLClass} from '../shared/register-html-class.js';

const tagName = 'slot';

/**
* @implements globalThis.HTMLSlotElement
*/
export class HTMLSlotElement extends HTMLElement {
constructor(ownerDocument, localName = 'slot') {
class HTMLSlotElement extends HTMLElement {
constructor(ownerDocument, localName = tagName) {
super(ownerDocument, localName);
}

/* c8 ignore start */
get name() { return this.getAttribute('name'); }
set name(value) { this.setAttribute('name', value); }

assign() {}

assignedNodes(options) {
const isNamedSlot = !!this.name;
const hostChildNodes = this.getRootNode().host?.childNodes ?? [];
let slottables;

if (isNamedSlot) {
slottables = [...hostChildNodes].filter(node => node.slot === this.name);
} else {
slottables = [...hostChildNodes].filter(node => !node.slot);
}

if (options?.flatten) {
const result = [];

// Element and Text nodes are slottables. A slot can be a slottable.
for (let slottable of slottables) {
if (slottable.localName === 'slot') {
result.push(...slottable.assignedNodes({ flatten: true }));
} else {
result.push(slottable);
}
}

slottables = result;
}

// If no assigned nodes are found, it returns the slot's fallback content.
return slottables.length ? slottables : [...this.childNodes];
}

assignedElements(options) {
const slottables = this.assignedNodes(options).filter(n => n.nodeType === 1);

// If no assigned elements are found, it returns the slot's fallback content.
return slottables.length ? slottables : [...this.children];
}
/* c8 ignore stop */
}

registerHTMLClass(tagName, HTMLSlotElement);

export {HTMLSlotElement};
3 changes: 3 additions & 0 deletions esm/interface/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ export class Element extends ParentNode {

get tabIndex() { return numericAttribute.get(this, 'tabindex') || -1; }
set tabIndex(value) { numericAttribute.set(this, 'tabindex', value); }

get slot() { return stringAttribute.get(this, 'slot'); }
set slot(value) { stringAttribute.set(this, 'slot', value); }
// </specialGetters>


Expand Down

0 comments on commit 0aade2c

Please sign in to comment.