Repository: lambda-fairy/maud
Pull request #315 (2021-11-07 12:04:11)

Disallow slashes (`/`) in void elements
# Overview

The following syntax will no longer work:

```rust
html! {
    br /
    link rel="stylesheet" href="styles.css" /
}
```

This should be changed to the following:

```rust
html! {
    br;
    link rel="stylesheet" href="styles.css";
}
```

# Rationale

The `;` syntax was introduced in #96; the rationale for it can be found there.

Removing support for the older `/` syntax will simplify the API surface, and allow for the space to be used for other things.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0055560..cf9264a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,8 @@
   [#309](https://github.com/lambda-fairy/maud/pull/309)
 - Remove Iron support
   [#289](https://github.com/lambda-fairy/maud/pull/289)
+- Disallow slashes (`/`) in void elements
+  [#315](https://github.com/lambda-fairy/maud/pull/315)
 
 ## [0.22.3] - 2021-09-27
 
diff --git a/docs/content/elements-attributes.md b/docs/content/elements-attributes.md
index 02e9a9e..1349c9a 100644
--- a/docs/content/elements-attributes.md
+++ b/docs/content/elements-attributes.md
@@ -16,13 +16,6 @@ html! {
 # ;
 ```
 
-Before version 0.18,
-Maud allowed the curly braces to be omitted.
-This syntax was [removed][#137]
-and now causes an error instead.
-
-[#137]: https://github.com/lambda-fairy/maud/pull/137
-
 ## Void elements: `br;`
 
 Terminate a void element using a semicolon:
@@ -47,13 +40,6 @@ html! {
 The result will be rendered with HTML syntax –
 `<br>` not `<br />`.
 
-Maud also supports ending a void element with a slash:
-`br /`.
-This syntax is [deprecated][#96]
-and should not be used in new code.
-
-[#96]: https://github.com/lambda-fairy/maud/pull/96
-
 ## Custom elements and `data` attributes
 
 Maud also supports elements and attributes with hyphens in them.
diff --git a/maud_macros/src/parse.rs b/maud_macros/src/parse.rs
index 50bbff4..a20bc31 100644
--- a/maud_macros/src/parse.rs
+++ b/maud_macros/src/parse.rs
@@ -538,6 +538,14 @@ impl Parser {
             {
                 // Void element
                 self.advance();
+                if punct.as_char() == '/' {
+                    emit_error!(
+                        punct,
+                        "void elements must use `;`, not `/`";
+                        help = "change this to `;`";
+                        help = "see https://github.com/lambda-fairy/maud/pull/96 for details";
+                    );
+                }
                 ast::ElementBody::Void {
                     semi_span: SpanRange::single_span(punct.span()),
                 }


Test patch:
diff --git a/maud/tests/basic_syntax.rs b/maud/tests/basic_syntax.rs
index c3679dc..c406938 100644
--- a/maud/tests/basic_syntax.rs
+++ b/maud/tests/basic_syntax.rs
@@ -51,12 +51,6 @@ fn empty_elements() {
     assert_eq!(result.into_string(), "pinkie<br>pie");
 }
 
-#[test]
-fn empty_elements_slash() {
-    let result = html! { "pinkie" br / "pie" };
-    assert_eq!(result.into_string(), "pinkie<br>pie");
-}
-
 #[test]
 fn simple_attributes() {
     let result = html! {
diff --git a/maud/tests/warnings/void-element-slash.rs b/maud/tests/warnings/void-element-slash.rs
new file mode 100644
index 0000000..2bd0ff0
--- /dev/null
+++ b/maud/tests/warnings/void-element-slash.rs
@@ -0,0 +1,9 @@
+use maud::html;
+
+fn main() {
+    html! {
+        br /
+        // Make sure we're not stopping on the first error
+        input type="text" /
+    };
+}
diff --git a/maud/tests/warnings/void-element-slash.stderr b/maud/tests/warnings/void-element-slash.stderr
new file mode 100644
index 0000000..7d28413
--- /dev/null
+++ b/maud/tests/warnings/void-element-slash.stderr
@@ -0,0 +1,17 @@
+error: void elements must use `;`, not `/`
+ --> $DIR/void-element-slash.rs:5:12
+  |
+5 |         br /
+  |            ^
+  |
+  = help: change this to `;`
+  = help: see https://github.com/lambda-fairy/maud/pull/96 for details
+
+error: void elements must use `;`, not `/`
+ --> $DIR/void-element-slash.rs:7:27
+  |
+7 |         input type="text" /
+  |                           ^
+  |
+  = help: change this to `;`
+  = help: see https://github.com/lambda-fairy/maud/pull/96 for details