(htmlString, wrapLetterString, wrapWordString)
| 56 | * @returns {string} transformed html string. |
| 57 | */ |
| 58 | export function wrapText (htmlString, wrapLetterString, wrapWordString) { |
| 59 | if (!wrapLetterString) { |
| 60 | wrapLetterString = l => l ? `<span class="letter" style="display: inline-block; opacity:0">${l}</span>` : '' |
| 61 | } |
| 62 | if (!wrapWordString) { |
| 63 | wrapWordString = l => l ? `<span class="word" style="display: inline-block; whites-space: nowrap">${l}</span>` : '' |
| 64 | } |
| 65 | |
| 66 | // Method that replaces text content within an html string. |
| 67 | function replaceHtmlContent (str, match, replaceFn) { |
| 68 | // we use the "g" and "i" flags to make it replace all occurrences and ignore case |
| 69 | const re = new RegExp(match, 'gi') |
| 70 | // this RegExp will match any char sequence that doesn't contain "<" or ">" |
| 71 | // and that is followed by a tag |
| 72 | return str.replace(/([^<>]+)(?=<[^>]+>)/g, function (s, content) { |
| 73 | return content.replace(re, replaceFn) |
| 74 | }) |
| 75 | } |
| 76 | |
| 77 | function wrapLetter (src, match) { |
| 78 | return replaceHtmlContent(src, match, wrapLetterString) |
| 79 | } |
| 80 | |
| 81 | // wrapWord maintains spaces on either side of the word. |
| 82 | function wrapWord (src, match) { |
| 83 | return replaceHtmlContent(src, match, function (str) { |
| 84 | let result = '' |
| 85 | if (str[0] === ' ') { |
| 86 | result += ' ' |
| 87 | } |
| 88 | result += wrapLetter(wrapWordString(str.trim()), /[^ ]/) |
| 89 | if (str.substr(-1) === ' ') { |
| 90 | result += ' ' |
| 91 | } |
| 92 | return result |
| 93 | }) |
| 94 | } |
| 95 | |
| 96 | return wrapWord(htmlString, / ?([^ ])+ ?/) |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Try to guess the frame of the camera and provide sensible defaults for the |
no test coverage detected