(runs: TextRun[])
| 102 | } |
| 103 | |
| 104 | function optimizeRuns(runs: TextRun[]): TextRun[] { |
| 105 | applyStickySpace(runs) |
| 106 | |
| 107 | const result: TextRun[] = [] |
| 108 | |
| 109 | const cleanedRuns = runs.map((run) => { |
| 110 | if (/^[\s\u200B-\u200D\uFEFF]*$/.test(run.text)) { |
| 111 | const newAttrs = { ...run.attrs } |
| 112 | delete newAttrs['color'] |
| 113 | delete newAttrs['text-decoration-color'] |
| 114 | delete newAttrs['text-decoration-line'] |
| 115 | delete newAttrs['background-color'] |
| 116 | return { ...run, attrs: newAttrs } |
| 117 | } |
| 118 | return run |
| 119 | }) |
| 120 | |
| 121 | for (const run of cleanedRuns) { |
| 122 | if (result.length === 0) { |
| 123 | result.push(run) |
| 124 | continue |
| 125 | } |
| 126 | |
| 127 | const prev = result[result.length - 1] |
| 128 | const isWhitespace = /^[\s\u200B-\u200D\uFEFF]*$/.test(run.text) |
| 129 | |
| 130 | if (isWhitespace) { |
| 131 | if (prev.marks.size !== run.marks.size) { |
| 132 | result.push(run) |
| 133 | continue |
| 134 | } |
| 135 | let marksMatch = true |
| 136 | for (const m of prev.marks) { |
| 137 | if (!run.marks.has(m)) { |
| 138 | marksMatch = false |
| 139 | break |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (marksMatch && prev.link === run.link) { |
| 144 | prev.text += run.text |
| 145 | continue |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (prev.marks.size !== run.marks.size) { |
| 150 | result.push(run) |
| 151 | continue |
| 152 | } |
| 153 | |
| 154 | let marksMatch = true |
| 155 | for (const m of prev.marks) { |
| 156 | if (!run.marks.has(m)) { |
| 157 | marksMatch = false |
| 158 | break |
| 159 | } |
| 160 | } |
| 161 | if (!marksMatch) { |
no test coverage detected