|
function n2blHQ() { |
|
|
|
// TODO: configuration (restrict to init?) |
|
// TODO: error strategy |
|
// TODO: third cool single letter function |
|
// TODO: addEventListener wrap, maybe? |
|
|
|
// #region NoneOneAll |
|
/** |
|
* Normalize a value into either null, |
|
an array, or a single thing, |
|
based on whether it coalesces to |
|
empty, many, or something else. |
|
* @param {T} predicate - Well this |
|
could be of a massive plethora |
|
of types. |
|
* @returns {null|T|T[]} - This may |
|
still be one in a (countable) |
|
infinite pool of types but now |
|
it's known that it may ALSO be |
|
null, or ALSO be a list of any |
|
combination of that |
|
aforementioned variety of things. |
|
*/ |
|
const NoneOneAll = (predicate) => ( |
|
( |
|
[ |
|
() => null, |
|
predicate => predicate[0], |
|
predicate => Array.from(predicate) |
|
][ |
|
( |
|
(len) => ( +!!len ) + ( len > 1 ) |
|
)(predicate?.length) |
|
] |
|
)(predicate) |
|
); |
|
// #endregion NoneOneAll |
|
// =================================================== |
|
|
|
// #region Definitions |
|
|
|
let defaultRootElm = []; |
|
try { |
|
const doc = document; |
|
if (!doc) { throw new Error("[H()+Q()]: Global variable Document (<html />) is not accessible"); } |
|
const body = document.body; |
|
if (!doc) { throw new Error("[H()+Q()]: Global reference to Body (<body />) is not accessible"); } |
|
defaultRootElm = [body]; |
|
} catch(err) { |
|
try { console.error(err); } |
|
catch (e2) { /* cannot console log, so end program in confusion */ } |
|
} |
|
|
|
const H_MIME_TYPES = Object.freeze({ |
|
HTML: "text/html", |
|
XML_OLD: "text/xml", |
|
XML: "application/xml", |
|
XHTML: "application/xhtml+xml", |
|
XML_SVG: "image/svg+xml", |
|
}); |
|
|
|
// #endregion Definitions |
|
// =================================================== |
|
|
|
// #region HTML_Generate |
|
|
|
/** |
|
* Parse an string into a DOM element tree. |
|
Returns either a single HMTLElement, |
|
or an array of HTMLElements, |
|
or null, |
|
depending on the input string. |
|
* H() will only return the HTMLElements, |
|
only what `body.children` returns, |
|
it doesn't parse stuff like text nodes. |
|
* If you really want these, wrap them in a single root |
|
element in the string, and access via `childNodes`, |
|
for example: H(" RE text1 text2 /RE ").childNodes, or |
|
`H("<div> my <br /> text <br /> nodes <br /> nice </div>").childNodes` |
|
* |
|
* @example |
|
* // returns {HTMLDivElement} |
|
* H("<div></div>"); |
|
* |
|
* @example |
|
* // returns {[HTMLSpanElement, HTMLAnchorElement]} |
|
* H(<span class="highlight-arrow" >→</span>"<a href="https://nibal.art">My Website</a>); |
|
* |
|
* @example |
|
* // returns {null} |
|
* H("<i am='your father! />") |
|
* |
|
* @example |
|
* // returns {HTMLUnknownElement} |
|
* H("i <am> your father!") |
|
* |
|
* @param {string} str - An document object tree description |
|
* @param {string=} [mime="text/html"] - How to parse this string. |
|
The default, "text/html" is loose, |
|
while all others are pretty much |
|
the same, but stricter. |
|
* @returns {(null|Document|Document[])} |
|
*/ |
|
function HTML_Generate(str, mime = H_MIME_TYPES.HTML) { |
|
// const H = (str, mime = H_MIME_TYPES.HTML) => { |
|
let validMime = mime; |
|
if (!mime || !Object.values(H_MIME_TYPES).includes(mime)) { |
|
validMime = H_MIME_TYPES.HTML; |
|
} |
|
const parsedRoot = new DOMParser().parseFromString(str, validMime); |
|
try { |
|
return NoneOneAll(parsedRoot.body.children); |
|
} catch (err) { |
|
console.error("[H()+Q()]: Error on H() at NoneOneAll"); |
|
console.error(err); |
|
return null; |
|
} |
|
return parsedRoot; |
|
} |
|
|
|
// #endregion HTML_Generate |
|
// =================================================== |
|
|
|
// #region QueryElms2 |
|
|
|
/** |
|
* Find elements from a (one or many) given DOM tree root(s) (by default document.body), |
|
and a given selector string (see "CSS selector"), returning null |
|
if nothing was found, one if found exactly one match, or an array |
|
if there was two or more matches. |
|
This function will try to instantiate `document.body` on it's first |
|
run, to use as the default root element. If you're running this |
|
code in an non-browser environment, it will default to `[]`, which |
|
means it will never find a match unless supplied with a proper root. |
|
* |
|
* @param {string} query - The selector string that finds the desired element(s). |
|
* @param {HTMLElement=|HTMLElement[]=} [rootElm=[document.body]] - from which point in |
|
the document model structure to begin the search from. |
|
Defaults to <body /> if your js environment has that, |
|
otherwise defaults to []. |
|
* @returns {null|HTMLElement|HTMLElement[]} |
|
*/ |
|
function QueryElementsInElement(query, rootElm = defaultRootElm) { |
|
return NoneOneAll( |
|
// const Q = (query, rootElm = defaultRootElm) => NoneOneAll( |
|
(Array.isArray(rootElm) |
|
? rootElm |
|
: [rootElm] |
|
).map( |
|
relm => (!relm || typeof relm.querySelectorAll !== "function") |
|
? null |
|
: NoneOneAll( relm.querySelectorAll(query) ) |
|
).flat().filter(Boolean) |
|
) |
|
} |
|
// #endregion QueryElms2 |
|
// =================================================== |
|
|
|
// #region Return |
|
|
|
return { |
|
Q: QueryElementsInElement, |
|
H: HTML_Generate, |
|
H_MIME_TYPES |
|
} |
|
|
|
// #endregion Return |
|
} |