Skip to content

Commit

Permalink
Manipulator: Add json parse support
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot committed Sep 28, 2021
1 parent c67bcde commit 8e54863
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
10 changes: 9 additions & 1 deletion js/src/dom/manipulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ function normalizeData(val) {
return null
}

return val
if (typeof val !== 'string') {
return val
}

try {
return JSON.parse(decodeURIComponent(val))
} catch {
return val
}
}

function normalizeDataKey(key) {
Expand Down
17 changes: 13 additions & 4 deletions js/tests/unit/dom/manipulator.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Manipulator from '../../../src/dom/manipulator'

/** Test helpers */
import { getFixture, clearFixture } from '../../helpers/fixture'
import { clearFixture, getFixture } from '../../helpers/fixture'

describe('Manipulator', () => {
let fixtureEl
Expand Down Expand Up @@ -94,7 +94,7 @@ describe('Manipulator', () => {
})

it('should normalize data', () => {
fixtureEl.innerHTML = '<div data-bs-test="false" ></div>'
fixtureEl.innerHTML = '<div data-bs-test="false" data-bs-test2=\'{"delay":{"show":100,"hide":10}}\'></div>'

const div = fixtureEl.querySelector('div')

Expand All @@ -103,8 +103,17 @@ describe('Manipulator', () => {
div.setAttribute('data-bs-test', 'true')
expect(Manipulator.getDataAttribute(div, 'test')).toEqual(true)

div.setAttribute('data-bs-test', '1')
expect(Manipulator.getDataAttribute(div, 'test')).toEqual(1)
let objectData = { 'Super Hero': ['Iron Man', 'Super Man'], url: 'http://localhost:8080/test?foo=bar' }
let dataStr = encodeURIComponent(JSON.stringify(objectData))
div.setAttribute('data-bs-test', dataStr)
expect(Manipulator.getDataAttribute(div, 'test')).toEqual(objectData)

objectData = { 'Super Hero': ['Iron Man', 'Super Man'], url: 'http://localhost:8080/test?foo=bar' }
dataStr = JSON.stringify(objectData)
div.setAttribute('data-bs-test', dataStr)
expect(Manipulator.getDataAttribute(div, 'test')).toEqual(objectData)

expect(Manipulator.getDataAttribute(div, 'test2')).toEqual({ delay: { show: 100, hide: 10 } })
})
})

Expand Down
11 changes: 4 additions & 7 deletions js/tests/unit/tooltip.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,15 +680,12 @@ describe('Tooltip', () => {
})

it('should not hide tooltip if leave event occurs and enter event occurs within the hide delay', done => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip" data-bs-delay=\'{"show":0,"hide":150}\'>'

const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, {
delay: {
show: 0,
hide: 150
}
})
const tooltip = new Tooltip(tooltipEl)

expect(tooltip._config.delay).toEqual({ show: 0, hide: 150 })

setTimeout(() => {
expect(tooltip.getTipElement().classList.contains('show')).toEqual(true)
Expand Down

0 comments on commit 8e54863

Please sign in to comment.