Skip to content

Commit

Permalink
Merge pull request #24 from DominickVale/fix-inline-styles
Browse files Browse the repository at this point in the history
Add support for reloading <style> tags (inline styles)
  • Loading branch information
jakewhiteley committed May 15, 2024
2 parents 60a9960 + bb07c66 commit f52bc0d
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 16 deletions.
2 changes: 1 addition & 1 deletion dist/taxi.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/taxi.esm.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/taxi.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/taxi.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/taxi.modern.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/taxi.modern.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/taxi.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/taxi.umd.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/Core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ export default class Core {
/**
* Load up styles from the target page if needed
*
* @param {HTMLLinkElement[]} cachedStyles
* @param {Array<HTMLLinkElement|HTMLStyleElement>} cachedStyles
*/
loadStyles(cachedStyles: HTMLLinkElement[]): void;
loadStyles(cachedStyles: Array<HTMLLinkElement | HTMLStyleElement>): void;
/**
* @private
* @param {string} links
Expand Down
32 changes: 26 additions & 6 deletions src/Core.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import E from '@unseenco/e'
import { appendScript, parseDom, processUrl, reloadScript } from './helpers'
import { appendScript, parseDom, processUrl, reloadScript, reloadInlineStyle, appendInlineStyle } from './helpers'
import Transition from './Transition'
import Renderer from './Renderer'
import RouteStore from './RouteStore'
Expand Down Expand Up @@ -357,16 +357,36 @@ export default class Core {
/**
* Load up styles from the target page if needed
*
* @param {HTMLLinkElement[]} cachedStyles
* @param {Array<HTMLLinkElement|HTMLStyleElement>} cachedStyles
*/
loadStyles(cachedStyles) {
const currentStyles = Array.from(document.querySelectorAll('link[rel="stylesheet"]')).filter(this.reloadCssFilter)
const currentInlineStyles = Array.from(document.querySelectorAll('style')).filter(this.reloadCssFilter)

cachedStyles.forEach(el => {
if (el.href && !currentStyles.find((link) => link.href === el.href)) {
const newInlineStyles = cachedStyles.filter(el => {
// no el.href, assume it's an inline style
if(!el.href){
return true
} else if(!currentStyles.find((link) => link.href === el.href)) {
document.body.append(el)
}
return false
}
})

// loop through all new inline styles
for (let i = 0; i < currentInlineStyles.length; i++) {
for (let n = 0; n < newInlineStyles.length; n++) {
if (currentInlineStyles[i].outerHTML === newInlineStyles[n].outerHTML) {
reloadInlineStyle(currentInlineStyles[i])
newInlineStyles.splice(n, 1)
break
}
}
}

for (const style of newInlineStyles) {
appendInlineStyle(style)
}
}

/**
Expand Down Expand Up @@ -544,7 +564,7 @@ export default class Core {
finalUrl: url,
skipCache: content.hasAttribute('data-taxi-nocache'),
scripts: this.reloadJsFilter ? Array.from(page.querySelectorAll('script')).filter(this.reloadJsFilter) : [],
styles: this.reloadCssFilter ? Array.from(page.querySelectorAll('link[rel="stylesheet"]')).filter(this.reloadCssFilter) : [],
styles: this.reloadCssFilter ? Array.from(page.querySelectorAll('link[rel="stylesheet"], style')).filter(this.reloadCssFilter) : [],
title: page.title,
renderer: new Renderer({
wrapper: this.wrapper,
Expand Down
16 changes: 16 additions & 0 deletions src/helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,30 @@ export function processUrl(url: string): {
* @param {HTMLElement|HTMLScriptElement} node
*/
export function reloadScript(node: HTMLElement | HTMLScriptElement): void;
/**
* Reloads a provided inline-stylesheet by replacing with itself.
* @param {HTMLStyleElement} node
*/
export function reloadInlineStyle(node: HTMLStyleElement): void;
/**
* Loads a provided script/stylesheet by appending a clone to the current document.
* @param {HTMLElement} node
*/
export function appendScript(node: HTMLElement): void;
/**
* Loads a provided inline-stylesheet by appending a clone to the current document.
* @param {HTMLStyleElement} node
*/
export function appendInlineStyle(node: HTMLStyleElement): void;
/**
* Creates a clone of a given HTMLElement
* @param {HTMLElement} node
* @return {HTMLElement}
*/
export function duplicateScript(node: HTMLElement): HTMLElement;
/**
* Creates a clone of a given HTMLStyleElement
* @param {HTMLStyleElement} node
* @return {HTMLStyleElement}
*/
export function duplicateInlineStyle(node: HTMLStyleElement): HTMLStyleElement;
45 changes: 45 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ export function reloadScript(node) {
node.parentNode.replaceChild(duplicateScript(node), node)
}

/**
* Reloads a provided inline-stylesheet by replacing with itself.
* @param {HTMLStyleElement} node
*/
export function reloadInlineStyle(node) {
node.parentNode.replaceChild(duplicateInlineStyle(node), node)
}

/**
* Loads a provided script/stylesheet by appending a clone to the current document.
* @param {HTMLElement} node
Expand All @@ -51,6 +59,18 @@ export function appendScript(node) {
}
}

/**
* Loads a provided inline-stylesheet by appending a clone to the current document.
* @param {HTMLStyleElement} node
*/
export function appendInlineStyle(node) {
if (node.parentNode.tagName === 'HEAD') {
document.head.appendChild(duplicateInlineStyle(node))
} else {
document.body.appendChild(duplicateInlineStyle(node))
}
}

/**
* Creates a clone of a given HTMLElement
* @param {HTMLElement} node
Expand All @@ -75,3 +95,28 @@ export function duplicateScript(node) {

return replacement
}

/**
* Creates a clone of a given HTMLStyleElement
* @param {HTMLStyleElement} node
* @return {HTMLStyleElement}
*/
export function duplicateInlineStyle(node) {
const replacement = document.createElement('STYLE')

// Loop Over Attributes
for (let k = 0; k < node.attributes.length; k++) {
// Get Attribute
const attr = node.attributes[k]

// Set Attribute
replacement.setAttribute(attr.nodeName, attr.nodeValue)
}

// Inline Script
if (node.innerHTML) {
replacement.innerHTML = node.innerHTML
}

return replacement
}

0 comments on commit f52bc0d

Please sign in to comment.