Skip to content

Instantly share code, notes, and snippets.

@gyng
Created February 3, 2018 22:00
Show Gist options
  • Save gyng/914734779b8330e2b33772916f99a72a to your computer and use it in GitHub Desktop.
Save gyng/914734779b8330e2b33772916f99a72a to your computer and use it in GitHub Desktop.
Simulates text-combine-upright: digits 4 (tate-chu-yoko) in Firefox
.num {
text-combine-upright: all;
}
// 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