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

feature/issue 153 CSSStyleSheet in DOM Shim #156

Merged
merged 4 commits into from
Apr 13, 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
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 All @@ -9,7 +17,7 @@

// https://developer.mozilla.org/en-US/docs/Web/API/Node
// EventTarget <- Node
// TODO should be an interface?

Check warning on line 20 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO should be an interface?'

Check warning on line 20 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected ' TODO' comment: 'TODO should be an interface?'

Check warning on line 20 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO should be an interface?'

Check warning on line 20 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected ' TODO' comment: 'TODO should be an interface?'

Check warning on line 20 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO should be an interface?'

Check warning on line 20 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO should be an interface?'

Check warning on line 20 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO should be an interface?'

Check warning on line 20 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO should be an interface?'

Check warning on line 20 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO should be an interface?'

Check warning on line 20 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO should be an interface?'

Check warning on line 20 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO should be an interface?'

Check warning on line 20 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected ' TODO' comment: 'TODO should be an interface?'

Check warning on line 20 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO should be an interface?'

Check warning on line 20 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO should be an interface?'

Check warning on line 20 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO should be an interface?'

Check warning on line 20 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected ' TODO' comment: 'TODO should be an interface?'
class Node extends EventTarget {
// eslint-disable-next-line
cloneNode(deep) {
Expand Down Expand Up @@ -93,6 +101,7 @@
constructor(options) {
super();
this.mode = options.mode || 'closed';
this.adoptedStyleSheets = [];
}
}

Expand All @@ -104,7 +113,7 @@
this.content = new DocumentFragment();
}

// TODO open vs closed shadow root

Check warning on line 116 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO open vs closed shadow root'

Check warning on line 116 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected ' TODO' comment: 'TODO open vs closed shadow root'

Check warning on line 116 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO open vs closed shadow root'

Check warning on line 116 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected ' TODO' comment: 'TODO open vs closed shadow root'

Check warning on line 116 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO open vs closed shadow root'

Check warning on line 116 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO open vs closed shadow root'

Check warning on line 116 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO open vs closed shadow root'

Check warning on line 116 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO open vs closed shadow root'

Check warning on line 116 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO open vs closed shadow root'

Check warning on line 116 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO open vs closed shadow root'

Check warning on line 116 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO open vs closed shadow root'

Check warning on line 116 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected ' TODO' comment: 'TODO open vs closed shadow root'

Check warning on line 116 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO open vs closed shadow root'

Check warning on line 116 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO open vs closed shadow root'

Check warning on line 116 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO open vs closed shadow root'

Check warning on line 116 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected ' TODO' comment: 'TODO open vs closed shadow root'
set innerHTML(html) {
if (this.content) {
this.content.innerHTML = `
Expand All @@ -123,13 +132,13 @@
// https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry
class CustomElementsRegistry {
constructor() {
// TODO this should probably be a set or otherwise follow the spec?

Check warning on line 135 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO this should probably be a set or...'

Check warning on line 135 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected ' TODO' comment: 'TODO this should probably be a set or...'

Check warning on line 135 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO this should probably be a set or...'

Check warning on line 135 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected ' TODO' comment: 'TODO this should probably be a set or...'

Check warning on line 135 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO this should probably be a set or...'

Check warning on line 135 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO this should probably be a set or...'

Check warning on line 135 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO this should probably be a set or...'

Check warning on line 135 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO this should probably be a set or...'

Check warning on line 135 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO this should probably be a set or...'

Check warning on line 135 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO this should probably be a set or...'

Check warning on line 135 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO this should probably be a set or...'

Check warning on line 135 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected ' TODO' comment: 'TODO this should probably be a set or...'

Check warning on line 135 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO this should probably be a set or...'

Check warning on line 135 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO this should probably be a set or...'

Check warning on line 135 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO this should probably be a set or...'

Check warning on line 135 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected ' TODO' comment: 'TODO this should probably be a set or...'
// https://github.com/ProjectEvergreen/wcc/discussions/145
this.customElementsRegistry = new Map();
}

define(tagName, BaseClass) {
// TODO this should probably fail as per the spec...

Check warning on line 141 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO this should probably fail as per...'

Check warning on line 141 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected ' TODO' comment: 'TODO this should probably fail as per...'

Check warning on line 141 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO this should probably fail as per...'

Check warning on line 141 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected ' TODO' comment: 'TODO this should probably fail as per...'

Check warning on line 141 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO this should probably fail as per...'

Check warning on line 141 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO this should probably fail as per...'

Check warning on line 141 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO this should probably fail as per...'

Check warning on line 141 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO this should probably fail as per...'

Check warning on line 141 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO this should probably fail as per...'

Check warning on line 141 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO this should probably fail as per...'

Check warning on line 141 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO this should probably fail as per...'

Check warning on line 141 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected ' TODO' comment: 'TODO this should probably fail as per...'

Check warning on line 141 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO this should probably fail as per...'

Check warning on line 141 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO this should probably fail as per...'

Check warning on line 141 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO this should probably fail as per...'

Check warning on line 141 in src/dom-shim.js

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected ' TODO' comment: 'TODO this should probably fail as per...'
// e.g. if(this.customElementsRegistry.get(tagName))
// https://github.com/ProjectEvergreen/wcc/discussions/145
this.customElementsRegistry.set(tagName, BaseClass);
Expand All @@ -146,4 +155,5 @@
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
Loading