all files / rich-text-editor/actions/ xhtml-validation.js

99.06% Statements 105/106
95.31% Branches 61/64
100% Functions 13/13
99.06% Lines 105/106
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   1748× 1748×   1748× 1748×   1747× 1747×   607× 54×       54×   54× 54× 54× 54× 54×   54×     119×     119× 119× 119× 119× 119×     119× 1428× 1428× 88× 88×   1428× 1428× 616× 88×       1428× 616×     119×   331× 504× 504× 10× 10×   494× 277×     331×   54× 54×       54× 54× 270×     270× 270× 50× 50×       54× 54×   54× 54×   54×   54× 540×     540× 540× 540× 1280× 1280×   1140×   1140× 1010× 2080× 2080×         2080× 680× 780×                                  
define(["require", "exports", "../base/constant", "@syncfusion/ej2-base"], function (require, exports, events, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var XhtmlValidation = (function () {
        function XhtmlValidation(parent) {
            this.parent = parent;
            this.addEventListener();
        }
        XhtmlValidation.prototype.addEventListener = function () {
            this.parent.on(events.xhtmlValidation, this.enableXhtmlValidation, this);
            this.parent.on(events.destroy, this.removeEventListener, this);
        };
        XhtmlValidation.prototype.removeEventListener = function () {
            this.parent.off(events.xhtmlValidation, this.enableXhtmlValidation);
            this.parent.off(events.destroy, this.removeEventListener);
        };
        XhtmlValidation.prototype.enableXhtmlValidation = function () {
            if (this.parent.enableXhtml) {
                Iif (ej2_base_1.isNullOrUndefined(this.parent.inputElement)) {
                    this.currentElement = this.parent.element;
                }
                else {
                    this.currentElement = this.parent.inputElement;
                }
                this.clean(this.currentElement);
                this.ImageTags();
                this.removeTags();
                this.RemoveUnsupported();
                this.currentElement.innerHTML = this.selfEncloseValidation(this.currentElement.innerHTML, this.currentElement.innerText === '\n' ?
                    this.currentElement.innerText.length : this.currentElement.innerText.trim().length);
                this.parent.setProperties({ value: this.currentElement.innerHTML }, true);
            }
        };
        XhtmlValidation.prototype.selfEncloseValidation = function (currentValue, valueLength) {
            if (valueLength === 0 && currentValue.indexOf('table') < 0 && currentValue.indexOf('img') < 0 &&
                currentValue.includes('&nbsp;')) {
                var arrayValue = currentValue.split('&nbsp;');
                arrayValue[arrayValue.length - 1] = '&#8203;' + arrayValue[arrayValue.length - 1];
                currentValue = arrayValue.join('');
            }
            currentValue = currentValue.replace(/<br>/g, '<br/>').replace(/<hr>/g, '<hr/>').replace(/ /g, ' ');
            var valueTemp;
            var valueDupe = [];
            var valueOriginal = [];
            var imgRegexp = [/<img(.*?)>/gi, /<area(.*?)>/gi, /<base(.*?)>/gi, /<col (.*?)>/gi, /<embed(.*?)>/gi,
                /<input(.*?)>/gi, /<link(.*?)>/gi, /<meta(.*?)>/gi, /<param(.*?)>/gi, /<source(.*?)>/gi,
                /<track(.*?)>/gi, /<wbr(.*?)>/gi];
            for (var j = 0; j < imgRegexp.length; j++) {
                valueTemp = imgRegexp[j].exec(currentValue);
                while ((valueTemp) !== null) {
                    valueDupe.push(valueTemp[0].toString());
                    valueTemp = imgRegexp[j].exec(currentValue);
                }
                valueOriginal = valueDupe.slice(0);
                for (var i = 0; i < valueDupe.length; i++) {
                    if (valueDupe[i].indexOf('/') === -1 || valueDupe[i].lastIndexOf('/') !== valueDupe[i].length - 2) {
                        valueDupe[i] = valueDupe[i].substr(0, valueDupe[i].length - 1) + ' /' +
                            valueDupe[i].substr(valueDupe[i].length - 1, valueDupe[i].length);
                    }
                }
                for (var g = 0; g <= valueDupe.length - 1; g++) {
                    currentValue = currentValue.replace(valueOriginal[g], valueDupe[g]);
                }
            }
            return currentValue;
        };
        XhtmlValidation.prototype.clean = function (node) {
            for (var n = 0; n < node.childNodes.length; n++) {
                var child = node.childNodes[n];
                if (child.nodeType === 8 || child.nodeName === 'V:IMAGE') {
                    node.removeChild(child);
                    n--;
                }
                else if (child.nodeType === 1) {
                    this.clean(child);
                }
            }
            return this.currentElement.innerHTML;
        };
        XhtmlValidation.prototype.ImageTags = function () {
            var imgNodes = this.currentElement.querySelectorAll('IMG');
            for (var i = imgNodes.length - 1; i >= 0; i--) {
                if (!imgNodes[i].hasAttribute('alt')) {
                    var img = imgNodes[i];
                    img.setAttribute('alt', '');
                }
            }
        };
        XhtmlValidation.prototype.removeTags = function () {
            var removeAttribute = [['br', 'ul'], ['br', 'ol'], ['table', 'span'], ['div', 'span'], ['p', 'span']];
            for (var i = 0; i < removeAttribute.length; i++) {
                this.RemoveElementNode(removeAttribute[i][0], removeAttribute[i][1]);
            }
        };
        XhtmlValidation.prototype.RemoveElementNode = function (rmvNode, parentNode) {
            var parentArray = this.currentElement.querySelectorAll(parentNode);
            for (var i = 0; i < parentArray.length; i++) {
                var rmvArray = parentArray[i].querySelectorAll(rmvNode);
                for (var j = rmvArray.length; j > 0; j--) {
                    ej2_base_1.detach(rmvArray[j - 1]);
                }
            }
        };
        XhtmlValidation.prototype.RemoveUnsupported = function () {
            var underlineEle = this.currentElement.querySelectorAll('u');
            for (var i = underlineEle.length - 1; i >= 0; i--) {
                var spanEle = this.parent.createElement('span');
                spanEle.style.textDecoration = 'underline';
                spanEle.innerHTML = underlineEle[i].innerHTML;
                underlineEle[i].parentNode.insertBefore(spanEle, underlineEle[i]);
                ej2_base_1.detach(underlineEle[i]);
            }
            var strongEle = this.currentElement.querySelectorAll('strong');
            for (var i = strongEle.length - 1; i >= 0; i--) {
                var boldEle = this.parent.createElement('b');
                boldEle.innerHTML = strongEle[i].innerHTML;
                strongEle[i].parentNode.insertBefore(boldEle, strongEle[i]);
                ej2_base_1.detach(strongEle[i]);
            }
            var attrArray = ['language', 'role', 'target', 'contenteditable', 'cellspacing',
                'cellpadding', 'border', 'valign', 'colspan'];
            for (var i = 0; i <= attrArray.length; i++) {
                this.RemoveAttributeByName(attrArray[i]);
            }
        };
        XhtmlValidation.prototype.RemoveAttributeByName = function (attrName) {
            Eif (this.currentElement.firstChild !== null) {
                Eif (this.currentElement.firstChild.nodeType !== 3) {
                    for (var i = 0; i < this.currentElement.childNodes.length; i++) {
                        var ele = this.currentElement.childNodes[i];
                        if (ele.nodeType !== 3 && ele.nodeName !== 'TABLE' && ele.nodeName !== 'TBODY' && ele.nodeName !== 'THEAD' &&
                            ele.nodeName !== 'TH' && ele.nodeName !== 'TR' && ele.nodeName !== 'TD') {
                            if (ele.hasAttribute(attrName)) {
                                ele.removeAttribute(attrName);
                            }
                            if (ele.hasChildNodes()) {
                                for (var j = 0; j < ele.childNodes.length; j++) {
                                    var childEle = ele.childNodes[j];
                                    if (childEle.nodeType !== 3 && childEle.nodeName !== 'TABLE' && childEle.nodeName !== 'TBODY' &&
                                        childEle.nodeName !== 'THEAD' && childEle.nodeName !== 'TH' && childEle.nodeName !== 'TR' &&
                                        childEle.nodeName !== 'TD' && childEle.hasAttribute(attrName) &&
                                        !childEle.classList.contains('e-mention-chip')) {
                                        childEle.removeAttribute(attrName);
                                    }
                                    if (childEle.hasChildNodes()) {
                                        for (var k = 0; k < childEle.childNodes.length; k++) {
                                            if (childEle.childNodes[k].nodeType !== 3 && childEle.childNodes[k].nodeName !== 'TABLE' &&
                                                childEle.childNodes[k].nodeName !== 'TBODY' && childEle.childNodes[k].nodeName !== 'THEAD' &&
                                                childEle.childNodes[k].nodeName !== 'TH' && childEle.childNodes[k].nodeName !== 'TR'
                                                && childEle.childNodes[k].nodeName !== 'TD'
                                                && childEle.childNodes[k].hasAttribute(attrName)) {
                                                childEle.childNodes[k].removeAttribute(attrName);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        };
        return XhtmlValidation;
    }());
    exports.XhtmlValidation = XhtmlValidation;
});