node.js 13.3 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
                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)
            }
        }
    }
C
campaign 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    function nodeTraversal(root,fn){
        if(root.children && root.children.length){
            for(var i= 0,ci;ci=root.children[i];){
                nodeTraversal(ci,fn);
                //ci被替换的情况,这里就不再走 fn了
                if(ci.parentNode ){
                    if(ci.children){
                        fn(ci)
                    }
                    if(ci.parentNode) i++
                }
            }
        }else{
            fn(root)
        }
C
campaign 已提交
160

C
campaign 已提交
161
    }
C
campaign 已提交
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
    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;
        },
C
campaign 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        previousSibling : function(){
            var parent = this.parentNode;
            for (var i = 0, ci; ci = parent.children[i]; i++) {
                if (ci === this) {
                   return i == 0 ? null : parent.children[i-1];
                }
            }

        },
        nextSibling : function(){
            var parent = this.parentNode;
            for (var i = 0, ci; ci = parent.children[i++];) {
                if (ci === this) {
                    return parent.children[i];
                }
            }
        },
C
campaign 已提交
232 233
        replaceChild:function (target, source) {
            if (this.children) {
C
campaign 已提交
234 235 236
                if(target.parentNode){
                    target.parentNode.removeChild(target);
                }
C
campaign 已提交
237 238 239 240 241 242 243 244 245 246 247
                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) {
C
campaign 已提交
248
            if (this.type == 'root' || (this.type == 'element' && !dtd.$empty[this.tagName])) {
C
campaign 已提交
249 250 251
                if (!this.children) {
                    this.children = []
                }
C
campaign 已提交
252 253 254
                if(node.parentNode){
                    node.parentNode.removeChild(node);
                }
C
campaign 已提交
255 256
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === node) {
C
campaign 已提交
257
                        this.children.splice(i, 1);
C
campaign 已提交
258 259 260 261 262 263 264 265
                        break;
                    }
                }
                this.children.push(node);
                node.parentNode = this;
                return node;
            }

C
campaign 已提交
266

C
campaign 已提交
267 268 269
        },
        insertBefore:function (target, source) {
            if (this.children) {
C
campaign 已提交
270 271 272
                if(target.parentNode){
                    target.parentNode.removeChild(target);
                }
C
campaign 已提交
273 274 275 276
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === source) {
                        this.children.splice(i, 0, target);
                        target.parentNode = this;
C
campaign 已提交
277
                        return target;
C
campaign 已提交
278 279
                    }
                }
C
campaign 已提交
280

C
campaign 已提交
281 282 283 284
            }
        },
        insertAfter:function (target, source) {
            if (this.children) {
C
campaign 已提交
285 286 287
                if(target.parentNode){
                    target.parentNode.removeChild(target);
                }
C
campaign 已提交
288 289
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === source) {
C
campaign 已提交
290
                        this.children.splice(i + 1, 0, target);
C
campaign 已提交
291
                        target.parentNode = this;
C
campaign 已提交
292
                        return target;
C
campaign 已提交
293
                    }
C
campaign 已提交
294

C
campaign 已提交
295 296 297
                }
            }
        },
C
campaign 已提交
298
        removeChild:function (node,keepChildren) {
C
campaign 已提交
299 300 301 302 303
            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 已提交
304 305 306 307 308 309 310
                        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 已提交
311 312 313 314 315 316
                        return ci;
                    }
                }
            }
        },
        getAttr:function (attrName) {
C
campaign 已提交
317
            return this.attrs && this.attrs[attrName.toLowerCase()]
C
campaign 已提交
318 319
        },
        setAttr:function (attrName, attrVal) {
C
campaign 已提交
320
            if (!attrName) {
C
campaign 已提交
321 322 323
                delete this.attrs;
                return;
            }
C
campaign 已提交
324 325 326
            if(!this.attrs){
                this.attrs = {};
            }
C
campaign 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
            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 已提交
344 345 346 347 348 349 350 351 352
        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 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
        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 已提交
375
        getStyle:function (name) {
C
campaign 已提交
376
            var cssStyle = this.getAttr('style');
C
campaign 已提交
377
            if (!cssStyle) {
C
campaign 已提交
378 379 380 381
                return ''
            }
            var reg = new RegExp(name + ':([^;]+)');
            var match = cssStyle.match(reg);
C
campaign 已提交
382
            if (match && match[0]) {
C
campaign 已提交
383 384 385 386
                return match[1]
            }
            return '';
        },
C
campaign 已提交
387 388 389 390 391 392
        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 已提交
393 394 395
                }

            }
C
campaign 已提交
396

C
campaign 已提交
397
            var cssStyle = this.getAttr('style');
C
campaign 已提交
398
            if (!cssStyle) {
C
campaign 已提交
399 400 401
                cssStyle = '';
            }
            if (utils.isObject(name)) {
C
campaign 已提交
402 403
                for (var a in name) {
                    exec(a, name[a])
C
campaign 已提交
404 405
                }
            } else {
C
campaign 已提交
406
                exec(name, val)
C
campaign 已提交
407
            }
C
campaign 已提交
408
            this.setAttr('style', cssStyle)
C
campaign 已提交
409 410 411 412 413 414
        },
        traversal:function(fn){
            if(this.children && this.children.length){
                nodeTraversal(this,fn);
            };
            return this;
C
campaign 已提交
415 416 417
        }
    }
})();