# all files / src/js/base/core/ dom.js

90.96% Statements 342/376
85.02% Branches 210/247
93.06% Functions 67/72
90.98% Lines 333/366

    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
    11051×
    1×
    1×
    1×

    1×
    1×

    1×
    3887×

    1×

    1×
    24×
    24×
    2348×

    1×
    3667×

    1×
    35×

    1×
    927×

    1×
    851×
    62×

    789×

    1×
    3×

    1×

    1×

    1×
    10×

    1×

    1×

    1×
    408×

    1×
    462×

    1×

    1×
    592×

    1×

    1×
    484×

    1×

    1×
    101×

    1×

    1×

    1×

    1×
    2×

    2×
    2×

    2×
    2×
    1×

    2×

    1×

    1×
    1608×
    450×

    1158×
    1158×

    1×
    559×

    559×
    7×
    552×

    48×
    504×

    504×

    1×
    52×
    11×

    1×
    885×
    1952×
    1224×

    1069×

    157×

    1×
    9×

    9×
    10×
    4×
    2×

    1×

    7×

    1×
    491×

    491×
    491×
    1054×
    669×

    1054×

    491×

    1×
    6×
    6×

    1×
    29×
    29×
    34×

    1×
    38×

    38×
    38×
    46×
    23×
    23×

    38×

    1×
    80×

    80×
    80×
    74×
    73×
    73×

    80×

    1×
    11×
    11×

    11×
    29×
    7×

    29×
    18×

    11×

    1×
    18×
    18×

    18×
    18×

    18×

    1×
    56×
    56×
    56×
    15×

    41×

    56×

    1×
    66×
    82×

    66×

    1×
    463×

    1×
    423×

    1×
    220×

    1×
    32×
    17×
    1×

    16×

    31×

    1×
    63×

    63×
    4×
    1×

    3×

    62×

    1×
    101×

    1×
    84×

    1×
    293×
    293×
    89×

    293×

    1×
    873×

    1×
    240×
    240×

    240×
    9×

    9×
    9×
    231×
    216×
    216×

    15×
    15×

    240×

    1×
    155×

    155×
    16×

    16×
    16×
    139×
    61×
    61×

    78×
    78×

    155×

    1×
    134×

    1×
    816×
    369×

    447×
    447×
    447×
    1×

    446×

    1×
    77×
    179×
    77×

    102×

    1×
    21×
    30×
    21×

    9×

    1×
    21×

    21×
    21×

    1×
    31×

    31×
    134×

    134×
    31×

    103×

    103×

    1×
    315×
    315×

    1×
    20×
    20×
    20×

    20×

    20×

    1×
    81×
    81×
    81×

    81×
    20×

    81×
    25×
    3×
    22×
    22×

    56×
    12×

    44×
    44×
    44×

    44×
    26×
    26×

    44×
    15×
    3×

    15×
    3×
    3×

    41×

    1×

    44×

    44×

    44×
    18×

    26×
    37×
    26×

    37×

    1×

    28×
    28×
    28×

    28×
    28×
    23×
    23×

    5×
    5×

    28×

    28×
    5×

    28×

    1×
    13×

    1×
    5×

    1×
    35×
    34×

    34×
    34×
    18×
    18×

    18×

    34×

    1×

    1×
    4×

    4×

    4×

    4×
    4×
    4×

    4×

    1×

    1×
    93×
    93×

    93×

    1×
    92×

    92×

    92×

    1×
    1×
    1×
    1×

    1×

    1×
    455×
    1092×

    1×
    10×
    24×

    1×
    3×

    1×

     import $ from 'jquery';
    import func from './func';
    import lists from './lists';
    import env from './env';

    const NBSP_CHAR = String.fromCharCode(160);
    const ZERO_WIDTH_NBSP_CHAR = '\ufeff';

    /**
     * @method isEditable
     *
     * returns whether node is `note-editable` or not.
     *
     * @param {Node} node
     * @return {Boolean}
     */
    function isEditable(node) {
      return node && $(node).hasClass('note-editable');
    }

    /**
     * @method isControlSizing
     *
     * returns whether node is `note-control-sizing` or not.
     *
     * @param {Node} node
     * @return {Boolean}
     */
    function isControlSizing(node) {
      return node && $(node).hasClass('note-control-sizing');
    }

    /**
     * @method makePredByNodeName
     *
     * returns predicate which judge whether nodeName is same
     *
     * @param {String} nodeName
     * @return {Function}
     */
    function makePredByNodeName(nodeName) {
      nodeName = nodeName.toUpperCase();
      return function(node) {
        return node && node.nodeName.toUpperCase() === nodeName;
      };
    }

    /**
     * @method isText
     *
     *
     *
     * @param {Node} node
     * @return {Boolean} true if node's type is text(3)
     */
    function isText(node) {
      return node && node.nodeType === 3;
    }

    /**
     * @method isElement
     *
     *
     *
     * @param {Node} node
     * @return {Boolean} true if node's type is element(1)
     */
    function isElement(node) {
      return node && node.nodeType === 1;
    }

    /**
     * ex) br, col, embed, hr, img, input, ...
     * @see http://www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements
     */
    function isVoid(node) {
      return node && /^BR|^IMG|^HR|^IFRAME|^BUTTON|^INPUT|^VIDEO|^EMBED/.test(node.nodeName.toUpperCase());
    }

    function isPara(node) {
      if (isEditable(node)) {
        return false;
      }

      // Chrome(v31.0), FF(v25.0.1) use DIV for paragraph
      return node && /^DIV|^P|^LI|^H[1-7]/.test(node.nodeName.toUpperCase());
    }

    function isHeading(node) {
      return node && /^H[1-7]/.test(node.nodeName.toUpperCase());
    }

    const isPre = makePredByNodeName('PRE');

    const isLi = makePredByNodeName('LI');

    function isPurePara(node) {
      return isPara(node) && !isLi(node);
    }

    const isTable = makePredByNodeName('TABLE');

    const isData = makePredByNodeName('DATA');

    function isInline(node) {
      return !isBodyContainer(node) &&
             !isList(node) &&
             !isHr(node) &&
             !isPara(node) &&
             !isTable(node) &&
             !isBlockquote(node) &&
             !isData(node);
    }

    function isList(node) {
      return node && /^UL|^OL/.test(node.nodeName.toUpperCase());
    }

    const isHr = makePredByNodeName('HR');

    function isCell(node) {
      return node && /^TD|^TH/.test(node.nodeName.toUpperCase());
    }

    const isBlockquote = makePredByNodeName('BLOCKQUOTE');

    function isBodyContainer(node) {
      return isCell(node) || isBlockquote(node) || isEditable(node);
    }

    const isAnchor = makePredByNodeName('A');

    function isParaInline(node) {
      return isInline(node) && !!ancestor(node, isPara);
    }

    function isBodyInline(node) {
      return isInline(node) && !ancestor(node, isPara);
    }

    const isBody = makePredByNodeName('BODY');

    /**
     * returns whether nodeB is closest sibling of nodeA
     *
     * @param {Node} nodeA
     * @param {Node} nodeB
     * @return {Boolean}
     */
    function isClosestSibling(nodeA, nodeB) {
      return nodeA.nextSibling === nodeB ||
             nodeA.previousSibling === nodeB;
    }

    /**
     * returns array of closest siblings with node
     *
     * @param {Node} node
     * @param {function} [pred] - predicate function
     * @return {Node[]}
     */
    function withClosestSiblings(node, pred) {
      pred = pred || func.ok;

      const siblings = [];
      Iif (node.previousSibling && pred(node.previousSibling)) {
        siblings.push(node.previousSibling);
      }
      siblings.push(node);
      if (node.nextSibling && pred(node.nextSibling)) {
        siblings.push(node.nextSibling);
      }
      return siblings;
    }

    /**
     * blank HTML for cursor position
     * - [workaround] old IE only works with &nbsp;
     * - [workaround] IE11 and other browser works with bogus br
     */
    const blankHTML = env.isMSIE && env.browserVersion < 11 ? '&nbsp;' : '<br>';

    /**
     * @method nodeLength
     *
     * returns #text's text size or element's childNodes size
     *
     * @param {Node} node
     */
    function nodeLength(node) {
      if (isText(node)) {
        return node.nodeValue.length;
      }

      Eif (node) {
        return node.childNodes.length;
      }

      return 0;
    }

    /**
     * returns whether node is empty or not.
     *
     * @param {Node} node
     * @return {Boolean}
     */
    function isEmpty(node) {
      const len = nodeLength(node);

      if (len === 0) {
        return true;
      } else if (!isText(node) && len === 1 && node.innerHTML === blankHTML) {
        // ex) <p><br></p>, <span><br></span>
        return true;
      } else Iif (lists.all(node.childNodes, isText) && node.innerHTML === '') {
        // ex) <p></p>, <span></span>
        return true;
      }

      return false;
    }

    /**
     * padding blankHTML if node is empty (for cursor position)
     */
    function paddingBlankHTML(node) {
      if (!isVoid(node) && !nodeLength(node)) {
        node.innerHTML = blankHTML;
      }
    }

    /**
     * find nearest ancestor predicate hit
     *
     * @param {Node} node
     * @param {Function} pred - predicate function
     */
    function ancestor(node, pred) {
      while (node) {
        if (pred(node)) { return node; }
        if (isEditable(node)) { break; }

        node = node.parentNode;
      }
      return null;
    }

    /**
     * find nearest ancestor only single child blood line and predicate hit
     *
     * @param {Node} node
     * @param {Function} pred - predicate function
     */
    function singleChildAncestor(node, pred) {
      node = node.parentNode;

      while (node) {
        if (nodeLength(node) !== 1) { break; }
        if (pred(node)) { return node; }
        if (isEditable(node)) { break; }

        node = node.parentNode;
      }
      return null;
    }

    /**
     * returns new array of ancestor nodes (until predicate hit).
     *
     * @param {Node} node
     * @param {Function} [optional] pred - predicate function
     */
    function listAncestor(node, pred) {
      pred = pred || func.fail;

      const ancestors = [];
      ancestor(node, function(el) {
        if (!isEditable(el)) {
          ancestors.push(el);
        }

        return pred(el);
      });
      return ancestors;
    }

    /**
     * find farthest ancestor predicate hit
     */
    function lastAncestor(node, pred) {
      const ancestors = listAncestor(node);
      return lists.last(ancestors.filter(pred));
    }

    /**
     * returns common ancestor node between two nodes.
     *
     * @param {Node} nodeA
     * @param {Node} nodeB
     */
    function commonAncestor(nodeA, nodeB) {
      const ancestors = listAncestor(nodeA);
      for (let n = nodeB; n; n = n.parentNode) {
        if ($.inArray(n, ancestors) > -1) { return n; }
      }
      return null; // difference document area
    }

    /**
     * listing all previous siblings (until predicate hit).
     *
     * @param {Node} node
     * @param {Function} [optional] pred - predicate function
     */
    function listPrev(node, pred) {
      pred = pred || func.fail;

      const nodes = [];
      while (node) {
        if (pred(node)) { break; }
        nodes.push(node);
        node = node.previousSibling;
      }
      return nodes;
    }

    /**
     * listing next siblings (until predicate hit).
     *
     * @param {Node} node
     * @param {Function} [pred] - predicate function
     */
    function listNext(node, pred) {
      pred = pred || func.fail;

      const nodes = [];
      while (node) {
        if (pred(node)) { break; }
        nodes.push(node);
        node = node.nextSibling;
      }
      return nodes;
    }

    /**
     * listing descendant nodes
     *
     * @param {Node} node
     * @param {Function} [pred] - predicate function
     */
    function listDescendant(node, pred) {
      const descendants = [];
      pred = pred || func.ok;

      // start DFS(depth first search) with node
      (function fnWalk(current) {
        if (node !== current && pred(current)) {
          descendants.push(current);
        }
        for (let idx = 0, len = current.childNodes.length; idx < len; idx++) {
          fnWalk(current.childNodes[idx]);
        }
      })(node);

      return descendants;
    }

    /**
     * wrap node with new tag.
     *
     * @param {Node} node
     * @param {Node} tagName of wrapper
     * @return {Node} - wrapper
     */
    function wrap(node, wrapperName) {
      const parent = node.parentNode;
      const wrapper = $('<' + wrapperName + '>')[0];

      parent.insertBefore(wrapper, node);
      wrapper.appendChild(node);

      return wrapper;
    }

    /**
     * insert node after preceding
     *
     * @param {Node} node
     * @param {Node} preceding - predicate function
     */
    function insertAfter(node, preceding) {
      const next = preceding.nextSibling;
      let parent = preceding.parentNode;
      if (next) {
        parent.insertBefore(node, next);
      } else {
        parent.appendChild(node);
      }
      return node;
    }

    /**
     * append elements.
     *
     * @param {Node} node
     * @param {Collection} aChild
     */
    function appendChildNodes(node, aChild) {
      $.each(aChild, function(idx, child) {
        node.appendChild(child);
      });
      return node;
    }

    /**
     * returns whether boundaryPoint is left edge or not.
     *
     * @param {BoundaryPoint} point
     * @return {Boolean}
     */
    function isLeftEdgePoint(point) {
      return point.offset === 0;
    }

    /**
     * returns whether boundaryPoint is right edge or not.
     *
     * @param {BoundaryPoint} point
     * @return {Boolean}
     */
    function isRightEdgePoint(point) {
      return point.offset === nodeLength(point.node);
    }

    /**
     * returns whether boundaryPoint is edge or not.
     *
     * @param {BoundaryPoint} point
     * @return {Boolean}
     */
    function isEdgePoint(point) {
      return isLeftEdgePoint(point) || isRightEdgePoint(point);
    }

    /**
     * returns whether node is left edge of ancestor or not.
     *
     * @param {Node} node
     * @param {Node} ancestor
     * @return {Boolean}
     */
    function isLeftEdgeOf(node, ancestor) {
      while (node && node !== ancestor) {
        if (position(node) !== 0) {
          return false;
        }
        node = node.parentNode;
      }

      return true;
    }

    /**
     * returns whether node is right edge of ancestor or not.
     *
     * @param {Node} node
     * @param {Node} ancestor
     * @return {Boolean}
     */
    function isRightEdgeOf(node, ancestor) {
      Iif (!ancestor) {
        return false;
      }
      while (node && node !== ancestor) {
        if (position(node) !== nodeLength(node.parentNode) - 1) {
          return false;
        }
        node = node.parentNode;
      }

      return true;
    }

    /**
     * returns whether point is left edge of ancestor or not.
     * @param {BoundaryPoint} point
     * @param {Node} ancestor
     * @return {Boolean}
     */
    function isLeftEdgePointOf(point, ancestor) {
      return isLeftEdgePoint(point) && isLeftEdgeOf(point.node, ancestor);
    }

    /**
     * returns whether point is right edge of ancestor or not.
     * @param {BoundaryPoint} point
     * @param {Node} ancestor
     * @return {Boolean}
     */
    function isRightEdgePointOf(point, ancestor) {
      return isRightEdgePoint(point) && isRightEdgeOf(point.node, ancestor);
    }

    /**
     * returns offset from parent.
     *
     * @param {Node} node
     */
    function position(node) {
      let offset = 0;
      while ((node = node.previousSibling)) {
        offset += 1;
      }
      return offset;
    }

    function hasChildren(node) {
      return !!(node && node.childNodes && node.childNodes.length);
    }

    /**
     * returns previous boundaryPoint
     *
     * @param {BoundaryPoint} point
     * @param {Boolean} isSkipInnerOffset
     * @return {BoundaryPoint}
     */
    function prevPoint(point, isSkipInnerOffset) {
      let node;
      let offset;

      if (point.offset === 0) {
        Iif (isEditable(point.node)) {
          return null;
        }

        node = point.node.parentNode;
        offset = position(point.node);
      } else if (hasChildren(point.node)) {
        node = point.node.childNodes[point.offset - 1];
        offset = nodeLength(node);
      } else {
        node = point.node;
        offset = isSkipInnerOffset ? 0 : point.offset - 1;
      }

      return {
        node: node,
        offset: offset
      };
    }

    /**
     * returns next boundaryPoint
     *
     * @param {BoundaryPoint} point
     * @param {Boolean} isSkipInnerOffset
     * @return {BoundaryPoint}
     */
    function nextPoint(point, isSkipInnerOffset) {
      let node, offset;

      if (nodeLength(point.node) === point.offset) {
        Iif (isEditable(point.node)) {
          return null;
        }

        node = point.node.parentNode;
        offset = position(point.node) + 1;
      } else if (hasChildren(point.node)) {
        node = point.node.childNodes[point.offset];
        offset = 0;
      } else {
        node = point.node;
        offset = isSkipInnerOffset ? nodeLength(point.node) : point.offset + 1;
      }

      return {
        node: node,
        offset: offset
      };
    }

    /**
     * returns whether pointA and pointB is same or not.
     *
     * @param {BoundaryPoint} pointA
     * @param {BoundaryPoint} pointB
     * @return {Boolean}
     */
    function isSamePoint(pointA, pointB) {
      return pointA.node === pointB.node && pointA.offset === pointB.offset;
    }

    /**
     * returns whether point is visible (can set cursor) or not.
     *
     * @param {BoundaryPoint} point
     * @return {Boolean}
     */
    function isVisiblePoint(point) {
      if (isText(point.node) || !hasChildren(point.node) || isEmpty(point.node)) {
        return true;
      }

      const leftNode = point.node.childNodes[point.offset - 1];
      const rightNode = point.node.childNodes[point.offset];
      if ((!leftNode || isVoid(leftNode)) && (!rightNode || isVoid(rightNode))) {
        return true;
      }

      return false;
    }

    /**
     * @method prevPointUtil
     *
     * @param {BoundaryPoint} point
     * @param {Function} pred
     * @return {BoundaryPoint}
     */
    function prevPointUntil(point, pred) {
      while (point) {
        if (pred(point)) {
          return point;
        }

        point = prevPoint(point);
      }

      return null;
    }

    /**
     * @method nextPointUntil
     *
     * @param {BoundaryPoint} point
     * @param {Function} pred
     * @return {BoundaryPoint}
     */
    function nextPointUntil(point, pred) {
      while (point) {
        if (pred(point)) {
          return point;
        }

        point = nextPoint(point);
      }

      return null;
    }

    /**
     * returns whether point has character or not.
     *
     * @param {Point} point
     * @return {Boolean}
     */
    function isCharPoint(point) {
      Iif (!isText(point.node)) {
        return false;
      }

      const ch = point.node.nodeValue.charAt(point.offset - 1);
      return ch && (ch !== ' ' && ch !== NBSP_CHAR);
    }

    /**
     * @method walkPoint
     *
     * @param {BoundaryPoint} startPoint
     * @param {BoundaryPoint} endPoint
     * @param {Function} handler
     * @param {Boolean} isSkipInnerOffset
     */
    function walkPoint(startPoint, endPoint, handler, isSkipInnerOffset) {
      let point = startPoint;

      while (point) {
        handler(point);

        if (isSamePoint(point, endPoint)) {
          break;
        }

        const isSkipOffset = isSkipInnerOffset &&
                           startPoint.node !== point.node &&
                           endPoint.node !== point.node;
        point = nextPoint(point, isSkipOffset);
      }
    }

    /**
     * @method makeOffsetPath
     *
     * return offsetPath(array of offset) from ancestor
     *
     * @param {Node} ancestor - ancestor node
     * @param {Node} node
     */
    function makeOffsetPath(ancestor, node) {
      const ancestors = listAncestor(node, func.eq(ancestor));
      return ancestors.map(position).reverse();
    }

    /**
     * @method fromOffsetPath
     *
     * return element from offsetPath(array of offset)
     *
     * @param {Node} ancestor - ancestor node
     * @param {array} offsets - offsetPath
     */
    function fromOffsetPath(ancestor, offsets) {
      let current = ancestor;
      for (let i = 0, len = offsets.length; i < len; i++) {
        Iif (current.childNodes.length <= offsets[i]) {
          current = current.childNodes[current.childNodes.length - 1];
        } else {
          current = current.childNodes[offsets[i]];
        }
      }
      return current;
    }

    /**
     * @method splitNode
     *
     * split element or #text
     *
     * @param {BoundaryPoint} point
     * @param {Object} [options]
     * @param {Boolean} [options.isSkipPaddingBlankHTML] - default: false
     * @param {Boolean} [options.isNotSplitEdgePoint] - default: false
     * @param {Boolean} [options.isDiscardEmptySplits] - default: false
     * @return {Node} right node of boundaryPoint
     */
    function splitNode(point, options) {
      let isSkipPaddingBlankHTML = options && options.isSkipPaddingBlankHTML;
      const isNotSplitEdgePoint = options && options.isNotSplitEdgePoint;
      const isDiscardEmptySplits = options && options.isDiscardEmptySplits;

      if (isDiscardEmptySplits) {
        isSkipPaddingBlankHTML = true;
      }

      // edge case
      if (isEdgePoint(point) && (isText(point.node) || isNotSplitEdgePoint)) {
        if (isLeftEdgePoint(point)) {
          return point.node;
        } else Eif (isRightEdgePoint(point)) {
          return point.node.nextSibling;
        }
      }

      // split #text
      if (isText(point.node)) {
        return point.node.splitText(point.offset);
      } else {
        const childNode = point.node.childNodes[point.offset];
        const clone = insertAfter(point.node.cloneNode(false), point.node);
        appendChildNodes(clone, listNext(childNode));

        if (!isSkipPaddingBlankHTML) {
          paddingBlankHTML(point.node);
          paddingBlankHTML(clone);
        }

        if (isDiscardEmptySplits) {
          if (isEmpty(point.node)) {
            remove(point.node);
          }
          if (isEmpty(clone)) {
            remove(clone);
            return point.node.nextSibling;
          }
        }

        return clone;
      }
    }

    /**
     * @method splitTree
     *
     * split tree by point
     *
     * @param {Node} root - split root
     * @param {BoundaryPoint} point
     * @param {Object} [options]
     * @param {Boolean} [options.isSkipPaddingBlankHTML] - default: false
     * @param {Boolean} [options.isNotSplitEdgePoint] - default: false
     * @return {Node} right node of boundaryPoint
     */
    function splitTree(root, point, options) {
      // ex) [#text, <span>, <p>]
      const ancestors = listAncestor(point.node, func.eq(root));

      Iif (!ancestors.length) {
        return null;
      } else if (ancestors.length === 1) {
        return splitNode(point, options);
      }

      return ancestors.reduce(function(node, parent) {
        if (node === point.node) {
          node = splitNode(point, options);
        }

        return splitNode({
          node: parent,
          offset: node ? position(node) : nodeLength(parent)
        }, options);
      });
    }

    /**
     * split point
     *
     * @param {Point} point
     * @param {Boolean} isInline
     * @return {Object}
     */
    function splitPoint(point, isInline) {
      // find splitRoot, container
      //  - inline: splitRoot is a child of paragraph
      //  - block: splitRoot is a child of bodyContainer
      const pred = isInline ? isPara : isBodyContainer;
      const ancestors = listAncestor(point.node, pred);
      const topAncestor = lists.last(ancestors) || point.node;

      let splitRoot, container;
      if (pred(topAncestor)) {
        splitRoot = ancestors[ancestors.length - 2];
        container = topAncestor;
      } else {
        splitRoot = topAncestor;
        container = splitRoot.parentNode;
      }

      // if splitRoot is exists, split with splitTree
      let pivot = splitRoot && splitTree(splitRoot, point, {
        isSkipPaddingBlankHTML: isInline,
        isNotSplitEdgePoint: isInline
      });

      // if container is point.node, find pivot with point.offset
      if (!pivot && container === point.node) {
        pivot = point.node.childNodes[point.offset];
      }

      return {
        rightNode: pivot,
        container: container
      };
    }

    function create(nodeName) {
      return document.createElement(nodeName);
    }

    function createText(text) {
      return document.createTextNode(text);
    }

    /**
     * @method remove
     *
     * remove node, (isRemoveChild: remove child or not)
     *
     * @param {Node} node
     * @param {Boolean} isRemoveChild
     */
    function remove(node, isRemoveChild) {
      if (!node || !node.parentNode) { return; }
      Iif (node.removeNode) { return node.removeNode(isRemoveChild); }

      const parent = node.parentNode;
      if (!isRemoveChild) {
        const nodes = [];
        for (let i = 0, len = node.childNodes.length; i < len; i++) {
          nodes.push(node.childNodes[i]);
        }

        for (let i = 0, len = nodes.length; i < len; i++) {
          parent.insertBefore(nodes[i], node);
        }
      }

      parent.removeChild(node);
    }

    /**
     * @method removeWhile
     *
     * @param {Node} node
     * @param {Function} pred
     */
    function removeWhile(node, pred) {
      while (node) {
        if (isEditable(node) || !pred(node)) {
          break;
        }

        const parent = node.parentNode;
        remove(node);
        node = parent;
      }
    }

    /**
     * @method replace
     *
     * replace node with provided nodeName
     *
     * @param {Node} node
     * @param {String} nodeName
     * @return {Node} - new node
     */
    function replace(node, nodeName) {
      Iif (node.nodeName.toUpperCase() === nodeName.toUpperCase()) {
        return node;
      }

      const newNode = create(nodeName);

      Iif (node.style.cssText) {
        newNode.style.cssText = node.style.cssText;
      }

      appendChildNodes(newNode, lists.from(node.childNodes));
      insertAfter(newNode, node);
      remove(node);

      return newNode;
    }

    const isTextarea = makePredByNodeName('TEXTAREA');

    /**
     * @param {jQuery} $node
     * @param {Boolean} [stripLinebreaks] - default: false
     */
    function value($node, stripLinebreaks) {
      const val = isTextarea($node[0]) ? $node.val() : $node.html();
      Iif (stripLinebreaks) {
        return val.replace(/[\n\r]/g, '');
      }
      return val;
    }

    /**
     * @method html
     *
     * get the HTML contents of node
     *
     * @param {jQuery} $node
     * @param {Boolean} [isNewlineOnBlock]
     */
    function html($node, isNewlineOnBlock) {
      let markup = value($node);

      Iif (isNewlineOnBlock) {
        const regexTag = /<(\/?)(\b(?!!)[^>\s]*)(.*?)(\s*\/?>)/g;
        markup = markup.replace(regexTag, function(match, endSlash, name) {
          name = name.toUpperCase();
          const isEndOfInlineContainer = /^DIV|^TD|^TH|^P|^LI|^H[1-7]/.test(name) &&
                                       !!endSlash;
          const isBlockNode = /^BLOCKQUOTE|^TABLE|^TBODY|^TR|^HR|^UL|^OL/.test(name);

          return match + ((isEndOfInlineContainer || isBlockNode) ? '\n' : '');
        });
        markup = $.trim(markup);
      }

      return markup;
    }

    function posFromPlaceholder(placeholder) {
      const $placeholder = $(placeholder);
      const pos = $placeholder.offset();
      const height = $placeholder.outerHeight(true); // include margin

      return {
        left: pos.left,
        top: pos.top + height
      };
    }

    function attachEvents($node, events) {
      Object.keys(events).forEach(function(key) {
        $node.on(key, events[key]);
      });
    }

    function detachEvents($node, events) {
      Object.keys(events).forEach(function(key) {
        $node.off(key, events[key]);
      });
    }

    /**
     * @method isCustomStyleTag
     *
     * assert if a node contains a "note-styletag" class,
     * which implies that's a custom-made style tag node
     *
     * @param {Node} an HTML DOM node
     */
    function isCustomStyleTag(node) {
      return node && !isText(node) && lists.contains(node.classList, 'note-styletag');
    }

    export default {
      /** @property {String} NBSP_CHAR */
      NBSP_CHAR,
      /** @property {String} ZERO_WIDTH_NBSP_CHAR */
      ZERO_WIDTH_NBSP_CHAR,
      /** @property {String} blank */
      blank: blankHTML,
      /** @property {String} emptyPara */
      emptyPara: `<p>${blankHTML}</p>`,
      makePredByNodeName,
      isEditable,
      isControlSizing,
      isText,
      isElement,
      isVoid,
      isPara,
      isPurePara,
      isHeading,
      isInline,
      isBlock: func.not(isInline),
      isBodyInline,
      isBody,
      isParaInline,
      isPre,
      isList,
      isTable,
      isData,
      isCell,
      isBlockquote,
      isBodyContainer,
      isAnchor,
      isDiv: makePredByNodeName('DIV'),
      isLi,
      isBR: makePredByNodeName('BR'),
      isSpan: makePredByNodeName('SPAN'),
      isB: makePredByNodeName('B'),
      isU: makePredByNodeName('U'),
      isS: makePredByNodeName('S'),
      isI: makePredByNodeName('I'),
      isImg: makePredByNodeName('IMG'),
      isTextarea,
      isEmpty,
      isEmptyAnchor: func.and(isAnchor, isEmpty),
      isClosestSibling,
      withClosestSiblings,
      nodeLength,
      isLeftEdgePoint,
      isRightEdgePoint,
      isEdgePoint,
      isLeftEdgeOf,
      isRightEdgeOf,
      isLeftEdgePointOf,
      isRightEdgePointOf,
      prevPoint,
      nextPoint,
      isSamePoint,
      isVisiblePoint,
      prevPointUntil,
      nextPointUntil,
      isCharPoint,
      walkPoint,
      ancestor,
      singleChildAncestor,
      listAncestor,
      lastAncestor,
      listNext,
      listPrev,
      listDescendant,
      commonAncestor,
      wrap,
      insertAfter,
      appendChildNodes,
      position,
      hasChildren,
      makeOffsetPath,
      fromOffsetPath,
      splitTree,
      splitPoint,
      create,
      createText,
      remove,
      removeWhile,
      replace,
      html,
      value,
      posFromPlaceholder,
      attachEvents,
      detachEvents,
      isCustomStyleTag
    };