@oat-sa/tao-core-ui 1.64.1 → 1.64.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +339 -339
- package/README.md +22 -22
- package/dist/actionbar.js +92 -92
- package/dist/adder.js +109 -109
- package/dist/animable/absorbable/absorbable.js +29 -29
- package/dist/animable/absorbable/css/absorb.css +7 -7
- package/dist/animable/absorbable/css/absorb.css.map +1 -1
- package/dist/animable/pulsable/css/pulse.css +7 -7
- package/dist/animable/pulsable/css/pulse.css.map +1 -1
- package/dist/animable/pulsable/pulsable.js +23 -23
- package/dist/areaBroker.js +51 -51
- package/dist/autocomplete/css/autocomplete.css +7 -7
- package/dist/autocomplete/css/autocomplete.css.map +1 -1
- package/dist/autocomplete.js +384 -384
- package/dist/autoscroll.js +22 -22
- package/dist/badge/badge.js +35 -35
- package/dist/badge/css/badge.css +7 -7
- package/dist/badge/css/badge.css.map +1 -1
- package/dist/breadcrumbs/css/breadcrumbs.css +7 -7
- package/dist/breadcrumbs/css/breadcrumbs.css.map +1 -1
- package/dist/breadcrumbs.js +57 -57
- package/dist/btngrouper.js +64 -64
- package/dist/bulkActionPopup/css/bulkActionPopup.css +7 -7
- package/dist/bulkActionPopup/css/bulkActionPopup.css.map +1 -1
- package/dist/bulkActionPopup.js +91 -91
- package/dist/button.js +89 -89
- package/dist/calculator/css/calculator.css +10 -10
- package/dist/calculator/css/calculator.css.map +1 -1
- package/dist/calculator.js +51 -51
- package/dist/cascadingComboBox.js +34 -34
- package/dist/ckeditor/ckConfigurator.js +49 -49
- package/dist/ckeditor/dtdHandler.js +110 -110
- package/dist/class/css/selector.css +7 -7
- package/dist/class/css/selector.css.map +1 -1
- package/dist/class/selector.js +98 -98
- package/dist/component/alignable.js +81 -81
- package/dist/component/containable.js +37 -37
- package/dist/component/css/components.css +7 -7
- package/dist/component/css/components.css.map +1 -1
- package/dist/component/css/windowComponent.css +7 -7
- package/dist/component/css/windowComponent.css.map +1 -1
- package/dist/component/draggable.js +25 -25
- package/dist/component/placeable.js +70 -70
- package/dist/component/resizable.js +61 -61
- package/dist/component/stackable.js +20 -20
- package/dist/component/windowed.js +46 -46
- package/dist/component.js +140 -140
- package/dist/container.js +76 -76
- package/dist/contextualPopup/css/contextualPopup.css +7 -7
- package/dist/contextualPopup/css/contextualPopup.css.map +1 -1
- package/dist/contextualPopup.js +100 -100
- package/dist/dashboard/css/dashboard.css +7 -7
- package/dist/dashboard/css/dashboard.css.map +1 -1
- package/dist/dashboard.js +62 -62
- package/dist/datalist/css/datalist.css +7 -7
- package/dist/datalist/css/datalist.css.map +1 -1
- package/dist/datalist.js +147 -147
- package/dist/datatable/css/datatable.css +7 -7
- package/dist/datatable/css/datatable.css.map +1 -1
- package/dist/datatable/filterStrategy/filterStrategy.js +27 -27
- package/dist/datatable/filterStrategy/multiple.js +25 -25
- package/dist/datatable/filterStrategy/single.js +25 -25
- package/dist/datatable.js +377 -377
- package/dist/dateRange/css/dateRange.css +7 -7
- package/dist/dateRange/css/dateRange.css.map +1 -1
- package/dist/dateRange/dateRange.js +97 -97
- package/dist/datetime/css/picker.css +7 -7
- package/dist/datetime/css/picker.css.map +1 -1
- package/dist/datetime/picker.js +161 -161
- package/dist/deleter.js +79 -79
- package/dist/destination/css/selector.css +7 -7
- package/dist/destination/css/selector.css.map +1 -1
- package/dist/destination/selector.js +52 -52
- package/dist/dialog/alert.js +26 -26
- package/dist/dialog/confirm.js +27 -27
- package/dist/dialog/confirmDelete.js +31 -31
- package/dist/dialog.js +141 -141
- package/dist/disabler.js +90 -90
- package/dist/documentViewer/css/documentViewer.css +7 -7
- package/dist/documentViewer/css/documentViewer.css.map +1 -1
- package/dist/documentViewer/providers/pdfViewer/fallback/viewer.js +30 -30
- package/dist/documentViewer/providers/pdfViewer/pdfjs/areaBroker.js +25 -25
- package/dist/documentViewer/providers/pdfViewer/pdfjs/findBar.js +120 -120
- package/dist/documentViewer/providers/pdfViewer/pdfjs/pageView.js +89 -89
- package/dist/documentViewer/providers/pdfViewer/pdfjs/pagesManager.js +52 -52
- package/dist/documentViewer/providers/pdfViewer/pdfjs/searchEngine.js +112 -112
- package/dist/documentViewer/providers/pdfViewer/pdfjs/textManager.js +67 -67
- package/dist/documentViewer/providers/pdfViewer/pdfjs/viewer.js +81 -81
- package/dist/documentViewer/providers/pdfViewer/pdfjs/wrapper.js +111 -111
- package/dist/documentViewer/providers/pdfViewer.js +29 -29
- package/dist/documentViewer/viewerFactory.js +71 -71
- package/dist/documentViewer.js +86 -86
- package/dist/dropdown/css/dropdown.css +7 -7
- package/dist/dropdown/css/dropdown.css.map +1 -1
- package/dist/dropdown.js +84 -84
- package/dist/durationer.js +58 -58
- package/dist/dynamicComponent/css/dynamicComponent.css +7 -7
- package/dist/dynamicComponent/css/dynamicComponent.css.map +1 -1
- package/dist/dynamicComponent.js +103 -103
- package/dist/feedback.js +84 -84
- package/dist/figure/FigureStateActive.js +111 -111
- package/dist/filesender.js +26 -26
- package/dist/filter.js +34 -34
- package/dist/form/css/dropdownForm.css +7 -7
- package/dist/form/css/dropdownForm.css.map +1 -1
- package/dist/form/css/form.css +7 -7
- package/dist/form/css/form.css.map +1 -1
- package/dist/form/dropdownForm.js +99 -99
- package/dist/form/form.js +248 -248
- package/dist/form/simpleForm.js +71 -71
- package/dist/form/validator/css/validator.css +7 -7
- package/dist/form/validator/css/validator.css.map +1 -1
- package/dist/form/validator/renderer.js +52 -52
- package/dist/form/validator/validator.js +103 -103
- package/dist/form/widget/css/widget.css +7 -7
- package/dist/form/widget/css/widget.css.map +1 -1
- package/dist/form/widget/definitions.js +24 -24
- package/dist/form/widget/loader.js +16 -16
- package/dist/form/widget/providers/checkBox.js +62 -62
- package/dist/form/widget/providers/comboBox.js +46 -46
- package/dist/form/widget/providers/default.js +35 -35
- package/dist/form/widget/providers/hidden.js +37 -37
- package/dist/form/widget/providers/hiddenBox.js +58 -58
- package/dist/form/widget/providers/radioBox.js +57 -57
- package/dist/form/widget/providers/textArea.js +35 -35
- package/dist/form/widget/providers/textBox.js +34 -34
- package/dist/form/widget/widget.js +141 -141
- package/dist/form.js +10 -10
- package/dist/formValidator/formValidator.js +61 -61
- package/dist/formValidator/highlighters/highlighter.js +41 -41
- package/dist/formValidator/highlighters/message.js +29 -29
- package/dist/formValidator/highlighters/tooltip.js +32 -32
- package/dist/generis/form/css/form.css +7 -7
- package/dist/generis/form/css/form.css.map +1 -1
- package/dist/generis/form/form.js +73 -73
- package/dist/generis/validator/css/validator.css +7 -7
- package/dist/generis/validator/css/validator.css.map +1 -1
- package/dist/generis/validator/validator.js +38 -38
- package/dist/generis/widget/checkBox/checkBox.js +39 -39
- package/dist/generis/widget/comboBox/comboBox.js +32 -32
- package/dist/generis/widget/css/widget.css +7 -7
- package/dist/generis/widget/css/widget.css.map +1 -1
- package/dist/generis/widget/hiddenBox/hiddenBox.js +40 -40
- package/dist/generis/widget/loader.js +20 -20
- package/dist/generis/widget/textBox/textBox.js +27 -27
- package/dist/generis/widget/widget.js +47 -47
- package/dist/groupedComboBox.js +36 -36
- package/dist/groupvalidator.js +19 -19
- package/dist/hider.js +41 -41
- package/dist/highlighter.js +278 -278
- package/dist/image/ImgStateActive/extractLabel.js +20 -20
- package/dist/image/ImgStateActive/helper.js +16 -16
- package/dist/image/ImgStateActive/initHelper.js +85 -85
- package/dist/image/ImgStateActive/initMediaEditor.js +48 -48
- package/dist/image/ImgStateActive/mediaSizer.js +32 -32
- package/dist/image/ImgStateActive.js +104 -104
- package/dist/incrementer.js +58 -58
- package/dist/inplacer.js +87 -87
- package/dist/interactUtils.js +42 -42
- package/dist/itemButtonList/css/item-button-list.css +23 -23
- package/dist/itemButtonList/css/item-button-list.css.map +1 -1
- package/dist/itemButtonList.js +102 -102
- package/dist/keyNavigation/navigableDomElement.js +76 -76
- package/dist/keyNavigation/navigator.js +158 -158
- package/dist/listbox/css/listbox.css +7 -7
- package/dist/listbox/css/listbox.css.map +1 -1
- package/dist/listbox.js +84 -84
- package/dist/liststyler.js +57 -57
- package/dist/loadingButton/css/button.css +7 -7
- package/dist/loadingButton/css/button.css.map +1 -1
- package/dist/loadingButton/loadingButton.js +35 -35
- package/dist/lock.js +112 -112
- package/dist/login/login.js +87 -87
- package/dist/maths/calculator/basicCalculator.js +50 -50
- package/dist/maths/calculator/calculatorComponent.js +29 -29
- package/dist/maths/calculator/core/areaBroker.js +25 -25
- package/dist/maths/calculator/core/board.js +300 -300
- package/dist/maths/calculator/core/expression.js +98 -98
- package/dist/maths/calculator/core/labels.js +16 -16
- package/dist/maths/calculator/core/plugin.js +19 -19
- package/dist/maths/calculator/core/terms.js +26 -26
- package/dist/maths/calculator/core/tokenizer.js +98 -98
- package/dist/maths/calculator/core/tokens.js +75 -75
- package/dist/maths/calculator/css/calculator.css +7 -7
- package/dist/maths/calculator/css/calculator.css.map +1 -1
- package/dist/maths/calculator/defaultCalculator.js +23 -23
- package/dist/maths/calculator/plugins/core/degrad.js +22 -22
- package/dist/maths/calculator/plugins/core/history.js +45 -45
- package/dist/maths/calculator/plugins/core/remind.js +22 -22
- package/dist/maths/calculator/plugins/core/stepNavigation.js +31 -31
- package/dist/maths/calculator/plugins/keyboard/templateKeyboard/templateKeyboard.js +48 -48
- package/dist/maths/calculator/plugins/modifiers/pow10.js +35 -35
- package/dist/maths/calculator/plugins/modifiers/sign.js +90 -90
- package/dist/maths/calculator/plugins/screen/simpleScreen/simpleScreen.js +47 -47
- package/dist/maths/calculator/pluginsLoader.js +21 -21
- package/dist/maths/calculator/scientificCalculator.js +78 -78
- package/dist/mediaEditor/mediaEditorComponent.js +51 -51
- package/dist/mediaEditor/plugins/mediaAlignment/helper.js +16 -16
- package/dist/mediaEditor/plugins/mediaAlignment/mediaAlignmentComponent.js +47 -47
- package/dist/mediaEditor/plugins/mediaAlignment/style.css +7 -7
- package/dist/mediaEditor/plugins/mediaDimension/helper.js +70 -70
- package/dist/mediaEditor/plugins/mediaDimension/mediaDimensionComponent.js +140 -140
- package/dist/mediaEditor/plugins/mediaDimension/style.css +141 -141
- package/dist/mediaplayer/css/player.css +7 -7
- package/dist/mediaplayer/css/player.css.map +1 -1
- package/dist/mediaplayer/players/html5.js +52 -52
- package/dist/mediaplayer/players/youtube.js +39 -39
- package/dist/mediaplayer/players.js +18 -18
- package/dist/mediaplayer/support.js +55 -55
- package/dist/mediaplayer/utils/reminder.js +100 -100
- package/dist/mediaplayer/utils/timeObserver.js +92 -92
- package/dist/mediaplayer/youtubeManager.js +51 -51
- package/dist/mediaplayer.js +447 -447
- package/dist/mediasizer.js +122 -122
- package/dist/modal.js +87 -87
- package/dist/movableComponent.js +35 -35
- package/dist/pageSizeSelector.js +31 -31
- package/dist/pageStatus.js +33 -33
- package/dist/pagination/css/pagination.css +7 -7
- package/dist/pagination/css/pagination.css.map +1 -1
- package/dist/pagination/paginationStrategy.js +23 -23
- package/dist/pagination/providers/pages.js +24 -24
- package/dist/pagination/providers/simple.js +22 -22
- package/dist/pagination.js +45 -45
- package/dist/previewer.js +67 -67
- package/dist/progressbar.js +58 -58
- package/dist/propertySelector/css/propertySelector.css +7 -7
- package/dist/propertySelector/css/propertySelector.css.map +1 -1
- package/dist/propertySelector/propertySelector.js +86 -86
- package/dist/report.js +73 -73
- package/dist/resource/css/selector.css +7 -7
- package/dist/resource/css/selector.css.map +1 -1
- package/dist/resource/filters.js +60 -60
- package/dist/resource/list.js +53 -53
- package/dist/resource/selectable.js +92 -92
- package/dist/resource/selector.js +182 -182
- package/dist/resource/tree.js +91 -91
- package/dist/resourcemgr/css/resourcemgr.css +7 -7
- package/dist/resourcemgr/css/resourcemgr.css.map +1 -1
- package/dist/resourcemgr/fileBrowser.js +75 -75
- package/dist/resourcemgr/fileSelector.js +45 -45
- package/dist/resourcemgr/util/updatePermissions.js +4 -4
- package/dist/resourcemgr.js +49 -49
- package/dist/scroller.js +26 -26
- package/dist/searchModal/advancedSearch.js +155 -155
- package/dist/searchModal/css/advancedSearch.css +7 -7
- package/dist/searchModal/css/advancedSearch.css.map +1 -1
- package/dist/searchModal/css/searchModal.css +7 -7
- package/dist/searchModal/css/searchModal.css.map +1 -1
- package/dist/searchModal.js +134 -134
- package/dist/selecter.js +6 -6
- package/dist/stacker.js +43 -43
- package/dist/switch/css/switch.css +7 -7
- package/dist/switch/css/switch.css.map +1 -1
- package/dist/switch/switch.js +62 -62
- package/dist/tableModel.js +33 -33
- package/dist/tabs/css/tabs.css +12 -12
- package/dist/tabs/css/tabs.css.map +1 -1
- package/dist/tabs.js +220 -220
- package/dist/taskQueue/css/taskQueue.css +7 -7
- package/dist/taskQueue/css/taskQueue.css.map +1 -1
- package/dist/taskQueue/status.js +59 -59
- package/dist/taskQueue/table.js +54 -54
- package/dist/taskQueue/taskQueue.js +18 -18
- package/dist/taskQueue/taskQueueModel.js +102 -102
- package/dist/taskQueue.js +47 -47
- package/dist/taskQueueButton/css/taskable.css +7 -7
- package/dist/taskQueueButton/css/taskable.css.map +1 -1
- package/dist/taskQueueButton/css/treeButton.css +7 -7
- package/dist/taskQueueButton/css/treeButton.css.map +1 -1
- package/dist/taskQueueButton/standardButton.js +39 -39
- package/dist/taskQueueButton/taskable.js +41 -41
- package/dist/taskQueueButton/treeButton.js +43 -43
- package/dist/themeLoader.js +75 -75
- package/dist/themes.js +84 -84
- package/dist/toggler.js +57 -57
- package/dist/tooltip.js +41 -41
- package/dist/tooltipster.js +17 -17
- package/dist/transformer.js +117 -117
- package/dist/tristateCheckboxGroup/css/tristateCheckboxGroup.css +7 -7
- package/dist/tristateCheckboxGroup/css/tristateCheckboxGroup.css.map +1 -1
- package/dist/tristateCheckboxGroup.js +62 -62
- package/dist/uploader.js +145 -145
- package/dist/validator/validators.js +48 -48
- package/dist/validator.js +23 -23
- package/dist/waitForMedia.js +33 -33
- package/dist/waitingDialog/css/waitingDialog.css +7 -7
- package/dist/waitingDialog/css/waitingDialog.css.map +1 -1
- package/dist/waitingDialog/waitingDialog.js +54 -54
- package/package.json +110 -110
- package/scss/basic.scss +16 -16
- package/scss/ckeditor/skins/tao/scss/inc/_ck-icons.scss +60 -60
- package/scss/ckeditor/skins/tao/scss/inc/_tao.scss +59 -59
- package/scss/font/tao/tao.svg +235 -235
- package/scss/inc/_base.scss +495 -495
- package/scss/inc/_bootstrap.scss +6 -6
- package/scss/inc/_buttons.scss +114 -114
- package/scss/inc/_colors.scss +93 -93
- package/scss/inc/_feedback.scss +154 -154
- package/scss/inc/_flex-grid.scss +15 -15
- package/scss/inc/_fonts.scss +4 -4
- package/scss/inc/_forms.scss +832 -832
- package/scss/inc/_functions.scss +283 -283
- package/scss/inc/_jquery.nouislider.scss +254 -254
- package/scss/inc/_normalize.scss +528 -528
- package/scss/inc/_report.scss +67 -67
- package/scss/inc/_secondary-properties.scss +89 -89
- package/scss/inc/_select2.scss +634 -634
- package/scss/inc/_toolbars.scss +155 -155
- package/scss/inc/_tooltip.scss +312 -312
- package/scss/inc/_variables.scss +21 -21
- package/scss/inc/base/_highlight.scss +5 -5
- package/scss/inc/base/_list-style.scss +58 -58
- package/scss/inc/base/_svg.scss +3 -3
- package/scss/inc/base/_table.scss +62 -62
- package/scss/inc/fonts/_source-sans-pro.scss +29 -29
- package/scss/inc/fonts/_tao-icon-classes.scss +227 -227
- package/scss/inc/fonts/_tao-icon-def.scss +12 -12
- package/scss/inc/fonts/_tao-icon-vars.scss +241 -241
- package/src/actionbar/tpl/main.tpl +8 -8
- package/src/actionbar.js +251 -251
- package/src/adder.js +250 -250
- package/src/animable/absorbable/absorbable.js +134 -134
- package/src/animable/absorbable/css/absorb.css +7 -7
- package/src/animable/absorbable/css/absorb.css.map +1 -1
- package/src/animable/absorbable/scss/absorb.scss +37 -37
- package/src/animable/pulsable/css/pulse.css +7 -7
- package/src/animable/pulsable/css/pulse.css.map +1 -1
- package/src/animable/pulsable/pulsable.js +90 -90
- package/src/animable/pulsable/scss/pulse.scss +22 -22
- package/src/areaBroker.js +160 -160
- package/src/autocomplete/css/autocomplete.css +7 -7
- package/src/autocomplete/css/autocomplete.css.map +1 -1
- package/src/autocomplete/scss/autocomplete.scss +37 -37
- package/src/autocomplete.js +1027 -1027
- package/src/autoscroll.js +57 -57
- package/src/badge/badge.js +119 -119
- package/src/badge/css/badge.css +7 -7
- package/src/badge/css/badge.css.map +1 -1
- package/src/badge/scss/badge.scss +92 -92
- package/src/badge/tpl/badge.tpl +4 -4
- package/src/breadcrumbs/css/breadcrumbs.css +7 -7
- package/src/breadcrumbs/css/breadcrumbs.css.map +1 -1
- package/src/breadcrumbs/scss/breadcrumbs.scss +52 -52
- package/src/breadcrumbs/tpl/breadcrumbs.tpl +20 -20
- package/src/breadcrumbs.js +99 -99
- package/src/btngrouper.js +213 -213
- package/src/bulkActionPopup/css/bulkActionPopup.css +7 -7
- package/src/bulkActionPopup/css/bulkActionPopup.css.map +1 -1
- package/src/bulkActionPopup/scss/bulkActionPopup.scss +63 -63
- package/src/bulkActionPopup/tpl/layout.tpl +76 -76
- package/src/bulkActionPopup/tpl/select.tpl +8 -8
- package/src/bulkActionPopup.js +274 -274
- package/src/button/tpl/button.tpl +4 -4
- package/src/button.js +135 -135
- package/src/calculator/css/calculator.css +10 -10
- package/src/calculator/css/calculator.css.map +1 -1
- package/src/calculator/scss/calculator.scss +139 -139
- package/src/calculator.js +188 -188
- package/src/cascadingComboBox.js +126 -126
- package/src/ckeditor/ckConfigurator.js +742 -742
- package/src/ckeditor/dtdHandler.js +1030 -1030
- package/src/class/css/selector.css +7 -7
- package/src/class/css/selector.css.map +1 -1
- package/src/class/scss/selector.scss +101 -101
- package/src/class/selector.js +329 -329
- package/src/class/tpl/listItem.tpl +9 -9
- package/src/class/tpl/selector.tpl +10 -10
- package/src/component/alignable.js +274 -274
- package/src/component/containable.js +122 -122
- package/src/component/css/components.css +7 -7
- package/src/component/css/components.css.map +1 -1
- package/src/component/css/windowComponent.css +7 -7
- package/src/component/css/windowComponent.css.map +1 -1
- package/src/component/draggable.js +104 -104
- package/src/component/placeable.js +233 -233
- package/src/component/resizable.js +195 -195
- package/src/component/scss/components.scss +507 -507
- package/src/component/scss/windowComponent.scss +62 -62
- package/src/component/stackable.js +67 -67
- package/src/component/tpl/window.tpl +7 -7
- package/src/component/windowed.js +206 -206
- package/src/component.js +401 -401
- package/src/container.js +200 -200
- package/src/contextualPopup/css/contextualPopup.css +7 -7
- package/src/contextualPopup/css/contextualPopup.css.map +1 -1
- package/src/contextualPopup/scss/contextualPopup.scss +78 -78
- package/src/contextualPopup/tpl/popup.tpl +10 -10
- package/src/contextualPopup.js +294 -294
- package/src/css/basic.css +104 -104
- package/src/css/basic.css.map +1 -1
- package/src/dashboard/css/dashboard.css +7 -7
- package/src/dashboard/css/dashboard.css.map +1 -1
- package/src/dashboard/scss/dashboard.scss +93 -93
- package/src/dashboard/tpl/dashboard.tpl +16 -16
- package/src/dashboard/tpl/dashboardMetricsList.tpl +15 -15
- package/src/dashboard.js +179 -179
- package/src/datalist/css/datalist.css +7 -7
- package/src/datalist/css/datalist.css.map +1 -1
- package/src/datalist/scss/datalist.scss +116 -116
- package/src/datalist/tpl/list.tpl +24 -24
- package/src/datalist/tpl/main.tpl +44 -44
- package/src/datalist.js +500 -500
- package/src/datatable/css/datatable.css +7 -7
- package/src/datatable/css/datatable.css.map +1 -1
- package/src/datatable/filterStrategy/filterStrategy.js +70 -70
- package/src/datatable/filterStrategy/multiple.js +126 -126
- package/src/datatable/filterStrategy/single.js +108 -108
- package/src/datatable/scss/datatable.scss +149 -149
- package/src/datatable/tpl/button.tpl +6 -6
- package/src/datatable/tpl/layout.tpl +158 -158
- package/src/datatable.js +1056 -1056
- package/src/dateRange/css/dateRange.css +7 -7
- package/src/dateRange/css/dateRange.css.map +1 -1
- package/src/dateRange/dateRange.js +341 -341
- package/src/dateRange/scss/dateRange.scss +7 -7
- package/src/dateRange/tpl/select.tpl +18 -18
- package/src/datetime/css/picker.css +7 -7
- package/src/datetime/css/picker.css.map +1 -1
- package/src/datetime/picker.js +576 -576
- package/src/datetime/scss/picker.scss +192 -192
- package/src/datetime/tpl/picker.tpl +18 -18
- package/src/deleter/undo.tpl +6 -6
- package/src/deleter.js +296 -296
- package/src/destination/css/selector.css +7 -7
- package/src/destination/css/selector.css.map +1 -1
- package/src/destination/scss/selector.scss +36 -36
- package/src/destination/selector.js +195 -195
- package/src/destination/tpl/selector.tpl +13 -13
- package/src/dialog/alert.js +70 -70
- package/src/dialog/confirm.js +85 -85
- package/src/dialog/confirmDelete.js +95 -95
- package/src/dialog/tpl/body.tpl +24 -24
- package/src/dialog/tpl/buttons.tpl +6 -6
- package/src/dialog/tpl/checkbox.tpl +5 -5
- package/src/dialog.js +517 -517
- package/src/disabler.js +230 -230
- package/src/documentViewer/css/documentViewer.css +7 -7
- package/src/documentViewer/css/documentViewer.css.map +1 -1
- package/src/documentViewer/providers/pdfViewer/fallback/viewer.js +69 -69
- package/src/documentViewer/providers/pdfViewer/pdfjs/areaBroker.js +41 -41
- package/src/documentViewer/providers/pdfViewer/pdfjs/findBar.js +475 -475
- package/src/documentViewer/providers/pdfViewer/pdfjs/findBar.tpl +20 -20
- package/src/documentViewer/providers/pdfViewer/pdfjs/match.tpl +1 -1
- package/src/documentViewer/providers/pdfViewer/pdfjs/page.tpl +4 -4
- package/src/documentViewer/providers/pdfViewer/pdfjs/pageView.js +318 -318
- package/src/documentViewer/providers/pdfViewer/pdfjs/pagesManager.js +167 -167
- package/src/documentViewer/providers/pdfViewer/pdfjs/searchEngine.js +451 -451
- package/src/documentViewer/providers/pdfViewer/pdfjs/textManager.js +252 -252
- package/src/documentViewer/providers/pdfViewer/pdfjs/viewer.js +299 -299
- package/src/documentViewer/providers/pdfViewer/pdfjs/viewer.tpl +16 -16
- package/src/documentViewer/providers/pdfViewer/pdfjs/wrapper.js +351 -351
- package/src/documentViewer/providers/pdfViewer.js +93 -93
- package/src/documentViewer/scss/documentViewer.scss +184 -184
- package/src/documentViewer/viewerFactory.js +191 -191
- package/src/documentViewer.js +238 -238
- package/src/dropdown/css/dropdown.css +7 -7
- package/src/dropdown/css/dropdown.css.map +1 -1
- package/src/dropdown/scss/dropdown.scss +99 -99
- package/src/dropdown/tpl/dropdown.tpl +8 -8
- package/src/dropdown/tpl/list-item.tpl +4 -4
- package/src/dropdown.js +255 -255
- package/src/durationer.js +220 -220
- package/src/dynamicComponent/css/dynamicComponent.css +7 -7
- package/src/dynamicComponent/css/dynamicComponent.css.map +1 -1
- package/src/dynamicComponent/scss/dynamicComponent.scss +98 -98
- package/src/dynamicComponent/tpl/layout.tpl +17 -17
- package/src/dynamicComponent.js +553 -553
- package/src/feedback/feedback.tpl +7 -7
- package/src/feedback.js +295 -295
- package/src/figure/FigureStateActive.js +184 -184
- package/src/filesender.js +112 -112
- package/src/filter/template.tpl +5 -5
- package/src/filter.js +129 -129
- package/src/form/css/dropdownForm.css +7 -7
- package/src/form/css/dropdownForm.css.map +1 -1
- package/src/form/css/form.css +7 -7
- package/src/form/css/form.css.map +1 -1
- package/src/form/dropdownForm.js +281 -281
- package/src/form/form.js +688 -688
- package/src/form/scss/dropdownForm.scss +60 -60
- package/src/form/scss/form.scss +25 -25
- package/src/form/simpleForm.js +125 -125
- package/src/form/tpl/dropdownForm.tpl +4 -4
- package/src/form/tpl/form.tpl +7 -7
- package/src/form/validator/css/validator.css +7 -7
- package/src/form/validator/css/validator.css.map +1 -1
- package/src/form/validator/renderer.js +118 -118
- package/src/form/validator/scss/validator.scss +14 -14
- package/src/form/validator/tpl/message.tpl +1 -1
- package/src/form/validator/tpl/validator.tpl +1 -1
- package/src/form/validator/validator.js +220 -220
- package/src/form/widget/css/widget.css +7 -7
- package/src/form/widget/css/widget.css.map +1 -1
- package/src/form/widget/definitions.js +51 -51
- package/src/form/widget/loader.js +40 -40
- package/src/form/widget/providers/checkBox.js +138 -138
- package/src/form/widget/providers/comboBox.js +63 -63
- package/src/form/widget/providers/default.js +90 -90
- package/src/form/widget/providers/hidden.js +62 -62
- package/src/form/widget/providers/hiddenBox.js +152 -152
- package/src/form/widget/providers/radioBox.js +99 -99
- package/src/form/widget/providers/textArea.js +52 -52
- package/src/form/widget/providers/textBox.js +48 -48
- package/src/form/widget/scss/widget.scss +55 -55
- package/src/form/widget/tpl/checkBox.tpl +25 -25
- package/src/form/widget/tpl/comboBox.tpl +13 -13
- package/src/form/widget/tpl/hidden.tpl +1 -1
- package/src/form/widget/tpl/hiddenBox.tpl +17 -17
- package/src/form/widget/tpl/label.tpl +6 -6
- package/src/form/widget/tpl/radioBox.tpl +25 -25
- package/src/form/widget/tpl/textArea.tpl +8 -8
- package/src/form/widget/tpl/widget.tpl +8 -8
- package/src/form/widget/widget.js +372 -372
- package/src/form.js +47 -47
- package/src/formValidator/formValidator.js +253 -253
- package/src/formValidator/highlighters/highlighter.js +103 -103
- package/src/formValidator/highlighters/message.js +68 -68
- package/src/formValidator/highlighters/tooltip.js +78 -78
- package/src/generis/form/css/form.css +7 -7
- package/src/generis/form/css/form.css.map +1 -1
- package/src/generis/form/form.js +239 -239
- package/src/generis/form/readme.md +70 -70
- package/src/generis/form/scss/form.scss +23 -23
- package/src/generis/form/tpl/form.tpl +16 -16
- package/src/generis/validator/css/validator.css +7 -7
- package/src/generis/validator/css/validator.css.map +1 -1
- package/src/generis/validator/readme.md +46 -46
- package/src/generis/validator/scss/validator.scss +13 -13
- package/src/generis/validator/validator.js +128 -128
- package/src/generis/widget/checkBox/checkBox.js +112 -112
- package/src/generis/widget/checkBox/checkBox.tpl +18 -18
- package/src/generis/widget/comboBox/comboBox.js +66 -66
- package/src/generis/widget/comboBox/comboBox.tpl +12 -12
- package/src/generis/widget/css/widget.css +7 -7
- package/src/generis/widget/css/widget.css.map +1 -1
- package/src/generis/widget/hiddenBox/hiddenBox.js +131 -131
- package/src/generis/widget/hiddenBox/hiddenBox.tpl +16 -16
- package/src/generis/widget/loader.js +49 -49
- package/src/generis/widget/readme.md +59 -59
- package/src/generis/widget/scss/widget.scss +61 -61
- package/src/generis/widget/textBox/textBox.js +64 -64
- package/src/generis/widget/textBox/textBox.tpl +7 -7
- package/src/generis/widget/widget.js +164 -164
- package/src/generis/widget/widget.tpl +5 -5
- package/src/groupedComboBox.js +99 -99
- package/src/groupvalidator.js +84 -84
- package/src/hider.js +88 -88
- package/src/highlighter.js +1192 -1192
- package/src/image/ImgStateActive/extractLabel.js +29 -29
- package/src/image/ImgStateActive/helper.js +36 -36
- package/src/image/ImgStateActive/initHelper.js +137 -137
- package/src/image/ImgStateActive/initMediaEditor.js +92 -92
- package/src/image/ImgStateActive/mediaSizer.js +63 -63
- package/src/image/ImgStateActive.js +115 -115
- package/src/incrementer.js +309 -309
- package/src/inplacer.js +315 -315
- package/src/interactUtils.js +140 -140
- package/src/itemButtonList/css/item-button-list.css +23 -23
- package/src/itemButtonList/css/item-button-list.css.map +1 -1
- package/src/itemButtonList/scss/item-button-list.scss +236 -236
- package/src/itemButtonList/tpl/itemButtonList.tpl +21 -21
- package/src/itemButtonList.js +274 -274
- package/src/keyNavigation/navigableDomElement.js +285 -285
- package/src/keyNavigation/navigator.js +535 -535
- package/src/listbox/css/listbox.css +7 -7
- package/src/listbox/css/listbox.css.map +1 -1
- package/src/listbox/scss/listbox.scss +116 -116
- package/src/listbox/tpl/list.tpl +14 -14
- package/src/listbox/tpl/main.tpl +9 -9
- package/src/listbox.js +252 -252
- package/src/liststyler.js +155 -155
- package/src/loadingButton/css/button.css +7 -7
- package/src/loadingButton/css/button.css.map +1 -1
- package/src/loadingButton/loadingButton.js +110 -110
- package/src/loadingButton/scss/button.scss +41 -41
- package/src/loadingButton/tpl/button.tpl +5 -5
- package/src/lock/lock.tpl +16 -16
- package/src/lock.js +393 -393
- package/src/login/login.js +317 -317
- package/src/login/tpl/login.tpl +29 -29
- package/src/login/tpl/passwordReveal.tpl +7 -7
- package/src/maths/calculator/basicCalculator.js +55 -55
- package/src/maths/calculator/calculatorComponent.js +128 -128
- package/src/maths/calculator/core/areaBroker.js +38 -38
- package/src/maths/calculator/core/board.js +841 -841
- package/src/maths/calculator/core/expression.js +430 -430
- package/src/maths/calculator/core/labels.js +116 -116
- package/src/maths/calculator/core/plugin.js +40 -40
- package/src/maths/calculator/core/terms.js +459 -459
- package/src/maths/calculator/core/tokenizer.js +245 -245
- package/src/maths/calculator/core/tokens.js +178 -178
- package/src/maths/calculator/core/tpl/board.tpl +4 -4
- package/src/maths/calculator/css/calculator.css +7 -7
- package/src/maths/calculator/css/calculator.css.map +1 -1
- package/src/maths/calculator/defaultCalculator.js +66 -66
- package/src/maths/calculator/plugins/core/degrad.js +90 -90
- package/src/maths/calculator/plugins/core/history.js +166 -166
- package/src/maths/calculator/plugins/core/remind.js +96 -96
- package/src/maths/calculator/plugins/core/stepNavigation.js +175 -175
- package/src/maths/calculator/plugins/keyboard/templateKeyboard/defaultTemplate.tpl +36 -36
- package/src/maths/calculator/plugins/keyboard/templateKeyboard/templateKeyboard.js +91 -91
- package/src/maths/calculator/plugins/modifiers/pow10.js +143 -143
- package/src/maths/calculator/plugins/modifiers/sign.js +339 -339
- package/src/maths/calculator/plugins/screen/simpleScreen/defaultTemplate.tpl +3 -3
- package/src/maths/calculator/plugins/screen/simpleScreen/history.tpl +3 -3
- package/src/maths/calculator/plugins/screen/simpleScreen/simpleScreen.js +190 -190
- package/src/maths/calculator/pluginsLoader.js +46 -46
- package/src/maths/calculator/scientificCalculator.js +74 -74
- package/src/maths/calculator/scss/calculator.scss +396 -396
- package/src/maths/calculator/tpl/basicKeyboard.tpl +37 -37
- package/src/maths/calculator/tpl/basicScreen.tpl +2 -2
- package/src/maths/calculator/tpl/scientificKeyboard.tpl +61 -61
- package/src/maths/calculator/tpl/scientificScreen.tpl +3 -3
- package/src/mediaEditor/mediaEditorComponent.js +141 -141
- package/src/mediaEditor/plugins/mediaAlignment/helper.js +110 -110
- package/src/mediaEditor/plugins/mediaAlignment/mediaAlignmentComponent.js +99 -99
- package/src/mediaEditor/plugins/mediaAlignment/style.css +7 -7
- package/src/mediaEditor/plugins/mediaAlignment/tpl/mediaAlignment.tpl +25 -25
- package/src/mediaEditor/plugins/mediaDimension/helper.js +190 -190
- package/src/mediaEditor/plugins/mediaDimension/mediaDimensionComponent.js +561 -561
- package/src/mediaEditor/plugins/mediaDimension/style.css +141 -141
- package/src/mediaEditor/plugins/mediaDimension/tpl/mediaDimension.tpl +55 -55
- package/src/mediaEditor/tpl/editor.tpl +4 -4
- package/src/mediaplayer/css/player.css +7 -7
- package/src/mediaplayer/css/player.css.map +1 -1
- package/src/mediaplayer/players/html5.js +564 -564
- package/src/mediaplayer/players/youtube.js +323 -323
- package/src/mediaplayer/players.js +29 -29
- package/src/mediaplayer/readme.md +305 -305
- package/src/mediaplayer/scss/player.scss +569 -569
- package/src/mediaplayer/support.js +126 -126
- package/src/mediaplayer/tpl/audio.tpl +6 -6
- package/src/mediaplayer/tpl/player.tpl +37 -37
- package/src/mediaplayer/tpl/source.tpl +1 -1
- package/src/mediaplayer/tpl/video.tpl +6 -6
- package/src/mediaplayer/tpl/youtube.tpl +1 -1
- package/src/mediaplayer/utils/reminder.js +184 -184
- package/src/mediaplayer/utils/timeObserver.js +143 -143
- package/src/mediaplayer/youtubeManager.js +161 -161
- package/src/mediaplayer.js +1606 -1606
- package/src/mediasizer/mediasizer.tpl +55 -55
- package/src/mediasizer.js +635 -635
- package/src/modal.js +365 -365
- package/src/movableComponent.js +78 -78
- package/src/pageSizeSelector/tpl/pageSizeSelector.tpl +9 -9
- package/src/pageSizeSelector.js +106 -106
- package/src/pageStatus.js +147 -147
- package/src/pagination/css/pagination.css +7 -7
- package/src/pagination/css/pagination.css.map +1 -1
- package/src/pagination/paginationStrategy.js +53 -53
- package/src/pagination/providers/pages.js +161 -161
- package/src/pagination/providers/simple.js +74 -74
- package/src/pagination/providers/tpl/pages/page.tpl +1 -1
- package/src/pagination/providers/tpl/pages.tpl +8 -8
- package/src/pagination/providers/tpl/simple.tpl +7 -7
- package/src/pagination/scss/pagination.scss +111 -111
- package/src/pagination.js +237 -237
- package/src/previewer.js +300 -300
- package/src/progressbar.js +165 -165
- package/src/propertySelector/css/propertySelector.css +7 -7
- package/src/propertySelector/css/propertySelector.css.map +1 -1
- package/src/propertySelector/propertySelector.js +286 -286
- package/src/propertySelector/scss/propertySelector.scss +66 -66
- package/src/propertySelector/tpl/property-description.tpl +12 -12
- package/src/propertySelector/tpl/property-selector.tpl +6 -6
- package/src/report/feedback.tpl +11 -11
- package/src/report/layout.tpl +10 -10
- package/src/report.js +184 -184
- package/src/resource/css/selector.css +7 -7
- package/src/resource/css/selector.css.map +1 -1
- package/src/resource/filters.js +208 -208
- package/src/resource/list.js +200 -200
- package/src/resource/scss/_filters.scss +26 -26
- package/src/resource/scss/_resource-list.scss +107 -107
- package/src/resource/scss/_resource-tree.scss +205 -205
- package/src/resource/scss/selector.scss +187 -187
- package/src/resource/selectable.js +322 -322
- package/src/resource/selector.js +871 -871
- package/src/resource/tpl/filters.tpl +2 -2
- package/src/resource/tpl/list.tpl +7 -7
- package/src/resource/tpl/listNode.tpl +4 -4
- package/src/resource/tpl/selector.tpl +46 -46
- package/src/resource/tpl/tree.tpl +4 -4
- package/src/resource/tpl/treeNode.tpl +30 -30
- package/src/resource/tree.js +398 -398
- package/src/resourcemgr/css/resourcemgr.css +7 -7
- package/src/resourcemgr/css/resourcemgr.css.map +1 -1
- package/src/resourcemgr/fileBrowser.js +381 -381
- package/src/resourcemgr/filePreview.js +73 -73
- package/src/resourcemgr/fileSelector.js +322 -322
- package/src/resourcemgr/scss/resourcemgr.scss +254 -254
- package/src/resourcemgr/tpl/fileSelect.tpl +39 -39
- package/src/resourcemgr/tpl/folder.tpl +11 -11
- package/src/resourcemgr/tpl/layout.tpl +84 -84
- package/src/resourcemgr/tpl/rootFolder.tpl +13 -13
- package/src/resourcemgr/util/updatePermissions.js +53 -53
- package/src/resourcemgr.js +216 -216
- package/src/scroller.js +94 -94
- package/src/scss/basic.scss +16 -16
- package/src/searchModal/advancedSearch.js +638 -638
- package/src/searchModal/css/advancedSearch.css +7 -7
- package/src/searchModal/css/advancedSearch.css.map +1 -1
- package/src/searchModal/css/searchModal.css +7 -7
- package/src/searchModal/css/searchModal.css.map +1 -1
- package/src/searchModal/scss/advancedSearch.scss +171 -171
- package/src/searchModal/scss/searchModal.scss +393 -393
- package/src/searchModal/tpl/advanced-search.tpl +9 -9
- package/src/searchModal/tpl/info-message.tpl +3 -3
- package/src/searchModal/tpl/invalid-criteria-warning.tpl +10 -10
- package/src/searchModal/tpl/layout.tpl +27 -27
- package/src/searchModal/tpl/list-checkbox-criterion.tpl +17 -17
- package/src/searchModal/tpl/list-select-criterion.tpl +12 -12
- package/src/searchModal/tpl/text-criterion.tpl +11 -11
- package/src/searchModal.js +760 -760
- package/src/selecter.js +43 -43
- package/src/stacker.js +133 -133
- package/src/switch/css/switch.css +7 -7
- package/src/switch/css/switch.css.map +1 -1
- package/src/switch/scss/switch.scss +83 -83
- package/src/switch/switch.js +195 -195
- package/src/switch/tpl/switch.tpl +7 -7
- package/src/tableModel.js +112 -112
- package/src/tabs/css/tabs.css +12 -12
- package/src/tabs/css/tabs.css.map +1 -1
- package/src/tabs/scss/tabs.scss +50 -50
- package/src/tabs/tpl/panel.tpl +3 -3
- package/src/tabs/tpl/tabs.tpl +10 -10
- package/src/tabs.js +528 -528
- package/src/taskQueue/css/taskQueue.css +7 -7
- package/src/taskQueue/css/taskQueue.css.map +1 -1
- package/src/taskQueue/scss/taskQueue.scss +47 -47
- package/src/taskQueue/status.js +228 -228
- package/src/taskQueue/table.js +350 -350
- package/src/taskQueue/taskQueue.js +33 -33
- package/src/taskQueue/taskQueueModel.js +548 -548
- package/src/taskQueue/tpl/statusMessage.tpl +7 -7
- package/src/taskQueue.js +216 -216
- package/src/taskQueueButton/css/taskable.css +7 -7
- package/src/taskQueueButton/css/taskable.css.map +1 -1
- package/src/taskQueueButton/css/treeButton.css +7 -7
- package/src/taskQueueButton/css/treeButton.css.map +1 -1
- package/src/taskQueueButton/scss/taskable.scss +4 -4
- package/src/taskQueueButton/scss/treeButton.scss +34 -34
- package/src/taskQueueButton/standardButton.js +108 -108
- package/src/taskQueueButton/taskable.js +202 -202
- package/src/taskQueueButton/tpl/report.tpl +5 -5
- package/src/taskQueueButton/tpl/treeButton.tpl +6 -6
- package/src/taskQueueButton/treeButton.js +108 -108
- package/src/themeLoader.js +252 -252
- package/src/themes.js +162 -162
- package/src/toggler.js +200 -200
- package/src/tooltip/default.tpl +3 -3
- package/src/tooltip.js +160 -160
- package/src/tooltipster.js +25 -25
- package/src/transformer.js +327 -327
- package/src/tristateCheckboxGroup/css/tristateCheckboxGroup.css +7 -7
- package/src/tristateCheckboxGroup/css/tristateCheckboxGroup.css.map +1 -1
- package/src/tristateCheckboxGroup/scss/tristateCheckboxGroup.scss +15 -15
- package/src/tristateCheckboxGroup/tpl/li.tpl +6 -6
- package/src/tristateCheckboxGroup.js +207 -207
- package/src/uploader/fileEntry.tpl +6 -6
- package/src/uploader/uploader.tpl +32 -32
- package/src/uploader.js +594 -594
- package/src/validator/Report.js +10 -10
- package/src/validator/Validator.js +108 -108
- package/src/validator/validators.js +217 -217
- package/src/validator.js +262 -262
- package/src/waitForMedia.js +82 -82
- package/src/waitingDialog/css/waitingDialog.css +7 -7
- package/src/waitingDialog/css/waitingDialog.css.map +1 -1
- package/src/waitingDialog/scss/waitingDialog.scss +34 -34
- package/src/waitingDialog/waitingDialog.js +240 -240
package/src/highlighter.js
CHANGED
|
@@ -1,1192 +1,1192 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This program is free software; you can redistribute it and/or
|
|
3
|
-
* modify it under the terms of the GNU General Public License
|
|
4
|
-
* as published by the Free Software Foundation; under version 2
|
|
5
|
-
* of the License (non-upgradable).
|
|
6
|
-
*
|
|
7
|
-
* This program is distributed in the hope that it will be useful,
|
|
8
|
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10
|
-
* GNU General Public License for more details.
|
|
11
|
-
*
|
|
12
|
-
* You should have received a copy of the GNU General Public License
|
|
13
|
-
* along with this program; if not, write to the Free Software
|
|
14
|
-
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
15
|
-
*
|
|
16
|
-
* Copyright (c) 2016-2021 (original work) Open Assessment Technologies SA;
|
|
17
|
-
*/
|
|
18
|
-
/**
|
|
19
|
-
* Highlighter helper: wraps every text node found within a Range object.
|
|
20
|
-
*
|
|
21
|
-
* @author Christophe Noël <christophe@taotesting.com>
|
|
22
|
-
*/
|
|
23
|
-
import _ from 'lodash';
|
|
24
|
-
import $ from 'jquery';
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Data attribute used to logically group the wrapping nodes into a single selection
|
|
28
|
-
* @type {string}
|
|
29
|
-
*/
|
|
30
|
-
var GROUP_ATTR = 'data-hl-group';
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Children of those nodes types cannot be highlighted
|
|
34
|
-
* @type {string[]}
|
|
35
|
-
*/
|
|
36
|
-
var defaultBlackList = ['textarea', 'math', 'script', '.select2-container'];
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* @param {Object} options
|
|
40
|
-
* @param {String} options.className - name of the class that will be used by the wrappers tags to highlight text
|
|
41
|
-
* @param {String} options.containerSelector - allows to select the root Node in which highlighting is allowed
|
|
42
|
-
* @param {Array<String>} [options.containersBlackList] - additional blacklist selectors to be added to module instance's blacklist
|
|
43
|
-
* @param {Array<String>} [options.containersWhiteList] - whitelist selectors; supported only in `keepEmptyNodes` mode.
|
|
44
|
-
* Priority of blacklist or whitelist is decided by which selector is closest to the node. If no match found, node is considered whitelisted.
|
|
45
|
-
* @param {Boolean} [options.clearOnClick] - clear single highlight node on click
|
|
46
|
-
* @param {Object} [options.colors] - keys is keeping as the "c" value of storing/restore the highlighters for indexing, values are wrappers class names
|
|
47
|
-
* @param {Boolean} [options.keepEmptyNodes] - retain original dom structure as far as possible and do not remove empty nodes if they were not created by highlighter
|
|
48
|
-
* @returns {Object} - the highlighter instance
|
|
49
|
-
*/
|
|
50
|
-
export default function (options) {
|
|
51
|
-
var className = options.className;
|
|
52
|
-
var containerSelector = options.containerSelector;
|
|
53
|
-
var keepEmptyNodes = options.keepEmptyNodes;
|
|
54
|
-
|
|
55
|
-
let highlightingClasses = [className];
|
|
56
|
-
|
|
57
|
-
// Multi-color mode
|
|
58
|
-
if (options.colors) {
|
|
59
|
-
highlightingClasses = Object.values(options.colors);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* list of node selectors which should NOT receive any highlighting from this instance
|
|
64
|
-
* an optional passed-in blacklist is merged with local defaults
|
|
65
|
-
* @type {Array}
|
|
66
|
-
*/
|
|
67
|
-
const containersBlackList = _.union(defaultBlackList, options.containersBlackList);
|
|
68
|
-
const containersBlackListSelector = containersBlackList.join(', ');
|
|
69
|
-
let containersWhiteListSelector = null;
|
|
70
|
-
let containersBlackAndWhiteListSelector = containersBlackListSelector;
|
|
71
|
-
if (options.keepEmptyNodes && options.containersWhiteList) {
|
|
72
|
-
containersWhiteListSelector = options.containersWhiteList.join(', ');
|
|
73
|
-
containersBlackAndWhiteListSelector = _.union(containersBlackList, options.containersWhiteList).join(', ');
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* used in recursive loops to decide if we should wrap or not the current node
|
|
78
|
-
* @type {boolean}
|
|
79
|
-
*/
|
|
80
|
-
let isWrapping = false;
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* performance improvement to break out of a potentially big recursive loop once the wrapping has ended
|
|
84
|
-
* @type {boolean}
|
|
85
|
-
*/
|
|
86
|
-
let hasWrapped = false;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* used in recursive loops to assign a group Id to the current wrapped node
|
|
90
|
-
* @type {number}
|
|
91
|
-
*/
|
|
92
|
-
let currentGroupId;
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* used in recursive loops to build the index of text nodes
|
|
96
|
-
* @type {number}
|
|
97
|
-
*/
|
|
98
|
-
let textNodesIndex;
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Returns the node in which highlighting is allowed
|
|
102
|
-
* @returns {Element}
|
|
103
|
-
*/
|
|
104
|
-
function getContainer() {
|
|
105
|
-
return $(containerSelector).get(0);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Returns all highlighted nodes, excluding any inside blacklisted elements
|
|
110
|
-
* @returns {JQuery<HTMLElement>}
|
|
111
|
-
*/
|
|
112
|
-
function getHighlightedNodes() {
|
|
113
|
-
return $(containerSelector)
|
|
114
|
-
.find(`.${highlightingClasses.join(',.')}`)
|
|
115
|
-
.filter((i, node) => !isBlacklisted(node));
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Attach data to wrapper node.
|
|
120
|
-
* Use it when deleting this highlight to know if highlight content should be merged with neighbour text nodes or not.
|
|
121
|
-
* Use it when building/restoring index to know if restored highlight content should be split off neighbour text node or not.
|
|
122
|
-
* Needed to keep markup the same as it was before highlighting.
|
|
123
|
-
* @param {HTMLElement} node
|
|
124
|
-
* @param {Boolean} beforeWasSplit
|
|
125
|
-
* @param {Boolean} afterWasSplit
|
|
126
|
-
*/
|
|
127
|
-
function addSplitData(node, beforeWasSplit, afterWasSplit) {
|
|
128
|
-
node.dataset.beforeWasSplit = beforeWasSplit;
|
|
129
|
-
node.dataset.afterWasSplit = afterWasSplit;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Highlight all text nodes within each given range
|
|
134
|
-
* @param {Range[]} ranges - array of ranges to highlight, may be given by the helper selector.getAllRanges()
|
|
135
|
-
*/
|
|
136
|
-
function highlightRanges(ranges) {
|
|
137
|
-
ranges.forEach(function (range) {
|
|
138
|
-
var rangeInfos;
|
|
139
|
-
|
|
140
|
-
if (isRangeValid(range)) {
|
|
141
|
-
currentGroupId = getAvailableGroupId();
|
|
142
|
-
|
|
143
|
-
// easy peasy: highlighting a plain text without any DOM nodes
|
|
144
|
-
// NOTE: The condition checks the whole node content and not a selected content in a given range, that allows to wrap whitespace
|
|
145
|
-
if (
|
|
146
|
-
isWrappable(range.commonAncestorContainer) &&
|
|
147
|
-
!isWrappingNode(range.commonAncestorContainer.parentNode)
|
|
148
|
-
) {
|
|
149
|
-
const wrapperNode = getWrapper(currentGroupId);
|
|
150
|
-
if (!keepEmptyNodes) {
|
|
151
|
-
range.surroundContents(wrapperNode);
|
|
152
|
-
} else {
|
|
153
|
-
addSplitData(
|
|
154
|
-
wrapperNode,
|
|
155
|
-
range.startOffset > 0,
|
|
156
|
-
range.endOffset < range.commonAncestorContainer.length
|
|
157
|
-
);
|
|
158
|
-
rangeSurroundContentsNoEmptyNodes(range, wrapperNode);
|
|
159
|
-
}
|
|
160
|
-
} else if (
|
|
161
|
-
isWrappable(range.commonAncestorContainer) &&
|
|
162
|
-
isWrappingNode(range.commonAncestorContainer.parentNode) &&
|
|
163
|
-
range.commonAncestorContainer.parentNode !== className
|
|
164
|
-
) {
|
|
165
|
-
highlightContainerNodes(range.commonAncestorContainer, className, range, currentGroupId);
|
|
166
|
-
|
|
167
|
-
// now the fun stuff: highlighting a mix of text and DOM nodes
|
|
168
|
-
} else {
|
|
169
|
-
rangeInfos = {
|
|
170
|
-
startNode: isElement(range.startContainer)
|
|
171
|
-
? range.startContainer.childNodes[range.startOffset]
|
|
172
|
-
: range.startContainer,
|
|
173
|
-
startNodeContainer: range.startContainer,
|
|
174
|
-
startOffset: range.startOffset,
|
|
175
|
-
|
|
176
|
-
endNode:
|
|
177
|
-
isElement(range.endContainer) && range.endOffset > 0
|
|
178
|
-
? range.endContainer.childNodes[range.endOffset - 1]
|
|
179
|
-
: range.endContainer,
|
|
180
|
-
endNodeContainer: range.endContainer,
|
|
181
|
-
endOffset: range.endOffset,
|
|
182
|
-
commonRange: range
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
isWrapping = false;
|
|
186
|
-
hasWrapped = false;
|
|
187
|
-
wrapTextNodesInRange(range.commonAncestorContainer, rangeInfos);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (!keepEmptyNodes) {
|
|
192
|
-
// clean up the markup after wrapping...
|
|
193
|
-
range.commonAncestorContainer.normalize();
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
currentGroupId = 0;
|
|
197
|
-
isWrapping = false;
|
|
198
|
-
reindexGroups(getContainer());
|
|
199
|
-
mergeAdjacentWrappingNodes(getContainer());
|
|
200
|
-
unWrapEmptyHighlights();
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
if (options.clearOnClick) {
|
|
204
|
-
$(containerSelector + ' .' + className)
|
|
205
|
-
.off('click')
|
|
206
|
-
.on('click', clearSingleHighlight);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Check if a range is valid
|
|
212
|
-
* @param {Range} range
|
|
213
|
-
* @returns {boolean}
|
|
214
|
-
*/
|
|
215
|
-
function isRangeValid(range) {
|
|
216
|
-
var rangeInContainer;
|
|
217
|
-
try {
|
|
218
|
-
rangeInContainer =
|
|
219
|
-
$.contains(getContainer(), range.commonAncestorContainer) ||
|
|
220
|
-
getContainer().isSameNode(range.commonAncestorContainer);
|
|
221
|
-
|
|
222
|
-
return rangeInContainer && !range.collapsed;
|
|
223
|
-
} catch (e) {
|
|
224
|
-
return false;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Core wrapping function. Traverse the DOM tree and highlight (= wraps) all text nodes within the given range.
|
|
230
|
-
* Recursive.
|
|
231
|
-
*
|
|
232
|
-
* @param {Node} rootNode - top of the node hierarchy in which text nodes will be searched
|
|
233
|
-
* @param {Object} rangeInfos
|
|
234
|
-
* @param {Node} rangeInfos.startNode - node on which the selection starts
|
|
235
|
-
* @param {Node} rangeInfos.startNodeContainer - container of the startNode, or the start node itself in case of text nodes
|
|
236
|
-
* @param {number} rangeInfos.startOffset - same as range.startOffset, but not read-only to allow override
|
|
237
|
-
* @param {Node} rangeInfos.endNode - node on which the selection ends
|
|
238
|
-
* @param {Node} rangeInfos.endNodeContainer - container of the endNode, or the end node itself in case of text nodes
|
|
239
|
-
* @param {number} rangeInfos.endOffset - same as range.endOffset, but not read-only to allow override
|
|
240
|
-
*/
|
|
241
|
-
function wrapTextNodesInRange(rootNode, rangeInfos) {
|
|
242
|
-
var childNodes = rootNode.childNodes;
|
|
243
|
-
var currentNode, i;
|
|
244
|
-
var splitDatas = [];
|
|
245
|
-
|
|
246
|
-
for (i = 0; i < childNodes.length; i++) {
|
|
247
|
-
if (hasWrapped) {
|
|
248
|
-
break;
|
|
249
|
-
}
|
|
250
|
-
currentNode = childNodes[i];
|
|
251
|
-
|
|
252
|
-
if (isBlacklisted(currentNode)) {
|
|
253
|
-
if (isElement(currentNode)) {
|
|
254
|
-
//go deeper in case a descendant of the current blacklisted is whitelisted
|
|
255
|
-
wrapTextNodesInRange(currentNode, rangeInfos);
|
|
256
|
-
}
|
|
257
|
-
} else {
|
|
258
|
-
const isCurrentNodeTextInsideOfAnotherHighlightingWrapper =
|
|
259
|
-
isText(currentNode) &&
|
|
260
|
-
isWrappingNode(currentNode.parentNode) &&
|
|
261
|
-
currentNode.parentNode.className !== className;
|
|
262
|
-
|
|
263
|
-
if (isCurrentNodeTextInsideOfAnotherHighlightingWrapper) {
|
|
264
|
-
const internalRange = new Range();
|
|
265
|
-
internalRange.selectNodeContents(currentNode);
|
|
266
|
-
|
|
267
|
-
if (rangeInfos.startNode === currentNode) {
|
|
268
|
-
internalRange.setStart(currentNode, rangeInfos.startOffset);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
if (rangeInfos.endNode === currentNode) {
|
|
272
|
-
internalRange.setEnd(currentNode, rangeInfos.endOffset);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
const isNodeInRange = rangeInfos.commonRange.isPointInRange(currentNode, internalRange.endOffset);
|
|
276
|
-
|
|
277
|
-
// Apply new highlighting color only for selected nodes
|
|
278
|
-
if (isNodeInRange) {
|
|
279
|
-
isWrapping = true;
|
|
280
|
-
highlightContainerNodes(currentNode, className, internalRange, currentGroupId);
|
|
281
|
-
}
|
|
282
|
-
} else {
|
|
283
|
-
// split current node in case the wrapping start/ends on a partially selected text node
|
|
284
|
-
if (currentNode.isSameNode(rangeInfos.startNode)) {
|
|
285
|
-
if (isText(rangeInfos.startNodeContainer) && rangeInfos.startOffset !== 0) {
|
|
286
|
-
// we defer the wrapping to the next iteration of the loop
|
|
287
|
-
//end of node should be highlighted
|
|
288
|
-
rangeInfos.startNode = currentNode.splitText(rangeInfos.startOffset);
|
|
289
|
-
rangeInfos.startOffset = 0;
|
|
290
|
-
splitDatas.push({ node: rangeInfos.startNode, beforeWasSplit: true, afterWasSplit: false });
|
|
291
|
-
} else {
|
|
292
|
-
//whole node should be highlighted
|
|
293
|
-
isWrapping = true;
|
|
294
|
-
splitDatas.push({ node: currentNode, beforeWasSplit: false, afterWasSplit: false });
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
if (currentNode.isSameNode(rangeInfos.endNode) && isText(rangeInfos.endNodeContainer)) {
|
|
299
|
-
if (rangeInfos.endOffset !== 0) {
|
|
300
|
-
if (rangeInfos.endOffset < currentNode.textContent.length) {
|
|
301
|
-
//start of node should be highlighted
|
|
302
|
-
currentNode.splitText(rangeInfos.endOffset);
|
|
303
|
-
splitDatas.push({ node: currentNode, beforeWasSplit: false, afterWasSplit: true });
|
|
304
|
-
} else {
|
|
305
|
-
//whole node should be highlighted
|
|
306
|
-
splitDatas.push({ node: currentNode, beforeWasSplit: false, afterWasSplit: false });
|
|
307
|
-
}
|
|
308
|
-
} else {
|
|
309
|
-
isWrapping = false;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
// wrap the current node...
|
|
314
|
-
if (isText(currentNode)) {
|
|
315
|
-
if (!keepEmptyNodes) {
|
|
316
|
-
wrapTextNode(currentNode, currentGroupId);
|
|
317
|
-
} else if (willHighlightNotBeEmptyAfterMerge(currentNode)) {
|
|
318
|
-
const wrapperNode = wrapTextNode(currentNode, currentGroupId);
|
|
319
|
-
if (wrapperNode) {
|
|
320
|
-
const splitData = splitDatas.find(d => d.node === currentNode);
|
|
321
|
-
addSplitData(
|
|
322
|
-
wrapperNode,
|
|
323
|
-
splitData ? splitData.beforeWasSplit : false,
|
|
324
|
-
splitData ? splitData.afterWasSplit : false
|
|
325
|
-
);
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
// ... or continue deeper in the node tree
|
|
330
|
-
} else if (isElement(currentNode)) {
|
|
331
|
-
//some selections end at the very start of the next node, we should end wrapping when we reach such node
|
|
332
|
-
if (!currentNode.isSameNode(rangeInfos.endNode) || rangeInfos.endOffset > 0) {
|
|
333
|
-
wrapTextNodesInRange(currentNode, rangeInfos);
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
// end wrapping ?
|
|
340
|
-
if (currentNode.isSameNode(rangeInfos.endNode)) {
|
|
341
|
-
isWrapping = false;
|
|
342
|
-
hasWrapped = true;
|
|
343
|
-
break;
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
/**
|
|
349
|
-
* Restructure content of the highlighted wrapper according to the selectedRange
|
|
350
|
-
* @param {Node} textNode
|
|
351
|
-
* @param {string} activeClass
|
|
352
|
-
* @param {Range} selectedRange
|
|
353
|
-
* @param {number} groupId
|
|
354
|
-
*/
|
|
355
|
-
function highlightContainerNodes(textNode, activeClass, selectedRange, groupId) {
|
|
356
|
-
const container = textNode.parentNode;
|
|
357
|
-
const range = new Range();
|
|
358
|
-
range.selectNodeContents(textNode);
|
|
359
|
-
|
|
360
|
-
const isSelectionCoversNodeStart = range.compareBoundaryPoints(Range.START_TO_START, selectedRange) === 0;
|
|
361
|
-
const isSelectionCoversNodeEnd = range.compareBoundaryPoints(Range.END_TO_END, selectedRange) === 0;
|
|
362
|
-
|
|
363
|
-
/*
|
|
364
|
-
There are 4 possible cases selected area is intersected with already highlighted element.
|
|
365
|
-
In examples below the border is represents the selection, "yellow" is class name of already highlighted
|
|
366
|
-
container, "red" is class name of currently active highlighter
|
|
367
|
-
**********************************************************************************************************
|
|
368
|
-
1. The container content is completely selected, so that we only have to change the highlighter class name
|
|
369
|
-
|
|
370
|
-
Input:
|
|
371
|
-
__________________________________________________
|
|
372
|
-
| |
|
|
373
|
-
|<span class="yellow"> Lorem ipsum dolor sit</span>|
|
|
374
|
-
|__________________________________________________|
|
|
375
|
-
|
|
376
|
-
Output:
|
|
377
|
-
<span class="red"> Lorem ipsum dolor sit</span>
|
|
378
|
-
|
|
379
|
-
**********************************************************************************************************
|
|
380
|
-
2. The container content is partially selected from the begging.
|
|
381
|
-
|
|
382
|
-
Input:
|
|
383
|
-
______________________________
|
|
384
|
-
| |
|
|
385
|
-
|<span class="yellow"> Lorem ip|sum dolor sit</span>
|
|
386
|
-
|______________________________|
|
|
387
|
-
|
|
388
|
-
Output:
|
|
389
|
-
<span class="red"> Lorem ip</span><span class="yellow">sum dolor sit</span>
|
|
390
|
-
|
|
391
|
-
**********************************************************************************************************
|
|
392
|
-
3. The container content is partially selected at the end.
|
|
393
|
-
|
|
394
|
-
Input:
|
|
395
|
-
____________________
|
|
396
|
-
| |
|
|
397
|
-
<span class="yellow"> Lorem ip|sum dolor sit</span>|
|
|
398
|
-
|____________________|
|
|
399
|
-
|
|
400
|
-
Output:
|
|
401
|
-
<span class="yellow"> Lorem ip</span><span class="red">sum dolor sit</span>
|
|
402
|
-
|
|
403
|
-
**********************************************************************************************************
|
|
404
|
-
4. The container content is partially selected in the middle.
|
|
405
|
-
|
|
406
|
-
Input:
|
|
407
|
-
___________
|
|
408
|
-
| |
|
|
409
|
-
<span class="yellow"> Lorem |ipsum dolor| sit</span>
|
|
410
|
-
|___________|
|
|
411
|
-
|
|
412
|
-
Output:
|
|
413
|
-
<span class="yellow"> Lorem </span><span class="red">ipsum dolor</span><span class="yellow"> sit</span>
|
|
414
|
-
*/
|
|
415
|
-
if (isSelectionCoversNodeStart && isSelectionCoversNodeEnd) {
|
|
416
|
-
textNode.parentNode.className = activeClass;
|
|
417
|
-
} else if (isSelectionCoversNodeStart) {
|
|
418
|
-
textNode.splitText(selectedRange.endOffset);
|
|
419
|
-
wrapContainerChildNodes(container, 0, activeClass, groupId);
|
|
420
|
-
} else if (isSelectionCoversNodeEnd) {
|
|
421
|
-
textNode.splitText(selectedRange.startOffset);
|
|
422
|
-
wrapContainerChildNodes(container, 1, activeClass, groupId);
|
|
423
|
-
} else {
|
|
424
|
-
textNode.splitText(selectedRange.startOffset).splitText(selectedRange.endOffset);
|
|
425
|
-
wrapContainerChildNodes(container, 1, activeClass, groupId);
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
/**
|
|
430
|
-
* Wraps all containers text nodes with highlighter element.
|
|
431
|
-
* The child node with index given by indexToWrapNode parameter will be wrap with class given by activeClass parameter
|
|
432
|
-
* @param {Element} container
|
|
433
|
-
* @param {number} indexToWrapNode
|
|
434
|
-
* @param {string} activeClass
|
|
435
|
-
* @param {number} groupId
|
|
436
|
-
*/
|
|
437
|
-
function wrapContainerChildNodes(container, indexToWrapNode, activeClass, groupId) {
|
|
438
|
-
const containerClass = container.className;
|
|
439
|
-
const fragment = new DocumentFragment();
|
|
440
|
-
const childNodesLength = container.childNodes.length;
|
|
441
|
-
|
|
442
|
-
container.childNodes.forEach((node, index) => {
|
|
443
|
-
var wrapperNode;
|
|
444
|
-
if (index === indexToWrapNode) {
|
|
445
|
-
wrapperNode = wrapNode(node.cloneNode(), activeClass, groupId);
|
|
446
|
-
} else {
|
|
447
|
-
wrapperNode = wrapNode(node.cloneNode(), containerClass, groupId);
|
|
448
|
-
}
|
|
449
|
-
fragment.appendChild(wrapperNode);
|
|
450
|
-
|
|
451
|
-
if (keepEmptyNodes) {
|
|
452
|
-
addSplitData(
|
|
453
|
-
wrapperNode,
|
|
454
|
-
index === 0 ? container.dataset.beforeWasSplit : true,
|
|
455
|
-
index === childNodesLength - 1 ? container.dataset.afterWasSplit : true
|
|
456
|
-
);
|
|
457
|
-
}
|
|
458
|
-
});
|
|
459
|
-
|
|
460
|
-
container.replaceWith(fragment);
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
/**
|
|
464
|
-
* wraps a text node into the highlight span
|
|
465
|
-
* @param {Node} node - the node to wrap
|
|
466
|
-
* @param {number} groupId - the highlight group
|
|
467
|
-
* @returns {Node|null} wrapper node, if it was created
|
|
468
|
-
*/
|
|
469
|
-
function wrapTextNode(node, groupId) {
|
|
470
|
-
if (isWrapping && !isWrappingNode(node.parentNode) && isWrappable(node)) {
|
|
471
|
-
$(node).wrap(getWrapper(groupId));
|
|
472
|
-
return node.parentNode;
|
|
473
|
-
}
|
|
474
|
-
return null;
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
/**
|
|
478
|
-
* We need to re-index the groups after a user highlight: either to merge groups or to resolve inconsistencies
|
|
479
|
-
* Recursive.
|
|
480
|
-
*
|
|
481
|
-
* @param {Node} rootNode
|
|
482
|
-
*/
|
|
483
|
-
function reindexGroups(rootNode) {
|
|
484
|
-
if (!rootNode) {
|
|
485
|
-
return;
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
const childNodes = rootNode.childNodes;
|
|
489
|
-
let i, currentNode, parent;
|
|
490
|
-
|
|
491
|
-
for (i = 0; i < childNodes.length; i++) {
|
|
492
|
-
currentNode = childNodes[i];
|
|
493
|
-
|
|
494
|
-
if (isWrappable(currentNode)) {
|
|
495
|
-
parent = currentNode.parentNode;
|
|
496
|
-
if (isWrappingNode(parent)) {
|
|
497
|
-
if (isWrapping === false) {
|
|
498
|
-
currentGroupId++;
|
|
499
|
-
}
|
|
500
|
-
isWrapping = true;
|
|
501
|
-
parent.setAttribute(GROUP_ATTR, currentGroupId); // set the new group Id
|
|
502
|
-
} else {
|
|
503
|
-
isWrapping = false;
|
|
504
|
-
}
|
|
505
|
-
} else if (isElement(currentNode)) {
|
|
506
|
-
reindexGroups(currentNode);
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
/**
|
|
512
|
-
* Some highlights may result in having adjacent wrapping nodes. We remove them here to get a cleaner markup.
|
|
513
|
-
*
|
|
514
|
-
* @param {Node} rootNode
|
|
515
|
-
*/
|
|
516
|
-
function mergeAdjacentWrappingNodes(rootNode) {
|
|
517
|
-
if (!rootNode) {
|
|
518
|
-
return;
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
const childNodes = rootNode.childNodes;
|
|
522
|
-
let i, currentNode;
|
|
523
|
-
|
|
524
|
-
for (i = 0; i < childNodes.length; i++) {
|
|
525
|
-
currentNode = childNodes[i];
|
|
526
|
-
|
|
527
|
-
if (isWrappingNode(currentNode)) {
|
|
528
|
-
if (keepEmptyNodes) {
|
|
529
|
-
currentNode.normalize();
|
|
530
|
-
}
|
|
531
|
-
while (
|
|
532
|
-
isWrappingNode(currentNode.nextSibling) &&
|
|
533
|
-
currentNode.className === currentNode.nextSibling.className
|
|
534
|
-
) {
|
|
535
|
-
if (keepEmptyNodes) {
|
|
536
|
-
currentNode.nextSibling.normalize();
|
|
537
|
-
}
|
|
538
|
-
currentNode.firstChild.textContent += currentNode.nextSibling.firstChild.textContent;
|
|
539
|
-
if (keepEmptyNodes) {
|
|
540
|
-
addSplitData(
|
|
541
|
-
currentNode,
|
|
542
|
-
currentNode.dataset.beforeWasSplit,
|
|
543
|
-
currentNode.nextSibling.dataset.afterWasSplit
|
|
544
|
-
);
|
|
545
|
-
}
|
|
546
|
-
currentNode.parentNode.removeChild(currentNode.nextSibling);
|
|
547
|
-
}
|
|
548
|
-
} else if (isElement(currentNode)) {
|
|
549
|
-
mergeAdjacentWrappingNodes(currentNode);
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
/**
|
|
555
|
-
* Unwraps highlighted nodes with a line break or with an empty content
|
|
556
|
-
*/
|
|
557
|
-
function unWrapEmptyHighlights() {
|
|
558
|
-
getHighlightedNodes().each((index, node) => {
|
|
559
|
-
const nodeContent = node.textContent;
|
|
560
|
-
|
|
561
|
-
if (nodeContent.trim().length === 0) {
|
|
562
|
-
if (nodeContent.length === 0 || /\r|\n/.exec(nodeContent)) {
|
|
563
|
-
clearSingleHighlight({ target: node });
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
});
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
/**
|
|
570
|
-
* Check condition to avoid the work of `unwrapEmptyHighlights` ahead of time, before `mergeAdjacentNodes` runs,
|
|
571
|
-
* because in `keepEmptyNodes` case we do not want to add nodes to dom unless necessary.
|
|
572
|
-
* Also be more strict and don't allow to select nodes with spaces only, because they may appear in unexpected places in markup
|
|
573
|
-
* (here it's not exactly same as `unwrapEmptyHighlights`).
|
|
574
|
-
* @param {Node} node - node which will be wrapped (highlighted)
|
|
575
|
-
* @returns {Boolean}
|
|
576
|
-
*/
|
|
577
|
-
function willHighlightNotBeEmptyAfterMerge(node) {
|
|
578
|
-
if (!node.textContent.length) {
|
|
579
|
-
return false;
|
|
580
|
-
}
|
|
581
|
-
if (node.textContent.trim().length) {
|
|
582
|
-
return true;
|
|
583
|
-
}
|
|
584
|
-
const prevNode = node.previousSibling;
|
|
585
|
-
const canWrapperBeMergedWithPreviousSibling =
|
|
586
|
-
prevNode && isWrappingNode(prevNode) && prevNode.className === className;
|
|
587
|
-
if (canWrapperBeMergedWithPreviousSibling) {
|
|
588
|
-
return true;
|
|
589
|
-
}
|
|
590
|
-
const nextNode = node.nextSibling;
|
|
591
|
-
const canWrapperBeMergedWithNextSibling =
|
|
592
|
-
nextNode && isWrappingNode(nextNode) && nextNode.className === className;
|
|
593
|
-
if (canWrapperBeMergedWithNextSibling) {
|
|
594
|
-
return true;
|
|
595
|
-
}
|
|
596
|
-
return false;
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
/**
|
|
600
|
-
* `range.surroundContents` can create empty text nodes,
|
|
601
|
-
* which will cause trouble in `mergeAdjacentNodes` later (in `keepEmptyNodes` case).
|
|
602
|
-
* This method surrounds range, then removes those nodes
|
|
603
|
-
* @param {Range} range
|
|
604
|
-
* @param {Node} wrapperNode
|
|
605
|
-
*/
|
|
606
|
-
function rangeSurroundContentsNoEmptyNodes(range, wrapperNode) {
|
|
607
|
-
const containerPreviousSibling = range.commonAncestorContainer.previousSibling;
|
|
608
|
-
const containerNextSibling = range.commonAncestorContainer.nextSibling;
|
|
609
|
-
|
|
610
|
-
range.surroundContents(wrapperNode);
|
|
611
|
-
|
|
612
|
-
removeEmptyTextNodeIfDifferent(wrapperNode.previousSibling, containerPreviousSibling);
|
|
613
|
-
removeEmptyTextNodeIfDifferent(wrapperNode.nextSibling, containerNextSibling);
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
/**
|
|
617
|
-
* Remove `node`, if it's an empty text node and is *not* the same node as `nodeToCompare`
|
|
618
|
-
* @param {Node} node
|
|
619
|
-
* @param {Node} nodeToCompare
|
|
620
|
-
*/
|
|
621
|
-
function removeEmptyTextNodeIfDifferent(node, nodeToCompare) {
|
|
622
|
-
if (node && node !== nodeToCompare && isText(node) && node.textContent.length === 0) {
|
|
623
|
-
node.remove();
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
/**
|
|
628
|
-
* Remove all wrapping nodes from markup
|
|
629
|
-
*/
|
|
630
|
-
function clearHighlights() {
|
|
631
|
-
getHighlightedNodes().each(function (i, elem) {
|
|
632
|
-
if (!keepEmptyNodes) {
|
|
633
|
-
const $wrapped = $(this);
|
|
634
|
-
$wrapped.replaceWith($wrapped.text());
|
|
635
|
-
} else {
|
|
636
|
-
clearSingleHighlight({ target: elem });
|
|
637
|
-
}
|
|
638
|
-
});
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
/**
|
|
642
|
-
* Remove unwrap dom node
|
|
643
|
-
*/
|
|
644
|
-
function clearSingleHighlight(e) {
|
|
645
|
-
if (!keepEmptyNodes) {
|
|
646
|
-
const $wrapped = $(e.target);
|
|
647
|
-
const text = $wrapped.text();
|
|
648
|
-
|
|
649
|
-
// NOTE: JQuery replaceWith is not working with empty string https://bugs.jquery.com/ticket/13401
|
|
650
|
-
if (text === '') {
|
|
651
|
-
$wrapped.remove();
|
|
652
|
-
} else {
|
|
653
|
-
$wrapped.replaceWith(text);
|
|
654
|
-
}
|
|
655
|
-
} else {
|
|
656
|
-
const nodeToRemove = e.target;
|
|
657
|
-
const nodeToRemoveText = nodeToRemove.textContent;
|
|
658
|
-
const beforeWasSplit = nodeToRemove.dataset.beforeWasSplit === 'true';
|
|
659
|
-
const afterWasSplit = nodeToRemove.dataset.afterWasSplit === 'true';
|
|
660
|
-
const prevNode = nodeToRemove.previousSibling;
|
|
661
|
-
const nextNode = nodeToRemove.nextSibling;
|
|
662
|
-
|
|
663
|
-
if (beforeWasSplit && prevNode && isText(prevNode) && prevNode.textContent) {
|
|
664
|
-
//append text to previous sibling
|
|
665
|
-
prevNode.textContent += nodeToRemoveText;
|
|
666
|
-
nodeToRemove.remove();
|
|
667
|
-
|
|
668
|
-
if (
|
|
669
|
-
afterWasSplit &&
|
|
670
|
-
prevNode.nextSibling &&
|
|
671
|
-
isText(prevNode.nextSibling) &&
|
|
672
|
-
prevNode.nextSibling.textContent
|
|
673
|
-
) {
|
|
674
|
-
//merge it with next sibling
|
|
675
|
-
prevNode.textContent += prevNode.nextSibling.textContent;
|
|
676
|
-
prevNode.nextSibling.remove();
|
|
677
|
-
}
|
|
678
|
-
} else if (afterWasSplit && nextNode && isText(nextNode) && nextNode.textContent) {
|
|
679
|
-
//append text to next sibling
|
|
680
|
-
nextNode.textContent = nodeToRemoveText + nextNode.textContent;
|
|
681
|
-
nodeToRemove.remove();
|
|
682
|
-
} else if (nodeToRemoveText) {
|
|
683
|
-
//keep text in a separate text node
|
|
684
|
-
nodeToRemove.replaceWith(document.createTextNode(nodeToRemoveText));
|
|
685
|
-
} else {
|
|
686
|
-
//text is empty, just remove it
|
|
687
|
-
nodeToRemove.remove();
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
/**
|
|
693
|
-
* Index-related functions:
|
|
694
|
-
* ========================
|
|
695
|
-
* To allow saving and restoring highlights on an equivalent, but different, DOM tree (for example if the markup is deleted and re-created)
|
|
696
|
-
* we build an index containing the status of each text node:
|
|
697
|
-
* - not highlighted
|
|
698
|
-
* - fully highlighted
|
|
699
|
-
* - partially highlighted (= with inline ranges)
|
|
700
|
-
*
|
|
701
|
-
* This index will be used to restore a selection on the new DOM tree
|
|
702
|
-
*/
|
|
703
|
-
|
|
704
|
-
/**
|
|
705
|
-
* Bootstrap the process of building the highlight index
|
|
706
|
-
* @returns {Object[]|BuildModelResultKeepEmpty|null}
|
|
707
|
-
*/
|
|
708
|
-
function getHighlightIndex() {
|
|
709
|
-
var rootNode = getContainer();
|
|
710
|
-
if (!keepEmptyNodes) {
|
|
711
|
-
const highlightIndex = [];
|
|
712
|
-
if (rootNode) {
|
|
713
|
-
rootNode.normalize();
|
|
714
|
-
textNodesIndex = 0;
|
|
715
|
-
buildHighlightIndex(rootNode, highlightIndex);
|
|
716
|
-
}
|
|
717
|
-
return highlightIndex;
|
|
718
|
-
} else {
|
|
719
|
-
if (rootNode) {
|
|
720
|
-
return buildHighlightModelKeepEmpty(rootNode);
|
|
721
|
-
} else {
|
|
722
|
-
return null;
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
|
|
727
|
-
/**
|
|
728
|
-
* Traverse the DOM tree to create the text Nodes index. Recursive.
|
|
729
|
-
* @param {Node} rootNode
|
|
730
|
-
* @param {Object[]} highlightIndex
|
|
731
|
-
*/
|
|
732
|
-
function buildHighlightIndex(rootNode, highlightIndex) {
|
|
733
|
-
const childNodes = rootNode.childNodes;
|
|
734
|
-
let i, currentNode;
|
|
735
|
-
var nodeInfos, inlineRange, inlineOffset, nodesToSkip;
|
|
736
|
-
|
|
737
|
-
for (i = 0; i < childNodes.length; i++) {
|
|
738
|
-
currentNode = childNodes[i];
|
|
739
|
-
|
|
740
|
-
// Skip blacklisted nodes
|
|
741
|
-
if (isBlacklisted(currentNode)) {
|
|
742
|
-
continue;
|
|
743
|
-
}
|
|
744
|
-
// A simple node not highlighted and isolated (= not followed by an wrapped text)
|
|
745
|
-
else if (isWrappable(currentNode) && !isWrappingNode(currentNode.nextSibling)) {
|
|
746
|
-
highlightIndex[textNodesIndex] = { highlighted: false };
|
|
747
|
-
textNodesIndex++;
|
|
748
|
-
|
|
749
|
-
// an isolated node (= not followed by a highlight table text) with its whole content highlighted
|
|
750
|
-
} else if (
|
|
751
|
-
isWrappingNode(currentNode) &&
|
|
752
|
-
!isText(currentNode.nextSibling) &&
|
|
753
|
-
(!isWrappingNode(currentNode.nextSibling) ||
|
|
754
|
-
currentNode.className === currentNode.nextSibling.className)
|
|
755
|
-
) {
|
|
756
|
-
highlightIndex[textNodesIndex] = {
|
|
757
|
-
highlighted: true,
|
|
758
|
-
groupId: currentNode.getAttribute(GROUP_ATTR),
|
|
759
|
-
c: getColorByClassName(currentNode.className)
|
|
760
|
-
};
|
|
761
|
-
textNodesIndex++;
|
|
762
|
-
|
|
763
|
-
// less straightforward: a succession of (at least) 1 wrapping node with 1 wrappable text node, in either order, and possibly more
|
|
764
|
-
// the trick is to create a unique text node on which we will be able to re-apply multiple partial highlights
|
|
765
|
-
// for this, we use 'inlineRanges'
|
|
766
|
-
} else if (isHotNode(currentNode)) {
|
|
767
|
-
nodeInfos = {
|
|
768
|
-
highlighted: true,
|
|
769
|
-
inlineRanges: []
|
|
770
|
-
};
|
|
771
|
-
|
|
772
|
-
nodesToSkip = -1;
|
|
773
|
-
inlineOffset = 0;
|
|
774
|
-
|
|
775
|
-
while (currentNode) {
|
|
776
|
-
if (isWrappingNode(currentNode)) {
|
|
777
|
-
inlineRange = {
|
|
778
|
-
groupId: currentNode.getAttribute(GROUP_ATTR),
|
|
779
|
-
c: getColorByClassName(currentNode.className)
|
|
780
|
-
};
|
|
781
|
-
|
|
782
|
-
if (isText(currentNode.previousSibling) || isWrappingNode(currentNode.previousSibling)) {
|
|
783
|
-
inlineRange.startOffset = inlineOffset;
|
|
784
|
-
}
|
|
785
|
-
if (isText(currentNode.nextSibling) || isWrappingNode(currentNode.nextSibling)) {
|
|
786
|
-
inlineRange.endOffset = inlineOffset + currentNode.textContent.length;
|
|
787
|
-
}
|
|
788
|
-
nodeInfos.inlineRanges.push(inlineRange);
|
|
789
|
-
}
|
|
790
|
-
|
|
791
|
-
inlineOffset += currentNode.textContent.length;
|
|
792
|
-
currentNode =
|
|
793
|
-
isHotNode(currentNode.nextSibling) || isText(currentNode.nextSibling)
|
|
794
|
-
? currentNode.nextSibling
|
|
795
|
-
: null;
|
|
796
|
-
nodesToSkip++;
|
|
797
|
-
}
|
|
798
|
-
i += nodesToSkip; // we increase the loop counter to avoid looping over the nodes that we just analyzed
|
|
799
|
-
|
|
800
|
-
highlightIndex[textNodesIndex] = nodeInfos;
|
|
801
|
-
textNodesIndex++;
|
|
802
|
-
|
|
803
|
-
// go deeper in the node tree...
|
|
804
|
-
} else if (isElement(currentNode)) {
|
|
805
|
-
buildHighlightIndex(currentNode, highlightIndex);
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
|
|
810
|
-
/**
|
|
811
|
-
* @typedef HighlightEntryKeepEmpty
|
|
812
|
-
* @property {String} groupId
|
|
813
|
-
* @property {String} c - color
|
|
814
|
-
* @property {Number} offsetBefore
|
|
815
|
-
* @property {Number} textLength
|
|
816
|
-
* @property {String} beforeWasSplit
|
|
817
|
-
* @property {String} afterWasSplit
|
|
818
|
-
* @property {Array<Number>} path - on each level from root container to highlight, index among siblings
|
|
819
|
-
*/
|
|
820
|
-
/**
|
|
821
|
-
* @typedef BuildModelResultKeepEmpty
|
|
822
|
-
* @property {HighlightEntryKeepEmpty[]} highlightModel
|
|
823
|
-
* @property {NodeList} wrapperNodes
|
|
824
|
-
*/
|
|
825
|
-
/**
|
|
826
|
-
* For `keepEmptyNodes` option, creates data model of highlights.
|
|
827
|
-
* Additionally returns array of highlight nodes. Traverses DOM tree.
|
|
828
|
-
* @param {Node} rootNode
|
|
829
|
-
* @returns {BuildModelResultKeepEmpty|null} result
|
|
830
|
-
*/
|
|
831
|
-
function buildHighlightModelKeepEmpty(rootNode) {
|
|
832
|
-
const classNames = options.colors ? Object.values(options.colors) : [className];
|
|
833
|
-
const wrapperNodesSelector = classNames.map(cls => containerSelector + ' .' + cls).join(', ');
|
|
834
|
-
const wrapperNodes = Array.from(document.querySelectorAll(wrapperNodesSelector)).filter(
|
|
835
|
-
node => !isBlacklisted(node)
|
|
836
|
-
);
|
|
837
|
-
|
|
838
|
-
if (!wrapperNodes.length) {
|
|
839
|
-
return null;
|
|
840
|
-
}
|
|
841
|
-
|
|
842
|
-
const highlightModel = [];
|
|
843
|
-
const indexCache = new Map();
|
|
844
|
-
for (let k = 0; k < wrapperNodes.length; k++) {
|
|
845
|
-
const wrapperNode = wrapperNodes[k];
|
|
846
|
-
|
|
847
|
-
//get info about highlight itself
|
|
848
|
-
let offsetBefore = 0;
|
|
849
|
-
const prevNode = wrapperNode.previousSibling;
|
|
850
|
-
if (prevNode && isText(prevNode)) {
|
|
851
|
-
const beforeWasSplit = wrapperNode.dataset.beforeWasSplit === 'true';
|
|
852
|
-
if (beforeWasSplit) {
|
|
853
|
-
offsetBefore = prevNode.textContent.length;
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
const highlightData = {
|
|
857
|
-
groupId: wrapperNode.getAttribute(GROUP_ATTR),
|
|
858
|
-
c: getColorByClassName(wrapperNode.className),
|
|
859
|
-
offsetBefore,
|
|
860
|
-
textLength: wrapperNode.textContent.length,
|
|
861
|
-
beforeWasSplit: wrapperNode.dataset.beforeWasSplit,
|
|
862
|
-
afterWasSplit: wrapperNode.dataset.afterWasSplit,
|
|
863
|
-
path: []
|
|
864
|
-
};
|
|
865
|
-
|
|
866
|
-
//get info about its position in the tree: path through all parents from rootNode to highlight
|
|
867
|
-
let currentNode = wrapperNode;
|
|
868
|
-
while (currentNode && currentNode !== rootNode) {
|
|
869
|
-
let indexInModel = indexCache.get(currentNode);
|
|
870
|
-
if (!indexInModel && indexInModel !== 0) {
|
|
871
|
-
//should be more reliable to ignore empty nodes when indexing
|
|
872
|
-
const childNodes = Array.from(currentNode.parentNode.childNodes).filter(
|
|
873
|
-
node => !(isText(node) && !node.textContent.length)
|
|
874
|
-
);
|
|
875
|
-
//index among its non-empty siblings
|
|
876
|
-
indexInModel = childNodes.indexOf(currentNode);
|
|
877
|
-
indexCache.set(currentNode, indexInModel);
|
|
878
|
-
}
|
|
879
|
-
highlightData.path.unshift(indexInModel);
|
|
880
|
-
currentNode = currentNode.parentNode;
|
|
881
|
-
}
|
|
882
|
-
|
|
883
|
-
//add info about highlight and its position to model
|
|
884
|
-
highlightModel.push(highlightData);
|
|
885
|
-
}
|
|
886
|
-
return {
|
|
887
|
-
highlightModel,
|
|
888
|
-
wrapperNodes
|
|
889
|
-
};
|
|
890
|
-
}
|
|
891
|
-
|
|
892
|
-
/**
|
|
893
|
-
* Bootstrap the process of restoring the highlights from an index
|
|
894
|
-
* @param {Object[]|HighlightEntryKeepEmpty[]|null} highlightIndex
|
|
895
|
-
*/
|
|
896
|
-
function highlightFromIndex(highlightIndex) {
|
|
897
|
-
var rootNode = getContainer();
|
|
898
|
-
if (rootNode) {
|
|
899
|
-
if (!keepEmptyNodes) {
|
|
900
|
-
rootNode.normalize();
|
|
901
|
-
textNodesIndex = 0;
|
|
902
|
-
restoreHighlight(rootNode, highlightIndex);
|
|
903
|
-
} else {
|
|
904
|
-
restoreHighlightKeepEmpty(rootNode, highlightIndex);
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
}
|
|
908
|
-
|
|
909
|
-
/**
|
|
910
|
-
* Traverse the DOM tree to wraps the text nodes according to the highlight index. Recursive.
|
|
911
|
-
* @param {Node} rootNode
|
|
912
|
-
* @param {Object[]} highlightIndex
|
|
913
|
-
*/
|
|
914
|
-
function restoreHighlight(rootNode, highlightIndex) {
|
|
915
|
-
var childNodes = rootNode.childNodes;
|
|
916
|
-
var i, currentNode, parent;
|
|
917
|
-
var nodeInfos, nodesToSkip, range, initialChildCount;
|
|
918
|
-
|
|
919
|
-
for (i = 0; i < childNodes.length; i++) {
|
|
920
|
-
currentNode = childNodes[i];
|
|
921
|
-
|
|
922
|
-
if (isBlacklisted(currentNode)) {
|
|
923
|
-
continue;
|
|
924
|
-
} else if (isWrappable(currentNode)) {
|
|
925
|
-
parent = currentNode.parentNode;
|
|
926
|
-
initialChildCount = parent.childNodes.length;
|
|
927
|
-
|
|
928
|
-
nodeInfos = highlightIndex[textNodesIndex];
|
|
929
|
-
|
|
930
|
-
if (nodeInfos.highlighted === true) {
|
|
931
|
-
if (_.isArray(nodeInfos.inlineRanges)) {
|
|
932
|
-
nodeInfos.inlineRanges.reverse();
|
|
933
|
-
nodeInfos.inlineRanges.forEach(function (inlineRange) {
|
|
934
|
-
range = document.createRange();
|
|
935
|
-
range.setStart(currentNode, inlineRange.startOffset || 0);
|
|
936
|
-
range.setEnd(currentNode, inlineRange.endOffset || currentNode.textContent.length);
|
|
937
|
-
range.surroundContents(getWrapper(inlineRange.groupId, getClassNameByColor(inlineRange.c)));
|
|
938
|
-
});
|
|
939
|
-
|
|
940
|
-
// fully highlighted text node
|
|
941
|
-
} else {
|
|
942
|
-
range = document.createRange();
|
|
943
|
-
range.selectNodeContents(currentNode);
|
|
944
|
-
range.surroundContents(getWrapper(nodeInfos.groupId, getClassNameByColor(nodeInfos.c)));
|
|
945
|
-
}
|
|
946
|
-
// we do want to loop over the nodes created by the wrapping operation
|
|
947
|
-
nodesToSkip = parent.childNodes.length - initialChildCount;
|
|
948
|
-
i += nodesToSkip;
|
|
949
|
-
}
|
|
950
|
-
textNodesIndex++;
|
|
951
|
-
} else if (isElement(currentNode)) {
|
|
952
|
-
restoreHighlight(currentNode, highlightIndex);
|
|
953
|
-
}
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
/**
|
|
958
|
-
* For `keepEmptyNodes` option, wraps the text nodes according to highlights data model.
|
|
959
|
-
* Traverses and updates DOM tree. Shouldn't throw errors in case of mismatches.
|
|
960
|
-
* @param {Node} rootNode
|
|
961
|
-
* @param {HighlightEntryKeepEmpty[]|null} highlightModel
|
|
962
|
-
*/
|
|
963
|
-
function restoreHighlightKeepEmpty(rootNode, highlightModel) {
|
|
964
|
-
if (!highlightModel) {
|
|
965
|
-
return;
|
|
966
|
-
}
|
|
967
|
-
|
|
968
|
-
let currentModel;
|
|
969
|
-
let range;
|
|
970
|
-
for (let k = 0; k < highlightModel.length; k++) {
|
|
971
|
-
currentModel = highlightModel[k];
|
|
972
|
-
|
|
973
|
-
//find node to wrap - go through nodes until we reach level where node to wrap will be
|
|
974
|
-
let childNodes;
|
|
975
|
-
let indexInModel;
|
|
976
|
-
let currentParentNode = rootNode;
|
|
977
|
-
let pathNotFound = false;
|
|
978
|
-
if (!currentModel.path || !currentModel.path.length) {
|
|
979
|
-
continue; //something went wrong
|
|
980
|
-
}
|
|
981
|
-
for (let m = 0; m < currentModel.path.length; m++) {
|
|
982
|
-
//path was counted among non-empty nodes
|
|
983
|
-
childNodes = Array.from(currentParentNode.childNodes).filter(
|
|
984
|
-
node => !(isText(node) && !node.textContent.length)
|
|
985
|
-
);
|
|
986
|
-
indexInModel = currentModel.path[m];
|
|
987
|
-
currentParentNode = childNodes[indexInModel];
|
|
988
|
-
if (!currentParentNode && m < currentModel.path.length - 1) {
|
|
989
|
-
//node on last level may not exist yet, no need to fail. See `nodeAtIndex`
|
|
990
|
-
pathNotFound = true;
|
|
991
|
-
break;
|
|
992
|
-
}
|
|
993
|
-
}
|
|
994
|
-
if (pathNotFound) {
|
|
995
|
-
continue; //something went wrong
|
|
996
|
-
}
|
|
997
|
-
|
|
998
|
-
//add single highlight
|
|
999
|
-
let nodeAtIndex = null;
|
|
1000
|
-
if (!currentModel.offsetBefore) {
|
|
1001
|
-
//wrap starts on this node
|
|
1002
|
-
nodeAtIndex = childNodes[indexInModel];
|
|
1003
|
-
if (!nodeAtIndex || !isText(nodeAtIndex) || isBlacklisted(nodeAtIndex)) {
|
|
1004
|
-
continue; //something went wrong
|
|
1005
|
-
}
|
|
1006
|
-
} else {
|
|
1007
|
-
//split previousSibling to create a node for wrapping
|
|
1008
|
-
const nodeBefore = childNodes[indexInModel - 1];
|
|
1009
|
-
if (
|
|
1010
|
-
!nodeBefore ||
|
|
1011
|
-
!isText(nodeBefore) ||
|
|
1012
|
-
nodeBefore.textContent.length <= currentModel.offsetBefore ||
|
|
1013
|
-
isBlacklisted(nodeBefore)
|
|
1014
|
-
) {
|
|
1015
|
-
continue; //something went wrong
|
|
1016
|
-
}
|
|
1017
|
-
nodeAtIndex = nodeBefore.splitText(currentModel.offsetBefore);
|
|
1018
|
-
}
|
|
1019
|
-
//cut off its end
|
|
1020
|
-
if (nodeAtIndex.textContent.length > currentModel.textLength) {
|
|
1021
|
-
nodeAtIndex.splitText(currentModel.textLength);
|
|
1022
|
-
}
|
|
1023
|
-
|
|
1024
|
-
//wrap
|
|
1025
|
-
const wrapperNode = getWrapper(currentModel.groupId, getClassNameByColor(currentModel.c));
|
|
1026
|
-
addSplitData(wrapperNode, currentModel.beforeWasSplit, currentModel.afterWasSplit);
|
|
1027
|
-
range = document.createRange();
|
|
1028
|
-
range.selectNodeContents(nodeAtIndex);
|
|
1029
|
-
rangeSurroundContentsNoEmptyNodes(range, wrapperNode);
|
|
1030
|
-
}
|
|
1031
|
-
}
|
|
1032
|
-
|
|
1033
|
-
/**
|
|
1034
|
-
* Set highlighter color
|
|
1035
|
-
* @param {string} color Active highlighter color
|
|
1036
|
-
*/
|
|
1037
|
-
function setActiveColor(color) {
|
|
1038
|
-
if (options.colors[color]) {
|
|
1039
|
-
className = options.colors[color];
|
|
1040
|
-
}
|
|
1041
|
-
}
|
|
1042
|
-
|
|
1043
|
-
/**
|
|
1044
|
-
* Helpers
|
|
1045
|
-
*/
|
|
1046
|
-
|
|
1047
|
-
/**
|
|
1048
|
-
* Return the object key contains the given value
|
|
1049
|
-
* @param {Object} object
|
|
1050
|
-
* @param {any} value
|
|
1051
|
-
* @return {string|undefined}
|
|
1052
|
-
*/
|
|
1053
|
-
function getKeyByValue(object, value) {
|
|
1054
|
-
return Object.keys(object).find(key => object[key] === value);
|
|
1055
|
-
}
|
|
1056
|
-
|
|
1057
|
-
/**
|
|
1058
|
-
* Returns color identifier for the given class name
|
|
1059
|
-
* @param {string} highlighterClassName Class name of highlighter classes
|
|
1060
|
-
* @returns {string|number} Color identifier
|
|
1061
|
-
*/
|
|
1062
|
-
function getColorByClassName(highlighterClassName) {
|
|
1063
|
-
if (options.colors) {
|
|
1064
|
-
return getKeyByValue(options.colors, highlighterClassName);
|
|
1065
|
-
}
|
|
1066
|
-
return className;
|
|
1067
|
-
}
|
|
1068
|
-
|
|
1069
|
-
/**
|
|
1070
|
-
* Returns class name for the given color identifier
|
|
1071
|
-
* @param {string|number} color Color identifier
|
|
1072
|
-
* @returns {string} Class name
|
|
1073
|
-
*/
|
|
1074
|
-
function getClassNameByColor(color) {
|
|
1075
|
-
if (options.colors && options.colors[color]) {
|
|
1076
|
-
return options.colors[color];
|
|
1077
|
-
}
|
|
1078
|
-
return className;
|
|
1079
|
-
}
|
|
1080
|
-
|
|
1081
|
-
/**
|
|
1082
|
-
* Check if the given node is a wrapper
|
|
1083
|
-
* @param {Node|Element} node
|
|
1084
|
-
* @returns {boolean}
|
|
1085
|
-
*/
|
|
1086
|
-
function isWrappingNode(node) {
|
|
1087
|
-
return isElement(node) && node.tagName.toLowerCase() === 'span' && highlightingClasses.includes(node.className);
|
|
1088
|
-
}
|
|
1089
|
-
|
|
1090
|
-
/**
|
|
1091
|
-
* Check if the given node can be wrapped
|
|
1092
|
-
* @param {Node} node
|
|
1093
|
-
* @returns {boolean}
|
|
1094
|
-
*/
|
|
1095
|
-
function isWrappable(node) {
|
|
1096
|
-
return isText(node) && !isBlacklisted(node);
|
|
1097
|
-
}
|
|
1098
|
-
|
|
1099
|
-
/**
|
|
1100
|
-
* Check if the given node is, or is within, a blacklisted container.
|
|
1101
|
-
* With `keepEmptyNodes` option, node inside blacklisted container can be whitelisted too.
|
|
1102
|
-
* Priority of blacklist or whitelist is decided by which selector is closest to the node.
|
|
1103
|
-
* If no match found, node is considered whitelisted.
|
|
1104
|
-
* @param {Node} node
|
|
1105
|
-
* @returns {boolean}
|
|
1106
|
-
*/
|
|
1107
|
-
function isBlacklisted(node) {
|
|
1108
|
-
const closest = $(node).closest(containersBlackAndWhiteListSelector);
|
|
1109
|
-
if (!closest.length) {
|
|
1110
|
-
return false;
|
|
1111
|
-
} else if (!containersWhiteListSelector) {
|
|
1112
|
-
return true;
|
|
1113
|
-
} else {
|
|
1114
|
-
return !closest.get(0).matches(containersWhiteListSelector);
|
|
1115
|
-
}
|
|
1116
|
-
}
|
|
1117
|
-
|
|
1118
|
-
/**
|
|
1119
|
-
* Wraps text node to the highlighter wrapper element
|
|
1120
|
-
* @param {Node} textNode Text node to wrap
|
|
1121
|
-
* @param {string} wrapperClassName Wrapper class name
|
|
1122
|
-
* @param {number} groupId Group id
|
|
1123
|
-
*/
|
|
1124
|
-
function wrapNode(textNode, wrapperClassName, groupId) {
|
|
1125
|
-
const element = getWrapper(groupId, wrapperClassName);
|
|
1126
|
-
element.appendChild(textNode);
|
|
1127
|
-
return element;
|
|
1128
|
-
}
|
|
1129
|
-
|
|
1130
|
-
/**
|
|
1131
|
-
* Create a wrapping node
|
|
1132
|
-
* @param {number} groupId
|
|
1133
|
-
* @returns {Element}
|
|
1134
|
-
*/
|
|
1135
|
-
function getWrapper(groupId, wrapperClass) {
|
|
1136
|
-
const wrapper = document.createElement('span');
|
|
1137
|
-
wrapper.className = wrapperClass || className;
|
|
1138
|
-
wrapper.setAttribute(GROUP_ATTR, `${groupId}`);
|
|
1139
|
-
return wrapper;
|
|
1140
|
-
}
|
|
1141
|
-
|
|
1142
|
-
/**
|
|
1143
|
-
* Returns the first unused group Id
|
|
1144
|
-
* @returns {number}
|
|
1145
|
-
*/
|
|
1146
|
-
function getAvailableGroupId() {
|
|
1147
|
-
var id = currentGroupId || 1;
|
|
1148
|
-
while ($(getContainer()).find('[' + GROUP_ATTR + '=' + id + ']').length !== 0) {
|
|
1149
|
-
id++;
|
|
1150
|
-
}
|
|
1151
|
-
return id;
|
|
1152
|
-
}
|
|
1153
|
-
|
|
1154
|
-
/**
|
|
1155
|
-
* Check if the given node is an element
|
|
1156
|
-
* @param {Node} node
|
|
1157
|
-
* @returns {boolean}
|
|
1158
|
-
*/
|
|
1159
|
-
function isElement(node) {
|
|
1160
|
-
return node && typeof node === 'object' && node.nodeType === window.Node.ELEMENT_NODE;
|
|
1161
|
-
}
|
|
1162
|
-
|
|
1163
|
-
/**
|
|
1164
|
-
* Check if the given node is of type text
|
|
1165
|
-
* @param {Node} node
|
|
1166
|
-
* @returns {boolean}
|
|
1167
|
-
*/
|
|
1168
|
-
function isText(node) {
|
|
1169
|
-
return node && typeof node === 'object' && node.nodeType === window.Node.TEXT_NODE;
|
|
1170
|
-
}
|
|
1171
|
-
|
|
1172
|
-
/**
|
|
1173
|
-
* a "Hot Node" is either wrappable text node or a wrapper
|
|
1174
|
-
* @param {Node} node
|
|
1175
|
-
* @returns {boolean}
|
|
1176
|
-
*/
|
|
1177
|
-
function isHotNode(node) {
|
|
1178
|
-
return isWrappingNode(node) || isWrappable(node);
|
|
1179
|
-
}
|
|
1180
|
-
|
|
1181
|
-
/**
|
|
1182
|
-
* Public API of the highlighter helper
|
|
1183
|
-
*/
|
|
1184
|
-
return {
|
|
1185
|
-
highlightRanges: highlightRanges,
|
|
1186
|
-
highlightFromIndex: highlightFromIndex,
|
|
1187
|
-
getHighlightIndex: getHighlightIndex,
|
|
1188
|
-
clearHighlights: clearHighlights,
|
|
1189
|
-
clearSingleHighlight: clearSingleHighlight,
|
|
1190
|
-
setActiveColor
|
|
1191
|
-
};
|
|
1192
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* This program is free software; you can redistribute it and/or
|
|
3
|
+
* modify it under the terms of the GNU General Public License
|
|
4
|
+
* as published by the Free Software Foundation; under version 2
|
|
5
|
+
* of the License (non-upgradable).
|
|
6
|
+
*
|
|
7
|
+
* This program is distributed in the hope that it will be useful,
|
|
8
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10
|
+
* GNU General Public License for more details.
|
|
11
|
+
*
|
|
12
|
+
* You should have received a copy of the GNU General Public License
|
|
13
|
+
* along with this program; if not, write to the Free Software
|
|
14
|
+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
15
|
+
*
|
|
16
|
+
* Copyright (c) 2016-2021 (original work) Open Assessment Technologies SA;
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Highlighter helper: wraps every text node found within a Range object.
|
|
20
|
+
*
|
|
21
|
+
* @author Christophe Noël <christophe@taotesting.com>
|
|
22
|
+
*/
|
|
23
|
+
import _ from 'lodash';
|
|
24
|
+
import $ from 'jquery';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Data attribute used to logically group the wrapping nodes into a single selection
|
|
28
|
+
* @type {string}
|
|
29
|
+
*/
|
|
30
|
+
var GROUP_ATTR = 'data-hl-group';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Children of those nodes types cannot be highlighted
|
|
34
|
+
* @type {string[]}
|
|
35
|
+
*/
|
|
36
|
+
var defaultBlackList = ['textarea', 'math', 'script', '.select2-container'];
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {Object} options
|
|
40
|
+
* @param {String} options.className - name of the class that will be used by the wrappers tags to highlight text
|
|
41
|
+
* @param {String} options.containerSelector - allows to select the root Node in which highlighting is allowed
|
|
42
|
+
* @param {Array<String>} [options.containersBlackList] - additional blacklist selectors to be added to module instance's blacklist
|
|
43
|
+
* @param {Array<String>} [options.containersWhiteList] - whitelist selectors; supported only in `keepEmptyNodes` mode.
|
|
44
|
+
* Priority of blacklist or whitelist is decided by which selector is closest to the node. If no match found, node is considered whitelisted.
|
|
45
|
+
* @param {Boolean} [options.clearOnClick] - clear single highlight node on click
|
|
46
|
+
* @param {Object} [options.colors] - keys is keeping as the "c" value of storing/restore the highlighters for indexing, values are wrappers class names
|
|
47
|
+
* @param {Boolean} [options.keepEmptyNodes] - retain original dom structure as far as possible and do not remove empty nodes if they were not created by highlighter
|
|
48
|
+
* @returns {Object} - the highlighter instance
|
|
49
|
+
*/
|
|
50
|
+
export default function (options) {
|
|
51
|
+
var className = options.className;
|
|
52
|
+
var containerSelector = options.containerSelector;
|
|
53
|
+
var keepEmptyNodes = options.keepEmptyNodes;
|
|
54
|
+
|
|
55
|
+
let highlightingClasses = [className];
|
|
56
|
+
|
|
57
|
+
// Multi-color mode
|
|
58
|
+
if (options.colors) {
|
|
59
|
+
highlightingClasses = Object.values(options.colors);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* list of node selectors which should NOT receive any highlighting from this instance
|
|
64
|
+
* an optional passed-in blacklist is merged with local defaults
|
|
65
|
+
* @type {Array}
|
|
66
|
+
*/
|
|
67
|
+
const containersBlackList = _.union(defaultBlackList, options.containersBlackList);
|
|
68
|
+
const containersBlackListSelector = containersBlackList.join(', ');
|
|
69
|
+
let containersWhiteListSelector = null;
|
|
70
|
+
let containersBlackAndWhiteListSelector = containersBlackListSelector;
|
|
71
|
+
if (options.keepEmptyNodes && options.containersWhiteList) {
|
|
72
|
+
containersWhiteListSelector = options.containersWhiteList.join(', ');
|
|
73
|
+
containersBlackAndWhiteListSelector = _.union(containersBlackList, options.containersWhiteList).join(', ');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* used in recursive loops to decide if we should wrap or not the current node
|
|
78
|
+
* @type {boolean}
|
|
79
|
+
*/
|
|
80
|
+
let isWrapping = false;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* performance improvement to break out of a potentially big recursive loop once the wrapping has ended
|
|
84
|
+
* @type {boolean}
|
|
85
|
+
*/
|
|
86
|
+
let hasWrapped = false;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* used in recursive loops to assign a group Id to the current wrapped node
|
|
90
|
+
* @type {number}
|
|
91
|
+
*/
|
|
92
|
+
let currentGroupId;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* used in recursive loops to build the index of text nodes
|
|
96
|
+
* @type {number}
|
|
97
|
+
*/
|
|
98
|
+
let textNodesIndex;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Returns the node in which highlighting is allowed
|
|
102
|
+
* @returns {Element}
|
|
103
|
+
*/
|
|
104
|
+
function getContainer() {
|
|
105
|
+
return $(containerSelector).get(0);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Returns all highlighted nodes, excluding any inside blacklisted elements
|
|
110
|
+
* @returns {JQuery<HTMLElement>}
|
|
111
|
+
*/
|
|
112
|
+
function getHighlightedNodes() {
|
|
113
|
+
return $(containerSelector)
|
|
114
|
+
.find(`.${highlightingClasses.join(',.')}`)
|
|
115
|
+
.filter((i, node) => !isBlacklisted(node));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Attach data to wrapper node.
|
|
120
|
+
* Use it when deleting this highlight to know if highlight content should be merged with neighbour text nodes or not.
|
|
121
|
+
* Use it when building/restoring index to know if restored highlight content should be split off neighbour text node or not.
|
|
122
|
+
* Needed to keep markup the same as it was before highlighting.
|
|
123
|
+
* @param {HTMLElement} node
|
|
124
|
+
* @param {Boolean} beforeWasSplit
|
|
125
|
+
* @param {Boolean} afterWasSplit
|
|
126
|
+
*/
|
|
127
|
+
function addSplitData(node, beforeWasSplit, afterWasSplit) {
|
|
128
|
+
node.dataset.beforeWasSplit = beforeWasSplit;
|
|
129
|
+
node.dataset.afterWasSplit = afterWasSplit;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Highlight all text nodes within each given range
|
|
134
|
+
* @param {Range[]} ranges - array of ranges to highlight, may be given by the helper selector.getAllRanges()
|
|
135
|
+
*/
|
|
136
|
+
function highlightRanges(ranges) {
|
|
137
|
+
ranges.forEach(function (range) {
|
|
138
|
+
var rangeInfos;
|
|
139
|
+
|
|
140
|
+
if (isRangeValid(range)) {
|
|
141
|
+
currentGroupId = getAvailableGroupId();
|
|
142
|
+
|
|
143
|
+
// easy peasy: highlighting a plain text without any DOM nodes
|
|
144
|
+
// NOTE: The condition checks the whole node content and not a selected content in a given range, that allows to wrap whitespace
|
|
145
|
+
if (
|
|
146
|
+
isWrappable(range.commonAncestorContainer) &&
|
|
147
|
+
!isWrappingNode(range.commonAncestorContainer.parentNode)
|
|
148
|
+
) {
|
|
149
|
+
const wrapperNode = getWrapper(currentGroupId);
|
|
150
|
+
if (!keepEmptyNodes) {
|
|
151
|
+
range.surroundContents(wrapperNode);
|
|
152
|
+
} else {
|
|
153
|
+
addSplitData(
|
|
154
|
+
wrapperNode,
|
|
155
|
+
range.startOffset > 0,
|
|
156
|
+
range.endOffset < range.commonAncestorContainer.length
|
|
157
|
+
);
|
|
158
|
+
rangeSurroundContentsNoEmptyNodes(range, wrapperNode);
|
|
159
|
+
}
|
|
160
|
+
} else if (
|
|
161
|
+
isWrappable(range.commonAncestorContainer) &&
|
|
162
|
+
isWrappingNode(range.commonAncestorContainer.parentNode) &&
|
|
163
|
+
range.commonAncestorContainer.parentNode !== className
|
|
164
|
+
) {
|
|
165
|
+
highlightContainerNodes(range.commonAncestorContainer, className, range, currentGroupId);
|
|
166
|
+
|
|
167
|
+
// now the fun stuff: highlighting a mix of text and DOM nodes
|
|
168
|
+
} else {
|
|
169
|
+
rangeInfos = {
|
|
170
|
+
startNode: isElement(range.startContainer)
|
|
171
|
+
? range.startContainer.childNodes[range.startOffset]
|
|
172
|
+
: range.startContainer,
|
|
173
|
+
startNodeContainer: range.startContainer,
|
|
174
|
+
startOffset: range.startOffset,
|
|
175
|
+
|
|
176
|
+
endNode:
|
|
177
|
+
isElement(range.endContainer) && range.endOffset > 0
|
|
178
|
+
? range.endContainer.childNodes[range.endOffset - 1]
|
|
179
|
+
: range.endContainer,
|
|
180
|
+
endNodeContainer: range.endContainer,
|
|
181
|
+
endOffset: range.endOffset,
|
|
182
|
+
commonRange: range
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
isWrapping = false;
|
|
186
|
+
hasWrapped = false;
|
|
187
|
+
wrapTextNodesInRange(range.commonAncestorContainer, rangeInfos);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (!keepEmptyNodes) {
|
|
192
|
+
// clean up the markup after wrapping...
|
|
193
|
+
range.commonAncestorContainer.normalize();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
currentGroupId = 0;
|
|
197
|
+
isWrapping = false;
|
|
198
|
+
reindexGroups(getContainer());
|
|
199
|
+
mergeAdjacentWrappingNodes(getContainer());
|
|
200
|
+
unWrapEmptyHighlights();
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
if (options.clearOnClick) {
|
|
204
|
+
$(containerSelector + ' .' + className)
|
|
205
|
+
.off('click')
|
|
206
|
+
.on('click', clearSingleHighlight);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Check if a range is valid
|
|
212
|
+
* @param {Range} range
|
|
213
|
+
* @returns {boolean}
|
|
214
|
+
*/
|
|
215
|
+
function isRangeValid(range) {
|
|
216
|
+
var rangeInContainer;
|
|
217
|
+
try {
|
|
218
|
+
rangeInContainer =
|
|
219
|
+
$.contains(getContainer(), range.commonAncestorContainer) ||
|
|
220
|
+
getContainer().isSameNode(range.commonAncestorContainer);
|
|
221
|
+
|
|
222
|
+
return rangeInContainer && !range.collapsed;
|
|
223
|
+
} catch (e) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Core wrapping function. Traverse the DOM tree and highlight (= wraps) all text nodes within the given range.
|
|
230
|
+
* Recursive.
|
|
231
|
+
*
|
|
232
|
+
* @param {Node} rootNode - top of the node hierarchy in which text nodes will be searched
|
|
233
|
+
* @param {Object} rangeInfos
|
|
234
|
+
* @param {Node} rangeInfos.startNode - node on which the selection starts
|
|
235
|
+
* @param {Node} rangeInfos.startNodeContainer - container of the startNode, or the start node itself in case of text nodes
|
|
236
|
+
* @param {number} rangeInfos.startOffset - same as range.startOffset, but not read-only to allow override
|
|
237
|
+
* @param {Node} rangeInfos.endNode - node on which the selection ends
|
|
238
|
+
* @param {Node} rangeInfos.endNodeContainer - container of the endNode, or the end node itself in case of text nodes
|
|
239
|
+
* @param {number} rangeInfos.endOffset - same as range.endOffset, but not read-only to allow override
|
|
240
|
+
*/
|
|
241
|
+
function wrapTextNodesInRange(rootNode, rangeInfos) {
|
|
242
|
+
var childNodes = rootNode.childNodes;
|
|
243
|
+
var currentNode, i;
|
|
244
|
+
var splitDatas = [];
|
|
245
|
+
|
|
246
|
+
for (i = 0; i < childNodes.length; i++) {
|
|
247
|
+
if (hasWrapped) {
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
currentNode = childNodes[i];
|
|
251
|
+
|
|
252
|
+
if (isBlacklisted(currentNode)) {
|
|
253
|
+
if (isElement(currentNode)) {
|
|
254
|
+
//go deeper in case a descendant of the current blacklisted is whitelisted
|
|
255
|
+
wrapTextNodesInRange(currentNode, rangeInfos);
|
|
256
|
+
}
|
|
257
|
+
} else {
|
|
258
|
+
const isCurrentNodeTextInsideOfAnotherHighlightingWrapper =
|
|
259
|
+
isText(currentNode) &&
|
|
260
|
+
isWrappingNode(currentNode.parentNode) &&
|
|
261
|
+
currentNode.parentNode.className !== className;
|
|
262
|
+
|
|
263
|
+
if (isCurrentNodeTextInsideOfAnotherHighlightingWrapper) {
|
|
264
|
+
const internalRange = new Range();
|
|
265
|
+
internalRange.selectNodeContents(currentNode);
|
|
266
|
+
|
|
267
|
+
if (rangeInfos.startNode === currentNode) {
|
|
268
|
+
internalRange.setStart(currentNode, rangeInfos.startOffset);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (rangeInfos.endNode === currentNode) {
|
|
272
|
+
internalRange.setEnd(currentNode, rangeInfos.endOffset);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const isNodeInRange = rangeInfos.commonRange.isPointInRange(currentNode, internalRange.endOffset);
|
|
276
|
+
|
|
277
|
+
// Apply new highlighting color only for selected nodes
|
|
278
|
+
if (isNodeInRange) {
|
|
279
|
+
isWrapping = true;
|
|
280
|
+
highlightContainerNodes(currentNode, className, internalRange, currentGroupId);
|
|
281
|
+
}
|
|
282
|
+
} else {
|
|
283
|
+
// split current node in case the wrapping start/ends on a partially selected text node
|
|
284
|
+
if (currentNode.isSameNode(rangeInfos.startNode)) {
|
|
285
|
+
if (isText(rangeInfos.startNodeContainer) && rangeInfos.startOffset !== 0) {
|
|
286
|
+
// we defer the wrapping to the next iteration of the loop
|
|
287
|
+
//end of node should be highlighted
|
|
288
|
+
rangeInfos.startNode = currentNode.splitText(rangeInfos.startOffset);
|
|
289
|
+
rangeInfos.startOffset = 0;
|
|
290
|
+
splitDatas.push({ node: rangeInfos.startNode, beforeWasSplit: true, afterWasSplit: false });
|
|
291
|
+
} else {
|
|
292
|
+
//whole node should be highlighted
|
|
293
|
+
isWrapping = true;
|
|
294
|
+
splitDatas.push({ node: currentNode, beforeWasSplit: false, afterWasSplit: false });
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (currentNode.isSameNode(rangeInfos.endNode) && isText(rangeInfos.endNodeContainer)) {
|
|
299
|
+
if (rangeInfos.endOffset !== 0) {
|
|
300
|
+
if (rangeInfos.endOffset < currentNode.textContent.length) {
|
|
301
|
+
//start of node should be highlighted
|
|
302
|
+
currentNode.splitText(rangeInfos.endOffset);
|
|
303
|
+
splitDatas.push({ node: currentNode, beforeWasSplit: false, afterWasSplit: true });
|
|
304
|
+
} else {
|
|
305
|
+
//whole node should be highlighted
|
|
306
|
+
splitDatas.push({ node: currentNode, beforeWasSplit: false, afterWasSplit: false });
|
|
307
|
+
}
|
|
308
|
+
} else {
|
|
309
|
+
isWrapping = false;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// wrap the current node...
|
|
314
|
+
if (isText(currentNode)) {
|
|
315
|
+
if (!keepEmptyNodes) {
|
|
316
|
+
wrapTextNode(currentNode, currentGroupId);
|
|
317
|
+
} else if (willHighlightNotBeEmptyAfterMerge(currentNode)) {
|
|
318
|
+
const wrapperNode = wrapTextNode(currentNode, currentGroupId);
|
|
319
|
+
if (wrapperNode) {
|
|
320
|
+
const splitData = splitDatas.find(d => d.node === currentNode);
|
|
321
|
+
addSplitData(
|
|
322
|
+
wrapperNode,
|
|
323
|
+
splitData ? splitData.beforeWasSplit : false,
|
|
324
|
+
splitData ? splitData.afterWasSplit : false
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// ... or continue deeper in the node tree
|
|
330
|
+
} else if (isElement(currentNode)) {
|
|
331
|
+
//some selections end at the very start of the next node, we should end wrapping when we reach such node
|
|
332
|
+
if (!currentNode.isSameNode(rangeInfos.endNode) || rangeInfos.endOffset > 0) {
|
|
333
|
+
wrapTextNodesInRange(currentNode, rangeInfos);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// end wrapping ?
|
|
340
|
+
if (currentNode.isSameNode(rangeInfos.endNode)) {
|
|
341
|
+
isWrapping = false;
|
|
342
|
+
hasWrapped = true;
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Restructure content of the highlighted wrapper according to the selectedRange
|
|
350
|
+
* @param {Node} textNode
|
|
351
|
+
* @param {string} activeClass
|
|
352
|
+
* @param {Range} selectedRange
|
|
353
|
+
* @param {number} groupId
|
|
354
|
+
*/
|
|
355
|
+
function highlightContainerNodes(textNode, activeClass, selectedRange, groupId) {
|
|
356
|
+
const container = textNode.parentNode;
|
|
357
|
+
const range = new Range();
|
|
358
|
+
range.selectNodeContents(textNode);
|
|
359
|
+
|
|
360
|
+
const isSelectionCoversNodeStart = range.compareBoundaryPoints(Range.START_TO_START, selectedRange) === 0;
|
|
361
|
+
const isSelectionCoversNodeEnd = range.compareBoundaryPoints(Range.END_TO_END, selectedRange) === 0;
|
|
362
|
+
|
|
363
|
+
/*
|
|
364
|
+
There are 4 possible cases selected area is intersected with already highlighted element.
|
|
365
|
+
In examples below the border is represents the selection, "yellow" is class name of already highlighted
|
|
366
|
+
container, "red" is class name of currently active highlighter
|
|
367
|
+
**********************************************************************************************************
|
|
368
|
+
1. The container content is completely selected, so that we only have to change the highlighter class name
|
|
369
|
+
|
|
370
|
+
Input:
|
|
371
|
+
__________________________________________________
|
|
372
|
+
| |
|
|
373
|
+
|<span class="yellow"> Lorem ipsum dolor sit</span>|
|
|
374
|
+
|__________________________________________________|
|
|
375
|
+
|
|
376
|
+
Output:
|
|
377
|
+
<span class="red"> Lorem ipsum dolor sit</span>
|
|
378
|
+
|
|
379
|
+
**********************************************************************************************************
|
|
380
|
+
2. The container content is partially selected from the begging.
|
|
381
|
+
|
|
382
|
+
Input:
|
|
383
|
+
______________________________
|
|
384
|
+
| |
|
|
385
|
+
|<span class="yellow"> Lorem ip|sum dolor sit</span>
|
|
386
|
+
|______________________________|
|
|
387
|
+
|
|
388
|
+
Output:
|
|
389
|
+
<span class="red"> Lorem ip</span><span class="yellow">sum dolor sit</span>
|
|
390
|
+
|
|
391
|
+
**********************************************************************************************************
|
|
392
|
+
3. The container content is partially selected at the end.
|
|
393
|
+
|
|
394
|
+
Input:
|
|
395
|
+
____________________
|
|
396
|
+
| |
|
|
397
|
+
<span class="yellow"> Lorem ip|sum dolor sit</span>|
|
|
398
|
+
|____________________|
|
|
399
|
+
|
|
400
|
+
Output:
|
|
401
|
+
<span class="yellow"> Lorem ip</span><span class="red">sum dolor sit</span>
|
|
402
|
+
|
|
403
|
+
**********************************************************************************************************
|
|
404
|
+
4. The container content is partially selected in the middle.
|
|
405
|
+
|
|
406
|
+
Input:
|
|
407
|
+
___________
|
|
408
|
+
| |
|
|
409
|
+
<span class="yellow"> Lorem |ipsum dolor| sit</span>
|
|
410
|
+
|___________|
|
|
411
|
+
|
|
412
|
+
Output:
|
|
413
|
+
<span class="yellow"> Lorem </span><span class="red">ipsum dolor</span><span class="yellow"> sit</span>
|
|
414
|
+
*/
|
|
415
|
+
if (isSelectionCoversNodeStart && isSelectionCoversNodeEnd) {
|
|
416
|
+
textNode.parentNode.className = activeClass;
|
|
417
|
+
} else if (isSelectionCoversNodeStart) {
|
|
418
|
+
textNode.splitText(selectedRange.endOffset);
|
|
419
|
+
wrapContainerChildNodes(container, 0, activeClass, groupId);
|
|
420
|
+
} else if (isSelectionCoversNodeEnd) {
|
|
421
|
+
textNode.splitText(selectedRange.startOffset);
|
|
422
|
+
wrapContainerChildNodes(container, 1, activeClass, groupId);
|
|
423
|
+
} else {
|
|
424
|
+
textNode.splitText(selectedRange.startOffset).splitText(selectedRange.endOffset);
|
|
425
|
+
wrapContainerChildNodes(container, 1, activeClass, groupId);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Wraps all containers text nodes with highlighter element.
|
|
431
|
+
* The child node with index given by indexToWrapNode parameter will be wrap with class given by activeClass parameter
|
|
432
|
+
* @param {Element} container
|
|
433
|
+
* @param {number} indexToWrapNode
|
|
434
|
+
* @param {string} activeClass
|
|
435
|
+
* @param {number} groupId
|
|
436
|
+
*/
|
|
437
|
+
function wrapContainerChildNodes(container, indexToWrapNode, activeClass, groupId) {
|
|
438
|
+
const containerClass = container.className;
|
|
439
|
+
const fragment = new DocumentFragment();
|
|
440
|
+
const childNodesLength = container.childNodes.length;
|
|
441
|
+
|
|
442
|
+
container.childNodes.forEach((node, index) => {
|
|
443
|
+
var wrapperNode;
|
|
444
|
+
if (index === indexToWrapNode) {
|
|
445
|
+
wrapperNode = wrapNode(node.cloneNode(), activeClass, groupId);
|
|
446
|
+
} else {
|
|
447
|
+
wrapperNode = wrapNode(node.cloneNode(), containerClass, groupId);
|
|
448
|
+
}
|
|
449
|
+
fragment.appendChild(wrapperNode);
|
|
450
|
+
|
|
451
|
+
if (keepEmptyNodes) {
|
|
452
|
+
addSplitData(
|
|
453
|
+
wrapperNode,
|
|
454
|
+
index === 0 ? container.dataset.beforeWasSplit : true,
|
|
455
|
+
index === childNodesLength - 1 ? container.dataset.afterWasSplit : true
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
container.replaceWith(fragment);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* wraps a text node into the highlight span
|
|
465
|
+
* @param {Node} node - the node to wrap
|
|
466
|
+
* @param {number} groupId - the highlight group
|
|
467
|
+
* @returns {Node|null} wrapper node, if it was created
|
|
468
|
+
*/
|
|
469
|
+
function wrapTextNode(node, groupId) {
|
|
470
|
+
if (isWrapping && !isWrappingNode(node.parentNode) && isWrappable(node)) {
|
|
471
|
+
$(node).wrap(getWrapper(groupId));
|
|
472
|
+
return node.parentNode;
|
|
473
|
+
}
|
|
474
|
+
return null;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* We need to re-index the groups after a user highlight: either to merge groups or to resolve inconsistencies
|
|
479
|
+
* Recursive.
|
|
480
|
+
*
|
|
481
|
+
* @param {Node} rootNode
|
|
482
|
+
*/
|
|
483
|
+
function reindexGroups(rootNode) {
|
|
484
|
+
if (!rootNode) {
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const childNodes = rootNode.childNodes;
|
|
489
|
+
let i, currentNode, parent;
|
|
490
|
+
|
|
491
|
+
for (i = 0; i < childNodes.length; i++) {
|
|
492
|
+
currentNode = childNodes[i];
|
|
493
|
+
|
|
494
|
+
if (isWrappable(currentNode)) {
|
|
495
|
+
parent = currentNode.parentNode;
|
|
496
|
+
if (isWrappingNode(parent)) {
|
|
497
|
+
if (isWrapping === false) {
|
|
498
|
+
currentGroupId++;
|
|
499
|
+
}
|
|
500
|
+
isWrapping = true;
|
|
501
|
+
parent.setAttribute(GROUP_ATTR, currentGroupId); // set the new group Id
|
|
502
|
+
} else {
|
|
503
|
+
isWrapping = false;
|
|
504
|
+
}
|
|
505
|
+
} else if (isElement(currentNode)) {
|
|
506
|
+
reindexGroups(currentNode);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Some highlights may result in having adjacent wrapping nodes. We remove them here to get a cleaner markup.
|
|
513
|
+
*
|
|
514
|
+
* @param {Node} rootNode
|
|
515
|
+
*/
|
|
516
|
+
function mergeAdjacentWrappingNodes(rootNode) {
|
|
517
|
+
if (!rootNode) {
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
const childNodes = rootNode.childNodes;
|
|
522
|
+
let i, currentNode;
|
|
523
|
+
|
|
524
|
+
for (i = 0; i < childNodes.length; i++) {
|
|
525
|
+
currentNode = childNodes[i];
|
|
526
|
+
|
|
527
|
+
if (isWrappingNode(currentNode)) {
|
|
528
|
+
if (keepEmptyNodes) {
|
|
529
|
+
currentNode.normalize();
|
|
530
|
+
}
|
|
531
|
+
while (
|
|
532
|
+
isWrappingNode(currentNode.nextSibling) &&
|
|
533
|
+
currentNode.className === currentNode.nextSibling.className
|
|
534
|
+
) {
|
|
535
|
+
if (keepEmptyNodes) {
|
|
536
|
+
currentNode.nextSibling.normalize();
|
|
537
|
+
}
|
|
538
|
+
currentNode.firstChild.textContent += currentNode.nextSibling.firstChild.textContent;
|
|
539
|
+
if (keepEmptyNodes) {
|
|
540
|
+
addSplitData(
|
|
541
|
+
currentNode,
|
|
542
|
+
currentNode.dataset.beforeWasSplit,
|
|
543
|
+
currentNode.nextSibling.dataset.afterWasSplit
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
currentNode.parentNode.removeChild(currentNode.nextSibling);
|
|
547
|
+
}
|
|
548
|
+
} else if (isElement(currentNode)) {
|
|
549
|
+
mergeAdjacentWrappingNodes(currentNode);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Unwraps highlighted nodes with a line break or with an empty content
|
|
556
|
+
*/
|
|
557
|
+
function unWrapEmptyHighlights() {
|
|
558
|
+
getHighlightedNodes().each((index, node) => {
|
|
559
|
+
const nodeContent = node.textContent;
|
|
560
|
+
|
|
561
|
+
if (nodeContent.trim().length === 0) {
|
|
562
|
+
if (nodeContent.length === 0 || /\r|\n/.exec(nodeContent)) {
|
|
563
|
+
clearSingleHighlight({ target: node });
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* Check condition to avoid the work of `unwrapEmptyHighlights` ahead of time, before `mergeAdjacentNodes` runs,
|
|
571
|
+
* because in `keepEmptyNodes` case we do not want to add nodes to dom unless necessary.
|
|
572
|
+
* Also be more strict and don't allow to select nodes with spaces only, because they may appear in unexpected places in markup
|
|
573
|
+
* (here it's not exactly same as `unwrapEmptyHighlights`).
|
|
574
|
+
* @param {Node} node - node which will be wrapped (highlighted)
|
|
575
|
+
* @returns {Boolean}
|
|
576
|
+
*/
|
|
577
|
+
function willHighlightNotBeEmptyAfterMerge(node) {
|
|
578
|
+
if (!node.textContent.length) {
|
|
579
|
+
return false;
|
|
580
|
+
}
|
|
581
|
+
if (node.textContent.trim().length) {
|
|
582
|
+
return true;
|
|
583
|
+
}
|
|
584
|
+
const prevNode = node.previousSibling;
|
|
585
|
+
const canWrapperBeMergedWithPreviousSibling =
|
|
586
|
+
prevNode && isWrappingNode(prevNode) && prevNode.className === className;
|
|
587
|
+
if (canWrapperBeMergedWithPreviousSibling) {
|
|
588
|
+
return true;
|
|
589
|
+
}
|
|
590
|
+
const nextNode = node.nextSibling;
|
|
591
|
+
const canWrapperBeMergedWithNextSibling =
|
|
592
|
+
nextNode && isWrappingNode(nextNode) && nextNode.className === className;
|
|
593
|
+
if (canWrapperBeMergedWithNextSibling) {
|
|
594
|
+
return true;
|
|
595
|
+
}
|
|
596
|
+
return false;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* `range.surroundContents` can create empty text nodes,
|
|
601
|
+
* which will cause trouble in `mergeAdjacentNodes` later (in `keepEmptyNodes` case).
|
|
602
|
+
* This method surrounds range, then removes those nodes
|
|
603
|
+
* @param {Range} range
|
|
604
|
+
* @param {Node} wrapperNode
|
|
605
|
+
*/
|
|
606
|
+
function rangeSurroundContentsNoEmptyNodes(range, wrapperNode) {
|
|
607
|
+
const containerPreviousSibling = range.commonAncestorContainer.previousSibling;
|
|
608
|
+
const containerNextSibling = range.commonAncestorContainer.nextSibling;
|
|
609
|
+
|
|
610
|
+
range.surroundContents(wrapperNode);
|
|
611
|
+
|
|
612
|
+
removeEmptyTextNodeIfDifferent(wrapperNode.previousSibling, containerPreviousSibling);
|
|
613
|
+
removeEmptyTextNodeIfDifferent(wrapperNode.nextSibling, containerNextSibling);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* Remove `node`, if it's an empty text node and is *not* the same node as `nodeToCompare`
|
|
618
|
+
* @param {Node} node
|
|
619
|
+
* @param {Node} nodeToCompare
|
|
620
|
+
*/
|
|
621
|
+
function removeEmptyTextNodeIfDifferent(node, nodeToCompare) {
|
|
622
|
+
if (node && node !== nodeToCompare && isText(node) && node.textContent.length === 0) {
|
|
623
|
+
node.remove();
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* Remove all wrapping nodes from markup
|
|
629
|
+
*/
|
|
630
|
+
function clearHighlights() {
|
|
631
|
+
getHighlightedNodes().each(function (i, elem) {
|
|
632
|
+
if (!keepEmptyNodes) {
|
|
633
|
+
const $wrapped = $(this);
|
|
634
|
+
$wrapped.replaceWith($wrapped.text());
|
|
635
|
+
} else {
|
|
636
|
+
clearSingleHighlight({ target: elem });
|
|
637
|
+
}
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* Remove unwrap dom node
|
|
643
|
+
*/
|
|
644
|
+
function clearSingleHighlight(e) {
|
|
645
|
+
if (!keepEmptyNodes) {
|
|
646
|
+
const $wrapped = $(e.target);
|
|
647
|
+
const text = $wrapped.text();
|
|
648
|
+
|
|
649
|
+
// NOTE: JQuery replaceWith is not working with empty string https://bugs.jquery.com/ticket/13401
|
|
650
|
+
if (text === '') {
|
|
651
|
+
$wrapped.remove();
|
|
652
|
+
} else {
|
|
653
|
+
$wrapped.replaceWith(text);
|
|
654
|
+
}
|
|
655
|
+
} else {
|
|
656
|
+
const nodeToRemove = e.target;
|
|
657
|
+
const nodeToRemoveText = nodeToRemove.textContent;
|
|
658
|
+
const beforeWasSplit = nodeToRemove.dataset.beforeWasSplit === 'true';
|
|
659
|
+
const afterWasSplit = nodeToRemove.dataset.afterWasSplit === 'true';
|
|
660
|
+
const prevNode = nodeToRemove.previousSibling;
|
|
661
|
+
const nextNode = nodeToRemove.nextSibling;
|
|
662
|
+
|
|
663
|
+
if (beforeWasSplit && prevNode && isText(prevNode) && prevNode.textContent) {
|
|
664
|
+
//append text to previous sibling
|
|
665
|
+
prevNode.textContent += nodeToRemoveText;
|
|
666
|
+
nodeToRemove.remove();
|
|
667
|
+
|
|
668
|
+
if (
|
|
669
|
+
afterWasSplit &&
|
|
670
|
+
prevNode.nextSibling &&
|
|
671
|
+
isText(prevNode.nextSibling) &&
|
|
672
|
+
prevNode.nextSibling.textContent
|
|
673
|
+
) {
|
|
674
|
+
//merge it with next sibling
|
|
675
|
+
prevNode.textContent += prevNode.nextSibling.textContent;
|
|
676
|
+
prevNode.nextSibling.remove();
|
|
677
|
+
}
|
|
678
|
+
} else if (afterWasSplit && nextNode && isText(nextNode) && nextNode.textContent) {
|
|
679
|
+
//append text to next sibling
|
|
680
|
+
nextNode.textContent = nodeToRemoveText + nextNode.textContent;
|
|
681
|
+
nodeToRemove.remove();
|
|
682
|
+
} else if (nodeToRemoveText) {
|
|
683
|
+
//keep text in a separate text node
|
|
684
|
+
nodeToRemove.replaceWith(document.createTextNode(nodeToRemoveText));
|
|
685
|
+
} else {
|
|
686
|
+
//text is empty, just remove it
|
|
687
|
+
nodeToRemove.remove();
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* Index-related functions:
|
|
694
|
+
* ========================
|
|
695
|
+
* To allow saving and restoring highlights on an equivalent, but different, DOM tree (for example if the markup is deleted and re-created)
|
|
696
|
+
* we build an index containing the status of each text node:
|
|
697
|
+
* - not highlighted
|
|
698
|
+
* - fully highlighted
|
|
699
|
+
* - partially highlighted (= with inline ranges)
|
|
700
|
+
*
|
|
701
|
+
* This index will be used to restore a selection on the new DOM tree
|
|
702
|
+
*/
|
|
703
|
+
|
|
704
|
+
/**
|
|
705
|
+
* Bootstrap the process of building the highlight index
|
|
706
|
+
* @returns {Object[]|BuildModelResultKeepEmpty|null}
|
|
707
|
+
*/
|
|
708
|
+
function getHighlightIndex() {
|
|
709
|
+
var rootNode = getContainer();
|
|
710
|
+
if (!keepEmptyNodes) {
|
|
711
|
+
const highlightIndex = [];
|
|
712
|
+
if (rootNode) {
|
|
713
|
+
rootNode.normalize();
|
|
714
|
+
textNodesIndex = 0;
|
|
715
|
+
buildHighlightIndex(rootNode, highlightIndex);
|
|
716
|
+
}
|
|
717
|
+
return highlightIndex;
|
|
718
|
+
} else {
|
|
719
|
+
if (rootNode) {
|
|
720
|
+
return buildHighlightModelKeepEmpty(rootNode);
|
|
721
|
+
} else {
|
|
722
|
+
return null;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
/**
|
|
728
|
+
* Traverse the DOM tree to create the text Nodes index. Recursive.
|
|
729
|
+
* @param {Node} rootNode
|
|
730
|
+
* @param {Object[]} highlightIndex
|
|
731
|
+
*/
|
|
732
|
+
function buildHighlightIndex(rootNode, highlightIndex) {
|
|
733
|
+
const childNodes = rootNode.childNodes;
|
|
734
|
+
let i, currentNode;
|
|
735
|
+
var nodeInfos, inlineRange, inlineOffset, nodesToSkip;
|
|
736
|
+
|
|
737
|
+
for (i = 0; i < childNodes.length; i++) {
|
|
738
|
+
currentNode = childNodes[i];
|
|
739
|
+
|
|
740
|
+
// Skip blacklisted nodes
|
|
741
|
+
if (isBlacklisted(currentNode)) {
|
|
742
|
+
continue;
|
|
743
|
+
}
|
|
744
|
+
// A simple node not highlighted and isolated (= not followed by an wrapped text)
|
|
745
|
+
else if (isWrappable(currentNode) && !isWrappingNode(currentNode.nextSibling)) {
|
|
746
|
+
highlightIndex[textNodesIndex] = { highlighted: false };
|
|
747
|
+
textNodesIndex++;
|
|
748
|
+
|
|
749
|
+
// an isolated node (= not followed by a highlight table text) with its whole content highlighted
|
|
750
|
+
} else if (
|
|
751
|
+
isWrappingNode(currentNode) &&
|
|
752
|
+
!isText(currentNode.nextSibling) &&
|
|
753
|
+
(!isWrappingNode(currentNode.nextSibling) ||
|
|
754
|
+
currentNode.className === currentNode.nextSibling.className)
|
|
755
|
+
) {
|
|
756
|
+
highlightIndex[textNodesIndex] = {
|
|
757
|
+
highlighted: true,
|
|
758
|
+
groupId: currentNode.getAttribute(GROUP_ATTR),
|
|
759
|
+
c: getColorByClassName(currentNode.className)
|
|
760
|
+
};
|
|
761
|
+
textNodesIndex++;
|
|
762
|
+
|
|
763
|
+
// less straightforward: a succession of (at least) 1 wrapping node with 1 wrappable text node, in either order, and possibly more
|
|
764
|
+
// the trick is to create a unique text node on which we will be able to re-apply multiple partial highlights
|
|
765
|
+
// for this, we use 'inlineRanges'
|
|
766
|
+
} else if (isHotNode(currentNode)) {
|
|
767
|
+
nodeInfos = {
|
|
768
|
+
highlighted: true,
|
|
769
|
+
inlineRanges: []
|
|
770
|
+
};
|
|
771
|
+
|
|
772
|
+
nodesToSkip = -1;
|
|
773
|
+
inlineOffset = 0;
|
|
774
|
+
|
|
775
|
+
while (currentNode) {
|
|
776
|
+
if (isWrappingNode(currentNode)) {
|
|
777
|
+
inlineRange = {
|
|
778
|
+
groupId: currentNode.getAttribute(GROUP_ATTR),
|
|
779
|
+
c: getColorByClassName(currentNode.className)
|
|
780
|
+
};
|
|
781
|
+
|
|
782
|
+
if (isText(currentNode.previousSibling) || isWrappingNode(currentNode.previousSibling)) {
|
|
783
|
+
inlineRange.startOffset = inlineOffset;
|
|
784
|
+
}
|
|
785
|
+
if (isText(currentNode.nextSibling) || isWrappingNode(currentNode.nextSibling)) {
|
|
786
|
+
inlineRange.endOffset = inlineOffset + currentNode.textContent.length;
|
|
787
|
+
}
|
|
788
|
+
nodeInfos.inlineRanges.push(inlineRange);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
inlineOffset += currentNode.textContent.length;
|
|
792
|
+
currentNode =
|
|
793
|
+
isHotNode(currentNode.nextSibling) || isText(currentNode.nextSibling)
|
|
794
|
+
? currentNode.nextSibling
|
|
795
|
+
: null;
|
|
796
|
+
nodesToSkip++;
|
|
797
|
+
}
|
|
798
|
+
i += nodesToSkip; // we increase the loop counter to avoid looping over the nodes that we just analyzed
|
|
799
|
+
|
|
800
|
+
highlightIndex[textNodesIndex] = nodeInfos;
|
|
801
|
+
textNodesIndex++;
|
|
802
|
+
|
|
803
|
+
// go deeper in the node tree...
|
|
804
|
+
} else if (isElement(currentNode)) {
|
|
805
|
+
buildHighlightIndex(currentNode, highlightIndex);
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/**
|
|
811
|
+
* @typedef HighlightEntryKeepEmpty
|
|
812
|
+
* @property {String} groupId
|
|
813
|
+
* @property {String} c - color
|
|
814
|
+
* @property {Number} offsetBefore
|
|
815
|
+
* @property {Number} textLength
|
|
816
|
+
* @property {String} beforeWasSplit
|
|
817
|
+
* @property {String} afterWasSplit
|
|
818
|
+
* @property {Array<Number>} path - on each level from root container to highlight, index among siblings
|
|
819
|
+
*/
|
|
820
|
+
/**
|
|
821
|
+
* @typedef BuildModelResultKeepEmpty
|
|
822
|
+
* @property {HighlightEntryKeepEmpty[]} highlightModel
|
|
823
|
+
* @property {NodeList} wrapperNodes
|
|
824
|
+
*/
|
|
825
|
+
/**
|
|
826
|
+
* For `keepEmptyNodes` option, creates data model of highlights.
|
|
827
|
+
* Additionally returns array of highlight nodes. Traverses DOM tree.
|
|
828
|
+
* @param {Node} rootNode
|
|
829
|
+
* @returns {BuildModelResultKeepEmpty|null} result
|
|
830
|
+
*/
|
|
831
|
+
function buildHighlightModelKeepEmpty(rootNode) {
|
|
832
|
+
const classNames = options.colors ? Object.values(options.colors) : [className];
|
|
833
|
+
const wrapperNodesSelector = classNames.map(cls => containerSelector + ' .' + cls).join(', ');
|
|
834
|
+
const wrapperNodes = Array.from(document.querySelectorAll(wrapperNodesSelector)).filter(
|
|
835
|
+
node => !isBlacklisted(node)
|
|
836
|
+
);
|
|
837
|
+
|
|
838
|
+
if (!wrapperNodes.length) {
|
|
839
|
+
return null;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
const highlightModel = [];
|
|
843
|
+
const indexCache = new Map();
|
|
844
|
+
for (let k = 0; k < wrapperNodes.length; k++) {
|
|
845
|
+
const wrapperNode = wrapperNodes[k];
|
|
846
|
+
|
|
847
|
+
//get info about highlight itself
|
|
848
|
+
let offsetBefore = 0;
|
|
849
|
+
const prevNode = wrapperNode.previousSibling;
|
|
850
|
+
if (prevNode && isText(prevNode)) {
|
|
851
|
+
const beforeWasSplit = wrapperNode.dataset.beforeWasSplit === 'true';
|
|
852
|
+
if (beforeWasSplit) {
|
|
853
|
+
offsetBefore = prevNode.textContent.length;
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
const highlightData = {
|
|
857
|
+
groupId: wrapperNode.getAttribute(GROUP_ATTR),
|
|
858
|
+
c: getColorByClassName(wrapperNode.className),
|
|
859
|
+
offsetBefore,
|
|
860
|
+
textLength: wrapperNode.textContent.length,
|
|
861
|
+
beforeWasSplit: wrapperNode.dataset.beforeWasSplit,
|
|
862
|
+
afterWasSplit: wrapperNode.dataset.afterWasSplit,
|
|
863
|
+
path: []
|
|
864
|
+
};
|
|
865
|
+
|
|
866
|
+
//get info about its position in the tree: path through all parents from rootNode to highlight
|
|
867
|
+
let currentNode = wrapperNode;
|
|
868
|
+
while (currentNode && currentNode !== rootNode) {
|
|
869
|
+
let indexInModel = indexCache.get(currentNode);
|
|
870
|
+
if (!indexInModel && indexInModel !== 0) {
|
|
871
|
+
//should be more reliable to ignore empty nodes when indexing
|
|
872
|
+
const childNodes = Array.from(currentNode.parentNode.childNodes).filter(
|
|
873
|
+
node => !(isText(node) && !node.textContent.length)
|
|
874
|
+
);
|
|
875
|
+
//index among its non-empty siblings
|
|
876
|
+
indexInModel = childNodes.indexOf(currentNode);
|
|
877
|
+
indexCache.set(currentNode, indexInModel);
|
|
878
|
+
}
|
|
879
|
+
highlightData.path.unshift(indexInModel);
|
|
880
|
+
currentNode = currentNode.parentNode;
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
//add info about highlight and its position to model
|
|
884
|
+
highlightModel.push(highlightData);
|
|
885
|
+
}
|
|
886
|
+
return {
|
|
887
|
+
highlightModel,
|
|
888
|
+
wrapperNodes
|
|
889
|
+
};
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
* Bootstrap the process of restoring the highlights from an index
|
|
894
|
+
* @param {Object[]|HighlightEntryKeepEmpty[]|null} highlightIndex
|
|
895
|
+
*/
|
|
896
|
+
function highlightFromIndex(highlightIndex) {
|
|
897
|
+
var rootNode = getContainer();
|
|
898
|
+
if (rootNode) {
|
|
899
|
+
if (!keepEmptyNodes) {
|
|
900
|
+
rootNode.normalize();
|
|
901
|
+
textNodesIndex = 0;
|
|
902
|
+
restoreHighlight(rootNode, highlightIndex);
|
|
903
|
+
} else {
|
|
904
|
+
restoreHighlightKeepEmpty(rootNode, highlightIndex);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
/**
|
|
910
|
+
* Traverse the DOM tree to wraps the text nodes according to the highlight index. Recursive.
|
|
911
|
+
* @param {Node} rootNode
|
|
912
|
+
* @param {Object[]} highlightIndex
|
|
913
|
+
*/
|
|
914
|
+
function restoreHighlight(rootNode, highlightIndex) {
|
|
915
|
+
var childNodes = rootNode.childNodes;
|
|
916
|
+
var i, currentNode, parent;
|
|
917
|
+
var nodeInfos, nodesToSkip, range, initialChildCount;
|
|
918
|
+
|
|
919
|
+
for (i = 0; i < childNodes.length; i++) {
|
|
920
|
+
currentNode = childNodes[i];
|
|
921
|
+
|
|
922
|
+
if (isBlacklisted(currentNode)) {
|
|
923
|
+
continue;
|
|
924
|
+
} else if (isWrappable(currentNode)) {
|
|
925
|
+
parent = currentNode.parentNode;
|
|
926
|
+
initialChildCount = parent.childNodes.length;
|
|
927
|
+
|
|
928
|
+
nodeInfos = highlightIndex[textNodesIndex];
|
|
929
|
+
|
|
930
|
+
if (nodeInfos.highlighted === true) {
|
|
931
|
+
if (_.isArray(nodeInfos.inlineRanges)) {
|
|
932
|
+
nodeInfos.inlineRanges.reverse();
|
|
933
|
+
nodeInfos.inlineRanges.forEach(function (inlineRange) {
|
|
934
|
+
range = document.createRange();
|
|
935
|
+
range.setStart(currentNode, inlineRange.startOffset || 0);
|
|
936
|
+
range.setEnd(currentNode, inlineRange.endOffset || currentNode.textContent.length);
|
|
937
|
+
range.surroundContents(getWrapper(inlineRange.groupId, getClassNameByColor(inlineRange.c)));
|
|
938
|
+
});
|
|
939
|
+
|
|
940
|
+
// fully highlighted text node
|
|
941
|
+
} else {
|
|
942
|
+
range = document.createRange();
|
|
943
|
+
range.selectNodeContents(currentNode);
|
|
944
|
+
range.surroundContents(getWrapper(nodeInfos.groupId, getClassNameByColor(nodeInfos.c)));
|
|
945
|
+
}
|
|
946
|
+
// we do want to loop over the nodes created by the wrapping operation
|
|
947
|
+
nodesToSkip = parent.childNodes.length - initialChildCount;
|
|
948
|
+
i += nodesToSkip;
|
|
949
|
+
}
|
|
950
|
+
textNodesIndex++;
|
|
951
|
+
} else if (isElement(currentNode)) {
|
|
952
|
+
restoreHighlight(currentNode, highlightIndex);
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
/**
|
|
958
|
+
* For `keepEmptyNodes` option, wraps the text nodes according to highlights data model.
|
|
959
|
+
* Traverses and updates DOM tree. Shouldn't throw errors in case of mismatches.
|
|
960
|
+
* @param {Node} rootNode
|
|
961
|
+
* @param {HighlightEntryKeepEmpty[]|null} highlightModel
|
|
962
|
+
*/
|
|
963
|
+
function restoreHighlightKeepEmpty(rootNode, highlightModel) {
|
|
964
|
+
if (!highlightModel) {
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
let currentModel;
|
|
969
|
+
let range;
|
|
970
|
+
for (let k = 0; k < highlightModel.length; k++) {
|
|
971
|
+
currentModel = highlightModel[k];
|
|
972
|
+
|
|
973
|
+
//find node to wrap - go through nodes until we reach level where node to wrap will be
|
|
974
|
+
let childNodes;
|
|
975
|
+
let indexInModel;
|
|
976
|
+
let currentParentNode = rootNode;
|
|
977
|
+
let pathNotFound = false;
|
|
978
|
+
if (!currentModel.path || !currentModel.path.length) {
|
|
979
|
+
continue; //something went wrong
|
|
980
|
+
}
|
|
981
|
+
for (let m = 0; m < currentModel.path.length; m++) {
|
|
982
|
+
//path was counted among non-empty nodes
|
|
983
|
+
childNodes = Array.from(currentParentNode.childNodes).filter(
|
|
984
|
+
node => !(isText(node) && !node.textContent.length)
|
|
985
|
+
);
|
|
986
|
+
indexInModel = currentModel.path[m];
|
|
987
|
+
currentParentNode = childNodes[indexInModel];
|
|
988
|
+
if (!currentParentNode && m < currentModel.path.length - 1) {
|
|
989
|
+
//node on last level may not exist yet, no need to fail. See `nodeAtIndex`
|
|
990
|
+
pathNotFound = true;
|
|
991
|
+
break;
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
if (pathNotFound) {
|
|
995
|
+
continue; //something went wrong
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
//add single highlight
|
|
999
|
+
let nodeAtIndex = null;
|
|
1000
|
+
if (!currentModel.offsetBefore) {
|
|
1001
|
+
//wrap starts on this node
|
|
1002
|
+
nodeAtIndex = childNodes[indexInModel];
|
|
1003
|
+
if (!nodeAtIndex || !isText(nodeAtIndex) || isBlacklisted(nodeAtIndex)) {
|
|
1004
|
+
continue; //something went wrong
|
|
1005
|
+
}
|
|
1006
|
+
} else {
|
|
1007
|
+
//split previousSibling to create a node for wrapping
|
|
1008
|
+
const nodeBefore = childNodes[indexInModel - 1];
|
|
1009
|
+
if (
|
|
1010
|
+
!nodeBefore ||
|
|
1011
|
+
!isText(nodeBefore) ||
|
|
1012
|
+
nodeBefore.textContent.length <= currentModel.offsetBefore ||
|
|
1013
|
+
isBlacklisted(nodeBefore)
|
|
1014
|
+
) {
|
|
1015
|
+
continue; //something went wrong
|
|
1016
|
+
}
|
|
1017
|
+
nodeAtIndex = nodeBefore.splitText(currentModel.offsetBefore);
|
|
1018
|
+
}
|
|
1019
|
+
//cut off its end
|
|
1020
|
+
if (nodeAtIndex.textContent.length > currentModel.textLength) {
|
|
1021
|
+
nodeAtIndex.splitText(currentModel.textLength);
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
//wrap
|
|
1025
|
+
const wrapperNode = getWrapper(currentModel.groupId, getClassNameByColor(currentModel.c));
|
|
1026
|
+
addSplitData(wrapperNode, currentModel.beforeWasSplit, currentModel.afterWasSplit);
|
|
1027
|
+
range = document.createRange();
|
|
1028
|
+
range.selectNodeContents(nodeAtIndex);
|
|
1029
|
+
rangeSurroundContentsNoEmptyNodes(range, wrapperNode);
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
/**
|
|
1034
|
+
* Set highlighter color
|
|
1035
|
+
* @param {string} color Active highlighter color
|
|
1036
|
+
*/
|
|
1037
|
+
function setActiveColor(color) {
|
|
1038
|
+
if (options.colors[color]) {
|
|
1039
|
+
className = options.colors[color];
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
/**
|
|
1044
|
+
* Helpers
|
|
1045
|
+
*/
|
|
1046
|
+
|
|
1047
|
+
/**
|
|
1048
|
+
* Return the object key contains the given value
|
|
1049
|
+
* @param {Object} object
|
|
1050
|
+
* @param {any} value
|
|
1051
|
+
* @return {string|undefined}
|
|
1052
|
+
*/
|
|
1053
|
+
function getKeyByValue(object, value) {
|
|
1054
|
+
return Object.keys(object).find(key => object[key] === value);
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
/**
|
|
1058
|
+
* Returns color identifier for the given class name
|
|
1059
|
+
* @param {string} highlighterClassName Class name of highlighter classes
|
|
1060
|
+
* @returns {string|number} Color identifier
|
|
1061
|
+
*/
|
|
1062
|
+
function getColorByClassName(highlighterClassName) {
|
|
1063
|
+
if (options.colors) {
|
|
1064
|
+
return getKeyByValue(options.colors, highlighterClassName);
|
|
1065
|
+
}
|
|
1066
|
+
return className;
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
/**
|
|
1070
|
+
* Returns class name for the given color identifier
|
|
1071
|
+
* @param {string|number} color Color identifier
|
|
1072
|
+
* @returns {string} Class name
|
|
1073
|
+
*/
|
|
1074
|
+
function getClassNameByColor(color) {
|
|
1075
|
+
if (options.colors && options.colors[color]) {
|
|
1076
|
+
return options.colors[color];
|
|
1077
|
+
}
|
|
1078
|
+
return className;
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
/**
|
|
1082
|
+
* Check if the given node is a wrapper
|
|
1083
|
+
* @param {Node|Element} node
|
|
1084
|
+
* @returns {boolean}
|
|
1085
|
+
*/
|
|
1086
|
+
function isWrappingNode(node) {
|
|
1087
|
+
return isElement(node) && node.tagName.toLowerCase() === 'span' && highlightingClasses.includes(node.className);
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
/**
|
|
1091
|
+
* Check if the given node can be wrapped
|
|
1092
|
+
* @param {Node} node
|
|
1093
|
+
* @returns {boolean}
|
|
1094
|
+
*/
|
|
1095
|
+
function isWrappable(node) {
|
|
1096
|
+
return isText(node) && !isBlacklisted(node);
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
/**
|
|
1100
|
+
* Check if the given node is, or is within, a blacklisted container.
|
|
1101
|
+
* With `keepEmptyNodes` option, node inside blacklisted container can be whitelisted too.
|
|
1102
|
+
* Priority of blacklist or whitelist is decided by which selector is closest to the node.
|
|
1103
|
+
* If no match found, node is considered whitelisted.
|
|
1104
|
+
* @param {Node} node
|
|
1105
|
+
* @returns {boolean}
|
|
1106
|
+
*/
|
|
1107
|
+
function isBlacklisted(node) {
|
|
1108
|
+
const closest = $(node).closest(containersBlackAndWhiteListSelector);
|
|
1109
|
+
if (!closest.length) {
|
|
1110
|
+
return false;
|
|
1111
|
+
} else if (!containersWhiteListSelector) {
|
|
1112
|
+
return true;
|
|
1113
|
+
} else {
|
|
1114
|
+
return !closest.get(0).matches(containersWhiteListSelector);
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
/**
|
|
1119
|
+
* Wraps text node to the highlighter wrapper element
|
|
1120
|
+
* @param {Node} textNode Text node to wrap
|
|
1121
|
+
* @param {string} wrapperClassName Wrapper class name
|
|
1122
|
+
* @param {number} groupId Group id
|
|
1123
|
+
*/
|
|
1124
|
+
function wrapNode(textNode, wrapperClassName, groupId) {
|
|
1125
|
+
const element = getWrapper(groupId, wrapperClassName);
|
|
1126
|
+
element.appendChild(textNode);
|
|
1127
|
+
return element;
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
/**
|
|
1131
|
+
* Create a wrapping node
|
|
1132
|
+
* @param {number} groupId
|
|
1133
|
+
* @returns {Element}
|
|
1134
|
+
*/
|
|
1135
|
+
function getWrapper(groupId, wrapperClass) {
|
|
1136
|
+
const wrapper = document.createElement('span');
|
|
1137
|
+
wrapper.className = wrapperClass || className;
|
|
1138
|
+
wrapper.setAttribute(GROUP_ATTR, `${groupId}`);
|
|
1139
|
+
return wrapper;
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
/**
|
|
1143
|
+
* Returns the first unused group Id
|
|
1144
|
+
* @returns {number}
|
|
1145
|
+
*/
|
|
1146
|
+
function getAvailableGroupId() {
|
|
1147
|
+
var id = currentGroupId || 1;
|
|
1148
|
+
while ($(getContainer()).find('[' + GROUP_ATTR + '=' + id + ']').length !== 0) {
|
|
1149
|
+
id++;
|
|
1150
|
+
}
|
|
1151
|
+
return id;
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
/**
|
|
1155
|
+
* Check if the given node is an element
|
|
1156
|
+
* @param {Node} node
|
|
1157
|
+
* @returns {boolean}
|
|
1158
|
+
*/
|
|
1159
|
+
function isElement(node) {
|
|
1160
|
+
return node && typeof node === 'object' && node.nodeType === window.Node.ELEMENT_NODE;
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
/**
|
|
1164
|
+
* Check if the given node is of type text
|
|
1165
|
+
* @param {Node} node
|
|
1166
|
+
* @returns {boolean}
|
|
1167
|
+
*/
|
|
1168
|
+
function isText(node) {
|
|
1169
|
+
return node && typeof node === 'object' && node.nodeType === window.Node.TEXT_NODE;
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
/**
|
|
1173
|
+
* a "Hot Node" is either wrappable text node or a wrapper
|
|
1174
|
+
* @param {Node} node
|
|
1175
|
+
* @returns {boolean}
|
|
1176
|
+
*/
|
|
1177
|
+
function isHotNode(node) {
|
|
1178
|
+
return isWrappingNode(node) || isWrappable(node);
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
/**
|
|
1182
|
+
* Public API of the highlighter helper
|
|
1183
|
+
*/
|
|
1184
|
+
return {
|
|
1185
|
+
highlightRanges: highlightRanges,
|
|
1186
|
+
highlightFromIndex: highlightFromIndex,
|
|
1187
|
+
getHighlightIndex: getHighlightIndex,
|
|
1188
|
+
clearHighlights: clearHighlights,
|
|
1189
|
+
clearSingleHighlight: clearSingleHighlight,
|
|
1190
|
+
setActiveColor
|
|
1191
|
+
};
|
|
1192
|
+
}
|