Skip to content

Commit

Permalink
Improving the boolean attributes support
Browse files Browse the repository at this point in the history
  • Loading branch information
Lcfvs committed Jul 1, 2021
1 parent 507c545 commit 6eceb88
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
27 changes: 10 additions & 17 deletions lib/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,27 +143,14 @@ const fill = template => {
const value = (tokens, node, name, template) => {
const { length } = tokens

if (!length) {
return
}

let value = node.getAttribute(name)

if (length === 1) {
const [{ key, optional }] = tokens

value = resolve(key, optional, template)

if (isFilled(value)) {
node.setAttribute(name, value)
} else {
node.removeAttribute(name)
}

if (!length) {
return
}

const values = []
const initial = node.getAttribute(name)
let value = initial

for (const { identifier, key, optional } of tokens) {
const [prefix] = value.split(identifier)
Expand All @@ -173,7 +160,13 @@ const value = (tokens, node, name, template) => {
value = value.replace(`${prefix}${identifier}`, '')
}

node.setAttribute(name, values.join(''))
value = values.join('')

if (length === 1 && value === '' && initial === tokens[0].identifier) {
node.removeAttribute(name)
} else {
node.setAttribute(name, value)
}
}

const content = (tokens, node, template) => {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lcf.vs/dom-engine",
"version": "4.0.1",
"version": "4.0.2",
"description": "A composable DOM based template engine",
"type": "module",
"scripts": {
Expand Down

0 comments on commit 6eceb88

Please sign in to comment.