18

Code is supposed to be exact and "as-is". Sometimes, showing two spaces is important. In a comment, it is not possible to show code with  multiple   spaces. This became apparent in this answer (see comments).

EDIT: apparently this bug is larger then I thought: multiple spaces here are neither preserved in normal question text. Pity (though I couldn't find this reported before, I'd assume it is).

Workaround: use Alt+0160 (windows), or U+00A0 (Unicode NBSP), as I did in the example above.

13
  • @voyλger nice alternative workaround (but try to select and copy it).
    – Abel
    Commented Nov 17, 2009 at 22:05
  • @Abel: What is the problem? On FF, If I select the text and paste it I get the spaces on the pasted text.
    – perbert
    Commented Nov 17, 2009 at 23:37
  • I used Opera (doesn't work). Now tested with IE (doesn't work) and FF (works). Looking at the code (<code>with </code><code> multiple </code><code> </code><code> spaces</code>) there are spaces (but most space is created by CSS), but when selecting they aren't "blue" in any browser, and only FF copies them. Interesting little browser quirks.
    – Abel
    Commented Nov 18, 2009 at 13:37
  • @Abel: you are right, Chrome doesn't like it either. It must be because generally in HTML 1+ space becomes 1 space. I think that firefox's (pronounce it out loud, I dare you) is the correct behavior in this case.
    – perbert
    Commented Nov 21, 2009 at 23:21
  • Firefox is correct in copying, but not in selecting. Unfortunately, the W3 and other standards bodies say little if anything about how selections are supposed to look (or what Ctrl-C should copy consecutively), that's totally up to the UI.
    – Abel
    Commented Nov 30, 2009 at 0:05
  • 3
    Couldn't code {white-space:pre-wrap} be added to the SE default stylesheets?
    – Lri
    Commented Jul 7, 2011 at 2:49
  • 2
    This is still an issue, and now unicode nbsp doesn't work either (at least as far as I can test).
    – Chris Down
    Commented Dec 14, 2011 at 16:09
  • I guess not, @Lri, as pre-wrap also enables newlines? Those don't get any love in comments.
    – Arjan
    Commented Mar 21, 2012 at 17:15
  • @Arjan But aren't newlines removed before the CSS anyway?
    – Lri
    Commented Mar 22, 2012 at 6:21
  • No, @Lri, they are not. (See the HTML page source for this comment.)
    – Arjan
    Commented Mar 22, 2012 at 6:52
  • 1
    @Oded Can you at least post an explanation as to why it is status by design or declined instead of simply flooding the front page with your retagging? Commented Feb 22, 2013 at 14:49
  • @LoremIpsum - Sorry, trying to close old old old bugs. Didn't mean to flood the front page.
    – Oded StaffMod
    Commented Feb 22, 2013 at 14:50
  • @Oded I don't mind the flooding; in fact, I'm happy the devs are closing/fixing bugs. However, without a comment/answer, I don't know if you mean "This is so old that changes since then have rendered it invalid" or "This is so lame that I don't care to respond" or "This is how it will be and I'm not going to fix it" Commented Feb 22, 2013 at 23:12

3 Answers 3

11

I've added this to our stylesheets:

code {
    white-space: pre-wrap; /* don't collapse multiple spaces in code */
}

– no idea why we never did that; it's an easy enough fix, and these days it's supported by all browsers. This is in the next build.

8
  • 10
    Well, there is one reason...
    – Tim Stone
    Commented Feb 24, 2014 at 21:42
  • 1
    @TimStone Well if that was the reason we never did it, I assume someone would've answered here :) Yep, stupid me, this behavior is right there in the spec. I read it and I still missed it.
    – balpha StaffMod
    Commented Feb 25, 2014 at 6:02
  • 1
    This is causing some problems in Firefox.
    – animuson StaffMod
    Commented Mar 29, 2014 at 15:56
  • 1
    Thanks for the pointer, @animuson. Will look.
    – balpha StaffMod
    Commented Mar 31, 2014 at 13:51
  • 1
    was this fixed correctly (if so, multiple consecutive spaces should show)? Apparently not, have you reverted the change of last year?
    – Abel
    Commented Sep 6, 2015 at 22:21
  • 2
    @Abel You're right, this was removed (or rather, overwritten) during the great CSS overhaul of 2014/2015. I'll talk to the person who made that change to find out what the intention was.
    – balpha StaffMod
    Commented Sep 7, 2015 at 5:37
  • 3
    So can the status-completed be removed please? Commented May 15, 2017 at 19:26
  • This never made it into mobile.css, which means this problem still exists on mobile. Commented Dec 7, 2019 at 17:18
1

You do have a workarround:

`with `` multiple `` `` spaces`

Looks like

with  multiple   spaces

in a comment

3
  • 1
    wowch. I'd hate to hand parse my code to add that...
    – Pollyanna
    Commented Nov 17, 2009 at 20:35
  • 1
    Unfortunately, doesn't work any longer..., see: comment with `` multiple `` `` spaces
    – Abel
    Commented Dec 19, 2011 at 15:24
  • 1
    See this post for a working workaround
    – CharlesB
    Commented Feb 16, 2012 at 9:01
1

In one of my comments I've suggested to apply sed s/ / /g… which looks completely stupid because spaces are not rendered properly (as you can (not) see).

Now, I've tested the CSS snipset provided in a comment by Lri:

code { white-space:pre-wrap; }

It does the job of fixing this. Why isn't this applied yet?

6
  • "Why isn't this applied yet?" >> my guess, it doesn't have priority. Which seems kinda odd for a site about coding though...
    – Abel
    Commented Mar 21, 2012 at 8:26
  • Or, @Abel, as it also enables newlines? Those don't get any love in comments.
    – Arjan
    Commented Mar 21, 2012 at 17:14
  • @Arjan: everything is filtered anyway for whatever illegal characters are disallowed. Can hardly think it's an issue to either replace spaces with non-breaking spaces, or to remove any \r\n.
    – Abel
    Commented Mar 21, 2012 at 20:34
  • 1
    Then I'd not vote for replacing spaces with non-breaking spaces, to avoid any issues with copying & pasting code fragments, @Abel. That may indeed make CSS with removing line breaks a good option then. (Though that might need some conversion of existing comments, if comments are sanitized upon saving rather than upon rendering.)
    – Arjan
    Commented Mar 21, 2012 at 21:03
  • @Arjan: while I agree (and I think this is up to the implementers, if at all they want to do something about this), copy/pasting with nbsp's in it doesn't give me trouble with Visual Studio. Not sure of other editors though.
    – Abel
    Commented Mar 21, 2012 at 21:13
  • 1
    @Abel At least on OS X non-breaking spaces are not usually converted to normal spaces, and running code with them results in an error.
    – Lri
    Commented Mar 22, 2012 at 6:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .