Permalink
Browse files

Refactor selector engine wrapper and add convenience helper methods t…

…o simplify implementation by third parties.
  • Loading branch information...
1 parent feba59b commit d9aee20322172ca203c844dc1df75abff2569aba @tobie tobie committed May 11, 2010
View
@@ -151,7 +151,7 @@ module PrototypeHelper
return if name == DEFAULT_SELECTOR_ENGINE || !name
submodule_path = File.join(ROOT_DIR, "vendor", name)
return submodule_path if File.exist?(File.join(submodule_path, "repository", ".git"))
-
+ return submodule_path if name === "legacy_selector"
get_submodule('the required selector engine', "#{name}/repository")
unless File.exist?(submodule_path)
puts "The selector engine you required isn't available at vendor/#{name}.\n\n"
View
@@ -20,9 +20,9 @@
**/
//= require "dom/dom"
-//= require <selector_engine>
//= require "dom/layout"
//= require "dom/selector"
+//= require <selector_engine>
//= require "dom/form"
//= require "dom/event"
View
@@ -95,44 +95,44 @@ window.$$ = function() {
* the choosen selector engine (Sizzle by default).
*
**/
+Prototype.Selector = (function() {
+
+ /**
+ * Prototype.Selector.select(expression[, root = document]) -> [Element...]
+ * - expression (String): A CSS selector.
+ * - root (Element | document): A "scope" to search within. All results will
+ * be descendants of this node.
+ *
+ * Searches `root` for elements that match the provided CSS selector and returns an
+ * array of extended [[Element]] objects.
+ **/
+ function select() {
+ throw new Error('Method "Prototype.Selector.select" must be defined.');
+ }
-// Implementation provided by selector engine.
-
-/**
- * Prototype.Selector.select(expression[, root = document]) -> [Element...]
- * - expression (String): A CSS selector.
- * - root (Element | document): A "scope" to search within. All results will
- * be descendants of this node.
- *
- * Searches `root` for elements that match the provided CSS selector and returns an
- * array of extended [[Element]] objects.
-**/
-
-// Implementation provided by selector engine.
-
-/**
- * Prototype.Selector.match(element, expression) -> Boolean
- * - element (Element): a DOM element.
- * - expression (String): A CSS selector.
- *
- * Tests whether `element` matches the CSS selector.
-**/
-
-// Implementation provided by selector engine.
+ /**
+ * Prototype.Selector.match(element, expression) -> Boolean
+ * - element (Element): a DOM element.
+ * - expression (String): A CSS selector.
+ *
+ * Tests whether `element` matches the CSS selector.
+ **/
+ function match() {
+ throw new Error('Method "Prototype.Selector.match" must be defined.');
+ }
-/**
- * Prototype.Selector.find(elements, expression[, index = 0]) -> Element
- * - elements (Enumerable): a collection of DOM elements.
- * - expression (String): A CSS selector.
- * - index: Numeric index of the match to return, defaults to 0.
- *
- * Filters the given collection of elements with `expression` and returns the
- * first matching element (or the `index`th matching element if `index` is
- * specified).
-**/
-if (!Prototype.Selector.find) {
- Prototype.Selector.find = function(elements, expression, index) {
- if (Object.isUndefined(index)) index = 0;
+ /**
+ * Prototype.Selector.find(elements, expression[, index = 0]) -> Element
+ * - elements (Enumerable): a collection of DOM elements.
+ * - expression (String): A CSS selector.
+ * - index: Numeric index of the match to return, defaults to 0.
+ *
+ * Filters the given collection of elements with `expression` and returns the
+ * first matching element (or the `index`th matching element if `index` is
+ * specified).
+ **/
+ function find(elements, expression, index) {
+ index = index || 0;
var match = Prototype.Selector.match, length = elements.length, matchIndex = 0, i;
for (i = 0; i < length; i++) {
@@ -141,5 +141,33 @@ if (!Prototype.Selector.find) {
}
}
}
-}
-
+
+ /**
+ * Prototype.Selector.extendElements(elements) -> Enumerable
+ * - elements (Enumerable): a collection of DOM elements.
+ *
+ * If necessary, extends the elements contained in `elements`
+ * and returns `elements` untouched. This is provided as a
+ * convenience method for selector engine wrapper implementors.
+ **/
+ function extendElements(elements) {
+ for (var i = 0, length = elements.length; i < length; i++) {
+ Element.extend(elements[i]);
+ }
+ return elements;
+ }
+
+ /** alias of: Element.extend
+ * Prototype.Selector.extendElement(element) -> Element
+ **/
+
+ var K = Prototype.K;
+
+ return {
+ select: select,
+ match: match,
+ find: find,
+ extendElements: (Element.extend === K) ? K : extendElements,
+ extendElement: Element.extend
+ };
+})();
View
@@ -1,27 +1,20 @@
Prototype._original_property = window.Sizzle;
//= require "sizzle"
-Prototype.Selector = (function(engine) {
- function extend(elements) {
- for (var i = 0, length = elements.length; i < length; i++) {
- Element.extend(elements[i]);
- }
- return elements;
- }
+;(function(engine) {
+ var extendElements = Prototype.Selector.extendElements;
function select(selector, scope) {
- return extend(engine(selector, scope || document));
+ return extendElements(engine(selector, scope || document));
}
function match(element, selector) {
return engine.matches(selector, [element]).length == 1;
}
-
- return {
- engine: engine,
- select: select,
- match: match
- };
+
+ Prototype.Selector.engine = engine;
+ Prototype.Selector.select = select;
+ Prototype.Selector.match = match;
})(Sizzle);
// Restore globals.
@@ -1,6 +1,6 @@
//= require "repository/legacy_selector"
-Prototype.Selector = (function(engine) {
+;(function(engine) {
function select(selector, scope) {
return engine.findChildElements(scope || document, [selector]);
}
@@ -9,10 +9,7 @@ Prototype.Selector = (function(engine) {
return !!engine.findElement([element], selector);
}
- return {
- engine: engine,
- select: select,
- match: match,
- filter: engine.matchElements
- };
+ Prototype.Selector.engine = engine;
+ Prototype.Selector.select = select;
+ Prototype.Selector.match = match;
})(Prototype.LegacySelector);
@@ -1,20 +1,16 @@
Prototype._original_property = window.NW;
//= require "repository/src/nwmatcher"
-Prototype.Selector = (function(engine) {
- var select = engine.select;
-
- if (Element.extend !== Prototype.K) {
- select = function select(selector, scope) {
- return engine.select(selector, scope, Element.extend);
- };
+;(function(engine) {
+ var extendElements = Prototype.Selector.extendElements;
+
+ function select(selector, scope) {
+ return extendElements(engine.select(selector, scope));
}
-
- return {
- engine: engine,
- select: select,
- match: engine.match
- };
+
+ Prototype.Selector.engine = engine;
+ Prototype.Selector.select = select;
+ Prototype.Selector.match = engine.match;
})(NW.Dom);
// Restore globals.
@@ -1,27 +1,20 @@
Prototype._original_property = window.Sizzle;
//= require "repository/sizzle"
-Prototype.Selector = (function(engine) {
- function extend(elements) {
- for (var i = 0, length = elements.length; i < length; i++) {
- Element.extend(elements[i]);
- }
- return elements;
- }
+;(function(engine) {
+ var extendElements = Prototype.Selector.extendElements;
function select(selector, scope) {
- return extend(engine(selector, scope || document));
+ return extendElements(engine(selector, scope || document));
}
function match(element, selector) {
return engine.matches(selector, [element]).length == 1;
}
-
- return {
- engine: engine,
- select: select,
- match: match
- };
+
+ Prototype.Selector.engine = engine;
+ Prototype.Selector.select = select;
+ Prototype.Selector.match = match;
})(Sizzle);
// Restore globals.
@@ -2,23 +2,16 @@ Prototype._original_property = window.Slick;
//= require "repository/Source/Slick.Parser.js"
//= require "repository/Source/Slick.Finder.js"
-Prototype.Selector = (function(engine) {
- function extend(elements) {
- for (var i = 0, length = elements.length; i < length; i++) {
- Element.extend(elements[i]);
- }
- return elements;
- }
+;(function(engine) {
+ var extendElements = Prototype.Selector.extendElements;
function select(selector, scope) {
- return extend(engine.search(scope || document, selector));
+ return extendElements(engine.search(scope || document, selector));
}
- return {
- engine: engine,
- select: select,
- match: engine.match
- };
+ Prototype.Selector.engine = engine;
+ Prototype.Selector.select = select;
+ Prototype.Selector.match = engine.match;
})(Slick);
// Restore globals.

0 comments on commit d9aee20

Please sign in to comment.