Last active
April 26, 2024 14:08
-
-
Save developit/54f3e3d1ce9ed0e5a171044edcd0784f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!Element.prototype.getInnerHTML) { | |
Element.prototype.getInnerHTML = function(opts) { | |
var html = this.innerHTML; | |
if (!opts || !opts.includeShadowRoots) return html; | |
var m = new (self.WeakMap || Map)(); | |
for (var c of (opts.closedRoots || [])) m.set(c.host, c); | |
var p = []; | |
function walk(node) { | |
var c, shadow = node.shadowRoot || m.get(node); | |
if (shadow) p.push(node.innerHTML, `<template shadowroot="${shadow.mode}">${shadow.innerHTML}</template>`); | |
var c = node.firstElementChild; | |
while (c) { walk(c); c = c.nextElementSibling; } | |
} | |
walk(this); | |
var out = '', c = 0, i = 0, o; | |
for (; c<p.length; c+=2) { | |
o = html.indexOf(p[c], i); | |
if (o < 0) continue; | |
out += html.substring(i, o) + p[c+1]; | |
i = o; | |
} | |
return out + html.substring(i); | |
}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would you mind including a license with this? I'd like to include a modified version of it (with attribution) in an MIT licensed tool. Thanks!