5. http://code.jquery.com/jquery-1.7.2.js
init: function( selector, context, rootjQuery ) {
var match, elem, ret, doc;
// Handle $(""), $(null), or $(undefined) $() <- jQuery Objectが
if ( !selector ) {
欲しいときなど
return this;
}
// Handle $(DOMElement)
if ( selector.nodeType ) {
this.context = this[0] = selector;
$(document)
this.length = 1; $(document.getElementById(“id”)) な
return this; ど
}
// The body element only exists once, optimize finding it
if ( selector === "body" && !context && document.body ) {
this.context = document;
this[0] = document.body;
this.selector = selector; $(“body”)用
this.length = 1;
return this;
}
6. ここからが本番
事前準備
// Handle HTML strings
if ( typeof selector === "string" ) {
// Are we dealing with HTML string or an ID?
if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) {
// Assume that strings that start and end with <> are HTML and skip the regex check
match = [ null, selector, null ];
Htmlタグを入力したとき
} else {
match = quickExpr.exec( selector );
}
quickExpr = /^(?:[^#<]*(<[wW]+>)[^>]*$|#([w-]*)$)/
タグ(<>で囲まれたワード)と#で始まる文
字を抜き出している
match[0] = selectorの該当全部
match[1] = タグの中身(があった場合)
match[2] = #から始まるワード(があった場
合)
7. タグの場合
// Verify a match, and that no context was specified for #id
if ( match && (match[1] || !context) ) {
// HANDLE: $(html) -> $(array)
if ( match[1] ) {
context = context instanceof jQuery ? context[0] : context;
doc = ( context ? context.ownerDocument || context : document );
// If a single string is passed in and it's a single tag
// just do a createElement and skip the rest
ret = rsingleTag.exec( selector );
Htmlタグからelementを作る
if ( ret ) {
if ( jQuery.isPlainObject( context ) ) {
selector = [ document.createElement( ret[1] ) ];
jQuery.fn.attr.call( selector, context, true );
} else {
selector = [ doc.createElement( ret[1] ) ];
}
} else {
ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
selector = ( ret.cacheable ? jQuery.clone(ret.fragment) : ret.fragment ).childNodes;
}
return jQuery.merge( this, selector );
8. $(“#id”)の場合
// HANDLE: $("#id")
} else {
elem = document.getElementById( match[2] );
// Check parentNode to catch when Blackberry 4.6 returns
// nodes that are no longer in the document #6963
if ( elem && elem.parentNode ) {
// Handle the case where IE and Opera return items
// by name instead of ID
if ( elem.id !== match[2] ) { IDからgetElementByIdで
return rootjQuery.find( selector ); Elementを取得
}
// Otherwise, we inject the element directly into the jQuery object
this.length = 1;
this[0] = elem;
}
this.context = document;
this.selector = selector;
return this;
}
17. jQueryからもSizzleのコードを削除したいのですが、
レガシーなブラウザのために残してあるのが実際のところでしょう
Androidのwebブラウザ・iPhoneのブラウザ等にも
当然querySelectorAll は実装されています。
https://developer.mozilla.org/en/DOM/Document.querySelectorAll
Firefox Internet Safari
Feature Chrome Opera
(Gecko) Explorer (WebKit)
Basic support 1 3.5 (1.9.1) 8 10 3.2 (525.3)
Firefox
Opera
Feature Android Mobile IE Mobile Safari Mobile
Mobile
(Gecko)
Basic support 2.1 yes 9 10.0 3.2
18. このことからjqMobiでは
function _selector(selector, what) {
var dom;
if (selector[0] === "#" && selector.indexOf(" ") === -1 && selector.indexOf(">") === -1) {
if (what == document)
dom = what.getElementById(selector.replace("#", ""));
else
dom = [].slice.call(what.querySelectorAll(selector));
return dom;
}
selector=selector.trim();
if (selector[0] === "<" && selector[selector.length - 1] === ">") //html
{
var tmp = document.createElement("div");
tmp.innerHTML = selector.trim();
dom = [].slice.call(tmp.childNodes);
} else {
dom = [].slice.call(what.querySelectorAll(selector));
}
return dom;
}
約20行程度:700byte弱(無圧縮時)にすることに成功しています