0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-19 11:52:21 +01:00
Bootstrap/js/dist/dom/manipulator.js

73 lines
2.4 KiB
JavaScript
Raw Normal View History

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