aboutsummaryrefslogtreecommitdiff
path: root/js/src
diff options
context:
space:
mode:
authorJohann-S <[email protected]>2018-06-09 21:11:05 +0200
committerXhmikosR <[email protected]>2019-02-20 22:05:45 +0200
commit64591b3722128d89252b8f1c840cd846940b7f5c (patch)
tree971c65a6fd9f506da55e4be31d1fece7f9b20b89 /js/src
parent4d6e41dea6492f18029f0dd70b118217c02f27d8 (diff)
downloadbootstrap-64591b3722128d89252b8f1c840cd846940b7f5c.tar.xz
bootstrap-64591b3722128d89252b8f1c840cd846940b7f5c.zip
fix(manipulator): increase coverage for manipulator
Diffstat (limited to 'js/src')
-rw-r--r--js/src/carousel.js4
-rw-r--r--js/src/collapse.js5
-rw-r--r--js/src/dom/manipulator.js80
-rw-r--r--js/src/dropdown.js2
-rw-r--r--js/src/modal.js20
-rw-r--r--js/src/scrollspy.js2
-rw-r--r--js/src/tooltip.js11
-rw-r--r--js/src/util.js49
8 files changed, 90 insertions, 83 deletions
diff --git a/js/src/carousel.js b/js/src/carousel.js
index 15a56bd76..352b23849 100644
--- a/js/src/carousel.js
+++ b/js/src/carousel.js
@@ -562,8 +562,8 @@ class Carousel {
}
const config = {
- ...Util.getDataAttributes(target),
- ...Util.getDataAttributes(this)
+ ...Manipulator.getDataAttributes(target),
+ ...Manipulator.getDataAttributes(this)
}
const slideIndex = this.getAttribute('data-slide-to')
diff --git a/js/src/collapse.js b/js/src/collapse.js
index d04743d03..eebac13bb 100644
--- a/js/src/collapse.js
+++ b/js/src/collapse.js
@@ -7,6 +7,7 @@
import Data from './dom/data'
import EventHandler from './dom/eventHandler'
+import Manipulator from './dom/manipulator'
import SelectorEngine from './dom/selectorEngine'
import Util from './util'
@@ -347,7 +348,7 @@ class Collapse {
let data = Data.getData(element, DATA_KEY)
const _config = {
...Default,
- ...Util.getDataAttributes(element),
+ ...Manipulator.getDataAttributes(element),
...typeof config === 'object' && config ? config : {}
}
@@ -391,7 +392,7 @@ EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (
event.preventDefault()
}
- const triggerData = Util.getDataAttributes(this)
+ const triggerData = Manipulator.getDataAttributes(this)
const selector = Util.getSelectorFromElement(this)
const selectorElements = Util.makeArray(SelectorEngine.find(selector))
diff --git a/js/src/dom/manipulator.js b/js/src/dom/manipulator.js
index 201902b77..db3113f88 100644
--- a/js/src/dom/manipulator.js
+++ b/js/src/dom/manipulator.js
@@ -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 {}
+ }
+
+ 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
+ }
+ }
}
- element.removeAttribute(`data-${key.replace(/[A-Z]/g, (chr) => `-${chr.toLowerCase()}`)}`)
+ 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) {
diff --git a/js/src/dropdown.js b/js/src/dropdown.js
index 282e7645f..b1487b64a 100644
--- a/js/src/dropdown.js
+++ b/js/src/dropdown.js
@@ -267,7 +267,7 @@ class Dropdown {
_getConfig(config) {
config = {
...this.constructor.Default,
- ...Util.getDataAttributes(this._element),
+ ...Manipulator.getDataAttributes(this._element),
...config
}
diff --git a/js/src/modal.js b/js/src/modal.js
index 4f23fff74..0ecd6948f 100644
--- a/js/src/modal.js
+++ b/js/src/modal.js
@@ -473,7 +473,7 @@ class Modal {
// Restore fixed content padding
Util.makeArray(SelectorEngine.find(Selector.FIXED_CONTENT))
.forEach((element) => {
- const padding = Util.getDataAttribute(element, 'padding-right')
+ const padding = Manipulator.getDataAttribute(element, 'padding-right')
if (typeof padding !== 'undefined') {
Manipulator.removeDataAttribute(element, 'padding-right')
element.style.paddingRight = padding
@@ -483,7 +483,7 @@ class Modal {
// Restore sticky content and navbar-toggler margin
Util.makeArray(SelectorEngine.find(`${Selector.STICKY_CONTENT}`))
.forEach((element) => {
- const margin = Util.getDataAttribute(element, 'margin-right')
+ const margin = Manipulator.getDataAttribute(element, 'margin-right')
if (typeof margin !== 'undefined') {
Manipulator.removeDataAttribute(element, 'margin-right')
element.style.marginRight = margin
@@ -491,17 +491,13 @@ class Modal {
})
// Restore body padding
- const padding = Util.getDataAttribute(document.body, 'padding-right')
+ const padding = Manipulator.getDataAttribute(document.body, 'padding-right')
if (typeof padding !== 'undefined') {
Manipulator.removeDataAttribute(document.body, 'padding-right')
document.body.style.paddingRight = padding
} else {
document.body.style.paddingRight = ''
}
-
- static _getInstance(element) {
- return Data.getData(element, DATA_KEY)
- }
}
_getScrollbarWidth() { // thx d.walsh
@@ -520,7 +516,7 @@ class Modal {
let data = Data.getData(this, DATA_KEY)
const _config = {
...Default,
- ...Util.getDataAttributes(this),
+ ...Manipulator.getDataAttributes(this),
...typeof config === 'object' && config ? config : {}
}
@@ -539,6 +535,10 @@ class Modal {
}
})
}
+
+ static _getInstance(element) {
+ return Data.getData(element, DATA_KEY)
+ }
}
/**
@@ -557,8 +557,8 @@ EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (
const config = Data.getData(target, DATA_KEY)
? 'toggle' : {
- ...Util.getDataAttributes(target),
- ...Util.getDataAttributes(this)
+ ...Manipulator.getDataAttributes(target),
+ ...Manipulator.getDataAttributes(this)
}
if (this.tagName === 'A' || this.tagName === 'AREA') {
diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js
index 458f5170e..f317284c9 100644
--- a/js/src/scrollspy.js
+++ b/js/src/scrollspy.js
@@ -322,7 +322,7 @@ class ScrollSpy {
EventHandler.on(window, Event.LOAD_DATA_API, () => {
Util.makeArray(SelectorEngine.find(Selector.DATA_SPY))
- .forEach((spy) => new ScrollSpy(spy, Util.getDataAttributes(spy)))
+ .forEach((spy) => new ScrollSpy(spy, Manipulator.getDataAttributes(spy)))
})
/**
diff --git a/js/src/tooltip.js b/js/src/tooltip.js
index 9b8b8263a..fbe9ed856 100644
--- a/js/src/tooltip.js
+++ b/js/src/tooltip.js
@@ -11,6 +11,7 @@ import {
} from './tools/sanitizer'
import Data from './dom/data'
import EventHandler from './dom/eventHandler'
+import Manipulator from './dom/manipulator'
import Popper from 'popper.js'
import SelectorEngine from './dom/selectorEngine'
import Util from './util'
@@ -671,7 +672,7 @@ class Tooltip {
}
_getConfig(config) {
- const dataAttributes = Util.getDataAttributes(this.element)
+ const dataAttributes = Manipulator.getDataAttributes(this.element)
Object.keys(dataAttributes)
.forEach((dataAttr) => {
@@ -741,10 +742,6 @@ class Tooltip {
.map((token) => token.trim())
.forEach((tClass) => tip.classList.remove(tClass))
}
-
- static _getInstance(element) {
- return Data.getData(element, DATA_KEY)
- }
}
_handlePopperPlacementChange(popperData) {
@@ -793,6 +790,10 @@ class Tooltip {
}
})
}
+
+ static _getInstance(element) {
+ return Data.getData(element, DATA_KEY)
+ }
}
/**
diff --git a/js/src/util.js b/js/src/util.js
index 78f5fe3fb..0b7f492fe 100644
--- a/js/src/util.js
+++ b/js/src/util.js
@@ -22,20 +22,6 @@ function toType(obj) {
return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase()
}
-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)
- }
-
- return val
-}
-
const Util = {
TRANSITION_END: 'bsTransitionEnd',
@@ -164,41 +150,6 @@ const Util = {
return [nodeList]
},
- getDataAttributes(element) {
- if (typeof element === 'undefined' || element === null) {
- return {}
- }
-
- let attributes
- if (Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'dataset')) {
- attributes = this.extend({}, 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-${key.replace(/[A-Z]/g, (chr) => `-${chr.toLowerCase()}`)}`))
- },
-
isVisible(element) {
if (typeof element === 'undefined' || element === null) {
return false