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

Need some way of not printing whitespace #19

Closed
wch opened this issue Nov 12, 2014 · 11 comments
Closed

Need some way of not printing whitespace #19

wch opened this issue Nov 12, 2014 · 11 comments

Comments

@wch
Copy link
Collaborator

wch commented Nov 12, 2014

For example:

> div("foo", "bar")
<div>
  foo
  bar
</div> 

It would be good to be able to do something like one of the following:

div(.nospace = TRUE, "foo", "bar")
div("foo", nospace(), "bar")
nospace(div("foo", "bar"))
@hadley
Copy link
Member

hadley commented Nov 25, 2014

The main problem is the leading and trailing whitespace. Why is the default to add it?

I think the smartest thing to do would be to only add leading/trailing whitespace around block tags.

@jcheng5
Copy link
Member

jcheng5 commented Nov 25, 2014

only add leading/trailing whitespace around block tags

I'm embarrassed to not have thought of that.

@kevinushey
Copy link

I bumped into this with code of the form with(tags, pre(code(...))), as the injected whitespace will screw up the display of the first line.

@wch
Copy link
Collaborator Author

wch commented Dec 17, 2015

One thing to think about: if we want to be really correct, we'd have to also eliminate whitespace when a div has display:inline;. For example, these should look the same, with no space between A and B:

<span>A</span><span>B</span>
<div style="display:inline;">A</div><div style="display:inline;">B</div>

But unfortunately it's not possible to catch these cases in general because display is often set through a CSS file or dynamically from JavaScript.

To take a step back: I think we're adding whitespace is because it makes the HTML more readable. As discussed this issue, we've assumed that adding whitespace has no visible effect for block elements, but it does have a visible effect for inline elements; therefore, it's OK to add whitespace for block elements, but not for inline elements. But we can't tell with 100% reliability whether an element is block vs. inline just based on the tag type, because the display property can be changed.

I'm not sure if this changes what we'll do, but it's worth keeping in mind.

@jcheng5
Copy link
Member

jcheng5 commented Dec 17, 2015

FWIW, haml handles this by letting you specify the whitespace behavior. http://haml.info/docs/yardoc/file.REFERENCE.html#whitespace_removal__and_

If we did something like this we could actually leave the default behavior alone, which would be so great from a backwards compatibility perspective.

@sbliven
Copy link

sbliven commented Oct 11, 2016

There might be a better way of doing this, but I ran into this problem when I tried to use htmlTemplate as a generic templating library. In my case I was trying to generate some javascript blocks. A simplified example might be

htmlTemplate(text_="var a = \"{{toupper(a)}}\";", a="hello")

Output:

var a = "
HELLO
";

In this case the extra whitespace is completely ruining my intended purpose. I realize this diverges somewhat from the intended use of htmlTemplate, but I'd rather not include a separate template package just for this.

Adding a parameter to htmlTemplate to disable the added newlines would be great.

@wch
Copy link
Collaborator Author

wch commented Jul 31, 2018

@sanjmeh
Copy link

sanjmeh commented Dec 11, 2018

It is now a year since the last activity on this issue. The whitespace on inline HTML tags is still appearing and this makes the tag$ functions less useful. In my case, I have to scrap an entire code section as a bug was traced to a white space in the HTML that was forcibly injected by tag$ used in a shinyapp. Is there any solution within R apart from the haml mentioned above by @jcheng5?

@cpsievert
Copy link
Collaborator

cpsievert commented Aug 27, 2019

Should be closed via #131, right @wch?

@wch wch closed this as completed Aug 27, 2019
@jcubic
Copy link

jcubic commented Jan 14, 2020

There is still issue with {{ ?? }}, is this separated issue?

@wch
Copy link
Collaborator Author

wch commented Jan 15, 2020

@jcubic Do you mind filing a new issue about that?

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