ractivejs/ractive — pull request #727 by Rich-Harris (2014-05-18 05:39:51+00:00)
Title: Better handling of whitespace in templates

Before, whitespace would be stripped from the start and end of templates, and from the start and end of element fragments (since `

foo

` is identical to `

foo

` to browsers, except in the rare cases where `p` elements have `white-space: pre`, for which we use the `preserveWhitespace` flag when parsing). Contiguous whitespace would be collapsed to a single character. But whitespace at the start and end of *sections* was preserved:

```html
{{#foo}}

bar

{{/foo}}
```

...becomes...

```js
[{"t": 4,"r": "foo","f": [" ",{"t": 7,"e": "p","f": ["bar"]}," "]}]
```

Note the `" "` strings either side of the `p` object.

Now, the parser is smarter about ditching unnecessary whitespace. The same template will now parse to

```js
[{"t": 4,"r": "foo","f": [{"t": 7,"e": "p","f": ["bar"]}]}]
```

Aside from being more compact, this will be quicker to render, because a) we don't need to create useless text nodes, and b) when stamping out the `foo` fragment, we don't need to create a document fragment to temporarily store multiple nodes (text node, `

`, text node) before appending them to the DOM, because there's only one node.