/* No orphans: never leave one word alone on the last line of a paragraph, card or heading. Joins the last two words of each block of text with a non-breaking space, on load and for text added later. Pairs with no-orphans.css (text-wrap: pretty / balance). Public domain (CC0). */ (function () { if (window.__noOrphans) return; window.__noOrphans = 1; var SKIP = /^(SCRIPT|STYLE|PRE|CODE|TEXTAREA|INPUT|SELECT|OPTION|SVG|MATH|KBD|SAMP|NOSCRIPT|TEMPLATE|IFRAME|CANVAS|TITLE)$/; var NB = ' '; var WS = /[ \t\n\r]/; function skipped(el) { for (var n = el; n && n.nodeType === 1; n = n.parentNode) { if (SKIP.test(n.nodeName.toUpperCase()) || n.isContentEditable || n.hasAttribute('data-orphans-ok')) return true; } return false; } // The box a run of text wraps inside: the nearest ancestor that is not an inline element. function blockOf(node, cache) { for (var el = node.parentNode; el && el.nodeType === 1; el = el.parentNode) { var d = cache.get(el); if (d === undefined) { d = getComputedStyle(el).display; cache.set(el, d); } if (d !== 'inline' && d !== 'contents') return el; } return null; } function fix(block, nodes) { var text = ''; for (var i = 0; i < nodes.length; i++) text += nodes[i].nodeValue; var words = text.trim().split(/\s+/); if (words.length < 3) return; var last = words[words.length - 1], prev = words[words.length - 2]; if (last.indexOf(NB) >= 0 || last.length + prev.length > 30) return; var seenWord = false; for (i = nodes.length - 1; i >= 0; i--) { var s = nodes[i].nodeValue; for (var j = s.length - 1; j >= 0; j--) { var c = s.charAt(j); if (c === NB) { if (seenWord) return; continue; } if (WS.test(c)) { if (!seenWord) continue; var k = j; while (k > 0 && WS.test(s.charAt(k - 1))) k--; var node = nodes[i], before = s; node.nodeValue = s.slice(0, k) + NB + s.slice(j + 1); // A long pair could push past a narrow box; put the space back if it does. if (last.length + prev.length > 14) { var de = document.documentElement; if (block.scrollWidth > block.clientWidth + 1 || de.scrollWidth > de.clientWidth + 1) node.nodeValue = before; } return; } seenWord = true; } } } function run(root) { if (!root || root.nodeType !== 1 || skipped(root)) return; var cache = new Map(), blocks = [], groups = new Map(); var w = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, { acceptNode: function (t) { return t.nodeValue.trim() && !skipped(t.parentNode) ? 1 : 3; } }); for (var t = w.nextNode(); t; t = w.nextNode()) { var b = blockOf(t, cache); if (!b) continue; var g = groups.get(b); if (!g) { g = []; groups.set(b, g); blocks.push(b); } g.push(t); } for (var i = 0; i < blocks.length; i++) { var blk = blocks[i]; // When only part of a block was handed in, read the whole block. if (!root.contains(blk) || blk === root.parentNode) continue; fix(blk, groups.get(blk)); } } var queued = [], timer = 0; function flush() { timer = 0; var q = queued; queued = []; var cache = new Map(), seen = new Set(); for (var i = 0; i < q.length; i++) { var n = q[i]; if (!n || !n.isConnected) continue; // Re-run from the text's own block so a new word at the end of a paragraph is handled. var b = n.nodeType === 3 ? blockOf(n, cache) : (n.nodeType === 1 ? (getComputedStyle(n).display === 'inline' ? blockOf(n, cache) : n) : null); if (b && !seen.has(b)) { seen.add(b); run(b); } } } function start() { run(document.body); if (!window.MutationObserver) return; new MutationObserver(function (muts) { for (var i = 0; i < muts.length; i++) { var m = muts[i]; if (m.type === 'characterData') queued.push(m.target); for (var j = 0; j < m.addedNodes.length; j++) queued.push(m.addedNodes[j]); } if (queued.length && !timer) timer = setTimeout(flush, 60); }).observe(document.body, { childList: true, subtree: true, characterData: true }); } // Wait for the page (and any app framework) to finish its first render. if (document.readyState === 'complete') setTimeout(start, 0); else window.addEventListener('load', function () { setTimeout(start, 0); }); })();