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

Respect whitespace #10

Closed
mattfysh opened this issue Dec 26, 2014 · 6 comments
Closed

Respect whitespace #10

mattfysh opened this issue Dec 26, 2014 · 6 comments

Comments

@mattfysh
Copy link

<my-first-tag>
<my-second-tag>

The new line between these two custom tags should be preserved.

@patrick-steele-idem
Copy link
Contributor

Hi @mattfysh, Marko will remove unnecessary whitespace based on some builtin rules, by default. These rules are partially based on the rules that browser's use to normalize whitespace and partially based on the goal of allowing nicely indented markup. These rules are as follows:

  • For whitespace before the first child element: text = text.replace(/^\n\s*/g, '')
  • For whitespace after the last child element: text = text.replace(/\n\s*$/g, '')
  • For whitespace between child elements: text = text.replace(/^\n\s*$/g, '')
  • Any contiguous sequence of whitespace characters is collapsed into a single whitespace character

FYI, the whitespace removal happens in the following code:
marko/compiler/TextNode.js:112

It's possible to disable whitespace removal on a per-element basis using a special attribute applied to a parent element. It's also possible to disable all whitespace removal by changing an option for the compiler. Unfortunately the docs are currently lacking for both these options so I'll describe them below for now:

Option 1) Disable whitespace removal using the c-whitespace attribute:

<div c-whitespace="preserve">
    <my-first-tag>
    <my-second-tag>
</div>

Option 2) Disable all whitespace removal by changing a compiler option

require('marko/compiler').defaultOptions.preserveWhitespace = true;

Also, you can avoid whitespace removal by avoiding the new line character. For example, the following whitespace between the elements will be preserved:

<my-first-tag> <my-second-tag>

I'll work on adding some formal documentation on the whitespace removal rules. Do these options meet your needs or are you looking for a different solution?

@patrick-steele-idem
Copy link
Contributor

@mattfysh, I added some documentation to clarify how Marko deals with whitespace:
https://github.com/raptorjs/marko#whitespace

Please let me know what you think.

Thanks,
Patrick

@philidem
Copy link
Member

Perhaps we should add a tag that can be used to enable preservation of whitespace.

For example, something like this:

<preserve-whitespace>
<my-first-tag>
<my-second-tag>
</preserve-whitespace>

Sometimes there isn't a convenient parent tag to add the c-whitespace="preserve" attribute

@patrick-steele-idem
Copy link
Contributor

I agree regarding introducing a <preserve-whitespace> tag. I have added Issue #11 to the backlog.

@patrick-steele-idem
Copy link
Contributor

Marking issue as closed since additional docs were added clarifying the whitespace rules for Marko. If there are still issues please let us know. Thanks.

@ivanfilhoz
Copy link

ivanfilhoz commented Nov 17, 2017

require('marko/compiler').defaultOptions.preserveWhitespace = true;

Sorry to reopen an old issue, but how to set this option with marko/express?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants