1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-08-08 22:56:46 +02:00

Manipulator: Add JSON parse support (#35077)

Support parsing JSON from each component's main element using the `data-bs-config` attribute.

The `bs-config` attribute will be reserved and omitted during `getDataAttributes` parsing.

With this commit, every component, will create its config object, using:

* defaults
* data-bs-config
* the rest of data attributes
* configuration object given during instance initialization

Co-authored-by: XhmikosR <xhmikosr@gmail.com>
Co-authored-by: Mark Otto <markd.otto@gmail.com>
Co-authored-by: Mark Otto <markdotto@gmail.com>
This commit is contained in:
GeoSot
2022-04-21 21:41:43 +03:00
committed by GitHub
parent 01cffa6822
commit 584600bda3
16 changed files with 163 additions and 18 deletions

View File

@@ -22,7 +22,15 @@ function normalizeData(value) {
return null
}
return value
if (typeof value !== 'string') {
return value
}
try {
return JSON.parse(decodeURIComponent(value))
} catch {
return value
}
}
function normalizeDataKey(key) {
@@ -44,7 +52,7 @@ const Manipulator = {
}
const attributes = {}
const bsKeys = Object.keys(element.dataset).filter(key => key.startsWith('bs'))
const bsKeys = Object.keys(element.dataset).filter(key => key.startsWith('bs') && !key.startsWith('bsConfig'))
for (const key of bsKeys) {
let pureKey = key.replace(/^bs/, '')

View File

@@ -38,8 +38,11 @@ class Config {
}
_mergeConfigObj(config, element) {
const jsonConfig = isElement(element) ? Manipulator.getDataAttribute(element, 'config') : {} // try to parse
return {
...this.constructor.Default,
...(typeof jsonConfig === 'object' ? jsonConfig : {}),
...(isElement(element) ? Manipulator.getDataAttributes(element) : {}),
...(typeof config === 'object' ? config : {})
}