Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add whitespace guidance document #197

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
107 changes: 107 additions & 0 deletions specs/whitespace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Whitespace in Mustache

*(This document is non-normative and summarizes the whitespace rules presented in the specs for implementation authors.)*

## Terms

<dl>
<dt>element</dt>
Copy link
Member

Choose a reason for hiding this comment

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

I somewhat agree that "element" covers the meaning better than "tag pair", but I wish there was a less HTML-centric term that we could use instead. Some people feel that Mustache is prioritizing HTML over other target languages because of escaping and I would rather avoid reinforcing that perception if possible.

I have not come up with another idea yet but will keep trying. Please help!

<dd>A section, inverted section, parent, or block tag plus its content up to and including its associated end tag. For example, <code>{{$foo}}bar{{/foo}}</code> is a single block element.</dd>
<dt>argument block</dt>
<dd>A block element that is a direct child of a parent tag.</dd>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<dd>A block element that is a direct child of a parent tag.</dd>
<dd>A block element that is a direct child of a parent element.</dd>

(or whatever other term for "element " we come up with).

This could probably also use an example ({{$}}...{{/}}) to clarify what you mean by "block".

<dt>parameter block</dt>
<dd>A block element that is not a direct child of a parent tag.</dd>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<dd>A block element that is not a direct child of a parent tag.</dd>
<dd>A block element that is not a direct child of a parent element.</dd>

</dl>

## Standalone tags and elements

Mustache tries to avoid adding undesired blank lines for non-interpolation tags.
For example, assuming that `boolean = true`, the following template:

```mustache
| This Is
{{#boolean}}
|
{{/boolean}}
| A Line
```

will render as:

```plain
| This Is
|
| A Line
```

instead of:

```plain
| This Is

|

| A Line
```

A tag is considered **standalone** if the tag is the only text on the line except whitespace.
Interpolation tags are never considered standalone.
The leading whitespace, trailing whitespace, and line ending of a standalone tag are ignored.
Copy link
Member

Choose a reason for hiding this comment

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

"Ignored" could be further clarified. I would say that this whitespace is not part of the literal template content, but could be considered part of the tag delimiters instead.


Additionally, parent elements and parameter blocks
are considered standalone if the start tag has zero or more whitespace characters preceding it on its line
and its associated end tag has zero or more whitespace characters following it on its line.
Comment on lines +52 to +53
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
are considered standalone if the start tag has zero or more whitespace characters preceding it on its line
and its associated end tag has zero or more whitespace characters following it on its line.
are considered standalone if the start tag has nothing except for whitespace characters preceding it on its line
and its associated end tag has nothing but whitespace characters following it on its line.

I don't think it would hurt to include "clearance" in the glossary, but this works.

In such cases, the leading whitespace of such a parent tag or parameter block's start tag,
the trailing whitespace of the associated end tag,
and the line ending of the associated end tag are ignored.

## Indentation

Mustache tries to keep indentation consistent when using partials, parents, and blocks.
For example, for the following template:

```mustache
{{<parent}}{{$block}}
one
two
{{/block}}{{/parent}}
```

and the following definition of `parent`:

```mustache
Hi,
{{$block}}
{{/block}}
```

the template will render as:

```plain
Hi,
one
two
```

instead of:

```plain
Hi,
one
two
```

If a block tag is at the end of a line (ignoring any trailing whitespace)
and it is either part of an argument block or part of a standalone parameter block,
Comment on lines +94 to +95
Copy link
Member

Choose a reason for hiding this comment

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

This wording is a bit confusing because it can be read as if you're only talking about nested blocks, i.e., blocks within blocks.

then the block element's **intrinsic indentation** is the sequence of leading whitespace characters on the line following the block tag.
Otherwise, the block element's intrinsic indentation is the empty string.
Mustache implementations must ignore the occurrence of the block element's intrinsic indentation
at the beginning of each line of the block element's content.
Comment on lines +98 to +99
Copy link
Member

Choose a reason for hiding this comment

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

Maybe more explicit:

Suggested change
Mustache implementations must ignore the occurrence of the block element's intrinsic indentation
at the beginning of each line of the block element's content.
At compile time, Mustache implementations must remove the occurrence of the block element's intrinsic indentation
from the beginning of each line of the block element's content.


When a standalone partial tag or standalone parent element is expanded,
the leading whitespace on the start tag's line is prepended to each line of the expanded template.
A similar behavior occurs for standalone parameter blocks.
If a standalone parameter block's start tag is at the end of a line (possibly with trailing whitespace),
then its intrinsic indentation is prepended to each line of the argument block that is overriding it.
Otherwise, the leading whitespace of the standalone parameter block's start tag
is prepended to each line of the argument block that is overriding it.
Comment on lines +101 to +107
Copy link
Member

Choose a reason for hiding this comment

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

Two things:

  1. When no block is provided from the outside, a block's own contents are expanded in place. The intrinsic indentation (which was previously removed) or the leading indentation should still be added to each line in that case. With the current wording, this is a bit unclear because someone might interpret a defaulted block as not overridden.
  2. The indentation is added to each line of the template that will be expanded in place, before that template is rendered. That part is crucial, but is not yet explicit in this text.