all files / editor-manager/plugin/ toolbar-status.js

98.51% Statements 265/269
97.99% Branches 390/398
100% Functions 19/19
98.51% Lines 265/269
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426     3575× 3575× 3575× 3575× 3575×   3575× 3575× 3575× 4874×             4874×   4874×   942× 942×     3575× 3932× 3932× 3916× 3916× 3780×   3916× 3914×   3916× 3892×   3916× 3910×   3916× 3909×   3916× 3827×   3916× 3877×   3916× 3911×   3916× 3912×   3916× 3913×   3916× 3072×   3916× 83×     3833×   3916× 271×     3645×   3916× 3893×   3916× 3771×   3916× 3809×   3916× 3709×   3916× 2888×     1028×   3916× 3844×   3916× 3771×   3916× 3809×   3916× 3916×   3916×     3575×   810× 842×   810×   3916× 3916× 3916×   3915×   3916×   3208×   3915× 3915× 3915× 3915× 3915×   6319× 3208× 3208× 3110×   3208×   6319×   3915× 7264× 7264× 7264× 306× 306× 306× 306×   7264× 336× 336× 336× 336×   7264× 27×   7264× 3792× 3792× 30×     7264× 98×   7264× 16×   7264× 7264×   3915× 472×   3915× 206×   3915× 123×   3915× 49×   3915× 10×   3915× 13×   3915× 3860×   3915× 147×   3915× 91×   3915× 487×   3915× 314×   3915× 258×   3915×   3915×   3860× 3860× 3721×   3860× 3860×           10163× 10163× 147×     10016×     10018× 10018× 10018× 107×   10018×   3598×   10018×   487×     9531×     10201× 10201× 10201× 129×   10201× 1937×   10201× 10201×   17477× 17477× 17477×     78×     91×     10110×     10122× 10122× 18×   10104× 243×   9861× 24×   9837× 29×     9808×     3792× 3792×       2758×   1034× 933×     101×     19477×   336× 336×   335×     335× 10×   325× 10×   315×   313×   309×   308× 308×           306× 306× 26×   280×   276×   274× 40×   234× 234×           10472× 3860×   10472× 10163×   10472× 10201× 10201× 91×     10472× 10018× 10018× 487×     10472× 10122×   10472× 10472× 251×     10221× 10221× 236×     10472× 10236× 9743×   10236× 10036×            
define(["require", "exports", "./isformatted", "./../base/constant", "./../../selection/index", "./../../common/util", "@syncfusion/ej2-base"], function (require, exports, isformatted_1, CONSTANT, index_1, util_1, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.statusCollection = util_1.getDefaultHtmlTbStatus();
    var ToolbarStatus = (function () {
        function ToolbarStatus() {
        }
        ToolbarStatus.get = function (docElement, rootNode, formatNode, fontSize, fontName, documentNode) {
            var formatCollection = JSON.parse(JSON.stringify(exports.statusCollection));
            var nodeCollection = JSON.parse(JSON.stringify(exports.statusCollection));
            var nodeSelection = new index_1.NodeSelection(rootNode);
            var range = nodeSelection.getRange(docElement);
            var nodes = documentNode ? [documentNode] : range.collapsed ? nodeSelection.getNodeCollection(range) :
                nodeSelection.getSelectionNodeCollectionBr(range);
            var nodesLength = nodes.length;
            var isNodeChanged = false;
            for (var index = 0; index < nodes.length; index++) {
                while (nodes[index] && nodes[index].nodeType === 3 && range.startContainer.nodeType === 3 &&
                    nodes[index].parentNode && nodes[index].parentNode.lastElementChild && nodes[index].parentNode.lastElementChild.nodeName !== 'BR' &&
                    (this.getImmediateBlockNode(nodes[index].parentNode)) &&
                    (this.getImmediateBlockNode(nodes[index].parentNode)).textContent.replace(/\u200B/g, '').length === 0 &&
                    range.startContainer.textContent.replace(/\u200B/g, '').length === 0 &&
                    nodeSelection.get(docElement).toString().replace(/\u200B/g, '').length === 0) {
                    nodes[index] = nodes[index].parentNode.lastElementChild.firstChild;
                    isNodeChanged = true;
                }
                if (isNodeChanged && nodes[index]) {
                    nodeSelection.setCursorPoint(docElement, nodes[index], nodes[index].textContent.length);
                    isNodeChanged = false;
                }
                if (nodes[index] && ((nodes[index].nodeName !== 'BR' && nodes[index].nodeType !== 3) ||
                    (nodesLength > 1 && nodes[index].nodeType === 3 && nodes[index].textContent.trim() === ''))) {
                    nodes.splice(index, 1);
                    index--;
                }
            }
            for (var index = 0; index < nodes.length; index++) {
                var closestColOrColgroup = ej2_base_1.closest(nodes[index], 'col, colgroup');
                if (ej2_base_1.isNullOrUndefined(closestColOrColgroup)) {
                    formatCollection = this.getFormatParent(docElement, formatCollection, nodes[index], rootNode, formatNode, fontSize, fontName);
                    if ((index === 0 && formatCollection.bold) || !formatCollection.bold) {
                        nodeCollection.bold = formatCollection.bold;
                    }
                    if ((index === 0 && formatCollection.insertcode) || !formatCollection.insertcode) {
                        nodeCollection.insertcode = formatCollection.insertcode;
                    }
                    if ((index === 0 && formatCollection.isCodeBlock) || !formatCollection.isCodeBlock) {
                        nodeCollection.isCodeBlock = formatCollection.isCodeBlock;
                    }
                    if ((index === 0 && formatCollection.isCheckList) || !formatCollection.isCheckList) {
                        nodeCollection.isCheckList = formatCollection.isCheckList;
                    }
                    if ((index === 0 && formatCollection.blockquote) || !formatCollection.blockquote) {
                        nodeCollection.blockquote = formatCollection.blockquote;
                    }
                    if ((index === 0 && formatCollection.italic) || !formatCollection.italic) {
                        nodeCollection.italic = formatCollection.italic;
                    }
                    if ((index === 0 && formatCollection.underline) || !formatCollection.underline) {
                        nodeCollection.underline = formatCollection.underline;
                    }
                    if ((index === 0 && formatCollection.strikethrough) || !formatCollection.strikethrough) {
                        nodeCollection.strikethrough = formatCollection.strikethrough;
                    }
                    if ((index === 0 && formatCollection.superscript) || !formatCollection.superscript) {
                        nodeCollection.superscript = formatCollection.superscript;
                    }
                    if ((index === 0 && formatCollection.subscript) || !formatCollection.subscript) {
                        nodeCollection.subscript = formatCollection.subscript;
                    }
                    if ((index === 0 && formatCollection.fontcolor) || !formatCollection.fontcolor) {
                        nodeCollection.fontcolor = formatCollection.fontcolor;
                    }
                    if (index === 0 && formatCollection.fontname) {
                        nodeCollection.fontname = formatCollection.fontname;
                    }
                    else {
                        nodeCollection.fontname = formatCollection.fontname === nodeCollection.fontname ? formatCollection.fontname : 'empty';
                    }
                    if (index === 0 && formatCollection.fontsize) {
                        nodeCollection.fontsize = formatCollection.fontsize;
                    }
                    else {
                        nodeCollection.fontsize = formatCollection.fontsize === nodeCollection.fontsize ? formatCollection.fontsize : 'empty';
                    }
                    if ((index === 0 && formatCollection.backgroundcolor) || !formatCollection.backgroundcolor) {
                        nodeCollection.backgroundcolor = formatCollection.backgroundcolor;
                    }
                    if ((index === 0 && formatCollection.orderedlist) || !formatCollection.orderedlist) {
                        nodeCollection.orderedlist = formatCollection.orderedlist;
                    }
                    if ((index === 0 && formatCollection.unorderedlist) || !formatCollection.unorderedlist) {
                        nodeCollection.unorderedlist = formatCollection.unorderedlist;
                    }
                    if ((index === 0 && formatCollection.alignments) || !formatCollection.alignments) {
                        nodeCollection.alignments = formatCollection.alignments;
                    }
                    if (index === 0 && formatCollection.formats) {
                        nodeCollection.formats = formatCollection.formats;
                    }
                    else {
                        nodeCollection.formats = formatCollection.formats === nodeCollection.formats ? formatCollection.formats : 'empty';
                    }
                    if ((index === 0 && formatCollection.createlink) || !formatCollection.createlink) {
                        nodeCollection.createlink = formatCollection.createlink;
                    }
                    if ((index === 0 && formatCollection.numberFormatList) || !formatCollection.numberFormatList) {
                        nodeCollection.numberFormatList = formatCollection.numberFormatList;
                    }
                    if ((index === 0 && formatCollection.bulletFormatList) || !formatCollection.bulletFormatList) {
                        nodeCollection.bulletFormatList = formatCollection.bulletFormatList;
                    }
                    Eif ((index === 0 && formatCollection.inlinecode) || !formatCollection.inlinecode) {
                        nodeCollection.inlinecode = formatCollection.inlinecode;
                    }
                    formatCollection = JSON.parse(JSON.stringify(exports.statusCollection));
                }
            }
            return nodeCollection;
        };
        ToolbarStatus.getImmediateBlockNode = function (node) {
            do {
                node = node.parentNode;
            } while (node && CONSTANT.BLOCK_TAGS.indexOf(node.nodeName.toLocaleLowerCase()) < 0);
            return node;
        };
        ToolbarStatus.getFormatParent = function (docElement, formatCollection, node, targetNode, formatNode, fontSize, fontName) {
            var isListUpdated = false;
            var isComplexListUpdated = false;
            if (targetNode.contains(node) ||
                (node && node.nodeType === 3 && targetNode.nodeType !== 3 && targetNode.contains(node.parentNode))) {
                formatCollection = this.isFormattedNode(docElement, formatCollection, node, isListUpdated, isComplexListUpdated, formatNode, fontSize, fontName, targetNode);
            }
            return formatCollection;
        };
        ToolbarStatus.checkCodeBlock = function (element) {
            return (element.nodeName === 'CODE' && element.parentElement && element.parentElement.nodeName === 'PRE' && element.parentElement.hasAttribute('data-language'));
        };
        ToolbarStatus.isFormattedNode = function (docElement, formatCollection, node, isListUpdated, isComplexListUpdated, formatNode, fontSize, fontName, targetNode) {
            var BLOCK_TAGS = CONSTANT.BLOCK_TAGS;
            var currentNode = node;
            var collectedTags = [];
            var collectedStyles = {};
            while (currentNode && (!(BLOCK_TAGS.indexOf(currentNode.nodeName.toLowerCase()) > -1)) &&
                (!targetNode || currentNode.nodeName !== targetNode.nodeName)) {
                if (currentNode.nodeType === 1) {
                    var element = currentNode;
                    if (!this.checkCodeBlock(element)) {
                        collectedTags.push(element.nodeName.toLowerCase());
                    }
                    this.collectStyles(currentNode, collectedStyles, docElement, fontName, fontSize);
                }
                currentNode = currentNode.parentNode;
            }
            while (currentNode && currentNode !== targetNode) {
                var nodeName = currentNode.nodeName.toLowerCase();
                var isCheckListElement = currentNode.tagName === 'UL' && currentNode.classList.contains('e-rte-checklist');
                if (!formatCollection.unorderedlist && nodeName === 'ul' && !isListUpdated && !isComplexListUpdated && !isCheckListElement) {
                    formatCollection.unorderedlist = true;
                    isListUpdated = true;
                    formatCollection.bulletFormatList = this.isBulletFormatList(currentNode);
                    isComplexListUpdated = formatCollection.bulletFormatList !== null ? true : false;
                }
                if (!formatCollection.orderedlist && nodeName === 'ol' && !isListUpdated && !isComplexListUpdated) {
                    formatCollection.orderedlist = true;
                    isListUpdated = true;
                    formatCollection.numberFormatList = this.isNumberFormatList(currentNode);
                    isComplexListUpdated = formatCollection.numberFormatList !== null ? true : false;
                }
                if (!formatCollection.blockquote && nodeName === 'blockquote') {
                    formatCollection.blockquote = true;
                }
                if (!formatCollection.formats) {
                    formatCollection.formats = this.isFormats(currentNode, formatNode);
                    if (formatCollection.formats === 'pre' && nodeName === 'pre' && currentNode.firstChild.nodeName !== 'CODE' && !currentNode.hasAttribute('data-language')) {
                        formatCollection.insertcode = true;
                    }
                }
                if (!formatCollection.isCodeBlock && currentNode.nodeName.toLocaleLowerCase() === 'pre' && currentNode.hasAttribute('data-language')) {
                    formatCollection.isCodeBlock = true;
                }
                if (!formatCollection.isCheckList && isCheckListElement) {
                    formatCollection.isCheckList = true;
                }
                this.collectStyles(currentNode, collectedStyles, docElement, fontName, fontSize);
                currentNode = currentNode.parentNode;
            }
            if (collectedTags.indexOf('b') > -1 || collectedTags.indexOf('strong') > -1) {
                formatCollection.bold = true;
            }
            if (collectedTags.indexOf('i') > -1 || collectedTags.indexOf('em') > -1) {
                formatCollection.italic = true;
            }
            if (collectedTags.indexOf('u') > -1 || (collectedStyles['underLine'])) {
                formatCollection.underline = true;
            }
            if (collectedTags.indexOf('s') > -1 || collectedTags.indexOf('del') > -1 || (collectedStyles['strikeThrough'])) {
                formatCollection.strikethrough = true;
            }
            if (collectedTags.indexOf('sup') > -1) {
                formatCollection.superscript = true;
            }
            if (collectedTags.indexOf('sub') > -1) {
                formatCollection.subscript = true;
            }
            if (collectedStyles['color']) {
                formatCollection.fontcolor = collectedStyles['color'];
            }
            if (collectedStyles['backgroundColor']) {
                formatCollection.backgroundcolor = collectedStyles['backgroundColor'];
            }
            if (collectedStyles['fontFamily']) {
                formatCollection.fontname = collectedStyles['fontFamily'];
            }
            if (collectedStyles['fontSize']) {
                formatCollection.fontsize = collectedStyles['fontSize'];
            }
            if (collectedStyles['textAlign']) {
                formatCollection.alignments = collectedStyles['textAlign'];
            }
            if (collectedTags.indexOf('a') > -1) {
                formatCollection.createlink = true;
            }
            if (collectedTags.indexOf('code') > -1) {
                formatCollection.inlinecode = true;
            }
            return formatCollection;
        };
        ToolbarStatus.isFontColor = function (docElement, node) {
            var color = node.style && node.style.color;
            if ((color === null || color === undefined || color === '') && node.nodeType !== 3) {
                color = this.getComputedStyle(docElement, node, 'color');
            }
            Eif (color !== null && color !== '' && color !== undefined) {
                return color;
            }
            else {
                return null;
            }
        };
        ToolbarStatus.isBackgroundColor = function (node) {
            var backColor = node.style && node.style.backgroundColor;
            if (backColor !== null && backColor !== '' && backColor !== undefined) {
                return backColor;
            }
            else {
                return null;
            }
        };
        ToolbarStatus.isFontSize = function (docElement, node, fontSize) {
            var size = node.style && node.style.fontSize;
            var isInlineTags = isformatted_1.IsFormatted.inlineTags.indexOf(node.nodeName.toLowerCase()) > -1;
            if (size && node.nodeType === 1 && !isInlineTags) {
                size = null;
            }
            if ((size === null || size === undefined || size === '') && node.nodeType !== 3 &&
                node.parentElement.classList.contains('e-content')) {
                size = this.getComputedStyle(docElement, node, 'font-size');
            }
            if ((size !== null && size !== '' && size !== undefined)
                && (fontSize === null || fontSize === undefined || (fontSize.indexOf(size) > -1))) {
                return size;
            }
            else {
                return null;
            }
        };
        ToolbarStatus.isFontName = function (docElement, node, fontName) {
            var name = node.style && node.style.fontFamily;
            var isInlineTags = isformatted_1.IsFormatted.inlineTags.indexOf(node.nodeName.toLowerCase()) > -1;
            if (name && node.nodeType === 1 && !isInlineTags) {
                name = null;
            }
            if ((name === null || name === undefined || name === '') && node.nodeType !== 3 && isInlineTags) {
                name = this.getComputedStyle(docElement, node, 'font-family');
            }
            var index = null;
            if ((name !== null && name !== '' && name !== undefined)
                && (fontName === null || fontName === undefined || (fontName.filter(function (value, pos) {
                    var regExp = RegExp;
                    var pattern = new regExp(name, 'i');
                    if ((value.replace(/"/g, '').replace(/ /g, '').toLowerCase() === name.replace(/"/g, '').replace(/ /g, '').toLowerCase()) ||
                        (value.split(',')[0] && !ej2_base_1.isNullOrUndefined(value.split(',')[0].trim().match(pattern)) &&
                            value.split(',')[0].trim() === value.split(',')[0].trim().match(pattern)[0])) {
                        index = pos;
                    }
                }) && (index !== null)))) {
                return (index !== null) ? fontName[index] : name.replace(/"/g, '');
            }
            else {
                return null;
            }
        };
        ToolbarStatus.isAlignment = function (node) {
            var align = node.style && node.style.textAlign;
            if (align === 'left') {
                return 'justifyleft';
            }
            else if (align === 'center') {
                return 'justifycenter';
            }
            else if (align === 'right') {
                return 'justifyright';
            }
            else if (align === 'justify') {
                return 'justifyfull';
            }
            else {
                return null;
            }
        };
        ToolbarStatus.isFormats = function (node, formatNode) {
            var tags = ['tbody', 'tfoot', 'thead', 'ol', 'ul', 'table', 'li', 'td', 'th'];
            if (((formatNode === undefined || formatNode === null)
                && CONSTANT.BLOCK_TAGS.indexOf(node.nodeName.toLocaleLowerCase()) > -1)
                || (formatNode !== null && formatNode !== undefined
                    && formatNode.indexOf(node.nodeName.toLocaleLowerCase()) > -1)) {
                return node.nodeName.toLocaleLowerCase();
            }
            else if (tags.indexOf(node.nodeName.toLocaleLowerCase()) > -1) {
                return 'p';
            }
            else {
                return null;
            }
        };
        ToolbarStatus.getComputedStyle = function (docElement, node, prop) {
            return docElement.defaultView.getComputedStyle(node, null).getPropertyValue(prop);
        };
        ToolbarStatus.isNumberFormatList = function (node) {
            var list = node.style && node.style.listStyleType;
            if (list === 'lower-alpha') {
                return 'Lower Alpha';
            }
            else Iif (list === 'number') {
                return 'Number';
            }
            else if (list === 'upper-alpha') {
                return 'Upper Alpha';
            }
            else if (list === 'lower-roman') {
                return 'Lower Roman';
            }
            else if (list === 'upper-roman') {
                return 'Upper Roman';
            }
            else if (list === 'lower-greek') {
                return 'Lower Greek';
            }
            else if (list === 'none' && node.nodeName === 'OL') {
                return 'None';
            }
            else Eif (node.nodeName === 'OL') {
                return true;
            }
            else {
                return null;
            }
        };
        ToolbarStatus.isBulletFormatList = function (node) {
            var list = node.style && node.style.listStyleType;
            if (list === 'circle') {
                return 'Circle';
            }
            else if (list === 'square') {
                return 'Square';
            }
            else if (list === 'none' && node.nodeName === 'UL') {
                return 'None';
            }
            else if (list === 'disc') {
                return 'Disc';
            }
            else Eif (node.nodeName === 'UL') {
                return true;
            }
            else {
                return null;
            }
        };
        ToolbarStatus.collectStyles = function (currentNode, collectedStyles, docElement, fontName, fontSize) {
            if (!collectedStyles['color']) {
                collectedStyles['color'] = this.isFontColor(docElement, currentNode);
            }
            if (!collectedStyles['backgroundColor']) {
                collectedStyles['backgroundColor'] = this.isBackgroundColor(currentNode);
            }
            if (!collectedStyles['fontFamily']) {
                var font = this.isFontName(docElement, currentNode, fontName);
                if (font) {
                    collectedStyles['fontFamily'] = font;
                }
            }
            if (!collectedStyles['fontSize']) {
                var size = this.isFontSize(docElement, currentNode, fontSize);
                if (size) {
                    collectedStyles['fontSize'] = size;
                }
            }
            if (!collectedStyles['textAlign']) {
                collectedStyles['textAlign'] = this.isAlignment(currentNode);
            }
            var textDecoration = null;
            if (currentNode.style && currentNode.style.textDecoration) {
                textDecoration = currentNode.style.textDecoration;
            }
            else {
                textDecoration = this.getComputedStyle(docElement, currentNode, 'text-decoration');
                if (currentNode.nodeName === 'A' && textDecoration.includes('underline')) {
                    textDecoration = null;
                }
            }
            if (textDecoration) {
                if (!collectedStyles['underLine']) {
                    collectedStyles['underLine'] = textDecoration.includes('underline') ? 'underline' : null;
                }
                if (!collectedStyles['strikeThrough']) {
                    collectedStyles['strikeThrough'] = textDecoration.includes('line-through') ? 'line-through' : null;
                }
            }
        };
        return ToolbarStatus;
    }());
    exports.ToolbarStatus = ToolbarStatus;
});