Skip to content

Commit

Permalink
feat: use data attr instead of id + prep dist
Browse files Browse the repository at this point in the history
  • Loading branch information
jsip committed Dec 8, 2023
1 parent 466978e commit eee1bd4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
18 changes: 9 additions & 9 deletions dist/turbo.es2017-esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,17 +360,17 @@ function nextMicrotask() {
function parseHTMLDocument(html = "") {
const styleMap = {};
html = html.replace(/<(\w+)([^>]*)style="([^"]*)"/g, (match, tag, otherAttrs, style) => {
const id = Math.random().toString(36).substr(2, 9);
styleMap[id] = style;
return `<${tag}${otherAttrs}id="${id}"`;
const uniqueKey = Math.random().toString(36).substr(2, 9);
styleMap[uniqueKey] = style;
return `<${tag}${otherAttrs.trim()} data-style-attribute="${uniqueKey}"`;
});
const doc = new DOMParser().parseFromString(html, "text/html");
Object.keys(styleMap).forEach(id => {
const element = doc.getElementById(id);
if (element) {
element.style.cssText = styleMap[id];
element.removeAttribute("id");
}
Object.keys(styleMap).forEach(uniqueKey => {
const elements = doc.querySelectorAll(`[data-style-attribute="${uniqueKey}"]`);
elements.forEach(element => {
element.style.cssText = styleMap[uniqueKey];
element.removeAttribute("data-style-attribute");
});
});
return doc;
}
Expand Down
18 changes: 9 additions & 9 deletions dist/turbo.es2017-umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,17 +366,17 @@ Copyright © 2023 37signals LLC
function parseHTMLDocument(html = "") {
const styleMap = {};
html = html.replace(/<(\w+)([^>]*)style="([^"]*)"/g, (match, tag, otherAttrs, style) => {
const id = Math.random().toString(36).substr(2, 9);
styleMap[id] = style;
return `<${tag}${otherAttrs}id="${id}"`;
const uniqueKey = Math.random().toString(36).substr(2, 9);
styleMap[uniqueKey] = style;
return `<${tag}${otherAttrs.trim()} data-style-attribute="${uniqueKey}"`;
});
const doc = new DOMParser().parseFromString(html, "text/html");
Object.keys(styleMap).forEach(id => {
const element = doc.getElementById(id);
if (element) {
element.style.cssText = styleMap[id];
element.removeAttribute("id");
}
Object.keys(styleMap).forEach(uniqueKey => {
const elements = doc.querySelectorAll(`[data-style-attribute="${uniqueKey}"]`);
elements.forEach(element => {
element.style.cssText = styleMap[uniqueKey];
element.removeAttribute("data-style-attribute");
});
});
return doc;
}
Expand Down
26 changes: 15 additions & 11 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,26 @@ export function nextMicrotask() {
return Promise.resolve()
}

export function parseHTMLDocument(html = "") {
export function parseHTMLDocument(html: string = ""): Document {

Check failure on line 69 in src/util.ts

View workflow job for this annotation

GitHub Actions / build

Type string trivially inferred from a string literal, remove type annotation
const styleMap: { [key: string]: string } = {};

Check failure on line 70 in src/util.ts

View workflow job for this annotation

GitHub Actions / build

Delete `;`

// Replace style attribute with data-style-attribute and ensure proper spacing
html = html.replace(/<(\w+)([^>]*)style="([^"]*)"/g, (match, tag, otherAttrs, style) => {
const id = Math.random().toString(36).substr(2, 9);
styleMap[id] = style;
return `<${tag}${otherAttrs}id="${id}"`;
const uniqueKey: string = Math.random().toString(36).substr(2, 9);

Check failure on line 74 in src/util.ts

View workflow job for this annotation

GitHub Actions / build

Delete `;·`
styleMap[uniqueKey] = style;

Check failure on line 75 in src/util.ts

View workflow job for this annotation

GitHub Actions / build

Delete `;`
// Ensure there is a space before data-style-attribute
return `<${tag}${otherAttrs.trim()} data-style-attribute="${uniqueKey}"`;

Check failure on line 77 in src/util.ts

View workflow job for this annotation

GitHub Actions / build

Delete `;`
});

Check failure on line 78 in src/util.ts

View workflow job for this annotation

GitHub Actions / build

Delete `;`

const doc = new DOMParser().parseFromString(html, "text/html");
const doc: Document = new DOMParser().parseFromString(html, "text/html");

Check failure on line 80 in src/util.ts

View workflow job for this annotation

GitHub Actions / build

Delete `;`

Object.keys(styleMap).forEach(id => {
const element = doc.getElementById(id);
if (element) {
element.style.cssText = styleMap[id];
element.removeAttribute("id");
}
// Apply styles and remove data-style-attribute
Object.keys(styleMap).forEach(uniqueKey => {

Check failure on line 83 in src/util.ts

View workflow job for this annotation

GitHub Actions / build

Replace `uniqueKey` with `(uniqueKey)`
const elements = doc.querySelectorAll(`[data-style-attribute="${uniqueKey}"]`);

Check failure on line 84 in src/util.ts

View workflow job for this annotation

GitHub Actions / build

Delete `;`
elements.forEach(element => {

Check failure on line 85 in src/util.ts

View workflow job for this annotation

GitHub Actions / build

Replace `(element` with `((element)`
(element as HTMLElement).style.cssText = styleMap[uniqueKey];
element.removeAttribute("data-style-attribute");
});
});

return doc;
Expand Down

0 comments on commit eee1bd4

Please sign in to comment.