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

documentElement doesn't have a parent #218

Merged
merged 1 commit into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions javascript/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default {
template.innerHTML = String(safeScalar(html)).trim()
operation.content = template.content
const parent = element.parentElement
const ordinal = Array.from(parent.children).indexOf(element)
const idx = parent && Array.from(parent.children).indexOf(element)
before(element, operation)
operate(operation, () => {
const { childrenOnly, focusSelector } = operation
Expand All @@ -102,21 +102,21 @@ export default {
)
assignFocus(focusSelector)
})
after(parent.children[ordinal], operation)
after(parent ? parent.children[idx] : document.documentElement, operation)
})
},

outerHtml: operation => {
processElements(operation, element => {
const parent = element.parentElement
const ordinal = Array.from(parent.children).indexOf(element)
const idx = parent && Array.from(parent.children).indexOf(element)
before(element, operation)
operate(operation, () => {
const { html, focusSelector } = operation
element.outerHTML = safeScalar(html)
assignFocus(focusSelector)
})
after(parent.children[ordinal], operation)
after(parent ? parent.children[idx] : document.documentElement, operation)
})
},

Expand Down Expand Up @@ -147,14 +147,14 @@ export default {
replace: operation => {
processElements(operation, element => {
const parent = element.parentElement
const ordinal = Array.from(parent.children).indexOf(element)
const idx = parent && Array.from(parent.children).indexOf(element)
before(element, operation)
operate(operation, () => {
const { html, focusSelector } = operation
element.outerHTML = safeScalar(html)
assignFocus(focusSelector)
})
after(parent.children[ordinal], operation)
after(parent ? parent.children[idx] : document.documentElement, operation)
})
},

Expand Down
7 changes: 6 additions & 1 deletion javascript/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ function safeObject (obj) {
return obj != null ? Object(obj) : {}
}

function fragmentToString (fragment) {
return new XMLSerializer().serializeToString(fragment)
}

// A proxy method to wrap a fetch call in error handling
//
// * url - the URL to fetch
Expand Down Expand Up @@ -200,5 +204,6 @@ export {
safeScalar,
safeString,
safeArray,
safeObject
safeObject,
fragmentToString
}