@@ -28,9 +26,7 @@ const template1 = `
`
-const App = Vue.extend({
- template: template1
-})
+const App = Vue.extend({ template })
describe('utils/dom', () => {
it('isElement works', async () => {
@@ -73,7 +69,7 @@ describe('utils/dom', () => {
expect($span.exists()).toBe(true)
expect(hasClass($span.element, 'barspan')).toBe(true)
expect(hasClass($span.element, 'foobar')).toBe(true)
- expect(hasClass($span.element, 'fizzlerocks')).toBe(false)
+ expect(hasClass($span.element, 'fizzle-rocks')).toBe(false)
expect(hasClass(null, 'foobar')).toBe(false)
wrapper.destroy()
@@ -119,7 +115,7 @@ describe('utils/dom', () => {
expect($baz.exists()).toBe(true)
expect(closest('div.baz', $btns.at(0).element)).toBeDefined()
expect(closest('div.baz', $btns.at(0).element)).toBe($baz.element)
- expect(closest('div.nothere', $btns.at(0).element)).toBe(null)
+ expect(closest('div.not-here', $btns.at(0).element)).toBe(null)
expect(closest('div.baz', $baz.element)).toBe(null)
expect(closest('div.baz', $baz.element, true)).toBe($baz.element)
@@ -207,15 +203,15 @@ describe('utils/dom', () => {
// With root element specified
expect(select('button', wrapper.element)).toBe($btns.at(0).element)
expect(select('button#button3', wrapper.element)).toBe($btns.at(2).element)
- expect(select('span.nothere', wrapper.element)).toBe(null)
+ expect(select('span.not-here', wrapper.element)).toBe(null)
- // Note: It appears that vue-test-utils is not detaching previous app instances
- // and elements once the test is complete!
+ // Note: It appears that `vue-test-utils` is not detaching previous
+ // app instances and elements once the test is complete!
expect(select('button')).not.toBe(null)
expect(select('button')).toBe($btns.at(0).element)
expect(select('button#button3')).not.toBe(null)
expect(select('button#button3')).toBe($btns.at(2).element)
- expect(select('span.nothere')).toBe(null)
+ expect(select('span.not-here')).toBe(null)
wrapper.destroy()
})
@@ -246,8 +242,8 @@ describe('utils/dom', () => {
expect(selectAll('div.baz button', wrapper.element)[2]).toBe($btns.at(2).element)
// Without root element specified (assumes document as root)
- // Note: It appears that vue-test-utils is not detaching previous app instances
- // and elements once the test is complete!
+ // Note: It appears that `vue-test-utils` is not detaching previous
+ // app instances and elements once the test is complete!
expect(Array.isArray(selectAll('button'))).toBe(true)
expect(selectAll('button')).not.toEqual([])
expect(selectAll('button').length).toBe(3)
@@ -266,55 +262,4 @@ describe('utils/dom', () => {
wrapper.destroy()
})
-
- it('event options parsing works', async () => {
- // JSDOM probably does not support passive mode
- if (hasPassiveEventSupport) {
- // Converts boolean to object
- expect(parseEventOptions(true)).toEqual({ useCapture: true })
- expect(parseEventOptions(false)).toEqual({ useCapture: false })
- expect(parseEventOptions()).toEqual({ useCapture: false })
-
- // Parses object correctly (returns as-is)
- expect(parseEventOptions({ useCapture: false })).toEqual({ useCapture: false })
- expect(parseEventOptions({ useCapture: true })).toEqual({ useCapture: true })
- expect(parseEventOptions({})).toEqual({})
- expect(
- parseEventOptions({
- useCapture: false,
- foobar: true
- })
- ).toEqual({ useCapture: false, foobar: true })
- expect(
- parseEventOptions({
- useCapture: true,
- foobar: false
- })
- ).toEqual({ useCapture: true, foobar: false })
- } else {
- // Converts non object to boolean
- expect(parseEventOptions(true)).toEqual(true)
- expect(parseEventOptions(false)).toEqual(false)
- expect(parseEventOptions()).toEqual(false)
- expect(parseEventOptions(null)).toEqual(false)
- // Converts object to boolean
- expect(parseEventOptions({ useCapture: false })).toEqual(false)
- expect(parseEventOptions({ useCapture: true })).toEqual(true)
- expect(parseEventOptions({})).toEqual(false)
- expect(
- parseEventOptions({
- useCapture: false,
- foobar: true
- })
- ).toEqual(false)
- expect(
- parseEventOptions({
- useCapture: true,
- foobar: true
- })
- ).toEqual(true)
- expect(parseEventOptions({ foobar: true })).toEqual(false)
- expect(parseEventOptions({ foobar: false })).toEqual(false)
- }
- })
})
diff --git a/src/utils/events.js b/src/utils/events.js
new file mode 100644
index 00000000000..2b489cc10f1
--- /dev/null
+++ b/src/utils/events.js
@@ -0,0 +1,42 @@
+import { hasPassiveEventSupport } from './env'
+import { isObject } from './inspect'
+
+// --- Constants ---
+
+export const EVENT_OPTIONS_PASSIVE = { passive: true }
+export const EVENT_OPTIONS_NO_CAPTURE = { passive: true, capture: true }
+
+// --- Utils ---
+
+// Normalize event options based on support of passive option
+// Exported only for testing purposes
+export const parseEventOptions = options => {
+ /* istanbul ignore else: can't test in JSDOM, as it supports passive */
+ if (hasPassiveEventSupport) {
+ return isObject(options) ? options : { capture: !!options || false }
+ } else {
+ // Need to translate to actual Boolean value
+ return !!(isObject(options) ? options.capture : options)
+ }
+}
+
+// Attach an event listener to an element
+export const eventOn = (el, evtName, handler, options) => {
+ if (el && el.addEventListener) {
+ el.addEventListener(evtName, handler, parseEventOptions(options))
+ }
+}
+
+// Remove an event listener from an element
+export const eventOff = (el, evtName, handler, options) => {
+ if (el && el.removeEventListener) {
+ el.removeEventListener(evtName, handler, parseEventOptions(options))
+ }
+}
+
+// Utility method to add/remove a event listener based on first argument (boolean)
+// It passes all other arguments to the `eventOn()` or `eventOff` method
+export const eventOnOff = (on, ...args) => {
+ const method = on ? eventOn : eventOff
+ method(...args)
+}
diff --git a/src/utils/events.spec.js b/src/utils/events.spec.js
new file mode 100644
index 00000000000..2c94b4c7c85
--- /dev/null
+++ b/src/utils/events.spec.js
@@ -0,0 +1,41 @@
+import { parseEventOptions } from './events'
+import { hasPassiveEventSupport } from './env'
+
+describe('utils/events', () => {
+ it('event options parsing works', async () => {
+ // JSDOM probably does not support passive mode
+ if (hasPassiveEventSupport) {
+ // Converts boolean to object
+ expect(parseEventOptions(true)).toEqual({ capture: true })
+ expect(parseEventOptions(false)).toEqual({ capture: false })
+ expect(parseEventOptions()).toEqual({ capture: false })
+
+ // Parses object correctly (returns as-is)
+ expect(parseEventOptions({ capture: false })).toEqual({ capture: false })
+ expect(parseEventOptions({ capture: true })).toEqual({ capture: true })
+ expect(parseEventOptions({})).toEqual({})
+ expect(parseEventOptions({ capture: false, foobar: true })).toEqual({
+ capture: false,
+ foobar: true
+ })
+ expect(parseEventOptions({ capture: true, foobar: false })).toEqual({
+ capture: true,
+ foobar: false
+ })
+ } else {
+ // Converts non object to boolean
+ expect(parseEventOptions(true)).toEqual(true)
+ expect(parseEventOptions(false)).toEqual(false)
+ expect(parseEventOptions()).toEqual(false)
+ expect(parseEventOptions(null)).toEqual(false)
+ // Converts object to boolean
+ expect(parseEventOptions({ capture: false })).toEqual(false)
+ expect(parseEventOptions({ capture: true })).toEqual(true)
+ expect(parseEventOptions({})).toEqual(false)
+ expect(parseEventOptions({ capture: false, foobar: true })).toEqual(false)
+ expect(parseEventOptions({ capture: true, foobar: true })).toEqual(true)
+ expect(parseEventOptions({ foobar: true })).toEqual(false)
+ expect(parseEventOptions({ foobar: false })).toEqual(false)
+ }
+ })
+})
diff --git a/src/utils/observe-dom.js b/src/utils/observe-dom.js
index 57a7b6ca1eb..9824b274362 100644
--- a/src/utils/observe-dom.js
+++ b/src/utils/observe-dom.js
@@ -5,10 +5,14 @@ import { warnNoMutationObserverSupport } from './warn'
* Observe a DOM element changes, falls back to eventListener mode
* @param {Element} el The DOM element to observe
* @param {Function} callback callback to be called on change
- * @param {object} [opts={childList: true, subtree: true}] observe options
+ * @param {object} [options={childList: true, subtree: true}] observe options
* @see http://stackoverflow.com/questions/3219758
*/
-const observeDom = (el, callback, opts) => /* istanbul ignore next: difficult to test in JSDOM */ {
+const observeDom = (
+ el,
+ callback,
+ options
+) => /* istanbul ignore next: difficult to test in JSDOM */ {
// Handle cases where we might be passed a Vue instance
el = el ? el.$el || el : null
@@ -64,7 +68,7 @@ const observeDom = (el, callback, opts) => /* istanbul ignore next: difficult to
})
// Have the observer observe foo for changes in children, etc
- obs.observe(el, { childList: true, subtree: true, ...opts })
+ obs.observe(el, { childList: true, subtree: true, ...options })
// We return a reference to the observer so that `obs.disconnect()`
// can be called if necessary
diff --git a/src/utils/plugins.js b/src/utils/plugins.js
index 71678a4867e..cabb101a504 100644
--- a/src/utils/plugins.js
+++ b/src/utils/plugins.js
@@ -55,7 +55,7 @@ export const installFactory = ({ components, directives, plugins } = {}) => {
* @returns {function} plugin install function
*/
export const installFactoryNoConfig = ({ components, directives, plugins } = {}) => {
- const install = (Vue, config = {}) => {
+ const install = Vue => {
if (install.installed) {
/* istanbul ignore next */
return
@@ -77,9 +77,9 @@ export const installFactoryNoConfig = ({ components, directives, plugins } = {})
* @param {object} { components, directives, plugins }
* @returns {object} plugin install object
*/
-export const pluginFactory = (opts = {}, extend = {}) => ({
+export const pluginFactory = (options = {}, extend = {}) => ({
...extend,
- install: installFactory(opts)
+ install: installFactory(options)
})
/**
@@ -87,9 +87,9 @@ export const pluginFactory = (opts = {}, extend = {}) => ({
* @param {object} { components, directives, plugins }
* @returns {object} plugin install object
*/
-export const pluginFactoryNoConfig = (opts = {}, extend = {}) => ({
+export const pluginFactoryNoConfig = (options = {}, extend = {}) => ({
...extend,
- install: installFactoryNoConfig(opts)
+ install: installFactoryNoConfig(options)
})
/**
diff --git a/src/utils/target.js b/src/utils/target.js
index 88ab909869a..a25bff09fe8 100644
--- a/src/utils/target.js
+++ b/src/utils/target.js
@@ -1,5 +1,5 @@
import { keys } from './object'
-import { eventOn, eventOff } from './dom'
+import { eventOn, eventOff } from './events'
const allListenTypes = { hover: true, click: true, focus: true }
diff --git a/src/utils/transporter.spec.js b/src/utils/transporter.spec.js
index ebf68c5efa3..5008dd2fbad 100644
--- a/src/utils/transporter.spec.js
+++ b/src/utils/transporter.spec.js
@@ -8,7 +8,7 @@ describe('utils/transporter component', () => {
it('renders in-pace when disabled=true', async () => {
const App = localVue.extend({
render(h) {
- return h(BTransporterSingle, { props: { disabled: true } }, [h('div', {}, 'content')])
+ return h(BTransporterSingle, { props: { disabled: true } }, [h('div', 'content')])
}
})