bootstrap/js/dist/dom/manipulator.js

73 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-03-02 00:31:34 +08:00
/*!
2025-04-24 03:00:20 +08:00
* Bootstrap manipulator.js v5.3.6 (https://getbootstrap.com/)
* Copyright 2011-2025 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2020-06-17 02:50:01 +08:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2019-03-02 00:31:34 +08:00
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
2020-09-14 23:12:06 +08:00
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Manipulator = factory());
2021-10-05 23:50:18 +08:00
})(this, (function () { 'use strict';
2019-03-02 00:31:34 +08:00
/**
* --------------------------------------------------------------------------
* Bootstrap dom/manipulator.js
2020-06-17 02:50:01 +08:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2019-03-02 00:31:34 +08:00
* --------------------------------------------------------------------------
*/
2022-05-13 14:07:23 +08:00
function normalizeData(value) {
if (value === 'true') {
2019-03-02 00:31:34 +08:00
return true;
}
2022-05-13 14:07:23 +08:00
if (value === 'false') {
2019-03-02 00:31:34 +08:00
return false;
}
2022-05-13 14:07:23 +08:00
if (value === Number(value).toString()) {
return Number(value);
2019-03-02 00:31:34 +08:00
}
2022-05-13 14:07:23 +08:00
if (value === '' || value === 'null') {
2019-03-02 00:31:34 +08:00
return null;
}
2022-05-13 14:07:23 +08:00
if (typeof value !== 'string') {
return value;
}
try {
return JSON.parse(decodeURIComponent(value));
} catch (_unused) {
return value;
}
2019-03-02 00:31:34 +08:00
}
function normalizeDataKey(key) {
2021-03-24 00:26:54 +08:00
return key.replace(/[A-Z]/g, chr => `-${chr.toLowerCase()}`);
2019-03-02 00:31:34 +08:00
}
2021-03-24 00:26:54 +08:00
const Manipulator = {
setDataAttribute(element, key, value) {
element.setAttribute(`data-bs-${normalizeDataKey(key)}`, value);
2019-03-02 00:31:34 +08:00
},
2021-03-24 00:26:54 +08:00
removeDataAttribute(element, key) {
element.removeAttribute(`data-bs-${normalizeDataKey(key)}`);
2019-03-02 00:31:34 +08:00
},
2021-03-24 00:26:54 +08:00
getDataAttributes(element) {
2019-03-02 00:31:34 +08:00
if (!element) {
return {};
}
2021-03-24 00:26:54 +08:00
const attributes = {};
2022-05-13 14:07:23 +08:00
const bsKeys = Object.keys(element.dataset).filter(key => key.startsWith('bs') && !key.startsWith('bsConfig'));
for (const key of bsKeys) {
2021-03-24 00:26:54 +08:00
let pureKey = key.replace(/^bs/, '');
pureKey = pureKey.charAt(0).toLowerCase() + pureKey.slice(1);
2020-11-23 21:17:16 +08:00
attributes[pureKey] = normalizeData(element.dataset[key]);
2022-05-13 14:07:23 +08:00
}
2019-03-02 00:31:34 +08:00
return attributes;
},
2021-03-24 00:26:54 +08:00
getDataAttribute(element, key) {
return normalizeData(element.getAttribute(`data-bs-${normalizeDataKey(key)}`));
2019-03-02 00:31:34 +08:00
}
};
return Manipulator;
2021-10-05 23:50:18 +08:00
}));
2019-03-02 00:31:34 +08:00
//# sourceMappingURL=manipulator.js.map