|
863 | 863 | return issues; |
864 | 864 | } |
865 | 865 |
|
| 866 | + // Korean puts spaces between words, yet under the default `word-break: normal` |
| 867 | + // Chrome may end a line between any two Hangul syllables, or between a Hangul |
| 868 | + // particle and the Latin word or number it is attached to ("HyperFrames로"), |
| 869 | + // so a caption word wraps in half. `word-break: keep-all` keeps them together. |
| 870 | + // Hangul Jamo, Compatibility Jamo, Jamo Extended-A, Syllables, Jamo Extended-B. |
| 871 | + const HANGUL_CHAR = /[ᄀ-ᇿ-ꥠ-가-ힰ-]/; |
| 872 | + const WORD_CHAR = /[\p{L}\p{M}\p{N}]/u; |
| 873 | + const WORD_SPLIT_SCAN_LIMIT = 2000; |
| 874 | + |
| 875 | + function joinsKoreanWord(left, right) { |
| 876 | + return ( |
| 877 | + WORD_CHAR.test(left) && |
| 878 | + WORD_CHAR.test(right) && |
| 879 | + (HANGUL_CHAR.test(left) || HANGUL_CHAR.test(right)) |
| 880 | + ); |
| 881 | + } |
| 882 | + |
| 883 | + function charRect(range, node, index) { |
| 884 | + range.setStart(node, index); |
| 885 | + range.setEnd(node, index + 1); |
| 886 | + const rect = range.getBoundingClientRect(); |
| 887 | + return rect.width > 0.5 && rect.height > 0.5 ? rect : null; |
| 888 | + } |
| 889 | + |
| 890 | + // Line boxes stack vertically: two adjacent characters whose centres sit more |
| 891 | + // than half a glyph apart are on different lines. A font-fallback fragment on |
| 892 | + // the same line shifts by a few px; a small caption tilt barely moves it. |
| 893 | + function onLaterLine(before, after) { |
| 894 | + const centre = (rect) => rect.top + rect.height / 2; |
| 895 | + return Math.abs(centre(after) - centre(before)) > Math.max(before.height, after.height) / 2; |
| 896 | + } |
| 897 | + |
| 898 | + function wordAround(text, index) { |
| 899 | + let start = index; |
| 900 | + while (start > 0 && WORD_CHAR.test(text[start - 1])) start--; |
| 901 | + let end = index; |
| 902 | + while (end < text.length && WORD_CHAR.test(text[end])) end++; |
| 903 | + return `${text.slice(start, index)}|${text.slice(index, end)}`; |
| 904 | + } |
| 905 | + |
| 906 | + /** Where a line break first cuts a Korean word in this text node ("줄바꿈되는|지"), or null. */ |
| 907 | + function splitKoreanWord(node) { |
| 908 | + const text = (node.textContent || "").slice(0, WORD_SPLIT_SCAN_LIMIT); |
| 909 | + if (!HANGUL_CHAR.test(text)) return null; |
| 910 | + const range = document.createRange(); |
| 911 | + try { |
| 912 | + range.selectNodeContents(node); |
| 913 | + // One client rect per line box: a node on a single line cannot split a word. |
| 914 | + const lines = Array.from(range.getClientRects()).filter( |
| 915 | + (rect) => rect.width > 0.5 && rect.height > 0.5, |
| 916 | + ); |
| 917 | + if (lines.length < 2) return null; |
| 918 | + let previous = null; |
| 919 | + for (let index = 1; index < text.length; index++) { |
| 920 | + if (!joinsKoreanWord(text[index - 1], text[index])) continue; |
| 921 | + const before = |
| 922 | + previous && previous.index === index - 1 |
| 923 | + ? previous.rect |
| 924 | + : charRect(range, node, index - 1); |
| 925 | + const after = charRect(range, node, index); |
| 926 | + previous = { index, rect: after }; |
| 927 | + if (before && after && onLaterLine(before, after)) return wordAround(text, index); |
| 928 | + } |
| 929 | + return null; |
| 930 | + } finally { |
| 931 | + range.detach(); |
| 932 | + } |
| 933 | + } |
| 934 | + |
| 935 | + function koreanWordSplitIssue(element, time) { |
| 936 | + for (const node of directTextNodes(element)) { |
| 937 | + const split = splitKoreanWord(node); |
| 938 | + if (!split) continue; |
| 939 | + const keepAll = getComputedStyle(element).wordBreak === "keep-all"; |
| 940 | + return { |
| 941 | + code: "text_word_split", |
| 942 | + severity: "warning", |
| 943 | + time, |
| 944 | + selector: uniqueSelectorFor(element), |
| 945 | + text: textContentFor(element, true), |
| 946 | + message: `A line break cuts a Korean word in two ("${split}").`, |
| 947 | + rect: |
| 948 | + unionRects(textClientRects(element, true).map(toRect)) || |
| 949 | + toRect(element.getBoundingClientRect()), |
| 950 | + fixHint: keepAll |
| 951 | + ? "The word is wider than its line even with word-break: keep-all: widen the box or lower the font size." |
| 952 | + : "Set word-break: keep-all on the text or a parent so Korean lines break only at spaces.", |
| 953 | + }; |
| 954 | + } |
| 955 | + return null; |
| 956 | + } |
| 957 | + |
866 | 958 | function isNowrapTextChild(child) { |
867 | 959 | if (!isVisibleElement(child) || hasAllowOverflowFlag(child)) return false; |
868 | 960 | if (getComputedStyle(child).whiteSpace !== "nowrap") return false; |
|
2872 | 2964 | const clipped = clippedTextIssue(element, time, tolerance); |
2873 | 2965 | if (clipped) issues.push(clipped); |
2874 | 2966 | issues.push(...textOverflowIssues(element, root, rootRect, time, tolerance, clipped)); |
| 2967 | + const split = koreanWordSplitIssue(element, time); |
| 2968 | + if (split) issues.push(split); |
2875 | 2969 | const occluded = occludedTextIssue(element, time, proseCoverageFloor, matteResults); |
2876 | 2970 | if (occluded) { |
2877 | 2971 | const lifetime = clipLifetime(element); |
|
0 commit comments