diff --git a/src/_hyperscript.js b/src/_hyperscript.js index 01be8755..1eea8030 100644 --- a/src/_hyperscript.js +++ b/src/_hyperscript.js @@ -6521,10 +6521,20 @@ var onExpr = parser.requireElement("implicitMeTarget", tokens); } } else if (tokens.matchToken("between")) { - var between = true; var classRef = parser.parseElement("classRef", tokens); - tokens.requireToken("and"); - var classRef2 = parser.requireElement("classRef", tokens); + if (classRef != null) { + var betweenClass = true; + tokens.requireToken("and"); + var classRef2 = parser.requireElement("classRef", tokens); + } else { + var betweenAttr = true + var attributeRef = parser.parseElement("attributeRef", tokens); + if (attributeRef == null) { + parser.raiseParseError(tokens, "Expected either a class reference or attribute expression"); + } + tokens.requireToken("and"); + var attributeRef2 = parser.requireElement("attributeRef", tokens); + } } else { var classRef = parser.parseElement("classRef", tokens); var attributeRef = null; @@ -6563,6 +6573,7 @@ classRef2: classRef2, classRefs: classRefs, attributeRef: attributeRef, + attributeRef2: attributeRef2, on: onExpr, time: time, evt: evt, @@ -6573,7 +6584,7 @@ runtime.implicitLoop(on, function (target) { hideShowStrategy("toggle", target); }); - } else if (between) { + } else if (betweenClass) { runtime.implicitLoop(on, function (target) { if (target.classList.contains(classRef.className)) { target.classList.remove(classRef.className); @@ -6583,6 +6594,17 @@ target.classList.remove(classRef2.className); } }); + } else if (betweenAttr) { + runtime.implicitLoop(on, function (target) { + var currentValue = target.getAttribute(attributeRef.name); + if (currentValue === attributeRef.value) { + target.setAttribute(attributeRef2.name, attributeRef2.value); + if (attributeRef.name !== attributeRef2.name) target.removeAttribute(attributeRef.name); + } else { + target.setAttribute(attributeRef.name, attributeRef.value); + if (attributeRef.name !== attributeRef2.name) target.removeAttribute(attributeRef2.name); + } + }); } else if (classRefs) { runtime.forEach(classRefs, function (classRef) { runtime.implicitLoop(on, function (target) { diff --git a/test/commands/toggle.js b/test/commands/toggle.js index 5a46c562..f39b0859 100644 --- a/test/commands/toggle.js +++ b/test/commands/toggle.js @@ -196,4 +196,25 @@ describe("the toggle command", function () { form.click(); form.classList.contains("group-[:nth-of-type(3)_&]:block").should.equal(false); }); + + it("can toggle between two attribute values", function () { + var div = make("
"); + div.getAttribute("data-state").should.equal("active"); + div.click(); + div.getAttribute("data-state").should.equal("inactive"); + div.click(); + div.getAttribute("data-state").should.equal("active"); + }); + + it("can toggle between different attributes", function () { + var div = make("
"); + div.getAttribute("enabled").should.equal("true"); + div.hasAttribute("disabled").should.equal(false); + div.click(); + div.hasAttribute("enabled").should.equal(false); + div.getAttribute("disabled").should.equal("true"); + div.click(); + div.getAttribute("enabled").should.equal("true"); + div.hasAttribute("disabled").should.equal(false); + }); }); diff --git a/www/commands/toggle.md b/www/commands/toggle.md index 554bb7d3..c6678d86 100644 --- a/www/commands/toggle.md +++ b/www/commands/toggle.md @@ -7,7 +7,7 @@ title: toggle - ///_hyperscript ### Syntax ```ebnf -toggle ({} | | between and ) +toggle ({} | | between ( | ) and ( | )) [on ] [(for