all files / document-editor/implementation/search/ text-search.js

81.01% Statements 209/258
67.81% Branches 158/233
100% Functions 20/20
81.01% Lines 209/258
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391   146× 146× 146× 146×                         27×           25×                           15× 15× 15× 15× 15× 39× 39× 39× 39× 15×     24×                         39×     39× 14× 14×             14×     25× 25×           23×       15× 15×   15× 15× 15× 15×     15× 15× 15× 15× 26× 26× 26× 26× 15×   26× 15× 15× 12×   15× 15× 15× 15×   26× 15× 15× 10×   15×                         15×           15× 15×       15×   15× 15× 15× 15× 15×     15×   14×     14×                                                                                                                 12× 12×   12× 11× 11× 11×     11× 11× 11× 11×     12×     12× 12× 12× 12× 12× 12× 12× 14×   12× 12×   10× 10×   30× 30× 30× 30×     16× 16× 16× 16×     50×     16×           50×     16×              
define(["require", "exports", "../../base/dictionary", "@syncfusion/ej2-base", "../selection/selection-helper", "../viewer/page", "../../base/index"], function (require, exports, dictionary_1, ej2_base_1, selection_helper_1, page_1, index_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var TextSearch = (function () {
        function TextSearch(owner) {
            this.wordBefore = '\\b';
            this.wordAfter = '\\b';
            this.owner = owner;
            this.documentHelper = this.owner.documentHelper;
        }
        TextSearch.prototype.find = function (pattern, findOption) {
            var result = this.findNext(pattern, findOption, '0;0;0');
            Eif (!ej2_base_1.isNullOrUndefined(result)) {
                var eventArgs = { source: this.documentHelper.owner };
                this.documentHelper.owner.trigger(index_1.searchResultsChangeEvent, eventArgs);
            }
            return result;
        };
        TextSearch.prototype.findNext = function (pattern, findOption, hierarchicalPosition) {
            Eif (typeof pattern === 'string') {
                pattern = this.stringToRegex(pattern, findOption);
            }
            Iif (hierarchicalPosition === undefined) {
                hierarchicalPosition = '0;0;0';
            }
            this.owner.searchModule.textSearchResults.clearResults();
            var results = this.owner.searchModule.textSearchResults;
            this.findDocument(results, pattern, true, findOption, hierarchicalPosition);
            return results.length > 0, results.currentSearchResult;
        };
        TextSearch.prototype.stringToRegex = function (textToFind, option) {
            Iif (textToFind.indexOf('\\') > -1) {
                textToFind = textToFind.split('\\').join('\\\\');
            }
            if (textToFind.indexOf('(') > -1 || textToFind.indexOf(')') > -1 || textToFind.indexOf('.') > -1 || textToFind.indexOf('[') > -1
                || textToFind.indexOf(']') > -1 || textToFind.indexOf('$') > -1 || textToFind.indexOf('{') > -1
                || textToFind.indexOf('}') > -1 || textToFind.indexOf('*') > -1 || textToFind.indexOf('|') > -1
                || textToFind.indexOf('^') > -1 || textToFind.indexOf('?') > -1 || textToFind.indexOf('+') > -1) {
                var text = '';
                for (var i = 0; i < textToFind.length; i++) {
                    if (textToFind[parseInt(i.toString(), 10)] === '(' || textToFind[parseInt(i.toString(), 10)] === ')' || textToFind[parseInt(i.toString(), 10)] === '.' || textToFind[parseInt(i.toString(), 10)] === '['
                        || textToFind[parseInt(i.toString(), 10)] === ']' || textToFind[parseInt(i.toString(), 10)] === '$' || textToFind[parseInt(i.toString(), 10)] === '{' || textToFind[parseInt(i.toString(), 10)] === '}'
                        || textToFind[parseInt(i.toString(), 10)] === '*' || textToFind[parseInt(i.toString(), 10)] === '|' || textToFind[parseInt(i.toString(), 10)] === '^' || textToFind[parseInt(i.toString(), 10)] === '?'
                        || textToFind[parseInt(i.toString(), 10)] === '+') {
                        text += '\\' + textToFind[parseInt(i.toString(), 10)];
                    }
                    else {
                        text += textToFind[parseInt(i.toString(), 10)];
                    }
                }
                textToFind = text;
            }
            Iif (option === 'WholeWord' || option === 'CaseSensitiveWholeWord') {
                textToFind = this.wordBefore + textToFind + this.wordAfter;
            }
            return RegExp(textToFind, (option === 'CaseSensitive' || option === 'CaseSensitiveWholeWord') ? 'g' : 'ig');
        };
        TextSearch.prototype.isPatternEmpty = function (pattern) {
            var wordEmpty = this.wordBefore + this.wordAfter;
            var patternRegExp = pattern.toString();
            return (patternRegExp.length === 0 || patternRegExp === wordEmpty);
        };
        TextSearch.prototype.findAll = function (pattern, findOption, hierarchicalPosition) {
            Eif (typeof pattern === 'string') {
                pattern = this.stringToRegex(pattern, findOption);
            }
            Eif (hierarchicalPosition === undefined) {
                hierarchicalPosition = '0;0;0';
            }
            this.owner.searchModule.textSearchResults.clearResults();
            var results = this.owner.searchModule.textSearchResults;
            this.findDocument(results, pattern, false, findOption, hierarchicalPosition);
            if (results.length > 0 && results.currentIndex < 0) {
                results.currentIndex = 0;
            }
            Eif (!ej2_base_1.isNullOrUndefined(results.currentSearchResult)) {
                var eventArgs = { source: this.documentHelper.owner };
                this.documentHelper.owner.trigger(index_1.searchResultsChangeEvent, eventArgs);
                return results;
            }
            return undefined;
        };
        TextSearch.prototype.getElementInfo = function (inlineElement, indexInInline, includeNextLine, pattern, findOption, isFirstMatch, results, selectionEnd, isSpellCheck) {
            var inlines = inlineElement;
            var stringBuilder = '';
            var spans = new dictionary_1.Dictionary();
            var previousElementCount = 0;
            do {
                Eif (inlineElement instanceof page_1.TextElementBox && (!ej2_base_1.isNullOrUndefined(inlineElement.text) && inlineElement.text !== '')) {
                    spans.add(inlineElement, isSpellCheck ? stringBuilder.length + previousElementCount : stringBuilder.length);
                    previousElementCount = 0;
                    if (inlineElement === inlines) {
                        stringBuilder = stringBuilder + (inlineElement.text.substring(indexInInline));
                    }
                    else {
                        stringBuilder = stringBuilder + (inlineElement.text);
                    }
                }
                else if (inlineElement instanceof page_1.FieldElementBox) {
                    var fieldBegin = inlineElement;
                    if (!ej2_base_1.isNullOrUndefined(fieldBegin.fieldEnd)) {
                        inlineElement = ej2_base_1.isNullOrUndefined(fieldBegin.fieldSeparator) ? fieldBegin.fieldEnd : fieldBegin.fieldSeparator;
                    }
                }
                else if (inlineElement instanceof page_1.ShapeElementBox && !ej2_base_1.isNullOrUndefined(inlineElement.textFrame)
                    && inlineElement.textFrame.childWidgets.length > 0) {
                    this.findInlineText(inlineElement.textFrame, pattern, findOption, isFirstMatch, results, selectionEnd);
                }
                Iif (!(inlineElement instanceof page_1.TextElementBox)) {
                    previousElementCount += inlineElement.length;
                }
                if (!ej2_base_1.isNullOrUndefined(inlineElement) && ej2_base_1.isNullOrUndefined(inlineElement.nextNode)) {
                    var splittedParagraph = inlineElement.paragraph.nextSplitWidget;
                    Iif (!isSpellCheck && !ej2_base_1.isNullOrUndefined(splittedParagraph) && splittedParagraph !== inlineElement.paragraph
                        && splittedParagraph.childWidgets.length > 0 && splittedParagraph.childWidgets[0] instanceof page_1.LineWidget
                        && splittedParagraph.childWidgets[0].children.length > 0) {
                        inlineElement = splittedParagraph.childWidgets[0].children[0];
                        continue;
                    }
                    else {
                        break;
                    }
                }
                Eif (!ej2_base_1.isNullOrUndefined(inlineElement)) {
                    if ((!ej2_base_1.isNullOrUndefined(includeNextLine) && !includeNextLine)) {
                        var elementBoxes = inlineElement.line.children;
                        var length_1 = inlineElement.line.children.length;
                        if (elementBoxes.indexOf(inlineElement) < length_1 - 1) {
                            inlineElement = inlineElement.nextNode;
                        }
                        else {
                            inlineElement = undefined;
                            break;
                        }
                    }
                    else {
                        inlineElement = inlineElement.nextNode;
                    }
                }
            } while (true);
            var text = stringBuilder.toString();
            return { elementsWithOffset: spans, fullText: text };
        };
        TextSearch.prototype.updateMatchedTextLocation = function (matches, results, textInfo, indexInInline, inlines, isFirstMatch, selectionEnd, startPosition) {
            for (var i = 0; i < matches.length; i++) {
                var match = matches[parseInt(i.toString(), 10)];
                var isMatched = void 0;
                Iif (!(ej2_base_1.isNullOrUndefined(startPosition)) && match.index < startPosition) {
                    continue;
                }
                var result = results.addResult();
                var spanKeys = textInfo.keys;
                var isContainField = false;
                for (var j = 0; j < spanKeys.length; j++) {
                    var span = spanKeys[parseInt(j.toString(), 10)];
                    var startIndex = textInfo.get(span);
                    var spanLength = span.length;
                    if (span === inlines) {
                        spanLength -= indexInInline;
                    }
                    if (ej2_base_1.isNullOrUndefined(result.start) && match.index < startIndex + spanLength) {
                        var index = match.index - startIndex;
                        if (span === inlines) {
                            index += indexInInline;
                        }
                        var offset = (span.line).getOffset(span, index);
                        result.start = this.getTextPosition(span.line, offset.toString());
                        result.start.location = this.owner.selectionModule.getPhysicalPositionInternal(span.line, offset, true);
                        result.start.setPositionParagraph(span.line, offset);
                    }
                    if (match.index + match[0].length <= startIndex + spanLength) {
                        var index = (match.index + match[0].length) - startIndex;
                        if (span === inlines) {
                            index += indexInInline;
                        }
                        Iif (!(!ej2_base_1.isNullOrUndefined(this.owner.searchModule.textSearchResults) && this.owner.searchModule.textSearchResults.length !== 0) && span.text.charAt(span.text.length - 1) !== ' ' && !ej2_base_1.isNullOrUndefined(span.nextElement) && (span.nextElement instanceof page_1.BookmarkElementBox || span.nextElement instanceof page_1.CommentCharacterElementBox)) {
                            var element = span.nextElement;
                            while (element) {
                                element = element.nextElement;
                                if (element instanceof page_1.TextElementBox) {
                                    break;
                                }
                            }
                            if (element && element instanceof page_1.TextElementBox && element.text.charAt(0) !== ' ') {
                                isContainField = true;
                                continue;
                            }
                        }
                        Iif (!(!ej2_base_1.isNullOrUndefined(this.owner.searchModule.textSearchResults)
                            && this.owner.searchModule.textSearchResults.length !== 0) && span.previousElement
                            && (span.previousElement instanceof page_1.BookmarkElementBox
                                || span.previousElement instanceof page_1.CommentCharacterElementBox)) {
                            isContainField = true;
                        }
                        var offset = 0;
                        Iif (isContainField) {
                            offset = (span.line).getOffset(span, 0) + span.length;
                        }
                        else {
                            offset = (span.line).getOffset(span, index);
                        }
                        result.end = this.getTextPosition(span.line, offset.toString());
                        result.end.location = this.owner.selectionModule.getPhysicalPositionInternal(span.line, offset, true);
                        result.end.setPositionParagraph(span.line, offset);
                        isMatched = true;
                        break;
                    }
                }
                if (isFirstMatch) {
                    results.currentIndex = 0;
                    break;
                }
                else if (results.currentIndex < 0 && !ej2_base_1.isNullOrUndefined(selectionEnd) && (selectionEnd.isExistBefore(result.start) ||
                    selectionEnd.isAtSamePosition(result.start))) {
                    results.currentIndex = results.indexOf(result);
                }
                if (!ej2_base_1.isNullOrUndefined(startPosition) && isMatched) {
                    break;
                }
            }
        };
        TextSearch.prototype.findDocument = function (results, pattern, isFirstMatch, findOption, hierachicalPosition) {
            Iif (this.isPatternEmpty(pattern)) {
                return;
            }
            Iif (findOption === undefined) {
                findOption = 'None';
            }
            var inline = undefined;
            var selectionEnd = undefined;
            Eif (hierachicalPosition !== undefined) {
                selectionEnd = this.owner.selectionModule.end;
            }
            if (hierachicalPosition !== undefined && isFirstMatch && selectionEnd !== undefined && selectionEnd.paragraph !== undefined) {
                Eif (selectionEnd.paragraph instanceof page_1.ParagraphWidget) {
                    var indexInInline = 0;
                    var inlineElement = selectionEnd.currentWidget.getInline(this.owner.selectionModule.start.offset, indexInInline);
                    inline = inlineElement.element;
                    indexInInline = inlineElement.index;
                    Eif (!ej2_base_1.isNullOrUndefined(inline)) {
                        var nextParagraphWidget = undefined;
                        nextParagraphWidget = this.findInline(inline, pattern, findOption, indexInInline, isFirstMatch, results, selectionEnd);
                        while (results.length === 0 && !ej2_base_1.isNullOrUndefined(nextParagraphWidget)) {
                            while (!ej2_base_1.isNullOrUndefined(nextParagraphWidget) && nextParagraphWidget.childWidgets.length === 0) {
                                nextParagraphWidget = this.owner.selectionModule.getNextParagraph(nextParagraphWidget.containerWidget);
                            }
                            if (ej2_base_1.isNullOrUndefined(nextParagraphWidget)) {
                                break;
                            }
                            var lineWidget = nextParagraphWidget.childWidgets[0];
                            if (lineWidget.children[0] instanceof page_1.ListTextElementBox) {
                                inline = (lineWidget.children[2] instanceof page_1.TextElementBox) ? lineWidget.children[2] : undefined;
                            }
                            else {
                                inline = lineWidget.children[0];
                            }
                            if (ej2_base_1.isNullOrUndefined(inline)) {
                                break;
                            }
                            nextParagraphWidget = this.findInline(inline, pattern, findOption, 0, isFirstMatch, results, selectionEnd);
                        }
                        Iif (results.length > 0) {
                            return;
                        }
                    }
                }
            }
            var section;
            section = this.documentHelper.pages[0].bodyWidgets[0];
            while (!ej2_base_1.isNullOrUndefined(section) && section.childWidgets.length === 0) {
                section = section.nextWidget;
            }
            Iif (ej2_base_1.isNullOrUndefined(section) || section.childWidgets.length === 0) {
                return;
            }
            this.findInlineText(section, pattern, findOption, isFirstMatch, results, selectionEnd);
            var headerFootersColletion = this.documentHelper.headersFooters;
            for (var i = 0; i < headerFootersColletion.length; i++) {
                var headerFooters = headerFootersColletion[parseInt(i.toString(), 10)];
                Eif (headerFooters) {
                    for (var index in headerFooters) {
                        Eif (Object.prototype.hasOwnProperty.call(headerFooters, index)) {
                            var headerFooter = headerFooters[parseInt(index.toString(), 10)];
                            Eif (!ej2_base_1.isNullOrUndefined(headerFooter) && !ej2_base_1.isNullOrUndefined(headerFooter.page)) {
                                this.findInlineText(headerFooter, pattern, findOption, isFirstMatch, results, selectionEnd);
                            }
                        }
                    }
                }
            }
            var endNoteCollection = this.documentHelper.endnoteCollection;
            for (var i = 0; i < endNoteCollection.length; i++) {
                var endNote = endNoteCollection[parseInt(i.toString(), 10)];
                if (endNote) {
                    if (!ej2_base_1.isNullOrUndefined(endNote) && !ej2_base_1.isNullOrUndefined(endNote.bodyWidget.page)) {
                        this.findInlineText(endNote.bodyWidget, pattern, findOption, isFirstMatch, results, selectionEnd);
                    }
                }
            }
            var footNoteCollection = this.documentHelper.footnoteCollection;
            for (var i = 0; i < footNoteCollection.length; i++) {
                var footNote = footNoteCollection[parseInt(i.toString(), 10)];
                if (footNote) {
                    if (!ej2_base_1.isNullOrUndefined(footNote) && !ej2_base_1.isNullOrUndefined(footNote.bodyWidget.page)) {
                        this.findInlineText(footNote.bodyWidget, pattern, findOption, isFirstMatch, results, selectionEnd);
                    }
                }
            }
            if (isFirstMatch && !ej2_base_1.isNullOrUndefined(results) && results.length > 0) {
                return;
            }
        };
        TextSearch.prototype.findInlineText = function (section, pattern, findOption, isFirstMatch, results, selectionEnd) {
            var paragraphWidget = this.owner.documentHelper.getFirstParagraphBlock(section.childWidgets[0]);
            while (!ej2_base_1.isNullOrUndefined(paragraphWidget) && paragraphWidget.childWidgets.length === 1 && paragraphWidget.childWidgets[0].children.length === 0) {
                paragraphWidget = this.owner.selectionModule.getNextParagraphBlock(paragraphWidget);
            }
            while (!ej2_base_1.isNullOrUndefined(paragraphWidget) && paragraphWidget.childWidgets.length > 0) {
                var inlineElement = paragraphWidget.childWidgets[0];
                var inlineEle = inlineElement.children[0];
                Iif (ej2_base_1.isNullOrUndefined(inlineEle)) {
                    break;
                }
                this.findInline(inlineEle, pattern, findOption, 0, isFirstMatch, results, selectionEnd);
                paragraphWidget = this.owner.selectionModule.getNextParagraphBlock(paragraphWidget);
                while (!ej2_base_1.isNullOrUndefined(paragraphWidget) && (((paragraphWidget.childWidgets.length === 1) && paragraphWidget.childWidgets[0].children.length === 0) || !ej2_base_1.isNullOrUndefined(paragraphWidget.previousSplitWidget))) {
                    paragraphWidget = this.owner.selectionModule.getNextParagraphBlock(paragraphWidget);
                }
            }
            if (isFirstMatch && !ej2_base_1.isNullOrUndefined(results) && results.length > 0) {
                return;
            }
        };
        TextSearch.prototype.findInline = function (inlineElement, pattern, option, indexInInline, isFirstMatch, results, selectionEnd) {
            var inlines = inlineElement;
            var textInfo = this.getElementInfo(inlineElement, indexInInline, undefined, pattern, option, isFirstMatch, results, selectionEnd);
            var text = textInfo.fullText;
            var matches = [];
            var spans = textInfo.elementsWithOffset;
            var matchObject;
            while (!ej2_base_1.isNullOrUndefined(matchObject = pattern.exec(text))) {
                matches.push(matchObject);
            }
            this.updateMatchedTextLocation(matches, results, spans, indexInInline, inlines, isFirstMatch, selectionEnd);
            if (isFirstMatch) {
                return undefined;
            }
            var paragraphWidget = this.owner.selectionModule.getNextParagraphBlock(inlineElement.line.paragraph);
            return paragraphWidget;
        };
        TextSearch.prototype.getTextPosition = function (lineWidget, hierarchicalIndex) {
            var textPosition = new selection_helper_1.TextPosition(this.owner);
            var index = textPosition.getHierarchicalIndex(lineWidget, hierarchicalIndex);
            textPosition.setPositionForCurrentIndex(index);
            return textPosition;
        };
        return TextSearch;
    }());
    exports.TextSearch = TextSearch;
    var SearchWidgetInfo = (function () {
        function SearchWidgetInfo(left, width) {
            this.leftInternal = 0;
            this.widthInternal = 0;
            this.leftInternal = left;
            this.widthInternal = width;
        }
        Object.defineProperty(SearchWidgetInfo.prototype, "left", {
            get: function () {
                return this.leftInternal;
            },
            set: function (value) {
                this.leftInternal = value;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(SearchWidgetInfo.prototype, "width", {
            get: function () {
                return this.widthInternal;
            },
            set: function (value) {
                this.widthInternal = value;
            },
            enumerable: true,
            configurable: true
        });
        return SearchWidgetInfo;
    }());
    exports.SearchWidgetInfo = SearchWidgetInfo;
});