Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed 0 value problem with null checks #210

Merged
merged 9 commits into from
Jul 20, 2022
54 changes: 29 additions & 25 deletions javascript/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
processElements,
before,
after,
operate
operate,
safeString,
safeArray,
safeObject
} from './utils'

export default {
Expand All @@ -18,7 +21,7 @@ export default {
before(element, operation)
operate(operation, () => {
const { html, focusSelector } = operation
element.insertAdjacentHTML('beforeend', html || '')
element.insertAdjacentHTML('beforeend', safeString(html))
assignFocus(focusSelector)
})
after(element, operation)
Expand All @@ -45,7 +48,7 @@ export default {
before(element, operation)
operate(operation, () => {
const { html, focusSelector } = operation
element.innerHTML = html || ''
element.innerHTML = safeString(html)
assignFocus(focusSelector)
})
after(element, operation)
Expand All @@ -57,7 +60,7 @@ export default {
before(element, operation)
operate(operation, () => {
const { html, position, focusSelector } = operation
element.insertAdjacentHTML(position || 'beforeend', html || '')
element.insertAdjacentHTML(position || 'beforeend', safeString(html))
assignFocus(focusSelector)
})
after(element, operation)
Expand All @@ -69,7 +72,7 @@ export default {
before(element, operation)
operate(operation, () => {
const { text, position, focusSelector } = operation
element.insertAdjacentText(position || 'beforeend', text || '')
element.insertAdjacentText(position || 'beforeend', safeString(text))
assignFocus(focusSelector)
})
after(element, operation)
Expand Down Expand Up @@ -109,7 +112,7 @@ export default {
before(element, operation)
operate(operation, () => {
const { html, focusSelector } = operation
element.outerHTML = html || ''
element.outerHTML = safeString(html)
assignFocus(focusSelector)
})
after(parent.children[ordinal], operation)
Expand All @@ -121,7 +124,7 @@ export default {
before(element, operation)
operate(operation, () => {
const { html, focusSelector } = operation
element.insertAdjacentHTML('afterbegin', html || '')
element.insertAdjacentHTML('afterbegin', safeString(html))
assignFocus(focusSelector)
})
after(element, operation)
Expand All @@ -147,7 +150,7 @@ export default {
before(element, operation)
operate(operation, () => {
const { html, focusSelector } = operation
element.outerHTML = html || ''
element.outerHTML = safeString(html)
assignFocus(focusSelector)
})
after(parent.children[ordinal], operation)
Expand All @@ -159,7 +162,7 @@ export default {
before(element, operation)
operate(operation, () => {
const { text, focusSelector } = operation
element.textContent = text != null ? text : ''
element.textContent = safeString(text)
assignFocus(focusSelector)
})
after(element, operation)
Expand All @@ -173,7 +176,7 @@ export default {
before(element, operation)
operate(operation, () => {
const { name } = operation
element.classList.add(...getClassNames(name || ''))
element.classList.add(...getClassNames([safeString(name)]))
})
after(element, operation)
})
Expand All @@ -195,7 +198,7 @@ export default {
before(element, operation)
operate(operation, () => {
const { name } = operation
element.classList.remove(...getClassNames(name))
element.classList.remove(...getClassNames([name]))
})
after(element, operation)
})
Expand All @@ -206,7 +209,7 @@ export default {
before(element, operation)
operate(operation, () => {
const { name, value } = operation
element.setAttribute(name, value || '')
element.setAttribute(name, safeString(value))
})
after(element, operation)
})
Expand All @@ -217,7 +220,7 @@ export default {
before(element, operation)
operate(operation, () => {
const { name, value } = operation
element.dataset[name] = value || ''
element.dataset[name] = safeString(value)
})
after(element, operation)
})
Expand All @@ -228,7 +231,7 @@ export default {
before(element, operation)
operate(operation, () => {
const { name, value } = operation
if (name in element) element[name] = value || ''
if (name in element) element[name] = safeString(value)
})
after(element, operation)
})
Expand All @@ -239,7 +242,7 @@ export default {
before(element, operation)
operate(operation, () => {
const { name, value } = operation
element.style[name] = value || ''
element.style[name] = safeString(value)
})
after(element, operation)
})
Expand All @@ -251,7 +254,7 @@ export default {
operate(operation, () => {
const { styles } = operation
for (let [name, value] of Object.entries(styles))
element.style[name] = value || ''
element.style[name] = safeString(value)
})
after(element, operation)
})
Expand All @@ -262,7 +265,7 @@ export default {
before(element, operation)
operate(operation, () => {
const { value } = operation
element.value = value || ''
element.value = safeString(value)
})
after(element, operation)
})
Expand Down Expand Up @@ -358,7 +361,7 @@ export default {
before(window, operation)
operate(operation, () => {
const { state, title, url } = operation
history.pushState(state || {}, title || '', url)
history.pushState(safeObject(state), safeString(title), url)
})
after(window, operation)
},
Expand Down Expand Up @@ -403,7 +406,7 @@ export default {
before(window, operation)
operate(operation, () => {
const { state, title, url } = operation
history.replaceState(state || {}, title || '', url)
history.replaceState(safeObject(state), safeString(title), url)
})
after(window, operation)
},
Expand All @@ -421,7 +424,7 @@ export default {
before(document, operation)
operate(operation, () => {
const { cookie } = operation
document.cookie = cookie || ''
document.cookie = safeString(cookie)
})
after(document, operation)
},
Expand All @@ -440,7 +443,7 @@ export default {
operate(operation, () => {
const { key, value, type } = operation
const storage = type === 'session' ? sessionStorage : localStorage
storage.setItem(key, value || '')
storage.setItem(key, safeString(value))
})
after(document, operation)
},
Expand All @@ -452,8 +455,8 @@ export default {
operate(operation, () => {
const { message, level } = operation
level && ['warn', 'info', 'error'].includes(level)
? console[level](message || '')
: console.log(message || '')
? console[level](message)
: console.log(message)
})
after(document, operation)
},
Expand All @@ -462,7 +465,7 @@ export default {
before(document, operation)
operate(operation, () => {
const { data, columns } = operation
console.table(data, columns || [])
console.table(data, safeArray(columns))
})
after(document, operation)
},
Expand All @@ -473,7 +476,8 @@ export default {
const { title, options } = operation
Notification.requestPermission().then(result => {
operation.permission = result
if (result === 'granted') new Notification(title || '', options)
if (result === 'granted')
new Notification(safeString(title), safeObject(options))
})
})
after(document, operation)
Expand Down
19 changes: 17 additions & 2 deletions javascript/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const xpathToElementArray = xpath => {
//
// * names - could be a string or an array of strings for multiple classes.
//
const getClassNames = names => Array(names).flat()
const getClassNames = names => Array.from(names).flat()

// Perform operation for either the first or all of the elements returned by CSS selector
//
Expand Down Expand Up @@ -130,6 +130,18 @@ function handleErrors (response) {
return response
}

function safeString (str) {
return str != null ? String(str) : ''
}

function safeArray (arr) {
return arr != null ? Array.from(arr) : []
}

function safeObject (obj) {
return obj != null ? Object(obj) : {}
}

// A proxy method to wrap a fetch call in error handling
//
// * url - the URL to fetch
Expand Down Expand Up @@ -167,5 +179,8 @@ export {
debounce,
handleErrors,
graciouslyFetch,
kebabize
kebabize,
safeString,
safeArray,
safeObject
}