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

bug/issue 165 no premature double custom element instantiation in renderFromHTML #166

Merged
merged 1 commit into from
Oct 18, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/wcc.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ async function renderFromHTML(html, elements = []) {
const definitions = [];

for (const url of elements) {
await initializeCustomElement(url, undefined, undefined, definitions, true);
registerDependencies(url, definitions, 1);
}

const elementTree = getParse(html)(html);
Expand Down
9 changes: 5 additions & 4 deletions test/cases/render-from-html/render-from-html.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const expect = chai.expect;

describe('Run WCC ', function() {
const LABEL = 'Using renderFromHTML';
const TITLE = 'Welcome to my site';
let rawHtml;
let dom;
let assetMetadata;
Expand All @@ -30,7 +31,7 @@ describe('Run WCC ', function() {
<title>WCC</title>
</head>
<body>
<wcc-header></wcc-header>
<wcc-html-header title="${TITLE}"></wcc-html-header>
<h1>Home Page</h1>
</body>
</html>
Expand Down Expand Up @@ -71,7 +72,7 @@ describe('Run WCC ', function() {
let headerContentsDom;

before(function() {
headerContentsDom = new JSDOM(dom.window.document.querySelectorAll('wcc-header template[shadowrootmode="open"]')[0].innerHTML);
headerContentsDom = new JSDOM(dom.window.document.querySelectorAll('wcc-html-header template[shadowrootmode="open"]')[0].innerHTML);
});

it('should have a <header> tag within the <template> shadowroot', function() {
Expand All @@ -81,7 +82,7 @@ describe('Run WCC ', function() {
it('should have expected content within the <header> tag', function() {
const content = headerContentsDom.window.document.querySelector('header a h4').textContent;

expect(content).to.contain('My Personal Blog');
expect(content).to.contain(TITLE);
});

describe('nested navigation element', function() {
Expand Down Expand Up @@ -110,7 +111,7 @@ describe('Run WCC ', function() {

it('should have the correct attributes for each asset', function() {
Object.entries(assetMetadata).forEach((asset) => {
const isEntry = asset[0] === 'wcc-header';
const isEntry = asset[0] === 'wcc-html-header';

expect(asset[0]).to.not.be.undefined;
expect(asset[1].instanceName).to.not.be.undefined;
Expand Down
52 changes: 27 additions & 25 deletions test/cases/render-from-html/src/components/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@ import './navigation.js';

const template = document.createElement('template');

template.innerHTML = `
<header class="header">
<div class="head-wrap">
<div class="brand">
<a href="/">
<img src="/www/assets/greenwood-logo.jpg" alt="Greenwood logo"/>
<h4>My Personal Blog</h4>
</a>
</div>

<wcc-navigation></wcc-navigation>

<div class="social">
<a href="https://github.com/ProjectEvergreen/greenwood">
<img
src="https://img.shields.io/github/stars/ProjectEvergreen/greenwood.svg?style=social&logo=github&label=github"
alt="Greenwood GitHub badge"
class="github-badge"/>
</a>
</div>
</div>
</header>
`;

class Header extends HTMLElement {
connectedCallback() {
const title = this.getAttribute('title');

if (!this.shadowRoot) {
template.innerHTML = `
<header class="header">
<div class="head-wrap">
<div class="brand">
<a href="/">
<img src="/www/assets/greenwood-logo.jpg" alt="Greenwood logo"/>
<h4>${title}</h4>
</a>
</div>

<wcc-navigation></wcc-navigation>

<div class="social">
<a href="https://github.com/ProjectEvergreen/greenwood">
<img
src="https://img.shields.io/github/stars/ProjectEvergreen/greenwood.svg?style=social&logo=github&label=github"
alt="Greenwood GitHub badge"
class="github-badge"/>
</a>
</div>
</div>
</header>
`;

this.attachShadow({ mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
Expand All @@ -37,4 +39,4 @@ class Header extends HTMLElement {

export default Header;

customElements.define('wcc-header', Header);
customElements.define('wcc-html-header', Header);
Loading