Skip to content

Commit

Permalink
Merge pull request #61 from shgysk8zer0/patch/dom-and-bytes
Browse files Browse the repository at this point in the history
Several new polyfills
  • Loading branch information
shgysk8zer0 authored Jul 14, 2024
2 parents a47ec5f + 4e35623 commit ce6ae50
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ logs
*.min.js
*.min.js.map
.bak
.DS_STORE
npm-debug.log*
yarn-debug.log*
yarn-error.log*
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v0.3.11] - 2024-07-14

### Added
- Add polyfills for `setHTMLUnsafe` & `parseHTMLUnsafe` methods
- Add polyfills for `bytes` method in `Blob`, `Request`, and `Response`

## [v0.3.10] - 2024-04-10

### Added
Expand Down
4 changes: 3 additions & 1 deletion Document.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { polyfillGetterSetter } from './utils.js';
import { polyfillGetterSetter, polyfillMethod } from './utils.js';
import { adoptedStyleSheets } from './assets/adoptedStylesheets.js';
import { parseHTMLUnsafe } from './methods/dom.js';

polyfillGetterSetter(Document.prototype, 'adoptedStyleSheets', adoptedStyleSheets);
polyfillMethod(Document, 'parseHTMLUnsafe', parseHTMLUnsafe);
19 changes: 18 additions & 1 deletion ShadowRoot.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { polyfillGetterSetter } from './utils.js';
import { polyfillGetterSetter, polyfillMethod } from './utils.js';
import { adoptedStyleSheets } from './assets/adoptedStylesheets.js';
import { setHTMLUnsafe } from './methods/dom.js';

if ('ShadowRoot' in globalThis) {
polyfillGetterSetter(ShadowRoot.prototype, 'adoptedStyleSheets', adoptedStyleSheets);
polyfillMethod(ShadowRoot.prototype, 'setHTMLUnsafe', setHTMLUnsafe);

polyfillMethod(ShadowRoot.prototype, 'getHTML', function ({ shadowRoots, serializableShadowRoots = false } = {}) {
const clone = this.cloneNode(true);

if (serializableShadowRoots) {
//
}

if (Array.isArray(shadowRoots)) {
//
}

return clone.innerHTML;
});

}
2 changes: 2 additions & 0 deletions all.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import './weakMap.js';
import './window.js';
import './scheduler.js';
import './elementInternals.js';
import './blob.js';
import './request.js';
import './response.js';
import './Record.js';
import './Tuple.js';
Expand Down
5 changes: 5 additions & 0 deletions blob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { polyfillMethod } from './utils.js';

polyfillMethod(Blob.prototype, 'bytes', async function () {
return new Uint8Array(await this.arrayBuffer());
});
5 changes: 4 additions & 1 deletion element.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { aria } from './aom.js';
import { polyfillGetterSetter } from './utils.js';
import { polyfillGetterSetter, polyfillMethod } from './utils.js';
import { setHTMLUnsafe } from './methods/dom.js';
import './sanitizer.js';

polyfillMethod(Element.prototype, 'setHTMLUnsafe', setHTMLUnsafe);

function handlePopover({ currentTarget }) {
switch(currentTarget.popoverTargetAction) {
case 'show':
Expand Down
39 changes: 39 additions & 0 deletions methods/dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function attachShadow(template){
if (template instanceof HTMLTemplateElement && template.parentElement instanceof HTMLElement) {
const shadow = template.parentElement.attachShadow({
mode: template.getAttribute('shadowrootmode'),
clonable: template.hasAttribute('shadowrootclonable'),
delegatesFocus: template.hasAttribute('shadowrootdelegatesfocus'),
serializable: template.hasAttribute('shadowrootserializable'),
});

shadow.append(template.content);
template.remove();
}
}

export function parseHTMLUnsafe(input){
const parser = new DOMParser();
// Ensures `URL` is "about:blank"
const doc = document.implementation.createHTMLDocument();
// Rely on this method's TrustedTypes implementation, if available
const parsed = parser.parseFromString(input, 'text/html');
doc.head.append(...parsed.head.childNodes);
doc.body.append(...parsed.body.childNodes);
doc.querySelectorAll('template[shadowrootmode]').forEach(attachShadow);
return doc;
}

export function setHTMLUnsafe(input) {
const parser = new DOMParser();
// Rely on this method's TrustedTypes implementation, if available
const parsed = parser.parseFromString(input, 'text/html');
const frag = document.createDocumentFragment();
frag.append(...parsed.body.childNodes);
attachShadow(frag.querySelector('template[shadowroootmode]'));
this.replaceChildren(frag);
}

export function getHTML() {
return this.innerHTML;
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shgysk8zer0/polyfills",
"version": "0.3.10",
"version": "0.3.11",
"private": false,
"type": "module",
"description": "A collection of JavaScript polyfills",
Expand Down
5 changes: 5 additions & 0 deletions request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { polyfillMethod } from './utils.js';

polyfillMethod(Request.prototype, 'bytes', async function () {
return new Uint8Array(await this.arrayBuffer());
});
4 changes: 4 additions & 0 deletions response.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ polyfillMethod(Response, 'redirect', (url, status = 302) => {
headers: new Headers({ Location: url }),
});
});

polyfillMethod(Response.prototype, 'bytes', async function() {
return new Uint8Array(await this.arrayBuffer());
});
10 changes: 4 additions & 6 deletions test/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ class TestEl extends HTMLElement {
this.#shadow = this.attachShadow({ mode: 'open' });
this.#internals = this.attachInternals();

const tmp = Document.parseHTML(`<div id="container">
<h2 id="title">Lorem Ipsum</h2>
<p id="content"><slot name="content">Eum doloribus esse voluptate. Iste neque eum itaque harum non qui cumque id. Laborum officiis voluptatem at sed et repellendus molestiae et. Cum dolor doloribus reiciendis. Quisquam veniam cum officia ex reprehenderit voluptatem sequi id.</slot></p>
</div>`);

new CSSStyleSheet({ media: '(min-width: 800px)' }).replace(`
:host {
display: inline-block;
Expand All @@ -47,7 +42,10 @@ class TestEl extends HTMLElement {
}
`).then(sheet => {
this.#shadow.adoptedStyleSheets = [sheet];
this.#shadow.append(...tmp.body.children);
this.#shadow.setHTML(`<div id="container">
<h2 id="title">Lorem Ipsum</h2>
<p id="content"><slot name="content">Eum doloribus esse voluptate. Iste neque eum itaque harum non qui cumque id. Laborum officiis voluptatem at sed et repellendus molestiae et. Cum dolor doloribus reiciendis. Quisquam veniam cum officia ex reprehenderit voluptatem sequi id.</slot></p>
</div>`);
});
}
}
Expand Down

0 comments on commit ce6ae50

Please sign in to comment.