Branchwell

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