Skip to content

Commit

Permalink
Improvements to attribute()
Browse files Browse the repository at this point in the history
  • Loading branch information
gnat authored Jun 12, 2024
1 parent f1b2eaf commit 60e6ef0
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions surreal.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,28 +180,28 @@ let $ = { // Convenience for internals.
},
// Add or remove attributes from element(s)
attribute(e, name, value=undefined) {
// Get. This one is special. Format: "name", "value"
// Get. (Format: "name", "value") Special: Ends call chain.
if (typeof name === 'string' && value === undefined) {
if ($.isNodeList(e)) return [] // Not supported for Get. For many elements, wrap attribute() in any(...).run(...) or any(...).forEach(...)
if ($.isNode(e)) return e.getAttribute(name)
return null // No value.
return null // No value. Ends call chain.
}
// Remove.
if (typeof name === 'string' && value === null) {
if ($.isNodeList(e)) e.forEach(_ => { $.attribute(_, name, value) })
else e.removeAttribute(name)
if ($.isNode(e)) e.removeAttribute(name)
return e
}
// Add / Set.
if (typeof name === 'string') {
if ($.isNodeList(e)) e.forEach(_ => { $.attribute(_, name, value) })
else e.setAttribute(name, value)
if ($.isNode(e)) e.setAttribute(name, value)
return e
}
// Format: { "name": "value", "blah": true }
if (typeof name === 'object') {
if ($.isNodeList(e)) e.forEach(_ => { Object.entries(name).forEach(([key, val]) => { $.attribute(_, key, val) }) })
else if ($.isNode(e)) Object.entries(name).forEach(([key, val]) => { $.attribute(e, key, val) })
if ($.isNode(e)) Object.entries(name).forEach(([key, val]) => { $.attribute(e, key, val) })
return e
}
return e
Expand Down

0 comments on commit 60e6ef0

Please sign in to comment.