mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-24 20:14:37 -04:00
Update dom.
This commit is contained in:
parent
cfbae4c5d5
commit
6b3ba18da1
1 changed files with 19 additions and 34 deletions
|
@ -4,33 +4,24 @@ const {each, filter, hasLength, is, isStr, map, isInstanceOf, toArray} = require
|
||||||
const doc = win.document;
|
const doc = win.document;
|
||||||
|
|
||||||
const createElement = name => doc.createElement(name);
|
const createElement = name => doc.createElement(name);
|
||||||
const CONTAINER_DIV = createElement('div');
|
|
||||||
const CONTAINER_TABLE = createElement('table');
|
|
||||||
const CONTAINER_TBODY = createElement('tbody');
|
|
||||||
const CONTAINER_TR = createElement('tr');
|
|
||||||
const CONTAINER_COLGROUP = createElement('colgroup');
|
|
||||||
|
|
||||||
const publish = (obj, arr) => {
|
const reReady = /^(i|c|loade)/;
|
||||||
each(arr, (el, idx) => {
|
|
||||||
obj[idx] = el;
|
const containers = [
|
||||||
});
|
{re: /^<t(head|body|foot)|^<c(ap|olg)/i, el: createElement('table')},
|
||||||
obj.length = arr.length;
|
{re: /^<col/i, el: createElement('colgroup')},
|
||||||
};
|
{re: /^<tr/i, el: createElement('tbody')},
|
||||||
|
{re: /^<t[dh]/i, el: createElement('tr')}
|
||||||
|
];
|
||||||
|
const containerDiv = createElement('div');
|
||||||
|
|
||||||
const findContainer = str => {
|
const findContainer = str => {
|
||||||
if (/^<t(head|body|foot)|^<c(ap|olg)/i.test(str)) {
|
for (const {re, el} of containers) {
|
||||||
return CONTAINER_TABLE;
|
if (re.test(str)) {
|
||||||
|
return el;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (/^<col/i.test(str)) {
|
return containerDiv;
|
||||||
return CONTAINER_COLGROUP;
|
|
||||||
}
|
|
||||||
if (/^<tr/i.test(str)) {
|
|
||||||
return CONTAINER_TBODY;
|
|
||||||
}
|
|
||||||
if (/^<t[dh]/i.test(str)) {
|
|
||||||
return CONTAINER_TR;
|
|
||||||
}
|
|
||||||
return CONTAINER_DIV;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const parseHtml = str => {
|
const parseHtml = str => {
|
||||||
|
@ -42,9 +33,9 @@ const parseHtml = str => {
|
||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
const queryAll = (selector, context) => {
|
const queryAll = (selector, context = doc) => {
|
||||||
try {
|
try {
|
||||||
return toArray((context || doc).querySelectorAll(selector));
|
return toArray(context.querySelectorAll(selector));
|
||||||
} catch (err) {/* ignore */}
|
} catch (err) {/* ignore */}
|
||||||
return [];
|
return [];
|
||||||
};
|
};
|
||||||
|
@ -58,7 +49,7 @@ const addListener = (el, type, fn) => el.addEventListener(type, fn);
|
||||||
const removeListener = (el, type, fn) => el.removeEventListener(type, fn);
|
const removeListener = (el, type, fn) => el.removeEventListener(type, fn);
|
||||||
|
|
||||||
const onReady = fn => {
|
const onReady = fn => {
|
||||||
if (/^(i|c|loade)/.test(doc.readyState)) {
|
if (reReady.test(doc.readyState)) {
|
||||||
fn();
|
fn();
|
||||||
} else {
|
} else {
|
||||||
addListener(doc, 'DOMContentLoaded', fn);
|
addListener(doc, 'DOMContentLoaded', fn);
|
||||||
|
@ -97,9 +88,7 @@ const dom = arg => {
|
||||||
}
|
}
|
||||||
els = filter(els, isElDocWin);
|
els = filter(els, isElDocWin);
|
||||||
|
|
||||||
const inst = Object.create(dom.prototype);
|
return Object.assign(Object.create(dom.prototype), els, {length: els.length});
|
||||||
publish(inst, els);
|
|
||||||
return inst;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
dom.prototype = {
|
dom.prototype = {
|
||||||
|
@ -115,11 +104,7 @@ dom.prototype = {
|
||||||
},
|
},
|
||||||
|
|
||||||
find(selector) {
|
find(selector) {
|
||||||
let els = [];
|
return dom([].concat(...this.map(el => queryAll(selector, el))));
|
||||||
this.each(el => {
|
|
||||||
els = els.concat(queryAll(selector, el));
|
|
||||||
});
|
|
||||||
return dom(els);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
on(type, fn) {
|
on(type, fn) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue