Unverified Commit 2630b05e authored by Rohit Sharma's avatar Rohit Sharma Committed by GitHub
Browse files

Make the attribute methods bs specific (#32173)


Co-authored-by: default avatarXhmikosR <xhmikosr@gmail.com>
parent a96b118f
Showing with 34 additions and 30 deletions
+34 -30
...@@ -31,11 +31,11 @@ function normalizeDataKey(key) { ...@@ -31,11 +31,11 @@ function normalizeDataKey(key) {
const Manipulator = { const Manipulator = {
setDataAttribute(element, key, value) { setDataAttribute(element, key, value) {
element.setAttribute(`data-${normalizeDataKey(key)}`, value) element.setAttribute(`data-bs-${normalizeDataKey(key)}`, value)
}, },
removeDataAttribute(element, key) { removeDataAttribute(element, key) {
element.removeAttribute(`data-${normalizeDataKey(key)}`) element.removeAttribute(`data-bs-${normalizeDataKey(key)}`)
}, },
getDataAttributes(element) { getDataAttributes(element) {
...@@ -57,7 +57,7 @@ const Manipulator = { ...@@ -57,7 +57,7 @@ const Manipulator = {
}, },
getDataAttribute(element, key) { getDataAttribute(element, key) {
return normalizeData(element.getAttribute(`data-${normalizeDataKey(key)}`)) return normalizeData(element.getAttribute(`data-bs-${normalizeDataKey(key)}`))
}, },
offset(element) { offset(element) {
......
...@@ -473,7 +473,7 @@ class Modal { ...@@ -473,7 +473,7 @@ class Modal {
.forEach(element => { .forEach(element => {
const actualPadding = element.style.paddingRight const actualPadding = element.style.paddingRight
const calculatedPadding = window.getComputedStyle(element)['padding-right'] const calculatedPadding = window.getComputedStyle(element)['padding-right']
Manipulator.setDataAttribute(element, 'bs-padding-right', actualPadding) Manipulator.setDataAttribute(element, 'padding-right', actualPadding)
element.style.paddingRight = `${Number.parseFloat(calculatedPadding) + this._scrollbarWidth}px` element.style.paddingRight = `${Number.parseFloat(calculatedPadding) + this._scrollbarWidth}px`
}) })
...@@ -482,7 +482,7 @@ class Modal { ...@@ -482,7 +482,7 @@ class Modal {
.forEach(element => { .forEach(element => {
const actualMargin = element.style.marginRight const actualMargin = element.style.marginRight
const calculatedMargin = window.getComputedStyle(element)['margin-right'] const calculatedMargin = window.getComputedStyle(element)['margin-right']
Manipulator.setDataAttribute(element, 'bs-margin-right', actualMargin) Manipulator.setDataAttribute(element, 'margin-right', actualMargin)
element.style.marginRight = `${Number.parseFloat(calculatedMargin) - this._scrollbarWidth}px` element.style.marginRight = `${Number.parseFloat(calculatedMargin) - this._scrollbarWidth}px`
}) })
...@@ -490,7 +490,7 @@ class Modal { ...@@ -490,7 +490,7 @@ class Modal {
const actualPadding = document.body.style.paddingRight const actualPadding = document.body.style.paddingRight
const calculatedPadding = window.getComputedStyle(document.body)['padding-right'] const calculatedPadding = window.getComputedStyle(document.body)['padding-right']
Manipulator.setDataAttribute(document.body, 'bs-padding-right', actualPadding) Manipulator.setDataAttribute(document.body, 'padding-right', actualPadding)
document.body.style.paddingRight = `${Number.parseFloat(calculatedPadding) + this._scrollbarWidth}px` document.body.style.paddingRight = `${Number.parseFloat(calculatedPadding) + this._scrollbarWidth}px`
} }
...@@ -501,9 +501,9 @@ class Modal { ...@@ -501,9 +501,9 @@ class Modal {
// Restore fixed content padding // Restore fixed content padding
SelectorEngine.find(SELECTOR_FIXED_CONTENT) SelectorEngine.find(SELECTOR_FIXED_CONTENT)
.forEach(element => { .forEach(element => {
const padding = Manipulator.getDataAttribute(element, 'bs-padding-right') const padding = Manipulator.getDataAttribute(element, 'padding-right')
if (typeof padding !== 'undefined') { if (typeof padding !== 'undefined') {
Manipulator.removeDataAttribute(element, 'bs-padding-right') Manipulator.removeDataAttribute(element, 'padding-right')
element.style.paddingRight = padding element.style.paddingRight = padding
} }
}) })
...@@ -511,19 +511,19 @@ class Modal { ...@@ -511,19 +511,19 @@ class Modal {
// Restore sticky content and navbar-toggler margin // Restore sticky content and navbar-toggler margin
SelectorEngine.find(`${SELECTOR_STICKY_CONTENT}`) SelectorEngine.find(`${SELECTOR_STICKY_CONTENT}`)
.forEach(element => { .forEach(element => {
const margin = Manipulator.getDataAttribute(element, 'bs-margin-right') const margin = Manipulator.getDataAttribute(element, 'margin-right')
if (typeof margin !== 'undefined') { if (typeof margin !== 'undefined') {
Manipulator.removeDataAttribute(element, 'bs-margin-right') Manipulator.removeDataAttribute(element, 'margin-right')
element.style.marginRight = margin element.style.marginRight = margin
} }
}) })
// Restore body padding // Restore body padding
const padding = Manipulator.getDataAttribute(document.body, 'bs-padding-right') const padding = Manipulator.getDataAttribute(document.body, 'padding-right')
if (typeof padding === 'undefined') { if (typeof padding === 'undefined') {
document.body.style.paddingRight = '' document.body.style.paddingRight = ''
} else { } else {
Manipulator.removeDataAttribute(document.body, 'bs-padding-right') Manipulator.removeDataAttribute(document.body, 'padding-right')
document.body.style.paddingRight = padding document.body.style.paddingRight = padding
} }
} }
......
...@@ -15,13 +15,13 @@ describe('Manipulator', () => { ...@@ -15,13 +15,13 @@ describe('Manipulator', () => {
}) })
describe('setDataAttribute', () => { describe('setDataAttribute', () => {
it('should set data attribute', () => { it('should set data attribute prefixed with bs', () => {
fixtureEl.innerHTML = '<div></div>' fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
Manipulator.setDataAttribute(div, 'key', 'value') Manipulator.setDataAttribute(div, 'key', 'value')
expect(div.getAttribute('data-key')).toEqual('value') expect(div.getAttribute('data-bs-key')).toEqual('value')
}) })
it('should set data attribute in kebab case', () => { it('should set data attribute in kebab case', () => {
...@@ -30,37 +30,39 @@ describe('Manipulator', () => { ...@@ -30,37 +30,39 @@ describe('Manipulator', () => {
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
Manipulator.setDataAttribute(div, 'testKey', 'value') Manipulator.setDataAttribute(div, 'testKey', 'value')
expect(div.getAttribute('data-test-key')).toEqual('value') expect(div.getAttribute('data-bs-test-key')).toEqual('value')
}) })
}) })
describe('removeDataAttribute', () => { describe('removeDataAttribute', () => {
it('should remove data attribute', () => { it('should only remove bs-prefixed data attribute', () => {
fixtureEl.innerHTML = '<div data-key="value"></div>' fixtureEl.innerHTML = '<div data-bs-key="value" data-key-bs="postfixed" data-key="value"></div>'
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
Manipulator.removeDataAttribute(div, 'key') Manipulator.removeDataAttribute(div, 'key')
expect(div.getAttribute('data-key')).toBeNull() expect(div.getAttribute('data-bs-key')).toBeNull()
expect(div.getAttribute('data-key-bs')).toEqual('postfixed')
expect(div.getAttribute('data-key')).toEqual('value')
}) })
it('should remove data attribute in kebab case', () => { it('should remove data attribute in kebab case', () => {
fixtureEl.innerHTML = '<div data-test-key="value"></div>' fixtureEl.innerHTML = '<div data-bs-test-key="value"></div>'
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
Manipulator.removeDataAttribute(div, 'testKey') Manipulator.removeDataAttribute(div, 'testKey')
expect(div.getAttribute('data-test-key')).toBeNull() expect(div.getAttribute('data-bs-test-key')).toBeNull()
}) })
}) })
describe('getDataAttributes', () => { describe('getDataAttributes', () => {
it('should return empty object for null', () => { it('should return an empty object for null', () => {
expect(Manipulator.getDataAttributes(null)).toEqual({}) expect(Manipulator.getDataAttributes(null)).toEqual({})
expect().nothing() expect().nothing()
}) })
it('should get only bs prefixed data attributes without bs namespace', () => { 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" data-target-bs="#element" data-in-bs-out="in-between"></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')
...@@ -73,16 +75,18 @@ describe('Manipulator', () => { ...@@ -73,16 +75,18 @@ describe('Manipulator', () => {
}) })
describe('getDataAttribute', () => { describe('getDataAttribute', () => {
it('should get data attribute', () => { it('should only get bs-prefixed data attribute', () => {
fixtureEl.innerHTML = '<div data-test="null" ></div>' fixtureEl.innerHTML = '<div data-bs-key="value" data-test-bs="postFixed" data-toggle="tab"></div>'
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
expect(Manipulator.getDataAttribute(div, 'key')).toEqual('value')
expect(Manipulator.getDataAttribute(div, 'test')).toBeNull() expect(Manipulator.getDataAttribute(div, 'test')).toBeNull()
expect(Manipulator.getDataAttribute(div, 'toggle')).toBeNull()
}) })
it('should get data attribute in kebab case', () => { it('should get data attribute in kebab case', () => {
fixtureEl.innerHTML = '<div data-test-key="value" ></div>' fixtureEl.innerHTML = '<div data-bs-test-key="value" ></div>'
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
...@@ -90,22 +94,22 @@ describe('Manipulator', () => { ...@@ -90,22 +94,22 @@ describe('Manipulator', () => {
}) })
it('should normalize data', () => { it('should normalize data', () => {
fixtureEl.innerHTML = '<div data-test="false" ></div>' fixtureEl.innerHTML = '<div data-bs-test="false" ></div>'
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
expect(Manipulator.getDataAttribute(div, 'test')).toEqual(false) expect(Manipulator.getDataAttribute(div, 'test')).toEqual(false)
div.setAttribute('data-test', 'true') div.setAttribute('data-bs-test', 'true')
expect(Manipulator.getDataAttribute(div, 'test')).toEqual(true) expect(Manipulator.getDataAttribute(div, 'test')).toEqual(true)
div.setAttribute('data-test', '1') div.setAttribute('data-bs-test', '1')
expect(Manipulator.getDataAttribute(div, 'test')).toEqual(1) expect(Manipulator.getDataAttribute(div, 'test')).toEqual(1)
}) })
}) })
describe('offset', () => { describe('offset', () => {
it('should return object with two properties top and left, both numbers', () => { it('should return an object with two properties top and left, both numbers', () => {
fixtureEl.innerHTML = '<div></div>' fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
...@@ -118,7 +122,7 @@ describe('Manipulator', () => { ...@@ -118,7 +122,7 @@ describe('Manipulator', () => {
}) })
describe('position', () => { describe('position', () => {
it('should return object with two properties top and left, both numbers', () => { it('should return an object with two properties top and left, both numbers', () => {
fixtureEl.innerHTML = '<div></div>' fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment