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     3469× 3469× 3469× 3469× 3469×   3469× 3469× 3469× 4738×             4738×   4738×   916× 916×     3469× 3822× 3822× 3806× 3806× 3676×   3806× 3804×   3806× 3782×   3806× 3800×   3806× 3799×   3806× 3717×   3806× 3767×   3806× 3801×   3806× 3804×   3806× 3803×   3806× 2990×   3806× 83×     3723×   3806× 271×     3535×   3806× 3783×   3806× 3668×   3806× 3711×   3806× 3599×   3806× 2810×     996×   3806× 3736×   3806× 3668×   3806× 3711×   3806× 3806×   3806×     3469×   814× 846×   814×   3806× 3806× 3806×   3804×   3806×   3130×   3804× 3804× 3804× 3804× 3804×   6146× 3130× 3130× 3033×   3130×   6146×   3804× 7095× 7095× 7095× 277× 277× 277× 277×   7095× 329× 329× 329× 329×   7095× 27×   7095× 3680× 3680× 27×     7095× 97×   7095× 16×   7095× 7095×   3804× 459×   3804× 206×   3804× 123×   3804× 49×   3804×   3804× 13×   3804× 3749×   3804× 147×   3804× 91×   3804× 487×   3804× 314×   3804× 250×   3804×   3804×   3749× 3749× 3626×   3749× 3749×           9916× 9916× 147×     9769×     9771× 9771× 9771× 93×   9771×   3491×   9771×   487×     9284×     9954× 9954× 9954× 108×   9954× 1890×   9954× 9954×   16981× 16981× 16981×     78×     91×     9863×     9875× 9875× 18×   9857× 243×   9614× 24×   9590× 29×     9561×     3680× 3680×       2685×   995× 900×     95×     19000×   329× 329×   328×     328× 10×   318× 10×   308×   306×   302×   301× 301×           277× 277× 26×   251×   247×   245× 29×   216× 216×           10225× 3749×   10225× 9916×   10225× 9954× 9954× 91×     10225× 9771× 9771× 487×     10225× 9875×   10225× 10225× 232×     9993× 9993× 231×     10225× 9994× 9501×   9994× 9794×            
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;
});