node.js 23.9 KB
Newer Older
H
hancong03 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/**
 * 编辑器模拟的节点类
 * @file
 * @module UE
 * @class uNode
 * @since 1.2.6.1
 */

/**
 * UEditor公用空间,UEditor所有的功能都挂载在该空间下
 * @unfile
 * @module UE
 */

C
campaign 已提交
15
(function () {
H
hancong03 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

    /**
     * 编辑器模拟的节点类
     * @unfile
     * @module UE
     * @class uNode
     */

    /**
     * 通过一个键值对,创建一个uNode对象
     * @constructor
     * @param { Object } attr 传入要创建的uNode的初始属性
     * @example
     * ```javascript
     * var node = new uNode({
     *     type:'element',
     *     tagName:'span',
     *     attrs:{style:'font-size:14px;'}
     * }
     * ```
     */
C
campaign 已提交
37 38 39 40 41 42 43 44
    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;
    };
C
campaign 已提交
45 46 47

    var notTransAttrs = {
        'href':1,
C
campaign 已提交
48 49
        'src':1,
        '_src':1,
50 51
        '_href':1,
        'cdata_data':1
C
campaign 已提交
52 53
    };

C
campaign 已提交
54 55 56 57 58 59
    var notTransTagName = {
        pre:1,
        style:1,
        script:1
    }

C
campaign 已提交
60
    var indentChar = '    ',
C
campaign 已提交
61
        breakChar = '\n';
C
campaign 已提交
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 87

    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 已提交
88
    uNode.createText = function (data,noTrans) {
C
campaign 已提交
89 90
        return new UE.uNode({
            type:'text',
C
campaign 已提交
91
            'data':noTrans ? data : utils.unhtml(data || '')
C
campaign 已提交
92 93 94 95 96 97 98
        })
    };
    function nodeToHtml(node, arr, formatter, current) {
        switch (node.type) {
            case 'root':
                for (var i = 0, ci; ci = node.children[i++];) {
                    //插入新行
C
campaign 已提交
99
                    if (formatter && ci.type == 'element' && !dtd.$inlineWithA[ci.tagName] && i > 1) {
C
campaign 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
                        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 已提交
119
        arr.push(notTransTagName[node.parentNode.tagName] ? node.data : node.data.replace(/[ ]{2}/g,' &nbsp;'))
C
campaign 已提交
120 121 122 123 124 125 126 127
    }

    function isElement(node, arr, formatter, current) {
        var attrhtml = '';
        if (node.attrs) {
            attrhtml = [];
            var attrs = node.attrs;
            for (var a in attrs) {
C
campaign 已提交
128 129 130 131 132 133 134
                //这里就针对
                //<p>'<img src='http://nsclick.baidu.com/u.gif?&asdf=\"sdf&asdfasdfs;asdf'></p>
                //这里边的\"做转换,要不用innerHTML直接被截断了,属性src
                //有可能做的不够
                attrhtml.push(a + (attrs[a] !== undefined ? '="' + (notTransAttrs[a] ? utils.html(attrs[a]).replace(/["]/g, function (a) {
                   return '&quot;'
                }) : utils.unhtml(attrs[a])) + '"' : ''))
C
campaign 已提交
135 136 137 138
            }
            attrhtml = attrhtml.join(' ');
        }
        arr.push('<' + node.tagName +
C
campaign 已提交
139
            (attrhtml ? ' ' + attrhtml  : '') +
C
campaign 已提交
140 141 142
            (dtd.$empty[node.tagName] ? '\/' : '' ) + '>'
        );
        //插入新行
C
campaign 已提交
143 144 145 146 147 148
        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 已提交
149 150 151
        }
        if (node.children && node.children.length) {
            for (var i = 0, ci; ci = node.children[i++];) {
C
campaign 已提交
152
                if (formatter && ci.type == 'element' &&  !dtd.$inlineWithA[ci.tagName] && i > 1) {
C
campaign 已提交
153 154 155 156 157 158 159
                    insertLine(arr, current);
                    insertIndent(arr, current)
                }
                nodeToHtml(ci, arr, formatter, current)
            }
        }
        if (!dtd.$empty[node.tagName]) {
C
campaign 已提交
160 161 162 163 164 165
            if (formatter && !dtd.$inlineWithA[node.tagName]  && node.tagName != 'pre') {

                if(node.children && node.children.length){
                    current = insertLine(arr, current);
                    insertIndent(arr, current)
                }
C
campaign 已提交
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
            }
            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 已提交
200 201 202 203 204 205
    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 已提交
206
                    if(ci.children && ci.children.length){
C
campaign 已提交
207 208 209 210 211 212 213 214
                        fn(ci)
                    }
                    if(ci.parentNode) i++
                }
            }
        }else{
            fn(root)
        }
C
campaign 已提交
215

C
campaign 已提交
216
    }
C
campaign 已提交
217
    uNode.prototype = {
H
hancong03 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

        /**
         * 当前节点对象,转换成html文本
         * @method toHtml
         * @return { String } 返回转换后的html字符串
         * @example
         * ```javascript
         * node.toHtml();
         * ```
         */

        /**
         * 当前节点对象,转换成html文本
         * @method toHtml
         * @param { Boolean } formatter 是否格式化返回值
         * @return { String } 返回转换后的html字符串
         * @example
         * ```javascript
         * node.toHtml( true );
         * ```
         */
C
campaign 已提交
239 240 241 242 243
        toHtml:function (formatter) {
            var arr = [];
            nodeToHtml(this, arr, formatter, 0);
            return arr.join('')
        },
H
hancong03 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

        /**
         * 获取节点的html内容
         * @method innerHTML
         * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
         * @return { String } 返回节点的html内容
         * @example
         * ```javascript
         * var htmlstr = node.innerHTML();
         * ```
         */

        /**
         * 设置节点的html内容
         * @method innerHTML
         * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
         * @param { String } htmlstr 传入要设置的html内容
         * @return { UE.uNode } 返回节点本身
         * @example
         * ```javascript
         * node.innerHTML('<span>text</span>');
         * ```
         */
C
campaign 已提交
267 268 269 270
        innerHTML:function (htmlstr) {
            if (this.type != 'element' || dtd.$empty[this.tagName]) {
                return this;
            }
C
campaign 已提交
271
            if (utils.isString(htmlstr)) {
C
campaign 已提交
272 273 274 275
                if(this.children){
                    for (var i = 0, ci; ci = this.children[i++];) {
                        ci.parentNode = null;
                    }
C
campaign 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
                }
                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();
            }
        },
H
hancong03 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

        /**
         * 获取节点的纯文本内容
         * @method innerText
         * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
         * @return { String } 返回节点的存文本内容
         * @example
         * ```javascript
         * var textStr = node.innerText();
         * ```
         */

        /**
         * 设置节点的纯文本内容
         * @method innerText
         * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
         * @param { String } textStr 传入要设置的文本内容
         * @return { UE.uNode } 返回节点本身
         * @example
         * ```javascript
         * node.innerText('<span>text</span>');
         * ```
         */
C
campaign 已提交
315
        innerText:function (textStr,noTrans) {
C
campaign 已提交
316 317 318
            if (this.type != 'element' || dtd.$empty[this.tagName]) {
                return this;
            }
C
campaign 已提交
319 320 321 322 323 324 325
            if (textStr) {
                if(this.children){
                    for (var i = 0, ci; ci = this.children[i++];) {
                        ci.parentNode = null;
                    }
                }
                this.children = [];
C
campaign 已提交
326
                this.appendChild(uNode.createText(textStr,noTrans));
C
campaign 已提交
327 328 329 330
                return this;
            } else {
                return this.toHtml().replace(/<[^>]+>/g, '');
            }
C
campaign 已提交
331
        },
H
hancong03 已提交
332 333 334 335 336 337 338 339 340 341

        /**
         * 获取当前对象的data属性
         * @method getData
         * @return { Object } 若节点的type值是elemenet,返回空字符串,否则返回节点的data属性
         * @example
         * ```javascript
         * node.getData();
         * ```
         */
C
campaign 已提交
342 343 344 345 346
        getData:function () {
            if (this.type == 'element')
                return '';
            return this.data
        },
H
hancong03 已提交
347 348 349 350 351 352 353 354 355 356

        /**
         * 获取当前节点下的第一个子节点
         * @method firstChild
         * @return { UE.uNode } 返回第一个子节点
         * @example
         * ```javascript
         * node.firstChild(); //返回第一个子节点
         * ```
         */
C
campaign 已提交
357
        firstChild:function () {
C
campaign 已提交
358 359 360
//            if (this.type != 'element' || dtd.$empty[this.tagName]) {
//                return this;
//            }
C
campaign 已提交
361 362
            return this.children ? this.children[0] : null;
        },
H
hancong03 已提交
363 364 365 366 367 368 369 370 371 372

        /**
         * 获取当前节点下的最后一个子节点
         * @method lastChild
         * @return { UE.uNode } 返回最后一个子节点
         * @example
         * ```javascript
         * node.lastChild(); //返回最后一个子节点
         * ```
         */
C
campaign 已提交
373
        lastChild:function () {
C
campaign 已提交
374 375 376
//            if (this.type != 'element' || dtd.$empty[this.tagName] ) {
//                return this;
//            }
C
campaign 已提交
377 378
            return this.children ? this.children[this.children.length - 1] : null;
        },
H
hancong03 已提交
379 380 381 382 383 384 385 386 387 388

        /**
         * 获取和当前节点有相同父亲节点的前一个节点
         * @method previousSibling
         * @return { UE.uNode } 返回前一个节点
         * @example
         * ```javascript
         * node.children[2].previousSibling(); //返回子节点node.children[1]
         * ```
         */
C
campaign 已提交
389 390 391 392 393 394 395 396 397
        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];
                }
            }

        },
H
hancong03 已提交
398 399 400 401 402 403 404 405 406 407

        /**
         * 获取和当前节点有相同父亲节点的后一个节点
         * @method nextSibling
         * @return { UE.uNode } 返回后一个节点,找不到返回null
         * @example
         * ```javascript
         * node.children[2].nextSibling(); //如果有,返回子节点node.children[3]
         * ```
         */
C
campaign 已提交
408 409 410 411 412 413 414 415
        nextSibling : function(){
            var parent = this.parentNode;
            for (var i = 0, ci; ci = parent.children[i++];) {
                if (ci === this) {
                    return parent.children[i];
                }
            }
        },
H
hancong03 已提交
416 417 418 419 420 421 422 423 424 425 426 427

        /**
         * 用新的节点替换当前节点
         * @method replaceChild
         * @param { UE.uNode } target 要替换成该节点参数
         * @param { UE.uNode } source 要被替换掉的节点
         * @return { UE.uNode } 返回替换之后的节点对象
         * @example
         * ```javascript
         * node.replaceChild(newNode, childNode); //用newNode替换childNode,childNode是node的子节点
         * ```
         */
C
campaign 已提交
428 429
        replaceChild:function (target, source) {
            if (this.children) {
C
campaign 已提交
430 431 432
                if(target.parentNode){
                    target.parentNode.removeChild(target);
                }
C
campaign 已提交
433 434 435 436 437 438 439 440 441 442
                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;
                    }
                }
            }
        },
H
hancong03 已提交
443 444 445 446 447 448 449 450 451 452 453

        /**
         * 在节点的子节点列表最后位置插入一个节点
         * @method appendChild
         * @param { UE.uNode } node 要插入的节点
         * @return { UE.uNode } 返回刚插入的子节点
         * @example
         * ```javascript
         * node.appendChild( newNode ); //在node内插入子节点newNode
         * ```
         */
C
campaign 已提交
454
        appendChild:function (node) {
C
campaign 已提交
455
            if (this.type == 'root' || (this.type == 'element' && !dtd.$empty[this.tagName])) {
C
campaign 已提交
456 457 458
                if (!this.children) {
                    this.children = []
                }
C
campaign 已提交
459 460 461
                if(node.parentNode){
                    node.parentNode.removeChild(node);
                }
C
campaign 已提交
462 463
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === node) {
C
campaign 已提交
464
                        this.children.splice(i, 1);
C
campaign 已提交
465 466 467 468 469 470 471 472
                        break;
                    }
                }
                this.children.push(node);
                node.parentNode = this;
                return node;
            }

C
campaign 已提交
473

C
campaign 已提交
474
        },
H
hancong03 已提交
475 476 477 478 479 480 481 482 483 484 485 486

        /**
         * 在传入节点的前面插入一个节点
         * @method insertBefore
         * @param { UE.uNode } target 要插入的节点
         * @param { UE.uNode } source 在该参数节点前面插入
         * @return { UE.uNode } 返回刚插入的子节点
         * @example
         * ```javascript
         * node.parentNode.insertBefore(newNode, node); //在node节点后面插入newNode
         * ```
         */
C
campaign 已提交
487 488
        insertBefore:function (target, source) {
            if (this.children) {
C
campaign 已提交
489 490 491
                if(target.parentNode){
                    target.parentNode.removeChild(target);
                }
C
campaign 已提交
492 493 494 495
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === source) {
                        this.children.splice(i, 0, target);
                        target.parentNode = this;
C
campaign 已提交
496
                        return target;
C
campaign 已提交
497 498
                    }
                }
C
campaign 已提交
499

C
campaign 已提交
500 501
            }
        },
H
hancong03 已提交
502 503 504 505 506 507 508 509 510 511 512 513

        /**
         * 在传入节点的后面插入一个节点
         * @method insertAfter
         * @param { UE.uNode } target 要插入的节点
         * @param { UE.uNode } source 在该参数节点后面插入
         * @return { UE.uNode } 返回刚插入的子节点
         * @example
         * ```javascript
         * node.parentNode.insertAfter(newNode, node); //在node节点后面插入newNode
         * ```
         */
C
campaign 已提交
514 515
        insertAfter:function (target, source) {
            if (this.children) {
C
campaign 已提交
516 517 518
                if(target.parentNode){
                    target.parentNode.removeChild(target);
                }
C
campaign 已提交
519 520
                for (var i = 0, ci; ci = this.children[i]; i++) {
                    if (ci === source) {
C
campaign 已提交
521
                        this.children.splice(i + 1, 0, target);
C
campaign 已提交
522
                        target.parentNode = this;
C
campaign 已提交
523
                        return target;
C
campaign 已提交
524
                    }
C
campaign 已提交
525

C
campaign 已提交
526 527 528
                }
            }
        },
H
hancong03 已提交
529 530 531 532 533 534 535 536 537 538 539 540

        /**
         * 从当前节点的子节点列表中,移除节点
         * @method removeChild
         * @param { UE.uNode } node 要移除的节点引用
         * @param { Boolean } keepChildren 是否保留移除节点的子节点,若传入true,自动把移除节点的子节点插入到移除的位置
         * @return { * } 返回刚移除的子节点
         * @example
         * ```javascript
         * node.removeChild(childNode,true); //在node的子节点列表中移除child节点,并且吧child的子节点插入到移除的位置
         * ```
         */
C
campaign 已提交
541
        removeChild:function (node,keepChildren) {
C
campaign 已提交
542 543 544 545 546
            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 已提交
547 548 549 550 551 552 553
                        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 已提交
554 555 556 557 558
                        return ci;
                    }
                }
            }
        },
H
hancong03 已提交
559 560 561 562 563 564 565 566 567 568 569

        /**
         * 获取当前节点所代表的元素属性,即获取attrs对象下的属性值
         * @method getAttr
         * @param { String } attrName 要获取的属性名称
         * @return { * } 返回attrs对象下的属性值
         * @example
         * ```javascript
         * node.getAttr('title');
         * ```
         */
C
campaign 已提交
570
        getAttr:function (attrName) {
C
campaign 已提交
571
            return this.attrs && this.attrs[attrName.toLowerCase()]
C
campaign 已提交
572
        },
H
hancong03 已提交
573 574 575 576 577 578 579 580 581 582 583 584

        /**
         * 设置当前节点所代表的元素属性,即设置attrs对象下的属性值
         * @method setAttr
         * @param { String } attrName 要设置的属性名称
         * @param { * } attrVal 要设置的属性值,类型视设置的属性而定
         * @return { * } 返回attrs对象下的属性值
         * @example
         * ```javascript
         * node.setAttr('title','标题');
         * ```
         */
C
campaign 已提交
585
        setAttr:function (attrName, attrVal) {
C
campaign 已提交
586
            if (!attrName) {
C
campaign 已提交
587 588 589
                delete this.attrs;
                return;
            }
C
campaign 已提交
590 591 592
            if(!this.attrs){
                this.attrs = {};
            }
C
campaign 已提交
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
            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;
                }

            }
        },
H
hancong03 已提交
610 611 612 613 614 615 616 617 618 619

        /**
         * 获取当前节点在父节点下的位置索引
         * @method getIndex
         * @return { Number } 返回索引数值,如果没有父节点,返回-1
         * @example
         * ```javascript
         * node.getIndex();
         * ```
         */
C
campaign 已提交
620 621 622 623 624 625 626 627 628
        getIndex:function(){
            var parent = this.parentNode;
            for(var i= 0,ci;ci=parent.children[i];i++){
                if(ci === this){
                    return i;
                }
            }
            return -1;
        },
H
hancong03 已提交
629 630 631 632 633 634 635 636 637 638 639

        /**
         * 在当前节点下,根据id查找节点
         * @method getNodeById
         * @param { String } id 要查找的id
         * @return { UE.uNode } 返回找到的节点
         * @example
         * ```javascript
         * node.getNodeById('textId');
         * ```
         */
C
campaign 已提交
640 641 642 643 644 645 646 647 648 649
        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;
                    }
                }
            }
        },
H
hancong03 已提交
650 651 652 653 654 655 656 657 658 659 660

        /**
         * 在当前节点下,根据元素名称查找节点列表
         * @method getNodesByTagName
         * @param { String } tagNames 要查找的元素名称
         * @return { Array } 返回找到的节点列表
         * @example
         * ```javascript
         * node.getNodesByTagName('span');
         * ```
         */
C
campaign 已提交
661 662 663 664 665 666 667 668 669 670 671 672
        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;
        },
H
hancong03 已提交
673 674 675 676 677 678 679 680 681 682 683

        /**
         * 根据样式名称,获取节点的样式值
         * @method getStyle
         * @param { String } name 要获取的样式名称
         * @return { String } 返回样式值
         * @example
         * ```javascript
         * node.getStyle('font-size');
         * ```
         */
C
campaign 已提交
684
        getStyle:function (name) {
C
campaign 已提交
685
            var cssStyle = this.getAttr('style');
C
campaign 已提交
686
            if (!cssStyle) {
C
campaign 已提交
687 688
                return ''
            }
C
campaign 已提交
689
            var reg = new RegExp('(^|;)\\s*' + name + ':([^;]+)','i');
C
campaign 已提交
690
            var match = cssStyle.match(reg);
C
campaign 已提交
691
            if (match && match[0]) {
C
campaign 已提交
692
                return match[2]
C
campaign 已提交
693 694 695
            }
            return '';
        },
H
hancong03 已提交
696 697 698 699 700 701 702 703 704 705 706

        /**
         * 给节点设置样式
         * @method setStyle
         * @param { String } name 要设置的的样式名称
         * @param { String } val 要设置的的样值
         * @example
         * ```javascript
         * node.setStyle('font-size', '12px');
         * ```
         */
C
campaign 已提交
707 708
        setStyle:function (name, val) {
            function exec(name, val) {
C
campaign 已提交
709 710
                var reg = new RegExp('(^|;)\\s*' + name + ':([^;]+;?)', 'gi');
                cssStyle = cssStyle.replace(reg, '$1');
C
campaign 已提交
711 712
                if (val) {
                    cssStyle = name + ':' + utils.unhtml(val) + ';' + cssStyle
C
campaign 已提交
713 714 715
                }

            }
C
campaign 已提交
716

C
campaign 已提交
717
            var cssStyle = this.getAttr('style');
C
campaign 已提交
718
            if (!cssStyle) {
C
campaign 已提交
719 720 721
                cssStyle = '';
            }
            if (utils.isObject(name)) {
C
campaign 已提交
722 723
                for (var a in name) {
                    exec(a, name[a])
C
campaign 已提交
724 725
                }
            } else {
C
campaign 已提交
726
                exec(name, val)
C
campaign 已提交
727
            }
C
campaign 已提交
728
            this.setAttr('style', utils.trim(cssStyle))
C
campaign 已提交
729
        },
H
hancong03 已提交
730 731 732 733 734 735 736 737 738 739 740 741

        /**
         * 传入一个函数,递归遍历当前节点下的所有节点
         * @method traversal
         * @param { Function } fn 遍历到节点的时,传入节点作为参数,运行此函数
         * @example
         * ```javascript
         * traversal(node, function(){
         *     console.log(node.type);
         * });
         * ```
         */
C
campaign 已提交
742 743 744
        traversal:function(fn){
            if(this.children && this.children.length){
                nodeTraversal(this,fn);
C
campaign 已提交
745
            }
C
campaign 已提交
746
            return this;
C
campaign 已提交
747 748 749
        }
    }
})();