clean()可以HTML代码序列化
图片转自:《jQuery技术内幕》
clipboard.png

function clean( elems, context, fragment, scripts ) {
//elems:数组,包含了待转换的HTML带呀
//context:文档对象,参数在bulidFragment()中被修正为正确的文档对象
//fragment():文档片段,存放转换后的DOM元素占位符,在bulidFragment()中被创建
//scripts:用于存放转换后DOM元素中的script
var checkScriptType;

context = context || document;
//修正context

// !context.createElement fails in IE with an error but returns typeof 'object'
if ( typeof context.createElement === "undefined" ) {
    context = context.ownerDocument || context[0] && context[0].ownerDocument || document;
}

var ret = [], j;

for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
    if ( typeof elem === "number" ) {
        elem += "";
    }

    if ( !elem ) {
        continue;
    }

    // Convert html string into DOM nodes
    if ( typeof elem === "string" ) {
        if ( !rhtml.test( elem ) ) {
            // rhtml = /<|&#?\w+;/
            elem = context.createTextNode( elem );
        } else {
            // Fix "XHTML"-style tags in all browsers
            elem = elem.replace(rxhtmlTag, "<$1></$2>");
            // rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig,

            // Trim whitespace, otherwise indexOf won't work as expected
            var tag = ( rtagName.exec( elem ) || ["", ""] )[1].toLowerCase(),
                wrap = wrapMap[ tag ] || wrapMap._default,
                depth = wrap[0],
                div = context.createElement("div");

            // Append wrapper element to unknown element safe doc fragment
            if ( context === document ) {
                // Use the fragment we've already created for this document
                safeFragment.appendChild( div );
            } else {
                // Use a fragment created with the owner document
                createSafeFragment( context ).appendChild( div );
            }

            // Go to html and back, then peel off extra wrappers
            div.innerHTML = wrap[1] + elem + wrap[2];

            // Move to the right depth
            while ( depth-- ) {
                div = div.lastChild;
            }

            // Remove IE's autoinserted <tbody> from table fragments
            if ( !jQuery.support.tbody ) {

                // String was a <table>, *may* have spurious <tbody>
                var hasBody = rtbody.test(elem),
                    tbody = tag === "table" && !hasBody ?
                        div.firstChild && div.firstChild.childNodes :

                        // String was a bare <thead> or <tfoot>
                        wrap[1] === "<table>" && !hasBody ?
                            div.childNodes :
                            [];

                for ( j = tbody.length - 1; j >= 0 ; --j ) {
                    if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length ) {
                        tbody[ j ].parentNode.removeChild( tbody[ j ] );
                    }
                }
            }

            // IE completely kills leading whitespace when innerHTML is used
            if ( !jQuery.support.leadingWhitespace && rleadingWhitespace.test( elem ) ) {
                div.insertBefore( context.createTextNode( rleadingWhitespace.exec(elem)[0] ), div.firstChild );
            }

            elem = div.childNodes;
        }
    }

    // Resets defaultChecked for any radios and checkboxes
    // about to be appended to the DOM in IE 6/7 (#8060)
    var len;
    if ( !jQuery.support.appendChecked ) {
        if ( elem[0] && typeof (len = elem.length) === "number" ) {
            for ( j = 0; j < len; j++ ) {
                findInputs( elem[j] );
            }
        } else {
            findInputs( elem );
        }
    }

    if ( elem.nodeType ) {
        ret.push( elem );
    } else {
        ret = jQuery.merge( ret, elem );
    }
}

if ( fragment ) {
    checkScriptType = function( elem ) {
        return !elem.type || rscriptType.test( elem.type );
    };
    for ( i = 0; ret[i]; i++ ) {
        if ( scripts && jQuery.nodeName( ret[i], "script" ) && (!ret[i].type || ret[i].type.toLowerCase() === "text/javascript") ) {
            scripts.push( ret[i].parentNode ? ret[i].parentNode.removeChild( ret[i] ) : ret[i] );

        } else {
            if ( ret[i].nodeType === 1 ) {
                var jsTags = jQuery.grep( ret[i].getElementsByTagName( "script" ), checkScriptType );

                ret.splice.apply( ret, [i + 1, 0].concat( jsTags ) );
            }
            fragment.appendChild( ret[i] );
        }
    }
}

return ret;

}


lapsec
756 声望25 粉丝

引用和评论

0 条评论