Skip to content

Commit

Permalink
feature/issue 153 CSSStyleSheet in DOM Shim (#156)
Browse files Browse the repository at this point in the history
* CSSStylesheet DOM Shim

* add test case for constructable stylesheets

* shim an empty adoptedStylesheets property

* document CSSStyleSheet support in DOM shim
  • Loading branch information
thescientist13 authored Apr 13, 2024
1 parent c97da38 commit 25aafdf
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/pages/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ $ npm install wc-compiler --save-dev
- `[get|set|has]Attribute`
1. `<template>` / `DocumentFragment`
1. `addEventListener` (as a no-op)
1. Supports `CSSStyleSheet` (all methods act as no-ops)
1. Recursive rendering of nested custom elements
1. Metadata and runtime hints to support various progressive hydration and lazy loading strategies

Expand Down
12 changes: 11 additions & 1 deletion src/dom-shim.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
function noop() { }

// https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/CSSStyleSheet
class CSSStyleSheet {
insertRule() { }
deleteRule() { }
replace() { }
replaceSync() { }
}

// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
class EventTarget {
constructor() {
Expand Down Expand Up @@ -93,6 +101,7 @@ class ShadowRoot extends DocumentFragment {
constructor(options) {
super();
this.mode = options.mode || 'closed';
this.adoptedStyleSheets = [];
}
}

Expand Down Expand Up @@ -146,4 +155,5 @@ class CustomElementsRegistry {
globalThis.addEventListener = globalThis.addEventListener ?? noop;
globalThis.document = globalThis.document ?? new Document();
globalThis.customElements = globalThis.customElements ?? new CustomElementsRegistry();
globalThis.HTMLElement = globalThis.HTMLElement ?? HTMLElement;
globalThis.HTMLElement = globalThis.HTMLElement ?? HTMLElement;
globalThis.CSSStyleSheet = globalThis.CSSStyleSheet ?? CSSStyleSheet;
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Use Case
* Run wcc against a custom element using constructible stylesheets.
*
* User Result
* Should return the expected HTML and no error instatiating a CSSStyleSheet..
*
* User Workspace
* src/
* components/
* header/
* header.js
* pages/
* index.js
*/
import chai from 'chai';
import { JSDOM } from 'jsdom';
import { renderToString } from '../../../src/wcc.js';

const expect = chai.expect;

describe('Run WCC For ', function() {
const LABEL = 'Constructible Stylesheets usage';
let dom;

before(async function() {
const { html } = await renderToString(new URL('./src/pages/index.js', import.meta.url));

dom = new JSDOM(html);
});

describe(LABEL, function() {
it('should have one top level <wcc-header> element with a <template> with an open shadowroot', function() {
expect(dom.window.document.querySelectorAll('wcc-header template[shadowrootmode="open"]').length).to.equal(1);
expect(dom.window.document.querySelectorAll('template').length).to.equal(1);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const template = document.createElement('template');

template.innerHTML = `
<header>
<h1>Welcome to my website!</h1>
</header>
`;

const sheet = new CSSStyleSheet();
sheet.replaceSync('li{color:red;}');

export default class Header extends HTMLElement {
connectedCallback() {
if (!this.shadowRoot) {
this.attachShadow({ mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}

this.shadowRoot.adoptedStyleSheets = [sheet];
}
}

customElements.define('wcc-header', Header);
10 changes: 10 additions & 0 deletions test/cases/constructable-stylesheet/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import '../components/header/header.js';

export default class HomePage extends HTMLElement {
connectedCallback() {
this.innerHTML = `
<wcc-header></wcc-header>
<h1>Home Page</h1>
`;
}
}
6 changes: 2 additions & 4 deletions test/cases/import-attributes/import-attributes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ describe('Run WCC For ', function() {
});

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

it('should have a <header> tag within the document', function() {
Expand Down

0 comments on commit 25aafdf

Please sign in to comment.