mirror of
https://github.com/twbs/bootstrap.git
synced 2025-09-03 02:13:01 +02:00
fix(manipulator): increase coverage for manipulator
This commit is contained in:
@@ -1,12 +1,32 @@
|
||||
import Util from '../util'
|
||||
|
||||
/**
|
||||
* --------------------------------------------------------------------------
|
||||
* Bootstrap (v4.0.0-beta): dom/manipulator.js
|
||||
* Bootstrap (v4.1.1): dom/manipulator.js
|
||||
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
||||
* --------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
const regexDataKey = /[A-Z]/g
|
||||
|
||||
function normalizeData(val) {
|
||||
if (val === 'true') {
|
||||
return true
|
||||
} else if (val === 'false') {
|
||||
return false
|
||||
} else if (val === 'null') {
|
||||
return null
|
||||
} else if (val === Number(val).toString()) {
|
||||
return Number(val)
|
||||
} else if (val === '') {
|
||||
return null
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
|
||||
function normalizeDataKey(key) {
|
||||
return key.replace(regexDataKey, (chr) => chr.toLowerCase())
|
||||
}
|
||||
|
||||
const Manipulator = {
|
||||
setChecked(input, val) {
|
||||
if (input instanceof HTMLInputElement) {
|
||||
@@ -23,21 +43,55 @@ const Manipulator = {
|
||||
},
|
||||
|
||||
setDataAttribute(element, key, value) {
|
||||
const $ = Util.jQuery
|
||||
if (typeof $ !== 'undefined') {
|
||||
$(element).data(key, value)
|
||||
}
|
||||
|
||||
element.setAttribute(`data-${key.replace(/[A-Z]/g, (chr) => `-${chr.toLowerCase()}`)}`, value)
|
||||
element.setAttribute(`data-${normalizeDataKey(key)}`, value)
|
||||
},
|
||||
|
||||
removeDataAttribute(element, key) {
|
||||
const $ = Util.jQuery
|
||||
if (typeof $ !== 'undefined') {
|
||||
$(element).removeData(key)
|
||||
element.removeAttribute(`data-${normalizeDataKey(key)}`)
|
||||
},
|
||||
|
||||
getDataAttributes(element) {
|
||||
if (typeof element === 'undefined' || element === null) {
|
||||
return {}
|
||||
}
|
||||
|
||||
element.removeAttribute(`data-${key.replace(/[A-Z]/g, (chr) => `-${chr.toLowerCase()}`)}`)
|
||||
let attributes
|
||||
if (Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'dataset')) {
|
||||
attributes = {
|
||||
...element.dataset
|
||||
}
|
||||
} else {
|
||||
attributes = {}
|
||||
for (let i = 0; i < element.attributes.length; i++) {
|
||||
const attribute = element.attributes[i]
|
||||
|
||||
if (attribute.nodeName.indexOf('data-') !== -1) {
|
||||
// remove 'data-' part of the attribute name
|
||||
const attributeName = attribute
|
||||
.nodeName
|
||||
.substring('data-'.length)
|
||||
.replace(/-./g, (str) => str.charAt(1).toUpperCase())
|
||||
|
||||
attributes[attributeName] = attribute.nodeValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const key in attributes) {
|
||||
if (!Object.prototype.hasOwnProperty.call(attributes, key)) {
|
||||
continue
|
||||
}
|
||||
|
||||
attributes[key] = normalizeData(attributes[key])
|
||||
}
|
||||
|
||||
return attributes
|
||||
},
|
||||
|
||||
getDataAttribute(element, key) {
|
||||
return normalizeData(element
|
||||
.getAttribute(`data-${normalizeDataKey(key)}`)
|
||||
)
|
||||
},
|
||||
|
||||
offset(element) {
|
||||
|
Reference in New Issue
Block a user