Skip to content

Instantly share code, notes, and snippets.

@tillda
Last active August 29, 2015 14:05
Show Gist options
  • Save tillda/b458d3a528926923ee56 to your computer and use it in GitHub Desktop.
Save tillda/b458d3a528926923ee56 to your computer and use it in GitHub Desktop.
var textNormalizer = (function() {
function hasFontSize(dom) {
if (!dom) {
return false;
}
if (!dom.style) {
return false;
}
var fs = dom.style.fontSize;
return (fs != "inherit") && !!dom.style.fontSize;
}
function getFontSize(dom) {
if ((!dom) || (!dom.style) || (!dom.style.fontSize)) {
return null;
}
var parsedSize = parseFloat(dom.style.fontSize.replace("px",""), 10);
if (isNaN(parsedSize)) {
return null;
}
return parsedSize;
}
function normalize(domRoot) {
var removedOnAll = true, presentOnAll = true;
var ps = $(domRoot).children("p");
var firstP = null;
var domP;
var bElHasFontSize;
for (var j=0; j<ps.length; j++) {
domP = ps[j];
var originalParagraphFontSize = getFontSize(domP);
var fontSizeRequired, el, fontSizeForced = 0, fontSize;
// Remove unnecessary font sizes
fontSizeRequired = false;
var relevantFontSize;
for (var i=0; i<domP.childNodes.length; i++) {
el = domP.childNodes[i];
relevantFontSize = null;
bElHasFontSize = hasFontSize(el);
if ((el.nodeType === 3) && (el.textContent) && (el.textContent.trim())) {
fontSizeRequired = true;
relevantFontSize = getFontSize(el);
} else if ((el.nodeType === 1) && (!bElHasFontSize)) {
fontSizeRequired = true;
relevantFontSize = originalParagraphFontSize;
} else if ((el.nodeType === 1) && (bElHasFontSize)) {
fontSizeRequired = true;
relevantFontSize = getFontSize(el);
}
if (relevantFontSize) {
fontSizeForced = Math.max(relevantFontSize, fontSizeForced);
}
if (bElHasFontSize) {
$(el).addClass("font-sized");
}
}
if (originalParagraphFontSize) {
$(domP).attr("data-legacyfontsize", originalParagraphFontSize.toString());
}
if (!fontSizeRequired) {
domP.style.fontSize = "";
} else {
removedOnAll = false;
}
if (fontSizeForced) {
domP.style.fontSize = fontSizeForced + "px";
}
if (!hasFontSize(domP)) {
presentOnAll = false;
}
// Add &nbsp; to empty paragraphs
if (!domP.textContent.trim()) {
($(domP).children().get(0) || {}).innerHTML = "&nbsp;"
}
}
// Top margin hack - font size is on (first) paragraph
var domFirstP = ps.get(0);
var paragraphFontSize;
if (domFirstP) {
paragraphFontSize = getFontSize(ps.get(0));
if (paragraphFontSize) {
ps.get(0).style.marginTop = (paragraphFontSize/5) + "px";
$(domRoot).attr("data-mainfontsize", Math.round(paragraphFontSize));
}
}
var rootFontSize;
// Top margin hack - font size is on domRoot paragraph
if ((!paragraphFontSize) && (rootFontSize = getFontSize(domRoot))) {
if (ps.size() > 0) {
ps.get(0).style.marginTop = (rootFontSize/5) + "px";
} else {
domRoot.style.marginTop = (rootFontSize/5) + "px";
}
}
if (rootFontSize = getFontSize(domRoot)) {
$(domRoot).attr("data-legacyfontsize", rootFontSize);
}
if ((removedOnAll || presentOnAll) && (ps.size())) {
domRoot.style.fontSize = "";
}
$(domRoot).addClass("text-markup__root");
}
return {
normalize : function(domRoot) {
try {
normalize(domRoot);
} catch(e) {
window.console && window.console.error(e);
}
}
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment