Skip to content

Instantly share code, notes, and snippets.

@devonwaldon
Last active May 11, 2018 18:31
Show Gist options
  • Save devonwaldon/0defc300631f73dadc7ed678a44758ae to your computer and use it in GitHub Desktop.
Save devonwaldon/0defc300631f73dadc7ed678a44758ae to your computer and use it in GitHub Desktop.
Fix the case of a string throughout the DOM to deal with CSS 'transform: uppercase'.
function getNodesThatContain(text) {
var textNodes = $('#page').find(':not(iframe, script)')
.contents().filter(
function () {
return (this.nodeType == 3 && this.textContent.indexOf(text) > -1);
});
return textNodes.parent();
}
function forceTextNodeCase(proper_text) {
var nodes = getNodesThatContain(proper_text).each(function () {
this.childNodes.forEach(function (node) {
if (node.nodeType === 3) { // text node found, do the replacement
var wrappers = node.data.split(proper_text);
var index = 1;
wrappers.forEach(function (textOnly) {
node.parentNode.insertBefore(document.createTextNode(textOnly), node);
if (index !== wrappers.length) {
var temp = document.createElement('span');
temp.setAttribute('style', 'text-transform:none!important;');
temp.innerHTML = proper_text;
node.parentNode.insertBefore(temp, node);
}
index ++;
});
node.parentNode.removeChild(node);
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment