Skip to content

Commit deb69af

Browse files
authored
Inline regex performs persistently faster. Less code.
1 parent e456965 commit deb69af

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

script.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// 🌘 CSS Scope Inline (https://github.com/gnat/css-scope-inline)
22
window.cssScopeCount ??= 1 // Let extra copies share the scope count.
3-
new MutationObserver((mutations, observer) => {
4-
var cssScopePattern = new RegExp('\.(me|this|self)(?![A-Za-z0-9\_\-])', 'g') // Can use: .me .this .self
3+
new MutationObserver(mutations => {
54
for (var mutation of mutations) {
65
if (mutation.type !== "childList") continue // Skip if not mutating nodes.
76
var nodes = [...mutation.addedNodes] // Get new nodes.
@@ -12,7 +11,7 @@ new MutationObserver((mutations, observer) => {
1211
if (node.textContent.includes('.self__')) continue // Skip if already processed.
1312
var scope = 'self__'+(window.cssScopeCount++) // Ready. Make unique scope, example: .self__1234
1413
node.parentNode.classList.add(scope)
15-
node.textContent = node.textContent.replace(cssScopePattern, '.'+scope)
14+
node.textContent = node.textContent.replace(/\.(me|this|self)(?![A-Za-z0-9\_\-])/g, '.'+scope) // Can use: .me .this .self
1615
}
1716
}
1817
}).observe(document.documentElement, {childList: true, subtree: true})

0 commit comments

Comments
 (0)