| 177 | } |
| 178 | |
| 179 | function replaceCloseCode(str, closeSeq, openSeq, keepClose) { |
| 180 | const closeLen = closeSeq.length; |
| 181 | let index = str.indexOf(closeSeq); |
| 182 | if (index === -1) return str; |
| 183 | |
| 184 | let result = ''; |
| 185 | let lastIndex = 0; |
| 186 | const replacement = keepClose ? closeSeq + openSeq : openSeq; |
| 187 | |
| 188 | do { |
| 189 | const afterClose = index + closeLen; |
| 190 | if (afterClose < str.length) { |
| 191 | result += str.slice(lastIndex, index) + replacement; |
| 192 | lastIndex = afterClose; |
| 193 | } else { |
| 194 | break; |
| 195 | } |
| 196 | index = str.indexOf(closeSeq, lastIndex); |
| 197 | } while (index !== -1); |
| 198 | |
| 199 | return result + str.slice(lastIndex); |
| 200 | } |
| 201 | |
| 202 | // Matches #RGB or #RRGGBB |
| 203 | const hexColorRegExp = /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/; |