Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const regExtra = /<(\w+)\s*(.*)>([^{>]*)\{([^}]+)\}(.*)/g,
- regAttr = /([^= ]+)=['"]?([^"']+)['"]?/g;
- let body = document.body,
- html = body.innerHTML;
- body.innerHTML = html.replace(regExtra, (s, tag, eAttr, bCont, attr, aCont) => {
- if(['script','style'].indexOf(tag) !== -1) return s;
- let attrMap = {}, newAttr;
- eAttr.replace(regAttr, (a, aKey, aValue) => {
- attrMap[aKey] = aValue;
- });
- normalizeAttributes = (a, aKey, aValue) => {
- const existingValue = attrMap[aKey];
- if(existingValue !== undefined && aKey === 'class') {
- aValue = `${existingValue} ${aValue}`;
- }
- return `${aKey}="${aValue}"`;
- };
- newAttr = attr.split(" ").map(part => {
- return part
- .replace(/^\./, "class=")
- .replace(/^#/, "id=")
- .replace(regAttr, normalizeAttributes);
- });
- return `<${tag} ${newAttr.join(' ')}>${bCont} ${aCont}`;
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement