0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-12 00:08:59 +01:00
Bootstrap/js/dist/dom/manipulator.js

86 lines
2.4 KiB
JavaScript
Raw Normal View History

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