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: Ensure original named slot tags are retained when not using shadow root. #13

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[*]
charset = utf-8
end_of_line = lf

[{.*js,*.json}]
indent_style = space
indent_size = 2
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.DS_Store
coverage
coverage
.idea/
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, the rest is whitespace (or unit test updates).

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);
}
8 changes: 4 additions & 4 deletions tests/tag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,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).to.equal('<test-tag><h1>Main H1</h1> <div class="content">Main Default <div>HERE</div></div></test-tag>')
expect(el.innerHTML).to.equal('<test-tag><h1>Main H1</h1> <div class="content">Main Default <div><div slot="inner">HERE</div></div></div></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).to.equal('<test-tag><h1>Main H1</h1> <div class="content">BOOM! <div>HERE</div></div></test-tag>')
expect(el.innerHTML).to.equal('<test-tag><h1>Main H1</h1> <div class="content">BOOM! <div><div slot="inner">HERE</div></div></div></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).to.equal('<test-tag><h1>Main H1</h1> <div class="content"><h2>Nested</h2> <div>HERE</div></div></test-tag>')
expect(el.innerHTML).to.equal('<test-tag><h1>Main H1</h1> <div class="content"><h2>Nested</h2> <div><div slot="inner">HERE</div></div></div></test-tag>')
})

it("Unknown slot gets ignored", () => {
Expand Down Expand Up @@ -133,6 +133,6 @@ describe("Component Wrapper shadow true", () => {
shadowhtml = document.querySelector('test-shad').shadowRoot.innerHTML
expect(shadowhtml).to.equal('<div><h1>Main H1</h1> <div class="content"><slot></slot> <div>Inner Default</div></div></div>')
})

})

Loading