'use strict';
var Element = require('./element.js');
var elements = require('./elements.js');
var ElementsArray = require('./elements_array.js');
/** @namespace */
var $ = {};
$ = /** @function
* @param selector
* @param context */function (selector, context) {
// HANDLE: $(), $(""), $(null), $(undefined), $(false)
Iif (!selector) {
return '?';
}
Iif (typeof selector === 'string') {
if (selector.charAt(0) === '#' && selector.match(/^#([\w\-]+)$/)) {
return $.byId(selector.substr(1));
}
// TODO : is it an optimization ? Shouldn't this be done by the browser,
// in the native function querySelectorAll ?
var m = selector.match(/^(\.)?([\w\-]+)$/);
if (m) {
return $[m[1] ? 'byClassName' : 'byTagName'](m[2], context);
}
return $.find(selector, context);
} else Eif (selector.nodeType) {
// DOM element
return elements(selector);
} else if (selector[0]) {
return selector;
}
};
exports = module.exports = $;
require('./element_prototype_and_array_prototype.js');
$.Element = Element;
$._toEltArray = /** @function
* @param results */function (results) {
if (!results) {
return new ElementsArray();
}
// Object.setPrototypeOf(results, ElementsArray.prototype);
var ret = new ElementsArray();
ret._push.apply(ret, results);
return ret;
};
/**
* Find an element by id
* @param {String} id
* @return {Element}
*/
$.byId = /** @function
* @param id */function (id) {
var element = document.getElementById(id);
// Check parentNode to catch when Blackberry 4.6 returns
// nodes that are no longer in the document #6963
if (element && element.parentNode) {
// Handle the case where IE and Opera return items
// by name instead of ID ===> SEE es5-compat
return elements(element);
}
};
/**
* Find the first element
* @param {String} selectors see document.querySelector
* @param {HTMLElement} context
* @return {Element}
*/
$.first = /** @function
* @param selectors
* @param context */function (selectors, context) {
var result = (context || document).querySelector(selectors);
return result && elements(result);
};
/**
* Find all elements corresponding to a selector
* @param {String} selectors
* @param {HTMLElement} context
* @return {ElementsArray}
*/
$.find = /** @function
* @param selectors
* @param context */function (selectors, context) {
return $._toEltArray((context || document).querySelectorAll(selectors));
};
/**
* Find elements by class name
* @param {String} names
* @param {HTMLElement} context
* @return {ElementsArray}
*/
$.byClassName = /** @function
* @param names
* @param context */function (names, context) {
/* https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByClassName */
return $._toEltArray((context || document).getElementsByClassName(names));
};
/**
* Find elements by tag name
* @param {String} names
* @param {HTMLElement} context
* @return {ElementsArray}
*/
$.byTagName = /** @function
* @param names
* @param context */function (names, context) {
/* https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByTagName */
return $._toEltArray((context || document).getElementsByTagName(names));
};
/**
* Get all HTML elements by tag name
* @param {HTMLElement} context
* @param {String} tag
*/
$.getAll = /** @function
* @param context
* @param tag */function (context, tag) {
return context.getElementsByTagName(tag || '*');
};
$.disposeElements = elements.disposeElements;
/**
* Dispose an element
* @param {HTMLElement} element
*/
$.disposeElement = elements.disposeElement;
/**
* @return {Boolean}
*/
$.isWindow = /** @function
* @param obj */function (obj) {
return obj && obj.window == obj;
};
/**
* Creates an element and wrap it with {Element}
* @return {Element}
*/
$.create = /** @function
* @param tag */function (tag) {
return new Element(document.createElement(tag));
};
require('./$.create');
/**
* Creates an fragment and wrap it with {Element}
* @return {Element}
*/
$.createFragment = /** @function */function () {
return new Element(document.createDocumentFragment());
};
/**
* Creates an simple text node
* @return {Text}
*/
$.textNode = /** @function
* @param string */function (string) {
return document.createTextNode(string);
};
/**
* Parse an html string into an element
* @return {Text}
*/
$.parse = /** @function
* @param string
* @param context */function (string, context) {
return elements.parse(string, context);
};
window.$document = $(document);
window.$head = $(document.head);
window.$body = $(document.body);
//# sourceMappingURL=$.js.map |