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

rmWhitespace incorrectly alters HTML structure #406

Closed
nwoltman opened this issue Nov 14, 2018 · 1 comment
Closed

rmWhitespace incorrectly alters HTML structure #406

nwoltman opened this issue Nov 14, 2018 · 1 comment

Comments

@nwoltman
Copy link
Contributor

I would expect the rendered HTML to look exactly the same no matter the value of the rmWhitespace option. However, when EJS code is structured in certain ways, rmWhitespace causes the rendered HTML to be structured differently.

view.ejs

<h2>
  <%- 'EJS' %>
  Title
</h2>
<h2>
  HTML
  Title
</h2>
<div>
  <span>1</span>
  <span>2</span>

  <span>3</span>
</div>

index.js

const ejs = require('ejs');
const fs = require('fs');

const template = fs.readFileSync('./view.ejs', 'utf8');
const render = ejs.compile(template, {
  rmWhitespace: true,
});

console.log(render());

Output with rmWhitespace: false

<h2>
  EJS
  Title
</h2>
<h2>
  HTML
  Title
</h2>
<div>
  <span>1</span>
  <span>2</span>

  <span>3</span>
</div>

ejs-bug-expected

Output with rmWhitespace: true

<h2>
EJSTitle
</h2>
<h2>
HTML
Title
</h2>
<div>
<span>1</span>
<span>2</span><span>3</span>
</div>

ejs-bug

Notice how rmWhitespace: true removes all whitespace (including newlines) between EJS and Title as well as <span>2</span> and <span>3</span>.

This happens with both LF and CRLF line endings.

The EJS code that seems to cause this to happen is:

  • EJS tags <%- %> and <%= %>
  • A blank line between lines of HTML
@mde
Copy link
Owner

mde commented Nov 15, 2018

This highlights the problem of giving EJS the responsibility for (re-)formatting the output. And I think we've made things worse by providing multiple ways to do it now. Ultimately we should probably have the basic slurp, and then provide some sort of user-settable filter property that people can plug into to format the final output however they want.

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

2 participants