Skip to content

Commit

Permalink
Fix for #12 (take 2): Ensure original named slot tags are retained wh…
Browse files Browse the repository at this point in the history
…en not using shadow root. Take 2 required to account for new unit testing framework (Vitest) from PR #13.
  • Loading branch information
patricknelson committed Jun 5, 2023
1 parent fede9bc commit 9c3e66e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
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
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

0 comments on commit 9c3e66e

Please sign in to comment.