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

99.57% Statements 232/233
99.05% Branches 313/316
100% Functions 20/20
99.57% Lines 232/233
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     2016× 2016× 2016× 2016× 2016×   2016× 2016× 2016× 2868×           2868×   2868×   558× 558×     2016× 2310× 2310× 2216×   2310× 2309×   2310× 2307×   2310× 2238×   2310× 2282×   2310× 2309×   2310× 2310×   2310× 2307×   2310× 1711×   2310× 73×     2237×   2310× 251×     2059×   2310× 2261×   2310× 2214×   2310× 2230×   2310× 2139×   2310× 1243×     1067×   2310× 2272×   2310× 2214×   2310× 2227×   2310× 2310×   2310×   2016×   227× 243×   227×   2310× 2310× 2310×   2308× 8686× 8686× 858×   8686× 866×   8686×     2310×   8686× 7981×   8686× 8149×   8686× 8288×   8686× 8558×   8686× 8666×   8686× 8644×   8686× 4144×   8686× 8469×   8686× 8321×   8686× 8349×   8686× 8250×   8686× 8250×   8686× 8354×   8686× 7655× 7655× 20×     8686× 8662× 8662× 8618× 600× 600×     8018× 8018× 8018× 55848× 8018×         8662× 19×     8686× 8485×   8686× 8246×   8686× 8246×   8686× 8685×   8686×   4144× 4144× 2145×   4144× 2271×     1873×     8485× 140×     8345×     8349× 8349× 137×     8212×     8321× 8321×   2022×   8321×   416×     7905×     8469× 8469× 6253×   8469× 8469×   52158× 52158× 52158×     64×     78×     8391×     16468× 431×     16037×     16426× 321×     16105×     8354× 8354×   8345× 204×   8141× 21×   8120× 25×     8095×     16273×       3253×     13020×     10420×   8246× 8246×   8245×     8245×   8236× 10×   8226×   8224×   8220×   8218× 202×     8016×     8246× 8246× 29×   8217×   8214×   8212× 36×   8176× 128×     8048×          
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].nodeType === 3 && range.startContainer.nodeType === 3 && nodes[index].parentNode &&
                    nodes[index].parentNode.lastElementChild && nodes[index].parentNode.lastElementChild.nodeName !== 'BR' &&
                    (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].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++) {
                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.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;
                }
                Eif ((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.nodeType === 3 && targetNode.nodeType !== 3 && targetNode.contains(node.parentNode))) {
                do {
                    formatCollection = this.isFormattedNode(docElement, formatCollection, node, isListUpdated, isComplexListUpdated, formatNode, fontSize, fontName);
                    if (formatCollection.orderedlist || formatCollection.unorderedlist) {
                        isListUpdated = true;
                    }
                    if (formatCollection.bulletFormatList || formatCollection.numberFormatList) {
                        isComplexListUpdated = true;
                    }
                    node = node.parentNode;
                } while (node && (node !== targetNode));
            }
            return formatCollection;
        };
        ToolbarStatus.isFormattedNode = function (docElement, formatCollection, node, isListUpdated, isComplexListUpdated, formatNode, fontSize, fontName) {
            if (!formatCollection.bold) {
                formatCollection.bold = isformatted_1.IsFormatted.isBold(node);
            }
            if (!formatCollection.italic) {
                formatCollection.italic = isformatted_1.IsFormatted.isItalic(node);
            }
            if (!formatCollection.underline) {
                formatCollection.underline = isformatted_1.IsFormatted.isUnderline(node);
            }
            if (!formatCollection.strikethrough) {
                formatCollection.strikethrough = isformatted_1.IsFormatted.isStrikethrough(node);
            }
            if (!formatCollection.superscript) {
                formatCollection.superscript = isformatted_1.IsFormatted.isSuperscript(node);
            }
            if (!formatCollection.subscript) {
                formatCollection.subscript = isformatted_1.IsFormatted.isSubscript(node);
            }
            if (!formatCollection.fontcolor) {
                formatCollection.fontcolor = this.isFontColor(docElement, node);
            }
            if (!formatCollection.fontname) {
                formatCollection.fontname = this.isFontName(docElement, node, fontName);
            }
            if (!formatCollection.fontsize) {
                formatCollection.fontsize = this.isFontSize(docElement, node, fontSize);
            }
            if (!formatCollection.backgroundcolor) {
                formatCollection.backgroundcolor = this.isBackgroundColor(node);
            }
            if (!formatCollection.orderedlist && !isListUpdated) {
                formatCollection.orderedlist = this.isOrderedList(node);
            }
            if (!formatCollection.unorderedlist && !isListUpdated) {
                formatCollection.unorderedlist = this.isUnorderedList(node);
            }
            if (!formatCollection.alignments) {
                formatCollection.alignments = this.isAlignment(node);
            }
            if (!formatCollection.formats) {
                formatCollection.formats = this.isFormats(node, formatNode);
                if (formatCollection.formats === 'pre') {
                    formatCollection.insertcode = true;
                }
            }
            if (!formatCollection.blockquote) {
                var currentFormatCollection = void 0;
                if (!ej2_base_1.isNullOrUndefined(formatNode)) {
                    if (formatNode.indexOf('blockquote') > -1) {
                        formatCollection.formats = this.isFormats(node, formatNode);
                        currentFormatCollection = formatCollection.formats;
                    }
                    else {
                        formatNode.push('blockquote');
                        currentFormatCollection = this.isFormats(node, formatNode);
                        for (var i = formatNode.length - 1; i >= 0; i--) {
                            if (formatNode[i] === 'blockquote') {
                                formatNode.splice(i, 1);
                            }
                        }
                    }
                }
                if (currentFormatCollection === 'blockquote') {
                    formatCollection.blockquote = true;
                }
            }
            if (!formatCollection.createlink) {
                formatCollection.createlink = this.isLink(node);
            }
            if (!formatCollection.numberFormatList && !isComplexListUpdated) {
                formatCollection.numberFormatList = this.isNumberFormatList(node);
            }
            if (!formatCollection.bulletFormatList && !isComplexListUpdated) {
                formatCollection.bulletFormatList = this.isBulletFormatList(node);
            }
            if (!formatCollection.inlinecode) {
                formatCollection.inlinecode = isformatted_1.IsFormatted.isCode(node);
            }
            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');
            }
            if (color !== null && color !== '' && color !== undefined) {
                return color;
            }
            else {
                return null;
            }
        };
        ToolbarStatus.isLink = function (node) {
            if (node.nodeName.toLocaleLowerCase() === 'a') {
                return true;
            }
            else {
                return false;
            }
        };
        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;
            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;
            if ((name === null || name === undefined || name === '') && node.nodeType !== 3) {
                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.isOrderedList = function (node) {
            if (node.nodeName.toLocaleLowerCase() === 'ol') {
                return true;
            }
            else {
                return false;
            }
        };
        ToolbarStatus.isUnorderedList = function (node) {
            if (node.nodeName.toLocaleLowerCase() === 'ul') {
                return true;
            }
            else {
                return false;
            }
        };
        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) {
            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 {
                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') {
                return 'None';
            }
            else if (this.isOrderedList(node)) {
                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') {
                return 'None';
            }
            else if (list === 'disc') {
                return 'Disc';
            }
            else if (this.isUnorderedList(node)) {
                return true;
            }
            else {
                return null;
            }
        };
        return ToolbarStatus;
    }());
    exports.ToolbarStatus = ToolbarStatus;
});