Added Aziani. Added URL origin parameter to relevant qu methods.

This commit is contained in:
2020-03-28 01:40:02 +01:00
parent 238ebcbf34
commit 95d115b585
29 changed files with 288 additions and 34 deletions

View File

@@ -34,11 +34,15 @@ function formatDate(dateValue, format, inputFormat) {
return moment(dateValue).format(format);
}
function prefixProtocol(urlValue, protocol = 'https') {
function prefixUrl(urlValue, origin, protocol = 'https') {
if (protocol && /^\/\//.test(urlValue)) {
return `${protocol}:${urlValue}`;
}
if (origin && /^\//.test(urlValue)) {
return `${origin}${urlValue}`;
}
return urlValue;
}
@@ -48,7 +52,7 @@ function q(context, selector, attrArg, applyTrim = true) {
if (attr) {
const value = selector
? context.querySelector(selector)?.[attr] || context.querySelector(selector)?.attributes[attr]?.value
: context[attr] || context[attr]?.attributes[attr]?.value;
: context[attr] || context.attributes[attr]?.value;
return applyTrim && value ? trim(value) : value;
}
@@ -60,7 +64,7 @@ function all(context, selector, attrArg, applyTrim = true) {
const attr = attrArg === true ? 'textContent' : attrArg;
if (attr) {
return Array.from(context.querySelectorAll(selector), el => (applyTrim && el[attr] ? trim(el[attr]) : el[attr]));
return Array.from(context.querySelectorAll(selector), el => q(el, null, attr, applyTrim));
}
return Array.from(context.querySelectorAll(selector));
@@ -112,47 +116,47 @@ function date(context, selector, format, match, attr = 'textContent') {
return extractDate(dateString, format, match);
}
function image(context, selector = 'img', attr = 'src', protocol = 'https') {
function image(context, selector = 'img', attr = 'src', origin, protocol = 'https') {
const imageEl = q(context, selector, attr);
// no attribute means q output will be HTML element
return attr ? prefixProtocol(imageEl, protocol) : imageEl;
return attr ? prefixUrl(imageEl, origin, protocol) : imageEl;
}
function images(context, selector = 'img', attr = 'src', protocol = 'https') {
function images(context, selector = 'img', attr = 'src', origin, protocol = 'https') {
const imageEls = all(context, selector, attr);
return attr ? imageEls.map(imageEl => prefixProtocol(imageEl, protocol)) : imageEls;
return attr ? imageEls.map(imageEl => prefixUrl(imageEl, origin, protocol)) : imageEls;
}
function url(context, selector = 'a', attr = 'href', protocol = 'https') {
function url(context, selector = 'a', attr = 'href', origin, protocol = 'https') {
const urlEl = q(context, selector, attr);
return attr ? prefixProtocol(urlEl, protocol) : urlEl;
return attr ? prefixUrl(urlEl, origin, protocol) : urlEl;
}
function urls(context, selector = 'a', attr = 'href', protocol = 'https') {
function urls(context, selector = 'a', attr = 'href', origin, protocol = 'https') {
const urlEls = all(context, selector, attr);
return attr ? urlEls.map(urlEl => prefixProtocol(urlEl, protocol)) : urlEls;
return attr ? urlEls.map(urlEl => prefixUrl(urlEl, origin, protocol)) : urlEls;
}
function poster(context, selector = 'video', attr = 'poster', protocol = 'https') {
function poster(context, selector = 'video', attr = 'poster', origin, protocol = 'https') {
const posterEl = q(context, selector, attr);
return attr ? prefixProtocol(posterEl, protocol) : posterEl;
return attr ? prefixUrl(posterEl, origin, protocol) : posterEl;
}
function video(context, selector = 'source', attr = 'src', protocol = 'https') {
function video(context, selector = 'source', attr = 'src', origin, protocol = 'https') {
const trailerEl = q(context, selector, attr);
return attr ? prefixProtocol(trailerEl, protocol) : trailerEl;
return attr ? prefixUrl(trailerEl, origin, protocol) : trailerEl;
}
function videos(context, selector = 'source', attr = 'src', protocol = 'https') {
function videos(context, selector = 'source', attr = 'src', origin, protocol = 'https') {
const trailerEls = all(context, selector, attr);
return attr ? trailerEls.map(trailerEl => prefixProtocol(trailerEl, protocol)) : trailerEls;
return attr ? trailerEls.map(trailerEl => prefixUrl(trailerEl, origin, protocol)) : trailerEls;
}
function duration(context, selector, match, attr = 'textContent') {