import { window, document } from 'ssr-window';
|
import Dom7 from './dom7-class';
|
|
function $(selector, context) {
|
const arr = [];
|
let i = 0;
|
if (selector && !context) {
|
if (selector instanceof Dom7) {
|
return selector;
|
}
|
}
|
if (selector) {
|
// String
|
if (typeof selector === 'string') {
|
let els;
|
let tempParent;
|
const html = selector.trim();
|
if (html.indexOf('<') >= 0 && html.indexOf('>') >= 0) {
|
let toCreate = 'div';
|
if (html.indexOf('<li') === 0) toCreate = 'ul';
|
if (html.indexOf('<tr') === 0) toCreate = 'tbody';
|
if (html.indexOf('<td') === 0 || html.indexOf('<th') === 0) toCreate = 'tr';
|
if (html.indexOf('<tbody') === 0) toCreate = 'table';
|
if (html.indexOf('<option') === 0) toCreate = 'select';
|
tempParent = document.createElement(toCreate);
|
tempParent.innerHTML = html;
|
for (i = 0; i < tempParent.childNodes.length; i += 1) {
|
arr.push(tempParent.childNodes[i]);
|
}
|
} else {
|
if (!context && selector[0] === '#' && !selector.match(/[ .<>:~]/)) {
|
// Pure ID selector
|
els = [document.getElementById(selector.trim().split('#')[1])];
|
} else {
|
// Other selectors
|
els = (context || document).querySelectorAll(selector.trim());
|
}
|
for (i = 0; i < els.length; i += 1) {
|
if (els[i]) arr.push(els[i]);
|
}
|
}
|
} else if (selector.nodeType || selector === window || selector === document) {
|
// Node/element
|
arr.push(selector);
|
} else if (selector.length > 0 && selector[0].nodeType) {
|
// Array of elements or instance of Dom
|
for (i = 0; i < selector.length; i += 1) {
|
arr.push(selector[i]);
|
}
|
}
|
}
|
return new Dom7(arr);
|
}
|
|
$.fn = Dom7.prototype;
|
$.Class = Dom7;
|
$.Dom7 = Dom7;
|
|
export default $;
|