| 'use strict'; |
| Object.defineProperty(exports, "__esModule", { value: true }); |
| exports.LineBreaker = exports.inlineBreakOpportunities = exports.lineBreakAtIndex = exports.codePointsToCharacterClasses = exports.UnicodeTrie = exports.BREAK_ALLOWED = exports.BREAK_NOT_ALLOWED = exports.BREAK_MANDATORY = exports.classes = exports.LETTER_NUMBER_MODIFIER = void 0; |
| var utrie_1 = require("utrie"); |
| var linebreak_trie_1 = require("./linebreak-trie"); |
| var Util_1 = require("./Util"); |
| exports.LETTER_NUMBER_MODIFIER = 50; |
| |
| var BK = 1; |
| var CR = 2; |
| var LF = 3; |
| var CM = 4; |
| var NL = 5; |
| var SG = 6; |
| var WJ = 7; |
| var ZW = 8; |
| var GL = 9; |
| var SP = 10; |
| var ZWJ = 11; |
| |
| var B2 = 12; |
| var BA = 13; |
| var BB = 14; |
| var HY = 15; |
| var CB = 16; |
| |
| var CL = 17; |
| var CP = 18; |
| var EX = 19; |
| var IN = 20; |
| var NS = 21; |
| var OP = 22; |
| var QU = 23; |
| |
| var IS = 24; |
| var NU = 25; |
| var PO = 26; |
| var PR = 27; |
| var SY = 28; |
| |
| var AI = 29; |
| var AL = 30; |
| var CJ = 31; |
| var EB = 32; |
| var EM = 33; |
| var H2 = 34; |
| var H3 = 35; |
| var HL = 36; |
| var ID = 37; |
| var JL = 38; |
| var JV = 39; |
| var JT = 40; |
| var RI = 41; |
| var SA = 42; |
| var XX = 43; |
| var ea_OP = [0x2329, 0xff08]; |
| exports.classes = { |
| BK: BK, |
| CR: CR, |
| LF: LF, |
| CM: CM, |
| NL: NL, |
| SG: SG, |
| WJ: WJ, |
| ZW: ZW, |
| GL: GL, |
| SP: SP, |
| ZWJ: ZWJ, |
| B2: B2, |
| BA: BA, |
| BB: BB, |
| HY: HY, |
| CB: CB, |
| CL: CL, |
| CP: CP, |
| EX: EX, |
| IN: IN, |
| NS: NS, |
| OP: OP, |
| QU: QU, |
| IS: IS, |
| NU: NU, |
| PO: PO, |
| PR: PR, |
| SY: SY, |
| AI: AI, |
| AL: AL, |
| CJ: CJ, |
| EB: EB, |
| EM: EM, |
| H2: H2, |
| H3: H3, |
| HL: HL, |
| ID: ID, |
| JL: JL, |
| JV: JV, |
| JT: JT, |
| RI: RI, |
| SA: SA, |
| XX: XX, |
| }; |
| exports.BREAK_MANDATORY = '!'; |
| exports.BREAK_NOT_ALLOWED = '×'; |
| exports.BREAK_ALLOWED = '÷'; |
| exports.UnicodeTrie = utrie_1.createTrieFromBase64(linebreak_trie_1.base64, linebreak_trie_1.byteLength); |
| var ALPHABETICS = [AL, HL]; |
| var HARD_LINE_BREAKS = [BK, CR, LF, NL]; |
| var SPACE = [SP, ZW]; |
| var PREFIX_POSTFIX = [PR, PO]; |
| var LINE_BREAKS = HARD_LINE_BREAKS.concat(SPACE); |
| var KOREAN_SYLLABLE_BLOCK = [JL, JV, JT, H2, H3]; |
| var HYPHEN = [HY, BA]; |
| var codePointsToCharacterClasses = function (codePoints, lineBreak) { |
| if (lineBreak === void 0) { lineBreak = 'strict'; } |
| var types = []; |
| var indices = []; |
| var categories = []; |
| codePoints.forEach(function (codePoint, index) { |
| var classType = exports.UnicodeTrie.get(codePoint); |
| if (classType > exports.LETTER_NUMBER_MODIFIER) { |
| categories.push(true); |
| classType -= exports.LETTER_NUMBER_MODIFIER; |
| } |
| else { |
| categories.push(false); |
| } |
| if (['normal', 'auto', 'loose'].indexOf(lineBreak) !== -1) { |
| |
| if ([0x2010, 0x2013, 0x301c, 0x30a0].indexOf(codePoint) !== -1) { |
| indices.push(index); |
| return types.push(CB); |
| } |
| } |
| if (classType === CM || classType === ZWJ) { |
| |
| if (index === 0) { |
| indices.push(index); |
| return types.push(AL); |
| } |
| |
| |
| var prev = types[index - 1]; |
| if (LINE_BREAKS.indexOf(prev) === -1) { |
| indices.push(indices[index - 1]); |
| return types.push(prev); |
| } |
| indices.push(index); |
| return types.push(AL); |
| } |
| indices.push(index); |
| if (classType === CJ) { |
| return types.push(lineBreak === 'strict' ? NS : ID); |
| } |
| if (classType === SA) { |
| return types.push(AL); |
| } |
| if (classType === AI) { |
| return types.push(AL); |
| } |
| |
| |
| |
| if (classType === XX) { |
| if ((codePoint >= 0x20000 && codePoint <= 0x2fffd) || (codePoint >= 0x30000 && codePoint <= 0x3fffd)) { |
| return types.push(ID); |
| } |
| else { |
| return types.push(AL); |
| } |
| } |
| types.push(classType); |
| }); |
| return [indices, types, categories]; |
| }; |
| exports.codePointsToCharacterClasses = codePointsToCharacterClasses; |
| var isAdjacentWithSpaceIgnored = function (a, b, currentIndex, classTypes) { |
| var current = classTypes[currentIndex]; |
| if (Array.isArray(a) ? a.indexOf(current) !== -1 : a === current) { |
| var i = currentIndex; |
| while (i <= classTypes.length) { |
| i++; |
| var next = classTypes[i]; |
| if (next === b) { |
| return true; |
| } |
| if (next !== SP) { |
| break; |
| } |
| } |
| } |
| if (current === SP) { |
| var i = currentIndex; |
| while (i > 0) { |
| i--; |
| var prev = classTypes[i]; |
| if (Array.isArray(a) ? a.indexOf(prev) !== -1 : a === prev) { |
| var n = currentIndex; |
| while (n <= classTypes.length) { |
| n++; |
| var next = classTypes[n]; |
| if (next === b) { |
| return true; |
| } |
| if (next !== SP) { |
| break; |
| } |
| } |
| } |
| if (prev !== SP) { |
| break; |
| } |
| } |
| } |
| return false; |
| }; |
| var previousNonSpaceClassType = function (currentIndex, classTypes) { |
| var i = currentIndex; |
| while (i >= 0) { |
| var type = classTypes[i]; |
| if (type === SP) { |
| i--; |
| } |
| else { |
| return type; |
| } |
| } |
| return 0; |
| }; |
| var _lineBreakAtIndex = function (codePoints, classTypes, indicies, index, forbiddenBreaks) { |
| if (indicies[index] === 0) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| var currentIndex = index - 1; |
| if (Array.isArray(forbiddenBreaks) && forbiddenBreaks[currentIndex] === true) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| var beforeIndex = currentIndex - 1; |
| var afterIndex = currentIndex + 1; |
| var current = classTypes[currentIndex]; |
| |
| |
| var before = beforeIndex >= 0 ? classTypes[beforeIndex] : 0; |
| var next = classTypes[afterIndex]; |
| if (current === CR && next === LF) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| if (HARD_LINE_BREAKS.indexOf(current) !== -1) { |
| return exports.BREAK_MANDATORY; |
| } |
| |
| if (HARD_LINE_BREAKS.indexOf(next) !== -1) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if (SPACE.indexOf(next) !== -1) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if (previousNonSpaceClassType(currentIndex, classTypes) === ZW) { |
| return exports.BREAK_ALLOWED; |
| } |
| |
| if (exports.UnicodeTrie.get(codePoints[currentIndex]) === ZWJ) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if ((current === EB || current === EM) && exports.UnicodeTrie.get(codePoints[afterIndex]) === ZWJ) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if (current === WJ || next === WJ) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if (current === GL) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if ([SP, BA, HY].indexOf(current) === -1 && next === GL) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if ([CL, CP, EX, IS, SY].indexOf(next) !== -1) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if (previousNonSpaceClassType(currentIndex, classTypes) === OP) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if (isAdjacentWithSpaceIgnored(QU, OP, currentIndex, classTypes)) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if (isAdjacentWithSpaceIgnored([CL, CP], NS, currentIndex, classTypes)) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if (isAdjacentWithSpaceIgnored(B2, B2, currentIndex, classTypes)) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if (current === SP) { |
| return exports.BREAK_ALLOWED; |
| } |
| |
| if (current === QU || next === QU) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if (next === CB || current === CB) { |
| return exports.BREAK_ALLOWED; |
| } |
| |
| if ([BA, HY, NS].indexOf(next) !== -1 || current === BB) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if (before === HL && HYPHEN.indexOf(current) !== -1) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if (current === SY && next === HL) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if (next === IN) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if ((ALPHABETICS.indexOf(next) !== -1 && current === NU) || (ALPHABETICS.indexOf(current) !== -1 && next === NU)) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if ((current === PR && [ID, EB, EM].indexOf(next) !== -1) || |
| ([ID, EB, EM].indexOf(current) !== -1 && next === PO)) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if ((ALPHABETICS.indexOf(current) !== -1 && PREFIX_POSTFIX.indexOf(next) !== -1) || |
| (PREFIX_POSTFIX.indexOf(current) !== -1 && ALPHABETICS.indexOf(next) !== -1)) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if ( |
| |
| ([PR, PO].indexOf(current) !== -1 && |
| (next === NU || ([OP, HY].indexOf(next) !== -1 && classTypes[afterIndex + 1] === NU))) || |
| |
| ([OP, HY].indexOf(current) !== -1 && next === NU) || |
| |
| (current === NU && [NU, SY, IS].indexOf(next) !== -1)) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if ([NU, SY, IS, CL, CP].indexOf(next) !== -1) { |
| var prevIndex = currentIndex; |
| while (prevIndex >= 0) { |
| var type = classTypes[prevIndex]; |
| if (type === NU) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| else if ([SY, IS].indexOf(type) !== -1) { |
| prevIndex--; |
| } |
| else { |
| break; |
| } |
| } |
| } |
| |
| if ([PR, PO].indexOf(next) !== -1) { |
| var prevIndex = [CL, CP].indexOf(current) !== -1 ? beforeIndex : currentIndex; |
| while (prevIndex >= 0) { |
| var type = classTypes[prevIndex]; |
| if (type === NU) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| else if ([SY, IS].indexOf(type) !== -1) { |
| prevIndex--; |
| } |
| else { |
| break; |
| } |
| } |
| } |
| |
| if ((JL === current && [JL, JV, H2, H3].indexOf(next) !== -1) || |
| ([JV, H2].indexOf(current) !== -1 && [JV, JT].indexOf(next) !== -1) || |
| ([JT, H3].indexOf(current) !== -1 && next === JT)) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if ((KOREAN_SYLLABLE_BLOCK.indexOf(current) !== -1 && [IN, PO].indexOf(next) !== -1) || |
| (KOREAN_SYLLABLE_BLOCK.indexOf(next) !== -1 && current === PR)) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if (ALPHABETICS.indexOf(current) !== -1 && ALPHABETICS.indexOf(next) !== -1) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if (current === IS && ALPHABETICS.indexOf(next) !== -1) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if ((ALPHABETICS.concat(NU).indexOf(current) !== -1 && |
| next === OP && |
| ea_OP.indexOf(codePoints[afterIndex]) === -1) || |
| (ALPHABETICS.concat(NU).indexOf(next) !== -1 && current === CP)) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| |
| if (current === RI && next === RI) { |
| var i = indicies[currentIndex]; |
| var count = 1; |
| while (i > 0) { |
| i--; |
| if (classTypes[i] === RI) { |
| count++; |
| } |
| else { |
| break; |
| } |
| } |
| if (count % 2 !== 0) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| } |
| |
| if (current === EB && next === EM) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| return exports.BREAK_ALLOWED; |
| }; |
| var lineBreakAtIndex = function (codePoints, index) { |
| |
| if (index === 0) { |
| return exports.BREAK_NOT_ALLOWED; |
| } |
| |
| if (index >= codePoints.length) { |
| return exports.BREAK_MANDATORY; |
| } |
| var _a = exports.codePointsToCharacterClasses(codePoints), indices = _a[0], classTypes = _a[1]; |
| return _lineBreakAtIndex(codePoints, classTypes, indices, index); |
| }; |
| exports.lineBreakAtIndex = lineBreakAtIndex; |
| var cssFormattedClasses = function (codePoints, options) { |
| if (!options) { |
| options = { lineBreak: 'normal', wordBreak: 'normal' }; |
| } |
| var _a = exports.codePointsToCharacterClasses(codePoints, options.lineBreak), indicies = _a[0], classTypes = _a[1], isLetterNumber = _a[2]; |
| if (options.wordBreak === 'break-all' || options.wordBreak === 'break-word') { |
| classTypes = classTypes.map(function (type) { return ([NU, AL, SA].indexOf(type) !== -1 ? ID : type); }); |
| } |
| var forbiddenBreakpoints = options.wordBreak === 'keep-all' |
| ? isLetterNumber.map(function (letterNumber, i) { |
| return letterNumber && codePoints[i] >= 0x4e00 && codePoints[i] <= 0x9fff; |
| }) |
| : undefined; |
| return [indicies, classTypes, forbiddenBreakpoints]; |
| }; |
| var inlineBreakOpportunities = function (str, options) { |
| var codePoints = Util_1.toCodePoints(str); |
| var output = exports.BREAK_NOT_ALLOWED; |
| var _a = cssFormattedClasses(codePoints, options), indicies = _a[0], classTypes = _a[1], forbiddenBreakpoints = _a[2]; |
| codePoints.forEach(function (codePoint, i) { |
| output += |
| Util_1.fromCodePoint(codePoint) + |
| (i >= codePoints.length - 1 |
| ? exports.BREAK_MANDATORY |
| : _lineBreakAtIndex(codePoints, classTypes, indicies, i + 1, forbiddenBreakpoints)); |
| }); |
| return output; |
| }; |
| exports.inlineBreakOpportunities = inlineBreakOpportunities; |
| var Break = (function () { |
| function Break(codePoints, lineBreak, start, end) { |
| this.codePoints = codePoints; |
| this.required = lineBreak === exports.BREAK_MANDATORY; |
| this.start = start; |
| this.end = end; |
| } |
| Break.prototype.slice = function () { |
| return Util_1.fromCodePoint.apply(void 0, this.codePoints.slice(this.start, this.end)); |
| }; |
| return Break; |
| }()); |
| var LineBreaker = function (str, options) { |
| var codePoints = Util_1.toCodePoints(str); |
| var _a = cssFormattedClasses(codePoints, options), indicies = _a[0], classTypes = _a[1], forbiddenBreakpoints = _a[2]; |
| var length = codePoints.length; |
| var lastEnd = 0; |
| var nextIndex = 0; |
| return { |
| next: function () { |
| if (nextIndex >= length) { |
| return { done: true, value: null }; |
| } |
| var lineBreak = exports.BREAK_NOT_ALLOWED; |
| while (nextIndex < length && |
| (lineBreak = _lineBreakAtIndex(codePoints, classTypes, indicies, ++nextIndex, forbiddenBreakpoints)) === |
| exports.BREAK_NOT_ALLOWED) { } |
| if (lineBreak !== exports.BREAK_NOT_ALLOWED || nextIndex === length) { |
| var value = new Break(codePoints, lineBreak, lastEnd, nextIndex); |
| lastEnd = nextIndex; |
| return { value: value, done: false }; |
| } |
| return { done: true, value: null }; |
| }, |
| }; |
| }; |
| exports.LineBreaker = LineBreaker; |
| |