All files / src/pwk/content leaf_node.js

86.64% Statements 344/397
79.21% Branches 141/178
90.38% Functions 47/52
86.84% Lines 330/380

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201                                                1x         1x 1x 1x 1x 1x 1x 1x           1x                   3317x           3317x             3317x             3317x             3317x         2863x 2863x         2878x   2878x 2878x                 421x                 4786x         4786x 45x 45x 45x       4786x     4786x 4786x   4786x 4675x 4675x     4675x 19969x   19969x 4368x   15601x 15601x         4675x 4675x 4675x 203x     111x 111x       4786x 45x                     4791x 4791x   4791x                 137779x                 48999x                         48720x                                         560x                                                                                 9x 9x       9x 1x 1x   1x 1x         8x 8x 8x 8x 8x 8x     8x 26x 26x   26x   26x 48x 48x 48x   48x           48x                 48x         8x 8056x       8056x       8056x                       8x         8x                       2076x 2076x   2076x 2076x 23646x 5500x 5500x   5500x     2075x     16x 16x   2059x 2059x             2076x                       20x 20x 20x 20x 20x               20x 54x 54x 54x   54x   20x 34x 16x   18x       20x                     652x 652x   652x 651x       652x                       682x 682x 682x 682x 682x   682x 1213x 1213x         682x   37x     682x 682x 682x 682x 682x   682x                           21x   21x         21x 17x   6x 6x     11x 11x 11x 11x 11x   11x     78x   10x 68x 29x   39x     11x 4x       4x 2x   2x     7x         11x     4x     21x                     4x 4x   4x 26x 3x 23x 13x   10x     4x 3x 3x             1x                                                                                                                               11x     11x 5x 5x 5x 5x     11x                       9x 9x 9x     9x 4x 4x 4x 4x     9x                   5x 5x 5x                   6x 6x 6x   6x                       31x 31x 31x 31x       31x 117x 31x 31x   86x 86x       31x                     32x 32x     32x 32x                   11x 11x     11x 11x                     28x 28x     28x 28x                             13x 13x   13x 61x 61x       13x 13x                                 25x     25x 25x                               11x     11x 11x                   24x 24x   24x       24x 23x   23x         23x 14x 14x 14x       23x     23x 14x                                           22x 22x     22x 22x 22x                       650x   650x 650x 650x 650x 650x     650x 1661x   1661x   211x     1661x 563x 1098x 87x 1011x 87x   924x       650x             4033x 650x 650x 650x   650x 326x 326x     326x 1337x 1337x       650x     4033x 211x 211x 211x                   20x   20x                     9x 9x   9x 9x 9x 9x     9x                 172x                                             1x 1x 1x 1x 1x                 249x                     1770x 1770x 1770x 1770x                     1x                     3x 3x 3x 3x   3x 3x 3x 3x 3x                         1x 1x 1x   1x 1x 1x 1x 1x                                                                           2675x 2675x   2675x 1741x 1741x 1741x 1741x   934x 934x     2675x   2675x                                                       1x  
/**
 * @fileoverview Leaf node represent a content element on the document and could
 * not contains children node.
 * @author Dmytro Antonenko <dmitry.antonenko@pubwebkit.com>
 */
import {APPLICABLE_AREA, BaseNodeStyle} from '../style/node/base_node_style.js';
import {BranchNode} from './branch_node.js';
import {ContentRenderingReadyEvent} from './content_rendering_ready_event.js';
import {Content} from './content.js';
import {DEFAULT_INDENT_STEP} from '../style/node/indent_node_style.js';
import {DEFAULT_WIDTH_VALUE} from '../style/node/width_node_style.js';
import {DIRECTIONS, GRANULARITY} from '../base.js';
import {LeafNodeModel, NodeModel, NodeStylesModel} from '../typedefs.js';
import {Line} from './line.js';
import {NodeOffsetInfo} from '../range/node_offset_info.js';
import {NodeRange} from '../range/node_range.js';
import {NodeStyle} from '../style/node/node_style.js';
import {Node} from './node.js';
import {OffsetWordInfo} from './types/offset_word_info.js';
import {RangeOffset} from '../range/range_offset.js';
import {Range} from '../range/range.js';
import {RuntimeTextStylesConfiguration} from '../text/runtime_text_styles_configuration.js';
import {env} from '../env.js';
 
goog.declareModuleId('pwk.content.LeafNode');
 
// TODO(banguit): Implement RTL and LTR directions support.
//  Review tools under goog.i18n.bidi.
 
const Rect = goog.require('goog.math.Rect');
const TagName = goog.require('goog.dom.TagName');
const array = goog.require('goog.array');
const object = goog.require('goog.object');
const style = goog.require('goog.style');
const {assert} = goog.require('goog.asserts');
const {TextStyle} = goog.requireType('pwk.style.text.TextStyle');
 
/**
 * A word separator used to split text into parts.
 * @type {!RegExp}
 */
const WORDS_SEPARATOR = /(\s+|[.,:;!?\[\]{}<>/\\‚„…|@#%§¶*+=^~ˆ•–—˜¦¨¬¯±·])/;
 
/**
 * @extends {Node}
 */
export class LeafNode extends Node {
  /**
   * @param {!Content} content Node text content.
   */
  constructor(content) {
    super();
 
    /**
     * @const {!Content}
     * @protected
     */
    this.content_ = content;
 
    /**
     * Node selection range information.
     * @type {?NodeRange}
     * @protected
     */
    this.selectionRange_ = null;
 
    /**
     * Indicates if extra selection added in the end of the node.
     * @type {boolean}
     * @protected
     */
    this.extraSelection_ = false;
 
    /**
     * Runtime text styles configuration.
     * @const {!RuntimeTextStylesConfiguration}
     * @private
     */
    this.runtimeTextStylesConfiguration_ = RuntimeTextStylesConfiguration.getInstance();
  }
 
  /** @override */
  enterDocument() {
    super.enterDocument();
    this.dispatchEvent(new ContentRenderingReadyEvent(this));
  }
 
  /** @override */
  exitDocument() {
    super.exitDocument();
 
    this.unselect();
    this.extraSelection_ = false;
  }
 
  /**
   * Indicates whether any content in this node.
   * @return {boolean}
   * @override
   */
  hasContent() {
    return this.content_.hasContent();
  }
 
  /**
   * (Re-)Render node content.
   * @param {number=} opt_startLineIndex Line to start render content.
   * @override
   */
  renderContent(opt_startLineIndex = 0) {
    const selectionRange = this.selectionRange_;
    let selectionStart;
    let selectionEnd;
 
    // If node selected, remove selection before rendering.
    if (selectionRange) {
      selectionStart = selectionRange.getStartOffset().getNodeOffset();
      selectionEnd = selectionRange.getEndOffset().getNodeOffset();
      this.unselect();
    }
 
    // Set node styles
    this.setNodeStyles_();
 
    // Render content
    const content = this.content_;
    const maxContentWidth = this.getWidth();
 
    if (content.hasContent()) {
      const linesOfContent = content.fragmentText(maxContentWidth);
      const lastModifiedLineIndex = Math.max(opt_startLineIndex - 1, 0);
 
      // Update lines content / add new lines.
      for (let i = lastModifiedLineIndex, l = linesOfContent.length; i < l; i++) {
        let lineByIndex = /** @type {!Line} */(this.getChildAt(i));
 
        if (lineByIndex) {
          lineByIndex.setContent(linesOfContent[i]);
        } else {
          let line = new Line(linesOfContent[i]);
          this.addChildAt(line, i, true);
        }
      }
 
      // Remove remaining extra lines below updated content.
      let linesCount = this.getChildCount();
      let fragmentsCount = linesOfContent.length;
      for (let i = linesCount - 1, l = fragmentsCount - 1; i > l; i--) {
        this.removeChildAt(i, true);
      }
    } else {
      this.removeChildren(true);
      this.addChild(new Line(content), true);
    }
 
    // Update selection if node was selected before.
    if (selectionRange) {
      this.select(selectionStart, selectionEnd);
    }
  }
 
  /**
   * Sets leaf and parent branch nodes styles
   * @param {!Element=} opt_element Optional element to apply styles
   * @protected
   */
  setNodeStyles_(opt_element) {
    // TODO: Add check if styles already set, then avoid update
    const element = opt_element || this.getElement();
    const nodeStyles = BaseNodeStyle.getStylesheetsMapFor(
        env['nodeStyles'], this.content_.getNodeStyles(), APPLICABLE_AREA.LEAF_NODE, this.getNodeType());
    style.setStyle(element, nodeStyles);
  }
 
  /**
   * Gets node content length.
   * @return {number}
   * @override
   */
  getLength() {
    return this.content_.getLength();
  }
 
  /**
   * Gets node content.
   * @return {!Content}
   * @override
   */
  getContent() {
    return this.content_;
  }
 
  /**
   * Gets node styles in offset. This method returns the same information as
   * {@link Content#getNodeStyles}, but required to get aggregated node styles for complex nodes
   * like {@link BranchNode}
   * @param {number=} opt_startOffset Optional zero-based offset at which to begin extraction.
   * @param {number=} opt_endOffset Optional zero-based offset before which to end extraction.
   * @return {!NodeStylesModel}
   * @override
   */
  getNodeStyles(opt_startOffset, opt_endOffset) {
    return this.getContent().getNodeStyles();
  }
 
  /**
   * Extracts a section of a node content.
   * @param {number} startOffset The zero-based offset at which to begin
   *    extraction. If negative, it is treated as nodeContentLength + startOffset
   *    where nodeContentLength is the length of the node content (for example,
   *    if startOffset is -3 it is treated as nodeContentLength - 3). If
   *    startOffset is greater than or equal to the length of the node content,
   *    getContentAt() returns empty result.
   * @param {number=} opt_endOffset Optional zero-based offset before which to
   *    end extraction. The character at this offset will not be included. If
   *    opt_endOffset is omitted, getContentAt() extracts to the end of the
   *    string. If negative, it is treated as nodeContentLength + opt_endOffset
   *    where nodeContentLength is the length of the node content (for example, if
   *    opt_endOffset is -3 it is treated as nodeContentLength - 3).
   * @return {!Content}
   * @override
   */
  getContentAt(startOffset, opt_endOffset) {
    return this.content_.getContentAt(startOffset, opt_endOffset);
  }
 
  /**
   * Modifies text styles in the offset using given function. Apply style with the provided configuration
   * in case if style is not defined in the given offset and there no styles to clean based on stylePreprocessor result.
   * @param {number} startOffset The zero-based offset at which to begin
   *    extraction. If negative, it is treated as nodeContentLength + startOffset
   *    where nodeContentLength is the length of the node content (for example,
   *    if startOffset is -3 it is treated as nodeContentLength - 3). If
   *    startOffset is greater than or equal to the length of the node content,
   *    getContentAt() returns empty result.
   * @param {number|undefined} endOffset Optional zero-based offset before which to
   *    end extraction. The character at this offset will not be included. If
   *    opt_endOffset is omitted, getContentAt() extracts to the end of the
   *    string. If negative, it is treated as nodeContentLength + opt_endOffset
   *    where nodeContentLength is the length of the node content (for example, if
   *    opt_endOffset is -3 it is treated as nodeContentLength - 3).
   * @param {!TextStyle} textStyle Text style name.
   * @param {!Object<string, !Object>} configuration Configuration object.
   * @param {function(
   *          !Object<string, !Object>,
   *          !Object<string, !*>,
   *          !Content,
   *          !Object=):!Object<string, !Object>=} stylePreprocessor
   *    Optional preprocessor function to apply for each text style for each
   *    node content. Should return new configuration object which will be
   *    applied to content rage.
   *    - 1st argument: passed configuration;
   *    - 2nd argument: inline style configuration for selected range in the
   *      current node;
   *    - 3rg argument: current content metadata instance;
   *    - 4th argument: optional data object;
   * @param {!Object=} opt_data Optional data object to pass to
   *    opt_stylePreprocessor function.
   * @param {!Object=} opt_thisArg Object that should be used by default for
   *    opt_stylePreprocessor function calls. Current metadata instance will
   *    used by default.
   * @override
   */
  modifyTextStylesAndUpdate(startOffset, endOffset, textStyle, configuration, stylePreprocessor, opt_data, opt_thisArg) {
    const contentInOffset = this.getContentAt(startOffset, endOffset);
    const context = opt_thisArg || contentInOffset;
    let newConfig;
 
    // If node is empty, update default styles.
    if (!this.hasContent()) {
      const inlineStyles = /** @type {!Object<string, !*>} */ (this.getContent().getDefaultStyles()[textStyle]);
      newConfig = stylePreprocessor.call(context, configuration, inlineStyles, contentInOffset, opt_data);
 
      if (newConfig !== null) {
        this.applyTextStyle(textStyle, newConfig, startOffset, endOffset);
      } else E{
        this.clearTextStyles(textStyle, startOffset, endOffset);
      }
    } else {
      const inlineStyles = contentInOffset.getTextStyles();
      const stylesToUpdate = [];
      const /** @type {!Array<{startOffset: number, endOffset: number}>} */ stylesOffsetsToClean = [];
      const length = contentInOffset.getLength();
      const range = new Array(length).fill(false);
      const inlineStylesLength = inlineStyles[textStyle]?.length ?? 0;
 
      // Modify styles
      for (let i = 0; i < inlineStylesLength; i++) {
        const inlineStyle = inlineStyles[textStyle][i];
        const offsetsCount = inlineStyle['offsets'].length;
 
        newConfig = stylePreprocessor.call(context, configuration, inlineStyle, contentInOffset, opt_data);
 
        for (let j = 0; j < offsetsCount; j++) {
          const offset = inlineStyle['offsets'][j];
          const start = startOffset + offset['start'];
          const end = startOffset + offset['end'];
 
          Iif (newConfig === null) {
            stylesOffsetsToClean.push({
              startOffset: start,
              endOffset: end
            });
          } else {
            stylesToUpdate.push({
              textStyle: textStyle,
              configuration: newConfig,
              startOffset: start,
              endOffset: end,
            });
          }
 
          // Update offset range array
          range.fill(true, offset['start'], offset['end']);
        }
      }
 
      // Apply styles
      for (let i = 0, start = null, end = null; i <= length; i++) {
        Iif (start === null && range[i] === false) {
          start = i;
        }
 
        Iif (range[i] === false) {
          end = i;
        }
 
        Iif ((i === length || range[i] === true) && start !== null && end !== null) {
          stylesToUpdate.push({
            configuration,
            textStyle: textStyle,
            startOffset: startOffset + start,
            endOffset: startOffset + end + 1,
          });
          start = null;
          end = null;
        }
      }
      // Clear styles
      for (let i = 0; i < stylesOffsetsToClean.length; i++) {
        const offset = stylesOffsetsToClean[i];
        this.clearTextStyles(textStyle, offset.startOffset, offset.endOffset);
      }
 
      this.applyTextStyles(stylesToUpdate);
    }
  }
 
  /**
   * Gets detailed offset information.
   * @param {number} offset 0-based node offset.
   * @param {boolean=} opt_considerEndOfLineOffset Consider end of line offset
   *    as valid result.
   * @return {?NodeOffsetInfo}
   */
  getOffsetInfo(offset, opt_considerEndOfLineOffset) {
    let result = null;
    let totalLength = 0;
 
    if (offset >= 0) {
      this.forEachChild((line, index) => {
        if (result === null) {
          let lineLength = line.getContent().getLength();
          totalLength += lineLength;
 
          if (totalLength >= offset) {
            // Set first line position of the line below if it is last position
            // of the loop line.
            if (totalLength === offset &&
                this.getChildAt(index + 1) != null &&
                !opt_considerEndOfLineOffset) {
              let lineBelow = /** @type {!Line} */(this.getChildAt(index + 1));
              result = new NodeOffsetInfo(this, lineBelow, index + 1, 0, offset);
            } else {
              let lineOffset = lineLength - (totalLength - offset);
              result = new NodeOffsetInfo(this, line, index, lineOffset, offset);
            }
          }
        }
      });
    }
 
    return result;
  }
 
  /**
   * Gets detailed range offset information to the specified x coordinate and
   * the specified y coordinate.
   * @param {number} x Horizontal coordinate within the application client area.
   * @param {number} y Vertical coordinate within the application client area.
   * @return {?RangeOffset}
   * @override
   */
  getRangeOffsetFromPoint(x, y) {
    let nodeBounds = style.getBounds(this.getElement());
    let innerY = Math.min(Math.max(y - nodeBounds.top, 0), nodeBounds.height);
    let searchYCoordinate = nodeBounds.top + innerY;
    let startLineIndex = 0;
    let endLineIndex = this.getChildCount() - 1;
 
    // Get line in the offset.
    /** @type {number} */
    let loopIndex;
    /** @type {!Line} */
    let loopLine;
    let loopBounds;
    while (startLineIndex <= endLineIndex) {
      loopIndex = Math.floor((startLineIndex + endLineIndex) / 2);
      loopLine = /** @type {!Line} */(this.getChildAt(loopIndex));
      loopBounds = style.getBounds(loopLine.getElement());
 
      if (searchYCoordinate >= loopBounds.top &&
          searchYCoordinate <= loopBounds.top + loopBounds.height) {
        break;
      } else if (loopBounds.top < searchYCoordinate) {
        startLineIndex = loopIndex + 1;
      } else {
        endLineIndex = loopIndex - 1;
      }
    }
 
    return this.getLineRangeOffset_(loopLine, loopIndex, x);
  }
 
  /**
   * Gets detailed range offset information for specific offset.
   * @param {number} offset 0-based node offset.
   * @param {boolean=} opt_considerEndOfLineOffset Consider end of line offset as valid result.
   * @return {?RangeOffset}
   * @override
   */
  getRangeOffsetForOffset(offset, opt_considerEndOfLineOffset) {
    const nodeOffsetInfo = this.getOffsetInfo(offset, opt_considerEndOfLineOffset);
    let rangeOffset = null;
 
    if (nodeOffsetInfo !== null) {
      rangeOffset =
          this.getLineRangeOffsetInternal_(nodeOffsetInfo.getLine(), nodeOffsetInfo.getLineOffset(), nodeOffsetInfo);
    }
 
    return rangeOffset;
  }
 
  /**
   * Gets line range offset.
   * @param {!Line} line
   * @param {number} lineOffset
   * @param {!NodeOffsetInfo} nodeOffsetInfo
   * @return {!RangeOffset}
   * @private
   */
  getLineRangeOffsetInternal_(line, lineOffset, nodeOffsetInfo) {
    const lineContent = line.getContent();
    const lineContentElement = line.getContentRootElement();
    const offsetWidth = lineContent.getOffsetWidth(0, lineOffset);
    const offsetWidthDelta = lineContent.getOffsetWidth(0, lineOffset + 1);
    let totalWidth = 0;
    let closestSpanForOffset =
        array.find(lineContentElement.childNodes, (child) => {
          totalWidth += (/** @type {!HTMLElement} */(child)).offsetWidth;
          return Math.floor(offsetWidth) <=
              totalWidth + ((offsetWidthDelta - offsetWidth) / 2);
        }) || lineContentElement.lastChild;
 
    // Let's check the internal content, and if this is the only element, let's use it
    while (closestSpanForOffset.childNodes.length === 1 &&
           closestSpanForOffset.childNodes[0].nodeType === window.Node.ELEMENT_NODE) {
      closestSpanForOffset = closestSpanForOffset.childNodes[0];
    }
 
    const closestSpanForOffsetBounds = style.getBounds((/** @type {!Element} */(closestSpanForOffset)));
    const nodeBounds = style.getBounds(this.getElement());
    const offsetX = line.getXForOffset(lineOffset);
    const offsetY = closestSpanForOffsetBounds.top - nodeBounds.top;
    const offsetBounds = new Rect(offsetX, offsetY, 0, closestSpanForOffsetBounds.height);
 
    return new RangeOffset(this, nodeOffsetInfo, offsetBounds);
  }
 
  /**
   * Gets range offset around given offset in the specified direction and
   * distance.
   * @param {number} offset 0-based node offset.
   * @param {!DIRECTIONS} direction The direction in which to adjust the offset.
   * @param {!GRANULARITY} granularity The distance to adjust the offset. Only
   *    pwk.GRANULARITY.CHAR an pwk.GRANULARITY.WORD accepted.
   * @return {?RangeOffset}
   * @override
   */
  getRangeOffsetAround(offset, direction, granularity) {
    const length = this.getLength();
 
    assert(
        [GRANULARITY.CHAR, GRANULARITY.WORD].indexOf(granularity) !== -1,
        'Only granularity pwk.GRANULARITY.CHAR and pwk.GRANULARITY.WORD ' +
        'supported.');
 
    if (length > 0 && offset >= 0 && offset <= length) {
      switch (granularity) {
        case GRANULARITY.CHAR:
          offset += direction === DIRECTIONS.BACKWARD ? -1 : 1;
          break;
 
        case GRANULARITY.WORD:
          const content = this.getContent();
          const wordsMap = content.getWordsWidthsMap(WORDS_SEPARATOR);
          const wordsCount = wordsMap.length;
          const lastWordIndex = wordsCount - 1;
          const isEndOfNode = wordsMap[lastWordIndex].endOffset === offset;
          const mapIndex =
              isEndOfNode ?
                  wordsCount :
                  array.binarySearch(wordsMap, offset, (target, value) => {
                    if (target >= value.startOffset &&
                        target < value.endOffset) {
                      return 0;
                    } else if (target < value.startOffset) {
                      return -1;
                    }
                    return 1;
                  });
 
          if (direction === DIRECTIONS.BACKWARD) {
            const currentWord = wordsMap[mapIndex];
 
            // Move cursor to the beginning of the current node or to the end of
            // the previous word
            if (!currentWord || currentWord.startOffset === offset) {
              offset = wordsMap[Math.max(mapIndex - 1, 0)].startOffset;
            } else {
              offset = currentWord.startOffset;
            }
          } else {
            offset =
                mapIndex + 1 < wordsCount ?
                    wordsMap[mapIndex + 1].startOffset :
                    wordsMap[lastWordIndex].endOffset;
          }
          break;
      }
    } else {
      offset = 0;
    }
 
    return this.getRangeOffsetForOffset(offset);
  }
 
  /**
   * Gets range of the word in the specified x coordinate and the specified y
   * coordinate.
   * @param {number} offset 0-based node offset.
   * @return {?Range}
   * @override
   */
  getWordRangeOffsetFromOffset(offset) {
    const content = this.getContent();
    const wordsMap = content.getWordsWidthsMap(WORDS_SEPARATOR);
    const mapIndex =
        array.binarySearch(wordsMap, offset, (target, value) => {
          if (target >= value.startOffset && target < value.endOffset) {
            return 0;
          } else if (target < value.startOffset) {
            return -1;
          }
          return 1;
        });
 
    if (mapIndex >= 0) {
      const wordOffsets = wordsMap[mapIndex];
      return new Range(
          /** @type {!RangeOffset} */
          (this.getRangeOffsetForOffset(wordOffsets.startOffset)),
          /** @type {!RangeOffset} */
          (this.getRangeOffsetForOffset(wordOffsets.endOffset)));
    }
 
    return null;
  }
 
  /**
   * Get words around offset.
   * @param {number} offset 0-based node offset
   * @return {?OffsetWordInfo}
   * @override
   */
  getWordsInformationAroundOffset(offset) {
    let wordInfo = null;
    let offsetPosition = 0;
    let offsetDelta = 0;
    const content = this.getContent();
    const wordsMap = content.getWordsWidthsMap(WORDS_SEPARATOR, false);
    const mapIndex =
        array.binarySearch(wordsMap, offset, (target, value) => {
          if (target >= value.startOffset && target <= value.endOffset) {
            offsetDelta = value.endOffset - target;
            return 0;
          } else if (target < value.startOffset) {
            return -1;
          }
          return 1;
        });
 
    if (mapIndex >= 0) {
      // Get last 3 words
      const words = [];
 
      for (let i = 0; i <= 2 && mapIndex - i >= 0; i++) {
        words.unshift(wordsMap[mapIndex - i]);
      }
 
      const additionalWord = wordsMap[mapIndex + 1];
      if (additionalWord) {
        words.push(additionalWord);
        offsetDelta += additionalWord.endOffset - additionalWord.startOffset;
      }
 
      const firstWordOffsets = words[0];
      const lastWordOffsets = words[words.length - 1];
      const textContent = content.getContentAt(firstWordOffsets.startOffset, lastWordOffsets.endOffset);
      offsetPosition = textContent.getLength() - offsetDelta;
      const range = new Range(
          /** @type {!RangeOffset} */
          (this.getRangeOffsetForOffset(firstWordOffsets.startOffset)),
          /** @type {!RangeOffset} */
          (this.getRangeOffsetForOffset(lastWordOffsets.endOffset)));
      wordInfo = new OffsetWordInfo(textContent, range, offsetPosition);
    }
 
    return wordInfo;
  }
 
  /**
   * Gets position above of given offset. If given position is top of the node,
   * return `undefined`.
   * @param {!RangeOffset} startOffset Origin offset to search.
   * @param {number} x X axis position to align.
   * @return {!RangeOffset|undefined} New offset above or undefined.
   * @override
   */
  getOffsetAbove(startOffset, x) {
    const lineIndex = startOffset.getNodeOffsetInfo().getLineIndex();
    let newOffset;
 
    if (lineIndex !== 0) {
      const newOffsetLine = /** @type {!Line} */(this.getChildAt(lineIndex - 1));
      const newOffsetLineBounds = style.getBounds(newOffsetLine.getElement());
      const y = newOffsetLineBounds.top + (newOffsetLineBounds.height / 2);
      newOffset = /** @type {!RangeOffset} */(this.getRangeOffsetFromPoint(x, y));
    }
 
    return newOffset;
  }
 
  /**
   * Gets position below of given offset. If given position is bottom of the
   * node, return `undefined`.
   * @param {!RangeOffset} startOffset Origin offset to search.
   * @param {number} x X axis position to align.
   * @return {!RangeOffset|undefined} New offset below or undefined.
   * @override
   */
  getOffsetBelow(startOffset, x) {
    const lineIndex = startOffset.getNodeOffsetInfo().getLineIndex();
    const linesCount = this.getChildCount();
    const lastLineIndex = linesCount - 1;
    let newOffset;
 
    if (lineIndex !== lastLineIndex) {
      const newOffsetLine = /** @type {!Line} */(this.getChildAt(lineIndex + 1));
      const newOffsetLineBounds = style.getBounds(newOffsetLine.getElement());
      const y = newOffsetLineBounds.top + (newOffsetLineBounds.height / 2);
      newOffset = /** @type {!RangeOffset} */(this.getRangeOffsetFromPoint(x, y));
    }
 
    return newOffset;
  }
 
  /**
   * Gets topmost offset in the node by X axis.
   * @param {number} x X axis position to align.
   * @return {!RangeOffset} New offset below.
   * @override
   */
  getTopmostOffsetByX(x) {
    const firstLineIndex = 0;
    const firstLine = /** @type {!Line} */(this.getChildAt(firstLineIndex));
    return this.getLineRangeOffset_(firstLine, firstLineIndex, x);
  }
 
  /**
   * Gets bottommost offset in the node by X axis.
   * @param {number} x X axis position to align.
   * @return {!RangeOffset} New offset below.
   * @override
   */
  getBottommostOffsetByX(x) {
    const linesCount = this.getChildCount();
    const lastLineIndex = linesCount - 1;
    const lastLine = /** @type {!Line} */(this.getChildAt(lastLineIndex));
 
    return this.getLineRangeOffset_(lastLine, lastLineIndex, x);
  }
 
  /**
   * Gets line range offset.
   * @param {!Line} line
   * @param {number} lineIndex
   * @param {number} x X axis position to align.
   * @return {!RangeOffset} Line range offset.
   * @private
   */
  getLineRangeOffset_(line, lineIndex, x) {
    const loopLineContentBounds = style.getBounds(line.getElement());
    const innerX = Math.max(x - loopLineContentBounds.left, 0);
    const lineOffset = line.getOffsetByX(innerX);
    let nodeOffset = 0;
    /** @type {!NodeOffsetInfo} */
    let nodeOffsetInfo;
 
    for (let i = 0; i <= lineIndex; i++) {
      if (i === lineIndex) {
        nodeOffsetInfo = new NodeOffsetInfo(this, line, i, lineOffset, nodeOffset + lineOffset);
        break;
      } else {
        let line = /** @type {!Line} */(this.getChildAt(i));
        nodeOffset += line.getContent().getLength();
      }
    }
 
    return this.getLineRangeOffsetInternal_(line, lineOffset, nodeOffsetInfo);
  }
 
  /**
   * Inserts text at the given position.
   * @param {!Content} content The content to insert.
   * @param {number} offset The offset to insert text. Can't be negative.
   *    Represents symbol position in the text.
   * @override
   */
  insertContentAt(content, offset) {
    const offsetInfo = this.getOffsetInfo(offset);
    this.getContent().insertContentAt(content, offset);
 
    // Re-render content
    this.unselect();
    this.dispatchEvent(new ContentRenderingReadyEvent(this, offsetInfo.getLineIndex()));
  }
 
 
  /**
   * Appends text.
   * @param {!Content} content The content to append.
   * @override
   */
  appendText(content) {
    const lastLineIndex = this.getChildCount() - 1;
    this.getContent().appendText(content);
 
    // Re-render content
    this.unselect();
    this.dispatchEvent(new ContentRenderingReadyEvent(this, lastLineIndex > 0 ? lastLineIndex : 0));
  }
 
  /**
   * Removes specified range of the node content.
   * @param {number} startOffset Zero-based offset at which to begin removing.
   * @param {number=} opt_endOffset Optional zero-based offset before which to end removing.
   * @param {?Range=} opt_currentSelectionRange Optional current selection range.
   * @override
   */
  removeAt(startOffset, opt_endOffset, opt_currentSelectionRange) {
    const offsetInfo = this.getOffsetInfo(startOffset);
    this.getContent().removeAt(startOffset, opt_endOffset);
 
    // Re-render content
    this.unselect();
    this.dispatchEvent(new ContentRenderingReadyEvent(this, offsetInfo.getLineIndex()));
  }
 
  /**
   * Apply text styles for content in given offsets and re-render node content.
   * The styles will be applied in the order provided in the array.
   * @param {!Array<{
   *          textStyle: !TextStyle,
   *          configuration: !Object<string, !Object>,
   *          startOffset: number,
   *          endOffset: number
   *        }>} styles
   * @override
   */
  applyTextStyles(styles) {
    if (styles.length > 0) {
      let minUpdatedOffset = styles[0].startOffset;
 
      styles.forEach((style) => {
        this.content_.addTextStyle(style.textStyle, style.configuration, style.startOffset, style.endOffset);
        minUpdatedOffset = Math.min(minUpdatedOffset, style.startOffset);
      });
 
      // Re-render content
      const offsetInfo = this.getOffsetInfo(minUpdatedOffset);
      this.dispatchEvent(new ContentRenderingReadyEvent(this, offsetInfo.getLineIndex()));
    }
  }
 
  /**
   * Apply text style for content in offset and re-render node content.
   * @param {!TextStyle} textStyle Text style name.
   * @param {!Object<string, !Object>} configuration Configuration object.
   * @param {number=} opt_startOffset Optional zero-based offset where to apply
   *    text formatting. If opt_startOffset is omitted, the style will be
   *    applied from the start of the node content.
   * @param {number=} opt_endOffset Optional zero-based offset where to apply
   *    text formatting. If opt_endOffset is omitted, the style will be applied
   *    to the end of the node content.
   * @override
   */
  applyTextStyle(textStyle, configuration, opt_startOffset, opt_endOffset) {
    this.content_.addTextStyle(textStyle, configuration, opt_startOffset, opt_endOffset);
 
    // Re-render content
    const lineIndex = opt_startOffset ? this.getOffsetInfo(opt_startOffset).getLineIndex() : 0;
    this.dispatchEvent(new ContentRenderingReadyEvent(this, lineIndex));
  }
 
  /**
   * Clears text style in the specific offset.
   * @param {!TextStyle} textStyle Text style name. Should equal to style name
   *    registered in the global registry {@link pwk.env}.
   * @param {number=} opt_startOffset Optional zero-based offset where to apply
   *    text formatting. If opt_startOffset is omitted, the style will be
   *    applied from the start of the node content.
   * @param {number=} opt_endOffset Optional zero-based offset where to apply
   *    text formatting. If opt_endOffset is omitted, the style will be applied
   *    to the end of the node content.
   * @override
   */
  clearTextStyles(textStyle, opt_startOffset, opt_endOffset) {
    this.content_.clearTextStyles(opt_startOffset, opt_endOffset, textStyle);
 
    // Re-render content
    const lineIndex = opt_startOffset ? this.getOffsetInfo(opt_startOffset).getLineIndex() : 0;
    this.dispatchEvent(new ContentRenderingReadyEvent(this, lineIndex));
  }
 
  /**
   * Apply node style and re-render node content.
   * @param {!NodeStyle} nodeStyle Node style.
   * @param {?Object|string|number} configuration Configuration object.
   * @override
   */
  applyNodeStyle(nodeStyle, configuration) {
    const content = this.content_;
    const currentConfiguration = content.getNodeStyle(nodeStyle);
    const isConfigurationChanged =
        goog.isObject(currentConfiguration) ?
            !object.equals(currentConfiguration, /** @type {!Object} */(configuration)) :
            currentConfiguration !== configuration;
 
    if (isConfigurationChanged) {
      this.content_.setNodeStyle(nodeStyle, configuration);
 
      const selectionRange = this.selectionRange_;
      let selectionStart;
      let selectionEnd;
 
      // If node selected, remove selection before rendering.
      if (selectionRange) {
        selectionStart = selectionRange.getStartOffset().getNodeOffset();
        selectionEnd = selectionRange.getEndOffset().getNodeOffset();
        this.unselect();
      }
 
      // Re-render content
      this.dispatchEvent(new ContentRenderingReadyEvent(this));
 
      // Update selection in node was selected before.
      if (selectionRange) {
        this.select(selectionStart, selectionEnd);
      }
    }
  }
 
  /**
   * Cuts the specified range of the node content.
   * @param {number} startOffset Zero-based offset at which to begin
   *    cut. If negative, it is treated as lineContentLength + startOffset
   *    where lineContentLength is the length of the line content (for example,
   *    if opt_startOffset is -3 it is treated as lineContentLength - 3). If
   *    startOffset is greater than or equal to the length of the line
   *    content, cut() returns an empty string.
   * @param {number=} opt_endOffset Optional zero-based offset before which to
   *    end cut. The character at this offset will not be included. If
   *    opt_endOffset is omitted, cut() cuts to the end of the string.
   *    If negative, it is treated as lineContentLength + opt_endOffset where
   *    lineContentLength is the length of the line content (for example, if
   *    opt_endOffset is -3 it is treated as lineContentLength - 3).
   * @return {!Content}
   */
  cut(startOffset, opt_endOffset) {
    const offsetInfo = this.getOffsetInfo(startOffset);
    const cuttedContent = this.content_.cut(startOffset, opt_endOffset);
 
    // Re-render content
    this.unselect();
    this.dispatchEvent(new ContentRenderingReadyEvent(this, offsetInfo.getLineIndex()));
    return cuttedContent;
  }
 
  /**
   * Make a selection of node content.
   * @param {number=} opt_startOffset Optional selection start offset.
   * @param {number=} opt_endOffset Optional selection end offset.
   * @param {boolean=} opt_addExtraSpace Indicates if required to add extra space in the end of the node.
   * @param {boolean=} opt_isDocumentSelectionCollapsed Indicates if document selection is collapsed.
   * @override
   */
  select(opt_startOffset = 0, opt_endOffset = this.getLength(), opt_addExtraSpace, opt_isDocumentSelectionCollapsed) {
    this.unselect();
 
    const totalLength = this.getLength();
    const startLineOffset = this.getOffsetInfo(opt_startOffset);
    const endLineOffset = this.getOffsetInfo(opt_endOffset);
    const startLineIndex = startLineOffset.getLineIndex();
    const endLineIndex = endLineOffset.getLineIndex();
    let loopLine;
 
    for (let i = startLineIndex, l = endLineIndex; i <= l; i++) {
      loopLine = /** @type {!Line} */(this.getChildAt(i));
 
      if (i === endLineIndex &&
          (opt_addExtraSpace || (totalLength === 0 && !opt_isDocumentSelectionCollapsed))) {
        this.extraSelection_ = true;
      }
 
      if (i === startLineIndex && i === endLineIndex) {
        loopLine.select(startLineOffset.getLineOffset(), endLineOffset.getLineOffset(), this.extraSelection_);
      } else if (i === startLineIndex) {
        loopLine.select(startLineOffset.getLineOffset(), undefined, this.extraSelection_);
      } else if (i === endLineIndex) {
        loopLine.select(0, endLineOffset.getLineOffset(), this.extraSelection_);
      } else {
        loopLine.select(undefined, undefined, this.extraSelection_);
      }
    }
 
    this.selectionRange_ = new NodeRange(
        /** @type {!NodeOffsetInfo} */(startLineOffset),
        /** @type {!NodeOffsetInfo} */(endLineOffset));
  }
 
  /** @override */
  unselect() {
    if (this.selectionRange_) {
      const selectionRange = this.selectionRange_;
      const startOffset = selectionRange.getStartOffset();
      const endOffset = selectionRange.getEndOffset();
 
      if (!NodeOffsetInfo.equals(startOffset, endOffset)) {
        let startLineIndex = startOffset.getLineIndex();
        let endLineIndex = endOffset.getLineIndex();
        let loopLine;
 
        for (let i = startLineIndex; i <= endLineIndex; i++) {
          loopLine = /** @type {!Line} */(this.getChildAt(i));
          loopLine.unselect();
        }
      }
 
      this.selectionRange_ = null;
    }
 
    if (this.extraSelection_) {
      const lastLine = /** @type {!Line} */(this.getChildAt(this.getChildCount() - 1));
      lastLine.unselect();
      this.extraSelection_ = false;
    }
  }
 
  /**
   * Identifies whether node is selected entirely.
   * @return {boolean}
   * @override
   */
  isSelectedEntirely() {
    const selectionRange = this.selectionRange_;
 
    return selectionRange != null &&
        selectionRange.getStartOffset().getNodeOffset() === 0 &&
        selectionRange.getEndOffset().getNodeOffset() === this.getLength();
  }
 
  /**
   * Gets selected content.
   * @return {?Content}
   * @override
   */
  getSelectedContent() {
    let selectedContent = null;
    const selectionRange = this.selectionRange_;
 
    if (selectionRange != null) {
      const startOffset = selectionRange.getStartOffset().getNodeOffset();
      const endOffset = selectionRange.getEndOffset().getNodeOffset();
      selectedContent = this.content_.getContentAt(startOffset, endOffset);
    }
 
    return selectedContent;
  }
 
  /**
   * Gets node selection range.
   * @return {?NodeRange}
   * @override
   */
  getSelectionRange() {
    return this.selectionRange_;
  }
 
  /**
   * Gets html representation of the node selection. Use
   * Node.getContent().toHtmlString() if you need to obtain the HTML
   * representation of the content without considering node HTML structure.
   * @param {number} startOffset The zero-based offset at which to begin
   *    extraction. If negative, it is treated as lineContentLength + startOffset
   *    where lineContentLength is the length of the line content (for example,
   *    if startOffset is -3 it is treated as lineContentLength - 3). If
   *    startOffset is greater than or equal to the length of the line content,
   *    getContentAt() returns empty result.
   * @param {number=} opt_endOffset Optional zero-based offset before which to
   *    end extraction. The character at this offset will not be included. If
   *    opt_endOffset is omitted, getContentAt() extracts to the end of the
   *    string. If negative, it is treated as lineContentLength + opt_endOffset
   *    where lineContentLength is the length of the line content (for example, if
   *    opt_endOffset is -3 it is treated as lineContentLength - 3).
   * @return {string} HTML string
   * @override
   */
  getOuterHtml(startOffset, opt_endOffset) {
    const element = this.dom_.createElement(TagName.DIV);
    const contentRange = this.getContent().getContentAt(startOffset, opt_endOffset);
    element.innerHTML = contentRange.toHtmlString();
    this.setNodeStyles_(element);
    return element.outerHTML;
  }
 
  /**
   * Gets component default css class.
   * @return {string} Base CSS class for this component.
   * @override
   */
  getCssClass() {
    return LeafNode.CSS_CLASS;
  }
 
  /**
   * Gets model object.
   * @param {boolean=} selectedContentOnly If true, returns model object for
   *    selected content only.
   * @return {!NodeModel}
   * @override
   */
  toModel(selectedContentOnly) {
    const model = new LeafNodeModel();
    model.nodeType = this.getNodeType();
    model.content = selectedContentOnly ? this.getSelectedContent().toModel() : this.content_.toModel();
    return model;
  }
 
  /**
   * Converts current node content to plain text.
   * @param {number=} opt_startOffset Optional zero-based offset at which to begin extraction.
   * @param {number=} opt_endOffset Optional zero-based offset before which to end extraction.
   * @return {string}
   * @override
   */
  toPlainText(opt_startOffset = 0, opt_endOffset = this.getLength()) {
    return this.getContentAt(opt_startOffset, opt_endOffset).getText();
  }
 
  /**
   * Increase indentation. Offsets is useful for BranchNode components.
   * @param {number=} opt_startOffset
   * @param {number=} opt_endOffset
   * @return {boolean} Whether indent increased successfully
   * @override
   */
  increaseIndent(opt_startOffset = 0, opt_endOffset = this.getLength()) {
    const content = this.getContent();
    const nodeStyles = content.getNodeStyles();
    const currentIndent = nodeStyles.indent ?? 0;
    const currentWidth = this.getWidth();
 
    if (currentWidth - DEFAULT_INDENT_STEP > DEFAULT_INDENT_STEP) {
      this.unselect(); // node should be unselected to avoid problems with selection outside
      this.getContent().setNodeStyle(NodeStyle.INDENT, currentIndent + DEFAULT_INDENT_STEP);
      this.dispatchEvent(new ContentRenderingReadyEvent(this));
      return true;
    }
    return false;
  }
 
  /**
   * Decreases indentation. Offsets is useful for BranchNode components.
   * @param {number=} opt_startOffset
   * @param {number=} opt_endOffset
   * @return {boolean} Whether indent increased successfully
   * @override
   */
  decreaseIndent(opt_startOffset = 0, opt_endOffset = this.getLength()) {
    const content = this.getContent();
    const nodeStyles = content.getNodeStyles();
    const currentIndent = nodeStyles.indent ?? 0;
 
    if (currentIndent > 0) {
      this.unselect(); // node should be unselected to avoid problems with selection outside
      content.setNodeStyle(NodeStyle.INDENT, Math.max(currentIndent - DEFAULT_INDENT_STEP, 0));
      this.dispatchEvent(new ContentRenderingReadyEvent(this));
      return true;
    }
 
    return false;
  }
 
  /**
   * Gets content height.
   * @return {number}
   * @override
   */
  getHeight() {
    const content = this.content_;
    let height = 0;
 
    if (content.hasContent()) {
      const lines = content.fragmentText(this.getWidth());
      for (let i = 0; i < lines.length; i++) {
        height += lines[i].getHeight();
      }
    } else {
      const sizeMap = content.getCharsSizeMap();
      for (let i = 0; i < sizeMap.length; i++) {
        height += sizeMap[i].height;
      }
    }
 
    // TODO: Add branch styles check, which node affect height
 
    return height;
  }
 
  /**
   * Gets component width in pixels.
   * @return {number}
   * @override
   */
  getWidth() {
    let width = 0;
    const parent = /** @type {!BranchNode|null} */(this.getParent());
 
    if (parent instanceof BranchNode) {
      const parentWidth = parent.getBranchNodeStyles().width;
      const parentIndent = parent.getBranchNodeStyles().indent ?? 0;
      width += parentWidth !== DEFAULT_WIDTH_VALUE ? /** @type {number} */(parentWidth) : parent.getWidth();
      width -= parentIndent;
    } else {
      const documentSettings = this.runtimeTextStylesConfiguration_.getDocumentSettings();
      width += documentSettings?.getPageWidthPixel() ?? 0;
    }
 
    width -= this.getNodeStyles().indent ?? 0;
 
    return width;
  }
 
  /**
   * @override
   * Should be overridden by derived class.
   */
  getNodeType() {
    throw new Error('getNodeType method not implemented');
  }
 
  /**
   * Splits node. Returns second part of node as a result.
   * Should be overridden by derived class.
   * @param {!NodeOffsetInfo} nodeOffsetInfo
   * @return {!Node} Node to render below current node.
   * @override
   */
  split(nodeOffsetInfo) {
    throw new Error('split method not implemented');
  }
}
 
/**
 * Component default css class.
 * @type {string}
 * @override
 */
LeafNode.CSS_CLASS = 'pwk-leaf-node';