diff --git a/docs/components/footer.js b/docs/components/footer.js index 4a36ad5..63aa1a6 100644 --- a/docs/components/footer.js +++ b/docs/components/footer.js @@ -1,36 +1,36 @@ -const template = document.createElement('template'); - -template.innerHTML = ` - + footer p { + margin: 0 auto; + font-weight: bold; + } - -`; + footer a, footer a:visited { + color: #efefef; + text-decoration: none; + } + -class Footer extends HTMLElement { - connectedCallback() { - this.innerHTML = template.content.textContent; + + `; } } diff --git a/docs/components/header.js b/docs/components/header.js index 4c54f45..0b5495e 100644 --- a/docs/components/header.js +++ b/docs/components/header.js @@ -1,67 +1,67 @@ import './navigation.js'; -const template = document.createElement('template'); - -template.innerHTML = ` - + header img.github-badge { + float: right; + display: inline-block; + padding: 10px; + align-items: top; + } - - - - - - + header div.container { + max-width: 1200px; + margin: auto; + } + - - - - + + + + + + - - - -`; + + + + -class Header extends HTMLElement { - connectedCallback() { - this.innerHTML = template.content.textContent; + + + + `; } } diff --git a/docs/components/navigation.js b/docs/components/navigation.js index 731bc3d..a3a5c11 100644 --- a/docs/components/navigation.js +++ b/docs/components/navigation.js @@ -1,41 +1,40 @@ -// intentionally nested in the assets/ directory to test wcc nested dependency resolution logic -const template = document.createElement('template'); - -template.innerHTML = ` - + nav ul li { + float: left; + width: 33.3%; + text-align: center; + } - - - Home - Docs - Examples - - -`; + nav ul li a, nav ul li a:visited { + display: inline-block; + color: #efefef; + min-height: 48px; + font-size: 2.5rem; + } + -class Navigation extends HTMLElement { - connectedCallback() { - this.innerHTML = template.content.textContent; + + + Home + Docs + Examples + + + ` } } diff --git a/docs/layout.js b/docs/layout.js index 08a6740..7658720 100644 --- a/docs/layout.js +++ b/docs/layout.js @@ -1,43 +1,43 @@ import './components/footer.js'; import './components/header.js'; -const template = document.createElement('template'); - -template.innerHTML = ` - - - - - - - - - -`; - class Layout extends HTMLElement { connectedCallback() { - this.innerHTML = template.content.textContent; + this.innerHTML = this.render(); + } + + render() { + return ` + + + + + + + + + + `; } } diff --git a/src/dom-shim.js b/src/dom-shim.js index a41c343..71fcee0 100644 --- a/src/dom-shim.js +++ b/src/dom-shim.js @@ -3,6 +3,7 @@ class EventTarget { } // https://developer.mozilla.org/en-US/docs/Web/API/Node // EventTarget <- Node +// TODO should be an interface? class Node extends EventTarget { constructor() { super(); @@ -15,7 +16,7 @@ class Node extends EventTarget { } appendChild(node) { - this.innerHTML = this.innerHTML ? this.innerHTML += node.textContent : node.textContent; + this.innerHTML = this.innerHTML ? this.innerHTML += node.innerHTML : node.innerHTML; } } @@ -63,14 +64,9 @@ class HTMLElement extends Element { } // https://github.com/mfreed7/declarative-shadow-dom/blob/master/README.md#serialization + // eslint-disable-next-line getInnerHTML(options = {}) { - return options.includeShadowRoots - ? ` - - ${this.shadowRoot.innerHTML} - - ` - : this.shadowRoot.innerHTML; + return this.shadowRoot.innerHTML; } } @@ -101,11 +97,20 @@ class HTMLTemplateElement extends HTMLElement { super(); // console.debug('HTMLTemplateElement constructor'); - this.content = new DocumentFragment(this.innerHTML); + this.content = new DocumentFragment(); } + // TODO open vs closed shadow root set innerHTML(html) { - this.content.textContent = html; + this.content.innerHTML = ` + + ${html} + + `; + } + + get innerHTML() { + return this.content && this.content.innerHTML ? this.content.innerHTML : undefined; } } diff --git a/test/cases/attributes/attributes.spec.js b/test/cases/attributes/attributes.spec.js index fad814f..f35c070 100644 --- a/test/cases/attributes/attributes.spec.js +++ b/test/cases/attributes/attributes.spec.js @@ -21,24 +21,22 @@ const expect = chai.expect; describe('Run WCC For ', function() { const LABEL = 'Custom Element w/ Attributes and Shadow DOM'; let dom; - let pageContentsDom; before(async function() { const { html } = await renderToString(new URL('./src/index.js', import.meta.url)); dom = new JSDOM(html); - pageContentsDom = new JSDOM(dom.window.document.querySelectorAll('template[shadowroot="open"]')[0].innerHTML); }); describe(LABEL, function() { - it('should have one top level with an open shadowroot', function() { - expect(dom.window.document.querySelectorAll('template[shadowroot="open"]').length).to.equal(1); - expect(dom.window.document.querySelectorAll('template').length).to.equal(1); + it('should have one top level custom element with a with an open shadowroot', function() { + expect(dom.window.document.querySelectorAll('wcc-counter template[shadowroot="open"]').length).to.equal(1); + expect(dom.window.document.querySelectorAll('wcc-counter template').length).to.equal(1); }); describe('static page content', function() { it('should have the expected static content for the page', function() { - expect(pageContentsDom.window.document.querySelector('h1').textContent).to.equal('Counter'); + expect(dom.window.document.querySelector('h1').textContent).to.equal('Counter'); }); }); @@ -46,7 +44,7 @@ describe('Run WCC For ', function() { let counterContentsDom; before(function() { - counterContentsDom = new JSDOM(pageContentsDom.window.document.querySelectorAll('wcc-counter template[shadowroot="open"]')[0].innerHTML); + counterContentsDom = new JSDOM(dom.window.document.querySelectorAll('wcc-counter template[shadowroot="open"]')[0].innerHTML); }); it('should have two tags within the shadowroot', function() { @@ -55,7 +53,7 @@ describe('Run WCC For ', function() { it('should have a with the value of the attribute as its text content', function() { const count = counterContentsDom.window.document.querySelector('span#count').textContent; - + expect(count).to.equal('5'); }); }); diff --git a/test/cases/attributes/src/components/counter.js b/test/cases/attributes/src/components/counter.js index 61875f7..cf113ee 100644 --- a/test/cases/attributes/src/components/counter.js +++ b/test/cases/attributes/src/components/counter.js @@ -51,11 +51,13 @@ class Counter extends HTMLElement { render() { return ` - - Increment - Current Count: ${this.count} - Decrement - + + + Increment + Current Count: ${this.count} + Decrement + + `; } } diff --git a/test/cases/attributes/src/index.js b/test/cases/attributes/src/index.js index 7f758fd..9984027 100644 --- a/test/cases/attributes/src/index.js +++ b/test/cases/attributes/src/index.js @@ -1,25 +1,19 @@ import './components/counter.js'; -export default class HomePage extends HTMLElement { - constructor() { - super(); +const template = document.createElement('template'); - if (this.shadowRoot) { - // console.debug('HomePage => shadowRoot detected!'); - } else { - this.attachShadow({ mode: 'open' }); - } - } +template.innerHTML = ` + Counter - connectedCallback() { - this.shadowRoot.innerHTML = this.getTemplate(); - } + +`; - getTemplate() { - return ` - Counter +export default class HomePage extends HTMLElement { + connectedCallback() { + this.innerHTML = ` + Counter - - `; + + `; } } \ No newline at end of file diff --git a/test/cases/children-and-slots/children-and-slots.spec.js b/test/cases/children-and-slots/children-and-slots.spec.js index 6c22562..a3dcd97 100644 --- a/test/cases/children-and-slots/children-and-slots.spec.js +++ b/test/cases/children-and-slots/children-and-slots.spec.js @@ -22,27 +22,24 @@ const expect = chai.expect; describe('Run WCC For ', function() { const LABEL = 'Custom Element w/ Declarative Shadow DOM and using children and content'; let dom; - let pageContentsDom; before(async function() { const { html } = await renderToString(new URL('./src/pages/index.js', import.meta.url)); dom = new JSDOM(html); - - pageContentsDom = new JSDOM(dom.window.document.querySelectorAll('template[shadowroot="open"]')[0].innerHTML); }); describe(LABEL, function() { - it('should have one top level with an open shadowroot', function() { - expect(dom.window.document.querySelectorAll('template[shadowroot="open"]').length).to.equal(1); - expect(dom.window.document.querySelectorAll('template').length).to.equal(1); + it('should have one two top level custom elements with a with an open shadowroot', function() { + expect(dom.window.document.querySelectorAll('wcc-paragraph template[shadowroot="open"]').length).to.equal(2); + expect(dom.window.document.querySelectorAll('wcc-paragraph template').length).to.equal(2); }); describe(' with default content', function() { let paragraphContentsDom; before(function() { - paragraphContentsDom = new JSDOM(pageContentsDom.window.document.querySelectorAll('wcc-paragraph.default template[shadowroot="open"]')[0].innerHTML); + paragraphContentsDom = new JSDOM(dom.window.document.querySelectorAll('wcc-paragraph.default template[shadowroot="open"]')[0].innerHTML); }); it('should have one tag for the default content', function() { @@ -57,16 +54,16 @@ describe('Run WCC For ', function() { describe(' with custom content', function() { let paragraphContentsDom; let paragraphContentsLightDom; - + before(function() { - paragraphContentsDom = new JSDOM(pageContentsDom.window.document.querySelectorAll('wcc-paragraph.custom template[shadowroot="open"]')[0].innerHTML); - paragraphContentsLightDom = new JSDOM(pageContentsDom.window.document.querySelectorAll('wcc-paragraph.custom')[0].innerHTML); + paragraphContentsDom = new JSDOM(dom.window.document.querySelectorAll('wcc-paragraph.custom template[shadowroot="open"]')[0].innerHTML); + paragraphContentsLightDom = new JSDOM(dom.window.document.querySelectorAll('wcc-paragraph.custom')[0].innerHTML); }); - + it('should have one tag for the default content', function() { expect(paragraphContentsDom.window.document.querySelectorAll('p').length).to.equal(1); }); - + it('should have one tag with the custom content in the light DOM', function() { expect(paragraphContentsLightDom.window.document.querySelector('span').textContent).to.equal('Let\'s have some different text!'); }); diff --git a/test/cases/children-and-slots/src/pages/index.js b/test/cases/children-and-slots/src/pages/index.js index 7f5d534..ed1b9aa 100644 --- a/test/cases/children-and-slots/src/pages/index.js +++ b/test/cases/children-and-slots/src/pages/index.js @@ -2,10 +2,7 @@ import '../components/paragraph.js'; export default class HomePage extends HTMLElement { connectedCallback() { - if (!this.shadowRoot) { - this.attachShadow({ mode: 'open' }); - this.shadowRoot.innerHTML = this.getTemplate(); - } + this.innerHTML = this.getTemplate(); } getTemplate() { diff --git a/test/cases/get-data/get-data.spec.js b/test/cases/get-data/get-data.spec.js index b6d0aba..551f916 100644 --- a/test/cases/get-data/get-data.spec.js +++ b/test/cases/get-data/get-data.spec.js @@ -21,24 +21,22 @@ const expect = chai.expect; describe('Run WCC For ', function() { const LABEL = 'Custom Element w/ getData and Shadow DOM'; let dom; - let pageContentsDom; before(async function() { const { html } = await renderToString(new URL('./src/index.js', import.meta.url)); dom = new JSDOM(html); - pageContentsDom = new JSDOM(dom.window.document.querySelectorAll('template[shadowroot="open"]')[0].innerHTML); }); describe(LABEL, function() { - it('should have one top level with an open shadowroot', function() { - expect(dom.window.document.querySelectorAll('template[shadowroot="open"]').length).to.equal(1); - expect(dom.window.document.querySelectorAll('template').length).to.equal(1); + it('should have one top level custom element with a with an open shadowroot', function() { + expect(dom.window.document.querySelectorAll('wcc-counter template[shadowroot="open"]').length).to.equal(1); + expect(dom.window.document.querySelectorAll('wcc-counter template').length).to.equal(1); }); describe('static page content', function() { it('should have the expected static content for the page', function() { - expect(pageContentsDom.window.document.querySelector('h1').textContent).to.equal('Counter'); + expect(dom.window.document.querySelector('h1').textContent).to.equal('Counter'); }); }); @@ -47,7 +45,7 @@ describe('Run WCC For ', function() { let count; before(function() { - counterContentsDom = new JSDOM(pageContentsDom.window.document.querySelectorAll('wcc-counter template[shadowroot="open"]')[0].innerHTML); + counterContentsDom = new JSDOM(dom.window.document.querySelectorAll('wcc-counter template[shadowroot="open"]')[0].innerHTML); count = JSON.parse(counterContentsDom.window.document.querySelector('script[type="application/json"]').textContent).count; }); @@ -57,7 +55,7 @@ describe('Run WCC For ', function() { it('should have a with the value of the attribute as its text content', function() { const innerCount = counterContentsDom.window.document.querySelector('span#count').textContent; - + expect(count).to.equal(parseInt(innerCount, 10)); }); }); diff --git a/test/cases/get-data/src/components/counter.js b/test/cases/get-data/src/components/counter.js index e488fba..af70c90 100644 --- a/test/cases/get-data/src/components/counter.js +++ b/test/cases/get-data/src/components/counter.js @@ -52,15 +52,17 @@ class Counter extends HTMLElement { render() { return ` - + + - - Increment - Current Count: ${this.count} - Decrement - + + Increment + Current Count: ${this.count} + Decrement + + `; } } diff --git a/test/cases/get-data/src/index.js b/test/cases/get-data/src/index.js index 67c6d6c..0a5e4b9 100644 --- a/test/cases/get-data/src/index.js +++ b/test/cases/get-data/src/index.js @@ -1,18 +1,9 @@ import './components/counter.js'; export default class HomePage extends HTMLElement { - constructor() { - super(); - - if (this.shadowRoot) { - // console.debug('HomePage => shadowRoot detected!'); - } else { - this.attachShadow({ mode: 'open' }); - } - } connectedCallback() { - this.shadowRoot.innerHTML = this.getTemplate(); + this.innerHTML = this.getTemplate(); } getTemplate() { diff --git a/test/cases/light-dom/light-dom.spec.js b/test/cases/light-dom/light-dom.spec.js index 7f0a9cf..de03978 100644 --- a/test/cases/light-dom/light-dom.spec.js +++ b/test/cases/light-dom/light-dom.spec.js @@ -53,24 +53,24 @@ describe('Run WCC For ', function() { it('should have expected content within the tag', function() { const content = headerContentsDom.window.document.querySelector('a h4').textContent; - + expect(content).to.contain('My Personal Blog'); }); describe('nested navigation element', function() { let navigationContentsDom; - + before(function() { navigationContentsDom = new JSDOM(dom.window.document.querySelectorAll('wcc-navigation')[0].innerHTML); }); - + it('should have a tag within the shadowroot', function() { expect(navigationContentsDom.window.document.querySelectorAll('nav').length).to.equal(1); }); - + it('should have three links within the element', function() { const links = navigationContentsDom.window.document.querySelectorAll('nav ul li a'); - + expect(links.length).to.equal(3); }); }); diff --git a/test/cases/light-dom/src/components/navigation.js b/test/cases/light-dom/src/components/navigation.js index 2ff6aa4..f2256e0 100644 --- a/test/cases/light-dom/src/components/navigation.js +++ b/test/cases/light-dom/src/components/navigation.js @@ -1,18 +1,14 @@ -const template = document.createElement('template'); - -template.innerHTML = ` - - - Home - About - Artists - - -`; - class Navigation extends HTMLElement { connectedCallback() { - this.appendChild(template.content.cloneNode(true)); + this.innerHTML = ` + + + Home + About + Artists + + + `; } } diff --git a/test/cases/nested-elements/nested-elements.spec.js b/test/cases/nested-elements/nested-elements.spec.js index ee99dc4..4c274a2 100644 --- a/test/cases/nested-elements/nested-elements.spec.js +++ b/test/cases/nested-elements/nested-elements.spec.js @@ -53,7 +53,7 @@ describe('Run WCC For ', function() { it('should have expected content within the