Talk about jQuery Source Code and Tricks – 一起读jQuery源码 – manipulation



Talk about jQuery Source Code and Tricks – 一起读jQuery源码 – manipulation

1 0


talk-about-jquery-source-code

talk-about-jquery-source-code

On Github song940 / talk-about-jquery-source-code

Talk about jQuery Source Code and Tricks

一起读jQuery源码

Created by Lsong / @song940

  • manipulation
  • queue
  • traversing
  • exports

manipulation

buildFragment

function buildFragment( elems, context, scripts, selection, ignored ) {
    var elem, tmp, tag, wrap, contains, j,
        fragment = context.createDocumentFragment(),
        nodes = [],
        i = 0,
        l = elems.length;

    for ( ; i < l; i++ ) {
        elem = elems[ i ];

        if ( elem || elem === 0 ) {

            // Add nodes directly
            if ( jQuery.type( elem ) === "object" ) {
                // Support: Android<4.1, PhantomJS<2
                // push.apply(_, arraylike) throws on ancient WebKit
                jQuery.merge( nodes, elem.nodeType ? [ elem ] : elem );

            // Convert non-html into a text node
            } else if ( !rhtml.test( elem ) ) {
                nodes.push( context.createTextNode( elem ) );

            // Convert html into DOM nodes
            } else {
                tmp = tmp || fragment.appendChild( context.createElement( "div" ) );

                // Deserialize a standard representation
                tag = ( rtagName.exec( elem ) || [ "", "" ] )[ 1 ].toLowerCase();
                wrap = wrapMap[ tag ] || wrapMap._default;
                tmp.innerHTML = wrap[ 1 ] + jQuery.htmlPrefilter( elem ) + wrap[ 2 ];

                // Descend through wrappers to the right content
                j = wrap[ 0 ];
                while ( j-- ) {
                    tmp = tmp.lastChild;
                }

                // Support: Android<4.1, PhantomJS<2
                // push.apply(_, arraylike) throws on ancient WebKit
                jQuery.merge( nodes, tmp.childNodes );

                // Remember the top-level container
                tmp = fragment.firstChild;

                // Ensure the created nodes are orphaned (#12392)
                tmp.textContent = "";
            }
        }
    }

    // Remove wrapper from fragment
    fragment.textContent = "";

    i = 0;
    while ( ( elem = nodes[ i++ ] ) ) {

        // Skip elements already in the context collection (trac-4087)
        if ( selection && jQuery.inArray( elem, selection ) > -1 ) {
            if ( ignored ) {
                ignored.push( elem );
            }
            continue;
        }

        contains = jQuery.contains( elem.ownerDocument, elem );

        // Append to fragment
        tmp = getAll( fragment.appendChild( elem ), "script" );

        // Preserve script evaluation history
        if ( contains ) {
            setGlobalEval( tmp );
        }

        // Capture executables
        if ( scripts ) {
            j = 0;
            while ( ( elem = tmp[ j++ ] ) ) {
                if ( rscriptType.test( elem.type || "" ) ) {
                    scripts.push( elem );
                }
            }
        }
    }

    return fragment;
}

return buildFragment;
});
jquery/src/manipulation/buildFragment.js
var rhtml = /<|&#?\w+;/;

function buildFragment(elems, context, scripts, selection, ignored){
    for ( ; i < l; i++ ) {
        elem = elems[ i ];
        if(rhtml.test(elem)){
            nodes.push( context.createTextNode( elem ) );
        }else{
            tmp = tmp || fragment.appendChild( context.createElement( "div" ) );
            tmp.innerHTML = wrap[ 1 ] + jQuery.htmlPrefilter( elem ) + wrap[ 2 ];

            // ...

            jQuery.merge( nodes, tmp.childNodes );
            tmp = fragment.firstChild;
            tmp.textContent = "";
        }
    }

    while ( ( elem = nodes[ i++ ] ) ) {
        // Append to fragment
        tmp = getAll( fragment.appendChild( elem ), "script" );
    }

    // ...

    return fragment;
}
var rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:-]+)[^>]*)\/>/gi;

function htmlPrefilter( html ) {
  return html.replace( rxhtmlTag, "<$1></$2>" );
}
htmlPrefilter('<img/>');

// <img></img>

traversing

jquery/src/traversing.js

exports

UMD

var jQuery = function(){
    // do something.
};

// expose to global scope.
umdify(jQuery);

umdify

UMDIFY

function umdify(fn){
	if(typeof define == 'undefined' && define.amd){
		// AMD. Register as an anonymous module.
		define([], fn);
	}else if(module && module.exports){
		// Common.js
		module.exports = fn;
	}else{
		window[ 'YourAwesomeModule' ] = fn;
	}
};

Questions ?

Wait...

Other Things

  • rootJQuery
  • $.fn.init
  • $.fn.isPlainObject
  • $.each
http://stackoverflow.com/questions/7644199/jquery-source-code-questions

THE END

- jQuery JavaScript Library - jQuery Annotated Source

Talk about jQuery Source Code and Tricks 一起读jQuery源码 Created by Lsong / @song940