node.js 14.1 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

    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
            })
        }
    };
C
campaign 已提交
45
    uNode.createText = function (data,noTrans) {
C
campaign 已提交
46 47
        return new UE.uNode({
            type:'text',
C
campaign 已提交
48
            'data':noTrans ? data : utils.unhtml(data || '')
C
campaign 已提交
49 50 51 52 53 54 55
        })
    };
    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.$inlineWithA[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
                        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) {
C
campaign 已提交
76
        arr.push(node.parentNode.tagName == 'pre' ? node.data : node.data.replace(/[ ]{2}/g,' &nbsp;'))
C
campaign 已提交
77 78 79 80 81 82 83 84
    }

    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
            }
            attrhtml = attrhtml.join(' ');
        }
        arr.push('<' + node.tagName +
C
campaign 已提交
90
            (attrhtml ? ' ' + attrhtml  : '') +
C
campaign 已提交
91 92 93
            (dtd.$empty[node.tagName] ? '\/' : '' ) + '>'
        );
        //插入新行
C
campaign 已提交
94 95 96 97 98 99
        if (formatter  &&  !dtd.$inlineWithA[node.tagName] && node.tagName != 'pre') {
            if(node.children && node.children.length){
                current = insertLine(arr, current, true);
                insertIndent(arr, current)
            }

C
campaign 已提交
100 101 102
        }
        if (node.children && node.children.length) {
            for (var i = 0, ci; ci = node.children[i++];) {
C
campaign 已提交
103
                if (formatter && ci.type == 'element' &&  !dtd.$inlineWithA[ci.tagName] && i > 1) {
C
campaign 已提交
104 105 106 107 108 109 110
                    insertLine(arr, current);
                    insertIndent(arr, current)
                }
                nodeToHtml(ci, arr, formatter, current)
            }
        }
        if (!dtd.$empty[node.tagName]) {
C
campaign 已提交
111 112 113 114 115 116
            if (formatter && !dtd.$inlineWithA[node.tagName]  && node.tagName != 'pre') {

                if(node.children && node.children.length){
                    current = insertLine(arr, current);
                    insertIndent(arr, current)
                }
C
campaign 已提交
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
            }
            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 已提交
151 152 153 154 155 156
    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 ){
C
campaign 已提交
157
                    if(ci.children && ci.children.length){
C
campaign 已提交
158 159 160 161 162 163 164 165
                        fn(ci)
                    }
                    if(ci.parentNode) i++
                }
            }
        }else{
            fn(root)
        }
C
campaign 已提交
166

C
campaign 已提交
167
    }
C
campaign 已提交
168 169 170 171 172 173 174 175 176 177
    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;
            }
C
campaign 已提交
178
            if (utils.isString(htmlstr)) {
C
campaign 已提交
179 180 181 182
                if(this.children){
                    for (var i = 0, ci; ci = this.children[i++];) {
                        ci.parentNode = null;
                    }
C
campaign 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
                }
                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();
            }
        },
C
campaign 已提交
199
        innerText:function (textStr,noTrans) {
C
campaign 已提交
200 201 202
            if (this.type != 'element' || dtd.$empty[this.tagName]) {
                return this;
            }
C
campaign 已提交
203 204 205 206 207 208 209
            if (textStr) {
                if(this.children){
                    for (var i = 0, ci; ci = this.children[i++];) {
                        ci.parentNode = null;
                    }
                }
                this.children = [];
C
campaign 已提交
210
                this.appendChild(uNode.createText(textStr,noTrans));
C
campaign 已提交
211 212 213 214
                return this;
            } else {
                return this.toHtml().replace(/<[^>]+>/g, '');
            }
C
campaign 已提交
215 216 217 218 219 220 221
        },
        getData:function () {
            if (this.type == 'element')
                return '';
            return this.data
        },
        firstChild:function () {
C
campaign 已提交
222 223 224
//            if (this.type != 'element' || dtd.$empty[this.tagName]) {
//                return this;
//            }
C
campaign 已提交
225 226 227
            return this.children ? this.children[0] : null;
        },
        lastChild:function () {
C
campaign 已提交
228 229 230
//            if (this.type != 'element' || dtd.$empty[this.tagName] ) {
//                return this;
//            }
C
campaign 已提交
231 232
            return this.children ? this.children[this.children.length - 1] : null;
        },
C
campaign 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
        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 已提交
250 251
        replaceChild:function (target, source) {
            if (this.children) {
C
campaign 已提交
252 253 254
                if(target.parentNode){
                    target.parentNode.removeChild(target);
                }
C
campaign 已提交
255 256 257 258 259 260 261 262 263 264 265
                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 已提交
266
            if (this.type == 'root' || (this.type == 'element' && !dtd.$empty[this.tagName])) {
C
campaign 已提交
267 268 269
                if (!this.children) {
                    this.children = []
                }
C
campaign 已提交
270 271 272
                if(node.parentNode){
                    node.parentNode.removeChild(node);
                }
C
campaign 已提交
273 274
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === node) {
C
campaign 已提交
275
                        this.children.splice(i, 1);
C
campaign 已提交
276 277 278 279 280 281 282 283
                        break;
                    }
                }
                this.children.push(node);
                node.parentNode = this;
                return node;
            }

C
campaign 已提交
284

C
campaign 已提交
285 286 287
        },
        insertBefore:function (target, source) {
            if (this.children) {
C
campaign 已提交
288 289 290
                if(target.parentNode){
                    target.parentNode.removeChild(target);
                }
C
campaign 已提交
291 292 293 294
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === source) {
                        this.children.splice(i, 0, target);
                        target.parentNode = this;
C
campaign 已提交
295
                        return target;
C
campaign 已提交
296 297
                    }
                }
C
campaign 已提交
298

C
campaign 已提交
299 300 301 302
            }
        },
        insertAfter:function (target, source) {
            if (this.children) {
C
campaign 已提交
303 304 305
                if(target.parentNode){
                    target.parentNode.removeChild(target);
                }
C
campaign 已提交
306 307
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === source) {
C
campaign 已提交
308
                        this.children.splice(i + 1, 0, target);
C
campaign 已提交
309
                        target.parentNode = this;
C
campaign 已提交
310
                        return target;
C
campaign 已提交
311
                    }
C
campaign 已提交
312

C
campaign 已提交
313 314 315
                }
            }
        },
C
campaign 已提交
316
        removeChild:function (node,keepChildren) {
C
campaign 已提交
317 318 319 320 321
            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 已提交
322 323 324 325 326 327 328
                        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 已提交
329 330 331 332 333 334
                        return ci;
                    }
                }
            }
        },
        getAttr:function (attrName) {
C
campaign 已提交
335
            return this.attrs && this.attrs[attrName.toLowerCase()]
C
campaign 已提交
336 337
        },
        setAttr:function (attrName, attrVal) {
C
campaign 已提交
338
            if (!attrName) {
C
campaign 已提交
339 340 341
                delete this.attrs;
                return;
            }
C
campaign 已提交
342 343 344
            if(!this.attrs){
                this.attrs = {};
            }
C
campaign 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
            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 已提交
362 363 364 365 366 367 368 369 370
        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 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
        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 已提交
393
        getStyle:function (name) {
C
campaign 已提交
394
            var cssStyle = this.getAttr('style');
C
campaign 已提交
395
            if (!cssStyle) {
C
campaign 已提交
396 397
                return ''
            }
C
campaign 已提交
398
            var reg = new RegExp('(^|;)\\s*' + name + ':([^;]+)','i');
C
campaign 已提交
399
            var match = cssStyle.match(reg);
C
campaign 已提交
400
            if (match && match[0]) {
C
campaign 已提交
401
                return match[2]
C
campaign 已提交
402 403 404
            }
            return '';
        },
C
campaign 已提交
405 406
        setStyle:function (name, val) {
            function exec(name, val) {
C
campaign 已提交
407 408
                var reg = new RegExp('(^|;)\\s*' + name + ':([^;]+;?)', 'gi');
                cssStyle = cssStyle.replace(reg, '$1');
C
campaign 已提交
409 410
                if (val) {
                    cssStyle = name + ':' + utils.unhtml(val) + ';' + cssStyle
C
campaign 已提交
411 412 413
                }

            }
C
campaign 已提交
414

C
campaign 已提交
415
            var cssStyle = this.getAttr('style');
C
campaign 已提交
416
            if (!cssStyle) {
C
campaign 已提交
417 418 419
                cssStyle = '';
            }
            if (utils.isObject(name)) {
C
campaign 已提交
420 421
                for (var a in name) {
                    exec(a, name[a])
C
campaign 已提交
422 423
                }
            } else {
C
campaign 已提交
424
                exec(name, val)
C
campaign 已提交
425
            }
C
campaign 已提交
426
            this.setAttr('style', utils.trim(cssStyle))
C
campaign 已提交
427 428 429 430
        },
        traversal:function(fn){
            if(this.children && this.children.length){
                nodeTraversal(this,fn);
C
campaign 已提交
431
            }
C
campaign 已提交
432
            return this;
C
campaign 已提交
433 434 435
        }
    }
})();