| "use strict"; |
| |
| |
| |
| |
| |
| Object.defineProperty(exports, "__esModule", { value: true }); |
| exports.attr = attr; |
| exports.prop = prop; |
| exports.data = data; |
| exports.val = val; |
| exports.removeAttr = removeAttr; |
| exports.hasClass = hasClass; |
| exports.addClass = addClass; |
| exports.removeClass = removeClass; |
| exports.toggleClass = toggleClass; |
| const static_js_1 = require("../static.js"); |
| const utils_js_1 = require("../utils.js"); |
| const domhandler_1 = require("domhandler"); |
| const domutils_1 = require("domutils"); |
| const hasOwn = Object.prototype.hasOwnProperty; |
| const rspace = /\s+/; |
| const dataAttrPrefix = 'data-'; |
| |
| const rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i; |
| |
| const rbrace = /^{[^]*}$|^\[[^]*]$/; |
| function getAttr(elem, name, xmlMode) { |
| var _a; |
| if (!elem || !(0, domhandler_1.isTag)(elem)) |
| return undefined; |
| (_a = elem.attribs) !== null && _a !== void 0 ? _a : (elem.attribs = {}); |
| |
| if (!name) { |
| return elem.attribs; |
| } |
| if (hasOwn.call(elem.attribs, name)) { |
| |
| return !xmlMode && rboolean.test(name) ? name : elem.attribs[name]; |
| } |
| |
| if (elem.name === 'option' && name === 'value') { |
| return (0, static_js_1.text)(elem.children); |
| } |
| |
| if (elem.name === 'input' && |
| (elem.attribs['type'] === 'radio' || elem.attribs['type'] === 'checkbox') && |
| name === 'value') { |
| return 'on'; |
| } |
| return undefined; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function setAttr(el, name, value) { |
| if (value === null) { |
| removeAttribute(el, name); |
| } |
| else { |
| el.attribs[name] = `${value}`; |
| } |
| } |
| function attr(name, value) { |
| |
| if (typeof name === 'object' || value !== undefined) { |
| if (typeof value === 'function') { |
| if (typeof name !== 'string') { |
| { |
| throw new Error('Bad combination of arguments.'); |
| } |
| } |
| return (0, utils_js_1.domEach)(this, (el, i) => { |
| if ((0, domhandler_1.isTag)(el)) |
| setAttr(el, name, value.call(el, i, el.attribs[name])); |
| }); |
| } |
| return (0, utils_js_1.domEach)(this, (el) => { |
| if (!(0, domhandler_1.isTag)(el)) |
| return; |
| if (typeof name === 'object') { |
| for (const objName of Object.keys(name)) { |
| const objValue = name[objName]; |
| setAttr(el, objName, objValue); |
| } |
| } |
| else { |
| setAttr(el, name, value); |
| } |
| }); |
| } |
| return arguments.length > 1 |
| ? this |
| : getAttr(this[0], name, this.options.xmlMode); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function getProp(el, name, xmlMode) { |
| return name in el |
| ? |
| el[name] |
| : !xmlMode && rboolean.test(name) |
| ? getAttr(el, name, false) !== undefined |
| : getAttr(el, name, xmlMode); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function setProp(el, name, value, xmlMode) { |
| if (name in el) { |
| |
| el[name] = value; |
| } |
| else { |
| setAttr(el, name, !xmlMode && rboolean.test(name) ? (value ? '' : null) : `${value}`); |
| } |
| } |
| function prop(name, value) { |
| var _a; |
| if (typeof name === 'string' && value === undefined) { |
| const el = this[0]; |
| if (!el || !(0, domhandler_1.isTag)(el)) |
| return undefined; |
| switch (name) { |
| case 'style': { |
| const property = this.css(); |
| const keys = Object.keys(property); |
| for (let i = 0; i < keys.length; i++) { |
| property[i] = keys[i]; |
| } |
| property.length = keys.length; |
| return property; |
| } |
| case 'tagName': |
| case 'nodeName': { |
| return el.name.toUpperCase(); |
| } |
| case 'href': |
| case 'src': { |
| const prop = (_a = el.attribs) === null || _a === void 0 ? void 0 : _a[name]; |
| if (typeof URL !== 'undefined' && |
| ((name === 'href' && (el.tagName === 'a' || el.tagName === 'link')) || |
| (name === 'src' && |
| (el.tagName === 'img' || |
| el.tagName === 'iframe' || |
| el.tagName === 'audio' || |
| el.tagName === 'video' || |
| el.tagName === 'source'))) && |
| prop !== undefined && |
| this.options.baseURI) { |
| return new URL(prop, this.options.baseURI).href; |
| } |
| return prop; |
| } |
| case 'innerText': { |
| return (0, domutils_1.innerText)(el); |
| } |
| case 'textContent': { |
| return (0, domutils_1.textContent)(el); |
| } |
| case 'outerHTML': { |
| return this.clone().wrap('<container />').parent().html(); |
| } |
| case 'innerHTML': { |
| return this.html(); |
| } |
| default: { |
| return getProp(el, name, this.options.xmlMode); |
| } |
| } |
| } |
| if (typeof name === 'object' || value !== undefined) { |
| if (typeof value === 'function') { |
| if (typeof name === 'object') { |
| throw new TypeError('Bad combination of arguments.'); |
| } |
| return (0, utils_js_1.domEach)(this, (el, i) => { |
| if ((0, domhandler_1.isTag)(el)) { |
| setProp(el, name, value.call(el, i, getProp(el, name, this.options.xmlMode)), this.options.xmlMode); |
| } |
| }); |
| } |
| return (0, utils_js_1.domEach)(this, (el) => { |
| if (!(0, domhandler_1.isTag)(el)) |
| return; |
| if (typeof name === 'object') { |
| for (const key of Object.keys(name)) { |
| const val = name[key]; |
| setProp(el, key, val, this.options.xmlMode); |
| } |
| } |
| else { |
| setProp(el, name, value, this.options.xmlMode); |
| } |
| }); |
| } |
| return undefined; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function setData(elem, name, value) { |
| var _a; |
| (_a = elem.data) !== null && _a !== void 0 ? _a : (elem.data = {}); |
| if (typeof name === 'object') |
| Object.assign(elem.data, name); |
| else if (typeof name === 'string' && value !== undefined) { |
| elem.data[name] = value; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function readAllData(el) { |
| for (const domName of Object.keys(el.attribs)) { |
| if (!domName.startsWith(dataAttrPrefix)) { |
| continue; |
| } |
| const jsName = (0, utils_js_1.camelCase)(domName.slice(dataAttrPrefix.length)); |
| if (!hasOwn.call(el.data, jsName)) { |
| el.data[jsName] = parseDataValue(el.attribs[domName]); |
| } |
| } |
| return el.data; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function readData(el, name) { |
| const domName = dataAttrPrefix + (0, utils_js_1.cssCase)(name); |
| const data = el.data; |
| if (hasOwn.call(data, name)) { |
| return data[name]; |
| } |
| if (hasOwn.call(el.attribs, domName)) { |
| return (data[name] = parseDataValue(el.attribs[domName])); |
| } |
| return undefined; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function parseDataValue(value) { |
| if (value === 'null') |
| return null; |
| if (value === 'true') |
| return true; |
| if (value === 'false') |
| return false; |
| const num = Number(value); |
| if (value === String(num)) |
| return num; |
| if (rbrace.test(value)) { |
| try { |
| return JSON.parse(value); |
| } |
| catch { |
| |
| } |
| } |
| return value; |
| } |
| function data(name, value) { |
| var _a; |
| const elem = this[0]; |
| if (!elem || !(0, domhandler_1.isTag)(elem)) |
| return; |
| const dataEl = elem; |
| (_a = dataEl.data) !== null && _a !== void 0 ? _a : (dataEl.data = {}); |
| |
| if (name == null) { |
| return readAllData(dataEl); |
| } |
| |
| if (typeof name === 'object' || value !== undefined) { |
| (0, utils_js_1.domEach)(this, (el) => { |
| if ((0, domhandler_1.isTag)(el)) { |
| if (typeof name === 'object') |
| setData(el, name); |
| else |
| setData(el, name, value); |
| } |
| }); |
| return this; |
| } |
| return readData(dataEl, name); |
| } |
| function val(value) { |
| const querying = arguments.length === 0; |
| const element = this[0]; |
| if (!element || !(0, domhandler_1.isTag)(element)) |
| return querying ? undefined : this; |
| switch (element.name) { |
| case 'textarea': { |
| return this.text(value); |
| } |
| case 'select': { |
| const option = this.find('option:selected'); |
| if (!querying) { |
| if (this.attr('multiple') == null && typeof value === 'object') { |
| return this; |
| } |
| this.find('option').removeAttr('selected'); |
| const values = typeof value === 'object' ? value : [value]; |
| for (const val of values) { |
| this.find(`option[value="${val}"]`).attr('selected', ''); |
| } |
| return this; |
| } |
| return this.attr('multiple') |
| ? option.toArray().map((el) => (0, static_js_1.text)(el.children)) |
| : option.attr('value'); |
| } |
| case 'input': |
| case 'option': { |
| return querying |
| ? this.attr('value') |
| : this.attr('value', value); |
| } |
| } |
| return undefined; |
| } |
| |
| |
| |
| |
| |
| |
| |
| function removeAttribute(elem, name) { |
| if (!elem.attribs || !hasOwn.call(elem.attribs, name)) |
| return; |
| delete elem.attribs[name]; |
| } |
| |
| |
| |
| |
| |
| |
| |
| function splitNames(names) { |
| return names ? names.trim().split(rspace) : []; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function removeAttr(name) { |
| const attrNames = splitNames(name); |
| for (const attrName of attrNames) { |
| (0, utils_js_1.domEach)(this, (elem) => { |
| if ((0, domhandler_1.isTag)(elem)) |
| removeAttribute(elem, attrName); |
| }); |
| } |
| return this; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function hasClass(className) { |
| return this.toArray().some((elem) => { |
| const clazz = (0, domhandler_1.isTag)(elem) && elem.attribs['class']; |
| let idx = -1; |
| if (clazz && className.length > 0) { |
| while ((idx = clazz.indexOf(className, idx + 1)) > -1) { |
| const end = idx + className.length; |
| if ((idx === 0 || rspace.test(clazz[idx - 1])) && |
| (end === clazz.length || rspace.test(clazz[end]))) { |
| return true; |
| } |
| } |
| } |
| return false; |
| }); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function addClass(value) { |
| |
| if (typeof value === 'function') { |
| return (0, utils_js_1.domEach)(this, (el, i) => { |
| if ((0, domhandler_1.isTag)(el)) { |
| const className = el.attribs['class'] || ''; |
| addClass.call([el], value.call(el, i, className)); |
| } |
| }); |
| } |
| |
| if (!value || typeof value !== 'string') |
| return this; |
| const classNames = value.split(rspace); |
| const numElements = this.length; |
| for (let i = 0; i < numElements; i++) { |
| const el = this[i]; |
| |
| if (!(0, domhandler_1.isTag)(el)) |
| continue; |
| |
| const className = getAttr(el, 'class', false); |
| if (className) { |
| let setClass = ` ${className} `; |
| |
| for (const cn of classNames) { |
| const appendClass = `${cn} `; |
| if (!setClass.includes(` ${appendClass}`)) |
| setClass += appendClass; |
| } |
| setAttr(el, 'class', setClass.trim()); |
| } |
| else { |
| setAttr(el, 'class', classNames.join(' ').trim()); |
| } |
| } |
| return this; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function removeClass(name) { |
| |
| if (typeof name === 'function') { |
| return (0, utils_js_1.domEach)(this, (el, i) => { |
| if ((0, domhandler_1.isTag)(el)) { |
| removeClass.call([el], name.call(el, i, el.attribs['class'] || '')); |
| } |
| }); |
| } |
| const classes = splitNames(name); |
| const numClasses = classes.length; |
| const removeAll = arguments.length === 0; |
| return (0, utils_js_1.domEach)(this, (el) => { |
| if (!(0, domhandler_1.isTag)(el)) |
| return; |
| if (removeAll) { |
| |
| el.attribs['class'] = ''; |
| } |
| else { |
| const elClasses = splitNames(el.attribs['class']); |
| let changed = false; |
| for (let j = 0; j < numClasses; j++) { |
| const index = elClasses.indexOf(classes[j]); |
| if (index >= 0) { |
| elClasses.splice(index, 1); |
| changed = true; |
| |
| |
| |
| |
| j--; |
| } |
| } |
| if (changed) { |
| el.attribs['class'] = elClasses.join(' '); |
| } |
| } |
| }); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function toggleClass(value, stateVal) { |
| |
| if (typeof value === 'function') { |
| return (0, utils_js_1.domEach)(this, (el, i) => { |
| if ((0, domhandler_1.isTag)(el)) { |
| toggleClass.call([el], value.call(el, i, el.attribs['class'] || '', stateVal), stateVal); |
| } |
| }); |
| } |
| |
| if (!value || typeof value !== 'string') |
| return this; |
| const classNames = value.split(rspace); |
| const numClasses = classNames.length; |
| const state = typeof stateVal === 'boolean' ? (stateVal ? 1 : -1) : 0; |
| const numElements = this.length; |
| for (let i = 0; i < numElements; i++) { |
| const el = this[i]; |
| |
| if (!(0, domhandler_1.isTag)(el)) |
| continue; |
| const elementClasses = splitNames(el.attribs['class']); |
| |
| for (let j = 0; j < numClasses; j++) { |
| |
| const index = elementClasses.indexOf(classNames[j]); |
| |
| if (state >= 0 && index < 0) { |
| elementClasses.push(classNames[j]); |
| } |
| else if (state <= 0 && index >= 0) { |
| |
| elementClasses.splice(index, 1); |
| } |
| } |
| el.attribs['class'] = elementClasses.join(' '); |
| } |
| return this; |
| } |
| |