Created
February 3, 2018 22:00
-
-
Save gyng/914734779b8330e2b33772916f99a72a to your computer and use it in GitHub Desktop.
Simulates text-combine-upright: digits 4 (tate-chu-yoko) in Firefox
This file contains 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
.num { | |
text-combine-upright: all; | |
} |
This file contains 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
// Wraps all numbers in a <span class="num"> | |
function tateChuYokoify() { | |
function walkText(node) { | |
const regex = /([\d0-9]{1,4})/g; | |
let matches = []; | |
if (node.nodeType == 3) { | |
while (match = regex.exec(node.data)) { | |
match.index > 0 ? node.splitText(match.index) : node.splitText(match.index + match[0].length); | |
const replacementNode = document.createElement("span"); | |
replacementNode.textContent = node.data; | |
if (replacementNode.textContent.match(/^[\d0-9]{1,4}$/)) { | |
replacementNode.className = "num"; | |
} | |
node.replaceWith(replacementNode); | |
} | |
} | |
if (node.nodeType == 1 && node.nodeName != "SCRIPT") { | |
for (var i = 0; i < node.childNodes.length; i += 1) { | |
walkText(node.childNodes[i]); | |
} | |
} | |
} | |
walkText(document.body); | |
} | |
tateChuYokoify(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment