0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-01 13:24:25 +01:00

Get only bs prefixed data attributes

This commit is contained in:
Rohit Sharma 2020-11-11 12:07:04 +05:30 committed by XhmikosR
parent a7f04e9595
commit e530118117
2 changed files with 5 additions and 23 deletions

View File

@ -43,16 +43,14 @@ const Manipulator = {
return {} return {}
} }
const attributes = { const attributes = {}
...element.dataset
}
Object.keys(attributes) Object.keys(element.dataset)
.filter(key => key.startsWith('bs')) .filter(key => key.startsWith('bs'))
.forEach(key => { .forEach(key => {
let pureKey = key.replace(/^bs/, '') let pureKey = key.replace(/^bs/, '')
pureKey = pureKey.charAt(0).toLowerCase() + pureKey.slice(1, pureKey.length) pureKey = pureKey.charAt(0).toLowerCase() + pureKey.slice(1, pureKey.length)
attributes[pureKey] = normalizeData(attributes[key]) attributes[pureKey] = normalizeData(element.dataset[key])
}) })
return attributes return attributes

View File

@ -60,32 +60,16 @@ describe('Manipulator', () => {
expect().nothing() expect().nothing()
}) })
it('should get all data attributes, without bs prefixed as well', () => { it('should get only bs prefixed data attributes without bs namespace', () => {
fixtureEl.innerHTML = '<div data-bs-toggle="tabs" data-bs-target="#element" data-another="value"></div>' fixtureEl.innerHTML = '<div data-bs-toggle="tabs" data-bs-target="#element" data-another="value" data-target-bs="#element" data-in-bs-out="in-between"></div>'
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
expect(Manipulator.getDataAttributes(div)).toEqual({ expect(Manipulator.getDataAttributes(div)).toEqual({
bsToggle: 'tabs',
bsTarget: '#element',
another: 'value',
toggle: 'tabs', toggle: 'tabs',
target: '#element' target: '#element'
}) })
}) })
it('should remove just prefixed bs keyword from the attributes and override original attribute with bs prefixed', () => {
fixtureEl.innerHTML = '<div data-bs-toggle="tabs" data-toggle="override" data-target-bs="#element" data-in-bs-out="in-between"></div>'
const div = fixtureEl.querySelector('div')
expect(Manipulator.getDataAttributes(div)).toEqual({
bsToggle: 'tabs',
targetBs: '#element',
inBsOut: 'in-between',
toggle: 'tabs'
})
})
}) })
describe('getDataAttribute', () => { describe('getDataAttribute', () => {