Skip to content

Commit

Permalink
detect, use and respond with document or fragement html (#34)
Browse files Browse the repository at this point in the history
* detect, use and respond with document or fragement html

* use parseFragment for component roots
  • Loading branch information
thescientist13 authored May 27, 2022
1 parent 71c6c41 commit eb9eb1d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/wcc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import fs from 'node:fs/promises';

let definitions;

function getParse(html) {
return html.indexOf('<html>') >= 0 || html.indexOf('<body>') || html.indexOf('<head>')
? parse
: parseFragment;
}

async function renderComponentRoots(tree, includeShadowRoots = true) {
for (const node of tree.childNodes) {
if (node.tagName && node.tagName.indexOf('-') > 0) {
Expand Down Expand Up @@ -96,7 +102,7 @@ async function renderToString(elementURL, options = {}) {

const elementInstance = await initializeCustomElement(elementURL);
const elementHtml = elementInstance.getInnerHTML({ includeShadowRoots });
const elementTree = parseFragment(elementHtml);
const elementTree = getParse(elementHtml)(elementHtml);
const finalTree = await renderComponentRoots(elementTree, includeShadowRoots);

return {
Expand All @@ -115,7 +121,7 @@ async function renderFromHTML(html, elements = [], options = {}) {
await initializeCustomElement(url);
}

const elementTree = parse(html);
const elementTree = getParse(html)(html);
const finalTree = await renderComponentRoots(elementTree, includeShadowRoots);

return {
Expand Down
21 changes: 20 additions & 1 deletion test/cases/render-from-html/render-from-html.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import { renderFromHTML } from '../../../src/wcc.js';

const expect = chai.expect;

describe('Run WCC For ', function() {
describe('Run WCC ', function() {
const LABEL = 'Using renderFromHTML';
let rawHtml;
let dom;
let assetMetadata;

Expand All @@ -37,12 +38,30 @@ describe('Run WCC For ', function() {
new URL('./src/components/header.js', import.meta.url)
]);

rawHtml = html;
dom = new JSDOM(html);
assetMetadata = metadata;
});

describe(LABEL, function() {
describe('static page content', function() {
it('should have the expected <html> tag the page', function() {
expect(rawHtml.indexOf('<html>') >= 0).to.equal(true);
});

it('should have the expected <title> tag the page', function() {
expect(dom.window.document.querySelectorAll('html').length).to.equal(1);
expect(dom.window.document.querySelector('title').textContent).to.equal('WCC');
});

it('should have the expected <head> tag the page', function() {
expect(rawHtml.indexOf('<head>') >= 0).to.equal(true);
});

it('should have the expected <body> tag the page', function() {
expect(rawHtml.indexOf('<body>') >= 0).to.equal(true);
});

it('should have the expected static content for the page', function() {
expect(dom.window.document.querySelector('h1').textContent).to.equal('Home Page');
});
Expand Down

0 comments on commit eb9eb1d

Please sign in to comment.