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

Fix for #12 (take 2): Ensure original named slot tags are retained when not using shadow root. #15

Open
wants to merge 1 commit into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ function createSlots(slots) {
return {
c: noop,
m: function mount(target, anchor) {
insert(target, element.cloneNode(true), anchor);
insert(target, element.cloneNode(true), anchor);
},
d: function destroy(detaching) {
if (detaching && element.innerHTML){
d: function destroy(detaching) {
if (detaching && element.innerHTML){
detach(element);
}
}
},
l: noop,
};
Expand All @@ -42,7 +42,7 @@ export default function(opts){
let link = document.createElement('link');
link.setAttribute("href",opts.href)
link.setAttribute("rel","stylesheet")
root.appendChild(link);
root.appendChild(link);
}
if(opts.shadow){
this._root = document.createElement('div')
Expand Down Expand Up @@ -82,7 +82,7 @@ export default function(opts){
}
try{ this.elem.$destroy()}catch(err){} // detroy svelte element when removed from dom
}

unwrap(from){
let node = document.createDocumentFragment();
while (from.firstChild) {
Expand All @@ -95,7 +95,7 @@ export default function(opts){
const namedSlots = this.querySelectorAll('[slot]')
let slots = {}
namedSlots.forEach(n=>{
slots[n.slot] = this.unwrap(n)
slots[n.slot] = n
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the primary change (which includes the wrapping <div slot="inner"> which is typical in normal Svelte output when using named slots).

this.removeChild(n)
})
if(this.innerHTML.length){
Expand Down Expand Up @@ -144,6 +144,6 @@ export default function(opts){
this.elem.$set({[name]:newValue})
}
}
}
}
window.customElements.define(opts.tagname, Wrapper);
}
6 changes: 3 additions & 3 deletions tests/TestTag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ describe('Component Wrapper shadow false', () => {
el = document.createElement('div')
el.innerHTML = `<test-tag><div slot="inner">HERE</div></test-tag>`
document.body.appendChild(el)
expect(el.innerHTML).toBe('<test-tag><h1>Main H1</h1> <div class="content">Main Default <div>HERE</div></div><!--<TestTag>--></test-tag>')
expect(el.innerHTML).toBe('<test-tag><h1>Main H1</h1> <div class="content">Main Default <div><div slot="inner">HERE</div></div></div><!--<TestTag>--></test-tag>')
})

it('both slots', () => {
el = document.createElement('div')
el.innerHTML = `<test-tag>BOOM!<div slot="inner">HERE</div></test-tag>`
document.body.appendChild(el)
expect(el.innerHTML).toBe('<test-tag><h1>Main H1</h1> <div class="content">BOOM! <div>HERE</div></div><!--<TestTag>--></test-tag>')
expect(el.innerHTML).toBe('<test-tag><h1>Main H1</h1> <div class="content">BOOM! <div><div slot="inner">HERE</div></div></div><!--<TestTag>--></test-tag>')
})

it('nested tags', () => {
el = document.createElement('div')
el.innerHTML = `<test-tag><h2>Nested</h2><div slot="inner">HERE</div></test-tag>`
document.body.appendChild(el)
expect(el.innerHTML).toBe('<test-tag><h1>Main H1</h1> <div class="content"><h2>Nested</h2> <div>HERE</div></div><!--<TestTag>--></test-tag>')
expect(el.innerHTML).toBe('<test-tag><h1>Main H1</h1> <div class="content"><h2>Nested</h2> <div><div slot="inner">HERE</div></div></div><!--<TestTag>--></test-tag>')
})

it('Unknown slot gets ignored', () => {
Expand Down