node.js 12.0 KB
Newer Older
C
campaign 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
///import editor.js
///import core/utils.js
///import core/dom/dom.js
///import core/dom/dtd.js
///import core/htmlparser.js
//模拟的节点类
//by zhanyi
(function () {
    var uNode = UE.uNode = function (obj) {
        this.type = obj.type;
        this.data = obj.data;
        this.tagName = obj.tagName;
        this.parentNode = obj.parentNode;
        this.attrs = obj.attrs || {};
        this.children = obj.children;
    };
    var indentChar = '    ',
C
campaign 已提交
18
        breakChar = '\n';
C
campaign 已提交
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

    function insertLine(arr, current, begin) {
        arr.push(breakChar);
        return current + (begin ? 1 : -1);
    }

    function insertIndent(arr, current) {
        //插入缩进
        for (var i = 0; i < current; i++) {
            arr.push(indentChar);
        }
    }

    //创建uNode的静态方法
    //支持标签和html
    uNode.createElement = function (html) {
        if (/[<>]/.test(html)) {
            return UE.htmlparser(html).children[0]
        } else {
            return new uNode({
                type:'element',
                children:[],
                tagName:html
            })
        }
    };
    uNode.createText = function (data) {
        return new UE.uNode({
            type:'text',
            'data':data || ''
        })
    };
    function nodeToHtml(node, arr, formatter, current) {
        switch (node.type) {
            case 'root':
                for (var i = 0, ci; ci = node.children[i++];) {
                    //插入新行
C
campaign 已提交
56
                    if (formatter && ci.type == 'element' && !dtd.$inline[ci.tagName] && i > 1) {
C
campaign 已提交
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
                        insertLine(arr, current, true);
                        insertIndent(arr, current)
                    }
                    nodeToHtml(ci, arr, formatter, current)
                }
                break;
            case 'text':
                isText(node, arr);
                break;
            case 'element':
                isElement(node, arr, formatter, current);
                break;
            case 'comment':
                isComment(node, arr, formatter);
        }
        return arr;
    }

    function isText(node, arr) {
        arr.push(node.data)
    }

    function isElement(node, arr, formatter, current) {
        var attrhtml = '';
        if (node.attrs) {
            attrhtml = [];
            var attrs = node.attrs;
            for (var a in attrs) {
C
campaign 已提交
85
                attrhtml.push(a + (attrs[a] !== undefined ? '="' + utils.unhtml(attrs[a]) + '"' : ''))
C
campaign 已提交
86 87 88 89 90 91 92 93
            }
            attrhtml = attrhtml.join(' ');
        }
        arr.push('<' + node.tagName +
            (attrhtml ? ' ' + attrhtml + ' ' : '') +
            (dtd.$empty[node.tagName] ? '\/' : '' ) + '>'
        );
        //插入新行
C
campaign 已提交
94
        if (formatter &&  !dtd.$inline[node.tagName]) {
C
campaign 已提交
95 96 97 98 99
            current = insertLine(arr, current, true);
            insertIndent(arr, current)
        }
        if (node.children && node.children.length) {
            for (var i = 0, ci; ci = node.children[i++];) {
C
campaign 已提交
100
                if (formatter && ci.type == 'element' &&  !dtd.$inline[ci.tagName] && i > 1) {
C
campaign 已提交
101 102 103 104 105 106 107
                    insertLine(arr, current);
                    insertIndent(arr, current)
                }
                nodeToHtml(ci, arr, formatter, current)
            }
        }
        if (!dtd.$empty[node.tagName]) {
C
campaign 已提交
108
            if (formatter && !dtd.$inline[node.tagName]) {
C
campaign 已提交
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
                current = insertLine(arr, current);
                insertIndent(arr, current)
            }
            arr.push('<\/' + node.tagName + '>');
        }

    }

    function isComment(node, arr) {
        arr.push('<!--' + node.data + '-->');
    }

    function getNodeById(root, id) {
        var node;
        if (root.type == 'element' && root.getAttr('id') == id) {
            return root;
        }
        if (root.children && root.children.length) {
            for (var i = 0, ci; ci = root.children[i++];) {
                if (node = getNodeById(ci, id)) {
                    return node;
                }
            }
        }
    }

    function getNodesByTagName(node, tagName, arr) {
        if (node.type == 'element' && node.tagName == tagName) {
            arr.push(node);
        }
        if (node.children && node.children.length) {
            for (var i = 0, ci; ci = node.children[i++];) {
                getNodesByTagName(ci, tagName, arr)
            }
        }
    }

    uNode.prototype = {
        toHtml:function (formatter) {
            var arr = [];
            nodeToHtml(this, arr, formatter, 0);
            return arr.join('')
        },
        innerHTML:function (htmlstr) {
            if (this.type != 'element' || dtd.$empty[this.tagName]) {
                return this;
            }
            if (htmlstr) {
                for (var i = 0, ci; ci = this.children[i++];) {
                    ci.parentNode = null;
                }
                this.children = [];
                var tmpRoot = UE.htmlparser(htmlstr);
                for (var i = 0, ci; ci = tmpRoot.children[i++];) {
                    this.children.push(ci);
                    ci.parentNode = this;
                }
                return this;
            } else {
                var tmpRoot = new UE.uNode({
                    type:'root',
                    children:this.children
                });
                return tmpRoot.toHtml();
            }
        },
        innerText:function () {
            if (this.type != 'element' || dtd.$empty[this.tagName]) {
                return this;
            }
            var html = this.toHtml();
            return html.replace(/<[^>]+>/g, '');
        },
        getData:function () {
            if (this.type == 'element')
                return '';
            return this.data
        },
        firstChild:function () {
            if (this.type != 'element' || dtd.$empty[this.tagName]) {
                return this;
            }
            return this.children ? this.children[0] : null;
        },
        lastChild:function () {
            if (this.type != 'element' || dtd.$empty[this.tagName]) {
                return this;
            }
            return this.children ? this.children[this.children.length - 1] : null;
        },
        replaceChild:function (target, source) {
            if (this.children) {
C
campaign 已提交
201 202 203
                if(target.parentNode){
                    target.parentNode.removeChild(target);
                }
C
campaign 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === source) {
                        this.children.splice(i, 1, target);
                        source.parentNode = null;
                        target.parentNode = this;
                        return target;
                    }
                }
            }
        },
        appendChild:function (node) {
            if (this.type == 'element' && !dtd.$empty[this.tagName]) {
                if (!this.children) {
                    this.children = []
                }
C
campaign 已提交
219 220 221
                if(node.parentNode){
                    node.parentNode.removeChild(node);
                }
C
campaign 已提交
222 223
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === node) {
C
campaign 已提交
224
                        this.children.splice(i, 1);
C
campaign 已提交
225 226 227 228 229 230 231 232 233 234 235
                        break;
                    }
                }
                this.children.push(node);
                node.parentNode = this;
                return node;
            }

        },
        insertBefore:function (target, source) {
            if (this.children) {
C
campaign 已提交
236 237 238
                if(target.parentNode){
                    target.parentNode.removeChild(target);
                }
C
campaign 已提交
239 240 241 242
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === source) {
                        this.children.splice(i, 0, target);
                        target.parentNode = this;
C
campaign 已提交
243
                        return target;
C
campaign 已提交
244 245
                    }
                }
C
campaign 已提交
246

C
campaign 已提交
247 248 249 250
            }
        },
        insertAfter:function (target, source) {
            if (this.children) {
C
campaign 已提交
251 252 253
                if(target.parentNode){
                    target.parentNode.removeChild(target);
                }
C
campaign 已提交
254 255
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === source) {
C
campaign 已提交
256
                        this.children.splice(i + 1, 0, target);
C
campaign 已提交
257
                        target.parentNode = this;
C
campaign 已提交
258
                        return target;
C
campaign 已提交
259
                    }
C
campaign 已提交
260

C
campaign 已提交
261 262 263
                }
            }
        },
C
campaign 已提交
264
        removeChild:function (node,keepChildren) {
C
campaign 已提交
265 266 267 268 269
            if (this.children) {
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === node) {
                        this.children.splice(i, 1);
                        ci.parentNode = null;
C
campaign 已提交
270 271 272 273 274 275 276
                        if(keepChildren && ci.children && ci.children.length){
                            for(var j= 0,cj;cj=ci.children[j];j++){
                                this.children.splice(i+j,0,cj);
                                cj.parentNode = this;

                            }
                        }
C
campaign 已提交
277 278 279 280 281 282 283 284 285
                        return ci;
                    }
                }
            }
        },
        getAttr:function (attrName) {
            return this.attrs[attrName.toLowerCase()]
        },
        setAttr:function (attrName, attrVal) {
C
campaign 已提交
286
            if (!attrName) {
C
campaign 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
                delete this.attrs;
                return;
            }
            if (utils.isObject(attrName)) {
                for (var a in attrName) {
                    if (!attrName[a]) {
                        delete this.attrs[a]
                    } else {
                        this.attrs[a.toLowerCase()] = attrName[a];
                    }
                }
            } else {
                if (!attrVal) {
                    delete this.attrs[attrName]
                } else {
                    this.attrs[attrName.toLowerCase()] = attrVal;
                }

            }
        },
C
campaign 已提交
307 308 309 310 311 312 313 314 315
        getIndex:function(){
            var parent = this.parentNode;
            for(var i= 0,ci;ci=parent.children[i];i++){
                if(ci === this){
                    return i;
                }
            }
            return -1;
        },
C
campaign 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
        getNodeById:function (id) {
            var node;
            if (this.children && this.children.length) {
                for (var i = 0, ci; ci = this.children[i++];) {
                    if (node = getNodeById(ci, id)) {
                        return node;
                    }
                }
            }
        },
        getNodesByTagName:function (tagNames) {
            tagNames = utils.trim(tagNames).replace(/[ ]{2,}/g, ' ').split(' ');
            var arr = [], me = this;
            utils.each(tagNames, function (tagName) {
                if (me.children && me.children.length) {
                    for (var i = 0, ci; ci = me.children[i++];) {
                        getNodesByTagName(ci, tagName, arr)
                    }
                }
            });
            return arr;
        },
C
campaign 已提交
338
        getStyle:function (name) {
C
campaign 已提交
339
            var cssStyle = this.getAttr('style');
C
campaign 已提交
340
            if (!cssStyle) {
C
campaign 已提交
341 342 343 344
                return ''
            }
            var reg = new RegExp(name + ':([^;]+)');
            var match = cssStyle.match(reg);
C
campaign 已提交
345
            if (match && match[0]) {
C
campaign 已提交
346 347 348 349
                return match[1]
            }
            return '';
        },
C
campaign 已提交
350 351 352 353 354 355
        setStyle:function (name, val) {
            function exec(name, val) {
                var reg = new RegExp(name + ':([^;]+);?', 'gi');
                cssStyle = cssStyle.replace(reg, '');
                if (val) {
                    cssStyle = name + ':' + utils.unhtml(val) + ';' + cssStyle
C
campaign 已提交
356 357 358
                }

            }
C
campaign 已提交
359

C
campaign 已提交
360
            var cssStyle = this.getAttr('style');
C
campaign 已提交
361
            if (!cssStyle) {
C
campaign 已提交
362 363 364
                cssStyle = '';
            }
            if (utils.isObject(name)) {
C
campaign 已提交
365 366
                for (var a in name) {
                    exec(a, name[a])
C
campaign 已提交
367 368
                }
            } else {
C
campaign 已提交
369
                exec(name, val)
C
campaign 已提交
370
            }
C
campaign 已提交
371
            this.setAttr('style', cssStyle)
C
campaign 已提交
372 373 374
        }
    }
})();