node.js 11.9 KB
Newer Older
C
campaign 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
///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 = '    ',
        breakChar = '\n',
C
campaign 已提交
19
        needWrap = utils.extend(utils.extend({}, dtd.$block), dtd.$cdata);
C
campaign 已提交
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

    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++];) {
                    //插入新行
                    if (formatter && ci.type == 'element' && needWrap[ci.tagName] && i > 1) {
                        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 已提交
87
                attrhtml.push(a + (attrs[a] !== undefined ? '="' + utils.unhtml(attrs[a]) + '"' : ''))
C
campaign 已提交
88 89 90 91 92 93 94 95
            }
            attrhtml = attrhtml.join(' ');
        }
        arr.push('<' + node.tagName +
            (attrhtml ? ' ' + attrhtml + ' ' : '') +
            (dtd.$empty[node.tagName] ? '\/' : '' ) + '>'
        );
        //插入新行
C
campaign 已提交
96
        if (formatter && needWrap[node.tagName]) {
C
campaign 已提交
97 98 99 100 101
            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 已提交
102
                if (formatter && ci.type == 'element' && needWrap[ci.tagName] && i > 1) {
C
campaign 已提交
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 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 215 216 217 218 219
                    insertLine(arr, current);
                    insertIndent(arr, current)
                }
                nodeToHtml(ci, arr, formatter, current)
            }
        }
        if (!dtd.$empty[node.tagName]) {
            if (formatter && needWrap[node.tagName]) {
                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) {
                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 = []
                }
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === node) {
C
campaign 已提交
220
                        this.children.splice(i, 1);
C
campaign 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
                        break;
                    }
                }
                this.children.push(node);
                node.parentNode = this;
                return node;
            }

        },
        insertBefore:function (target, source) {
            if (this.children) {
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === target) {
                        this.children.splice(i, 1);
                        i--;
                    }
                    if (ci === source) {
                        this.children.splice(i, 0, target);
                        target.parentNode = this;
                        i++;
                    }
                }
                return target;
            }
        },
        insertAfter:function (target, source) {
            if (this.children) {
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === target) {
                        this.children.splice(i, 1);
                        i--;
                    }
                    if (ci === source) {
                        this.children.splice(i + 1, 1, target);
                        target.parentNode = this;
                        i = i + 2;

                    }
                    return target;
                }
            }
        },
C
campaign 已提交
263
        removeChild:function (node,keepChildren) {
C
campaign 已提交
264 265 266 267 268
            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 已提交
269 270 271 272 273 274 275
                        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 已提交
276 277 278 279 280 281 282 283 284
                        return ci;
                    }
                }
            }
        },
        getAttr:function (attrName) {
            return this.attrs[attrName.toLowerCase()]
        },
        setAttr:function (attrName, attrVal) {
C
campaign 已提交
285
            if (!attrName) {
C
campaign 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
                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 已提交
306 307 308 309 310 311 312 313 314
        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 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
        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 已提交
337
        getStyle:function (name) {
C
campaign 已提交
338
            var cssStyle = this.getAttr('style');
C
campaign 已提交
339
            if (!cssStyle) {
C
campaign 已提交
340 341 342 343
                return ''
            }
            var reg = new RegExp(name + ':([^;]+)');
            var match = cssStyle.match(reg);
C
campaign 已提交
344
            if (match[0]) {
C
campaign 已提交
345 346 347 348
                return match[1]
            }
            return '';
        },
C
campaign 已提交
349 350 351 352 353 354
        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 已提交
355 356 357
                }

            }
C
campaign 已提交
358

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

C
campaign 已提交
374 375 376
        }
    }
})();