|
8 | 8 | // Re-checked whenever a value's size changes (window resize, font load). |
9 | 9 | function updateFits(value) { |
10 | 10 | var wrap = value.parentElement; |
11 | | - if (wrap.classList.contains("is-expanded")) { |
| 11 | + if ( |
| 12 | + wrap.classList.contains("is-expanded") || |
| 13 | + wrap.classList.contains("is-rich") |
| 14 | + ) { |
12 | 15 | return; |
13 | 16 | } |
14 | 17 | wrap.classList.toggle("fits", value.scrollHeight <= value.clientHeight + 1); |
15 | 18 | } |
| 19 | + |
| 20 | + // A long value is clamped because it is a lot of text. If a plugin has |
| 21 | + // replaced that text with something else - a map, a chart, an image - then |
| 22 | + // its length no longer says anything about how much room it needs, so |
| 23 | + // mark it as rich and let CSS show all of it without the toggle. Elements |
| 24 | + // that are just marked-up text, such as links, do not count. |
| 25 | + var TEXT_ELEMENTS = |
| 26 | + "a, abbr, b, br, code, em, i, kbd, mark, small, span, strong, sub, sup, wbr"; |
| 27 | + function updateRich(value) { |
| 28 | + var rich = Array.prototype.some.call( |
| 29 | + value.querySelectorAll("*"), |
| 30 | + function (el) { |
| 31 | + return !el.matches(TEXT_ELEMENTS); |
| 32 | + }, |
| 33 | + ); |
| 34 | + value.parentElement.classList.toggle("is-rich", rich); |
| 35 | + if (!rich) { |
| 36 | + updateFits(value); |
| 37 | + } |
| 38 | + } |
| 39 | + |
16 | 40 | var longValues = document.querySelectorAll( |
17 | 41 | ".row-field-long > .row-field-value", |
18 | 42 | ); |
| 43 | + longValues.forEach(updateRich); |
| 44 | + // Plugin JavaScript usually runs after this script, so keep watching |
| 45 | + if (window.MutationObserver) { |
| 46 | + var richObserver = new MutationObserver(function (mutations) { |
| 47 | + var seen = []; |
| 48 | + mutations.forEach(function (mutation) { |
| 49 | + // childList mutations always have an element as their target |
| 50 | + var value = mutation.target.closest( |
| 51 | + ".row-field-long > .row-field-value", |
| 52 | + ); |
| 53 | + if (value && seen.indexOf(value) === -1) { |
| 54 | + seen.push(value); |
| 55 | + updateRich(value); |
| 56 | + } |
| 57 | + }); |
| 58 | + }); |
| 59 | + longValues.forEach(function (value) { |
| 60 | + richObserver.observe(value, { childList: true, subtree: true }); |
| 61 | + }); |
| 62 | + } |
19 | 63 | if (window.ResizeObserver) { |
20 | 64 | var observer = new ResizeObserver(function (entries) { |
21 | 65 | entries.forEach(function (entry) { |
|
0 commit comments