Branchwell / source / static / no-orphans.html
| 1 | <!-- no-orphans --><style id="no-orphans">h1,h2,h3,h4,h5,h6,summary,legend,caption,blockquote{text-wrap:balance}p,li,dd,dt,figcaption,td,th,label,small{text-wrap:pretty}</style><script id="no-orphans-js">(function () { |
| 2 | if (window.__noOrphans) return; |
| 3 | window.__noOrphans = 1; |
| 4 | var SKIP = /^(SCRIPT|STYLE|PRE|CODE|TEXTAREA|INPUT|SELECT|OPTION|SVG|MATH|KBD|SAMP|NOSCRIPT|TEMPLATE|IFRAME|CANVAS|TITLE)$/; |
| 5 | var NB = ' '; |
| 6 | var WS = /[ \t\n\r]/; |
| 7 | function skipped(el) { |
| 8 | for (var n = el; n && n.nodeType === 1; n = n.parentNode) { |
| 9 | if (SKIP.test(n.nodeName.toUpperCase()) || n.isContentEditable || n.hasAttribute('data-orphans-ok')) return true; |
| 10 | } |
| 11 | return false; |
| 12 | } |
| 13 | function blockOf(node, cache) { |
| 14 | for (var el = node.parentNode; el && el.nodeType === 1; el = el.parentNode) { |
| 15 | var d = cache.get(el); |
| 16 | if (d === undefined) { d = getComputedStyle(el).display; cache.set(el, d); } |
| 17 | if (d !== 'inline' && d !== 'contents') return el; |
| 18 | } |
| 19 | return null; |
| 20 | } |
| 21 | function fix(block, nodes) { |
| 22 | var text = ''; |
| 23 | for (var i = 0; i < nodes.length; i++) text += nodes[i].nodeValue; |
| 24 | var words = text.trim().split(/\s+/); |
| 25 | if (words.length < 3) return; |
| 26 | var last = words[words.length - 1], prev = words[words.length - 2]; |
| 27 | if (last.indexOf(NB) >= 0 || last.length + prev.length > 30) return; |
| 28 | var seenWord = false; |
| 29 | for (i = nodes.length - 1; i >= 0; i--) { |
| 30 | var s = nodes[i].nodeValue; |
| 31 | for (var j = s.length - 1; j >= 0; j--) { |
| 32 | var c = s.charAt(j); |
| 33 | if (c === NB) { if (seenWord) return; continue; } |
| 34 | if (WS.test(c)) { |
| 35 | if (!seenWord) continue; |
| 36 | var k = j; |
| 37 | while (k > 0 && WS.test(s.charAt(k - 1))) k--; |
| 38 | var node = nodes[i], before = s; |
| 39 | node.nodeValue = s.slice(0, k) + NB + s.slice(j + 1); |
| 40 | if (last.length + prev.length > 14) { |
| 41 | var de = document.documentElement; |
| 42 | if (block.scrollWidth > block.clientWidth + 1 || de.scrollWidth > de.clientWidth + 1) node.nodeValue = before; |
| 43 | } |
| 44 | return; |
| 45 | } |
| 46 | seenWord = true; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | function run(root) { |
| 51 | if (!root || root.nodeType !== 1 || skipped(root)) return; |
| 52 | var cache = new Map(), blocks = [], groups = new Map(); |
| 53 | var w = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, { |
| 54 | acceptNode: function (t) { return t.nodeValue.trim() && !skipped(t.parentNode) ? 1 : 3; } |
| 55 | }); |
| 56 | for (var t = w.nextNode(); t; t = w.nextNode()) { |
| 57 | var b = blockOf(t, cache); |
| 58 | if (!b) continue; |
| 59 | var g = groups.get(b); |
| 60 | if (!g) { g = []; groups.set(b, g); blocks.push(b); } |
| 61 | g.push(t); |
| 62 | } |
| 63 | for (var i = 0; i < blocks.length; i++) { |
| 64 | var blk = blocks[i]; |
| 65 | if (!root.contains(blk) || blk === root.parentNode) continue; |
| 66 | fix(blk, groups.get(blk)); |
| 67 | } |
| 68 | } |
| 69 | var queued = [], timer = 0; |
| 70 | function flush() { |
| 71 | timer = 0; |
| 72 | var q = queued; queued = []; |
| 73 | var cache = new Map(), seen = new Set(); |
| 74 | for (var i = 0; i < q.length; i++) { |
| 75 | var n = q[i]; |
| 76 | if (!n || !n.isConnected) continue; |
| 77 | var b = n.nodeType === 3 ? blockOf(n, cache) : (n.nodeType === 1 ? (getComputedStyle(n).display === 'inline' ? blockOf(n, cache) : n) : null); |
| 78 | if (b && !seen.has(b)) { seen.add(b); run(b); } |
| 79 | } |
| 80 | } |
| 81 | function start() { |
| 82 | run(document.body); |
| 83 | if (!window.MutationObserver) return; |
| 84 | new MutationObserver(function (muts) { |
| 85 | for (var i = 0; i < muts.length; i++) { |
| 86 | var m = muts[i]; |
| 87 | if (m.type === 'characterData') queued.push(m.target); |
| 88 | for (var j = 0; j < m.addedNodes.length; j++) queued.push(m.addedNodes[j]); |
| 89 | } |
| 90 | if (queued.length && !timer) timer = setTimeout(flush, 60); |
| 91 | }).observe(document.body, { childList: true, subtree: true, characterData: true }); |
| 92 | } |
| 93 | if (document.readyState === 'complete') setTimeout(start, 0); |
| 94 | else window.addEventListener('load', function () { setTimeout(start, 0); }); |
| 95 | })();</script><!-- /no-orphans --> |