Skip to content
Draft
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
40 changes: 40 additions & 0 deletions dist/_hyperscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -7454,6 +7454,46 @@
}
});

parser.addCommand("clear", function (parser, runtime, tokens) {
if (tokens.matchToken("clear")) {
var elementExpr = parser.parseElement("expression", tokens);
if (elementExpr == null) {
parser.raiseParseError(
tokens,
"Expected either an attribute expression or value expression"
);
}

if (tokens.matchToken("from")) {
var fromExpr = parser.requireElement("expression", tokens);
} else {
if (elementExpr == null) {
var fromExpr = parser.requireElement("implicitMeTarget", tokens);
}
}

console.log(elementExpr)

return {
elementExpr: elementExpr,
from: fromExpr,
args: [elementExpr, fromExpr],
op: function (context, element, from) {
runtime.nullCheck(element, elementExpr);
runtime.implicitLoop(element, function (target) {
if (target === element) {
target.replaceChildren()
} else if (target.parentElement && target.parentElement === element.relativeToElement
&& (from == null || from.contains(target))) {
target.replaceChildren()
}
});
return runtime.findNext(this, context);
},
};
}
});

config.conversions.dynamicResolvers.push(function (str, node) {
if (!(str === "Values" || str.indexOf("Values:") === 0)) {
return;
Expand Down
2 changes: 1 addition & 1 deletion dist/_hyperscript.min.js

Large diffs are not rendered by default.

Binary file modified dist/_hyperscript.min.js.gz
Binary file not shown.
40 changes: 40 additions & 0 deletions src/_hyperscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -7454,6 +7454,46 @@
}
});

parser.addCommand("clear", function (parser, runtime, tokens) {
if (tokens.matchToken("clear")) {
var elementExpr = parser.parseElement("expression", tokens);
if (elementExpr == null) {
parser.raiseParseError(
tokens,
"Expected either an attribute expression or value expression"
);
}

if (tokens.matchToken("from")) {
var fromExpr = parser.requireElement("expression", tokens);
} else {
if (elementExpr == null) {
var fromExpr = parser.requireElement("implicitMeTarget", tokens);
}
}

console.log(elementExpr)

return {
elementExpr: elementExpr,
from: fromExpr,
args: [elementExpr, fromExpr],
op: function (context, element, from) {
runtime.nullCheck(element, elementExpr);
runtime.implicitLoop(element, function (target) {
if (target === element) {
target.replaceChildren()
} else if (target.parentElement && target.parentElement === element.relativeToElement
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it's the right approach but that the only way I've found for the last test to pass.
Otherwise it would clear all

elements, even from the caller's parent.

&& (from == null || from.contains(target))) {
target.replaceChildren()
}
});
return runtime.findNext(this, context);
},
};
}
});

config.conversions.dynamicResolvers.push(function (str, node) {
if (!(str === "Values" || str.indexOf("Values:") === 0)) {
return;
Expand Down
58 changes: 58 additions & 0 deletions test/commands/clear.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
describe("the clear command", function () {
beforeEach(function () {
clearWorkArea();
});
afterEach(function () {
clearWorkArea();
});

it("can clear inner text", function () {
var div = make("<div _='on click clear me'>foo</div>");
div.innerHTML.should.equal("foo")
div.click();
div.innerHTML.should.equal("")
});

it("can clear other inner text", function () {
var div = make("<div _='on click clear #that'></div>");
var div2 = make("<div id='that'>foo</div>");
div2.innerHTML.should.equal("foo")
div.click();
div2.innerHTML.should.equal("")
});

it("can clear elements", function () {
var div = make("<div _='on click clear me'><p>foo</p></div>");
div.innerHTML.should.equal("<p>foo</p>")
div.click();
div.innerHTML.should.equal("")
});

it("can clear other elements", function () {
var div = make("<div _='on click clear #that'></div>");
var div2 = make("<div id='that'><p>foo</p></div>");
div2.innerHTML.should.equal("<p>foo</p>")
div.click();
div2.innerHTML.should.equal("")
});

it("can clear parent element", function () {
var div = make("<div id='p1'><button id='b1' _='on click clear my.parentElement'></button></div> ");
var btn = byId("b1");
div.childElementCount.should.equal(1)
btn.click();
div.childElementCount.should.equal(0)
});

it("can clear specific things", function () {
var div = make("<div><div id='d1' _='on click clear <p/>'><p>foo</p>bar</div><p>doh</p></div>");
var d1 = byId('d1');
div.innerHTML.includes("foo").should.equal(true);
div.innerHTML.includes("bar").should.equal(true);
div.innerHTML.includes("doh").should.equal(true);
d1.click();
div.innerHTML.includes("foo").should.equal(false);
div.innerHTML.includes("bar").should.equal(true);
div.innerHTML.includes("doh").should.equal(true);
});
});
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ <h2>Mocha Test Suite</h2>
<script src="commands/settle.js"></script>
<script src="commands/unlessModifier.js"></script>
<script src="commands/increment.js"></script>
<script src="commands/clear.js"></script>

<!-- expressions -->
<!--
Expand Down
32 changes: 32 additions & 0 deletions www/commands/clear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: clear - ///_hyperscript
---

## The `clear` Command

### Syntax

```ebnf
clear <expression> [from <expression>]
```

### Description

The `clear` command allows you to clear an element's children or content.

### Examples

```html
<div _="on click clear me">Clear Me!</div>

<div _="on click clear the first <span/>">Clear This <span>Span</span> Content!</div>

<div _="on click clear <p/>">
Clear <p>This</p> <span>Not This</span> <p>This Too</p>!
</div>

<div _="on click clear #another-div">
Clear Another Div!
</div>

```
40 changes: 40 additions & 0 deletions www/test/1.0.0-rc1/src/_hyperscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -7454,6 +7454,46 @@
}
});

parser.addCommand("clear", function (parser, runtime, tokens) {
if (tokens.matchToken("clear")) {
var elementExpr = parser.parseElement("expression", tokens);
if (elementExpr == null) {
parser.raiseParseError(
tokens,
"Expected either an attribute expression or value expression"
);
}

if (tokens.matchToken("from")) {
var fromExpr = parser.requireElement("expression", tokens);
} else {
if (elementExpr == null) {
var fromExpr = parser.requireElement("implicitMeTarget", tokens);
}
}

console.log(elementExpr)

return {
elementExpr: elementExpr,
from: fromExpr,
args: [elementExpr, fromExpr],
op: function (context, element, from) {
runtime.nullCheck(element, elementExpr);
runtime.implicitLoop(element, function (target) {
if (target === element) {
target.replaceChildren()
} else if (target.parentElement && target.parentElement === element.relativeToElement
&& (from == null || from.contains(target))) {
target.replaceChildren()
}
});
return runtime.findNext(this, context);
},
};
}
});

config.conversions.dynamicResolvers.push(function (str, node) {
if (!(str === "Values" || str.indexOf("Values:") === 0)) {
return;
Expand Down
58 changes: 58 additions & 0 deletions www/test/1.0.0-rc1/test/commands/clear.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
describe("the clear command", function () {
beforeEach(function () {
clearWorkArea();
});
afterEach(function () {
clearWorkArea();
});

it("can clear inner text", function () {
var div = make("<div _='on click clear me'>foo</div>");
div.innerHTML.should.equal("foo")
div.click();
div.innerHTML.should.equal("")
});

it("can clear other inner text", function () {
var div = make("<div _='on click clear #that'></div>");
var div2 = make("<div id='that'>foo</div>");
div2.innerHTML.should.equal("foo")
div.click();
div2.innerHTML.should.equal("")
});

it("can clear elements", function () {
var div = make("<div _='on click clear me'><p>foo</p></div>");
div.innerHTML.should.equal("<p>foo</p>")
div.click();
div.innerHTML.should.equal("")
});

it("can clear other elements", function () {
var div = make("<div _='on click clear #that'></div>");
var div2 = make("<div id='that'><p>foo</p></div>");
div2.innerHTML.should.equal("<p>foo</p>")
div.click();
div2.innerHTML.should.equal("")
});

it("can clear parent element", function () {
var div = make("<div id='p1'><button id='b1' _='on click clear my.parentElement'></button></div> ");
var btn = byId("b1");
div.childElementCount.should.equal(1)
btn.click();
div.childElementCount.should.equal(0)
});

it("can clear specific things", function () {
var div = make("<div><div id='d1' _='on click clear <p/>'><p>foo</p>bar</div><p>doh</p></div>");
var d1 = byId('d1');
div.innerHTML.includes("foo").should.equal(true);
div.innerHTML.includes("bar").should.equal(true);
div.innerHTML.includes("doh").should.equal(true);
d1.click();
div.innerHTML.includes("foo").should.equal(false);
div.innerHTML.includes("bar").should.equal(true);
div.innerHTML.includes("doh").should.equal(true);
});
});
1 change: 1 addition & 0 deletions www/test/1.0.0-rc1/test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ <h2>Mocha Test Suite</h2>
<script src="commands/settle.js"></script>
<script src="commands/unlessModifier.js"></script>
<script src="commands/increment.js"></script>
<script src="commands/clear.js"></script>

<!-- expressions -->
<!--
Expand Down