node.js 12.6 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
                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;
        },
C
campaign 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
        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 已提交
216 217
        replaceChild:function (target, source) {
            if (this.children) {
C
campaign 已提交
218 219 220
                if(target.parentNode){
                    target.parentNode.removeChild(target);
                }
C
campaign 已提交
221 222 223 224 225 226 227 228 229 230 231
                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 已提交
232
            if (this.type == 'root' || (this.type == 'element' && !dtd.$empty[this.tagName])) {
C
campaign 已提交
233 234 235
                if (!this.children) {
                    this.children = []
                }
C
campaign 已提交
236 237 238
                if(node.parentNode){
                    node.parentNode.removeChild(node);
                }
C
campaign 已提交
239 240
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === node) {
C
campaign 已提交
241
                        this.children.splice(i, 1);
C
campaign 已提交
242 243 244 245 246 247 248 249
                        break;
                    }
                }
                this.children.push(node);
                node.parentNode = this;
                return node;
            }

C
campaign 已提交
250

C
campaign 已提交
251 252 253
        },
        insertBefore:function (target, source) {
            if (this.children) {
C
campaign 已提交
254 255 256
                if(target.parentNode){
                    target.parentNode.removeChild(target);
                }
C
campaign 已提交
257 258 259 260
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === source) {
                        this.children.splice(i, 0, target);
                        target.parentNode = this;
C
campaign 已提交
261
                        return target;
C
campaign 已提交
262 263
                    }
                }
C
campaign 已提交
264

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

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

            }
C
campaign 已提交
380

C
campaign 已提交
381
            var cssStyle = this.getAttr('style');
C
campaign 已提交
382
            if (!cssStyle) {
C
campaign 已提交
383 384 385
                cssStyle = '';
            }
            if (utils.isObject(name)) {
C
campaign 已提交
386 387
                for (var a in name) {
                    exec(a, name[a])
C
campaign 已提交
388 389
                }
            } else {
C
campaign 已提交
390
                exec(name, val)
C
campaign 已提交
391
            }
C
campaign 已提交
392
            this.setAttr('style', cssStyle)
C
campaign 已提交
393 394 395
        }
    }
})();