Print this page

Line breaks and consecutive spaces in outputText are removed on postback

Knowledge Article Number 000194152
Description On postback, line breaks and consecutive spaces in content displayed by outputText are removed. 
This is how it can be reproduced:

1) Create the following class:
public with sharing class PostbackIssue {
    public String selectedValue { get; set; }
    public CaseComment comment { get; set; }
    public PostbackIssue() {
        comment = new CaseComment(CommentBody = 'There   are    3    spaces   between   each   word.\r\n\r\nThere is a blank line before this one.');
    public PageReference myAction() {
        return null;
2) Create a VF page called PostbackIssue using below markup:
<apex:page controller="PostbackIssue" showHeader="false" standardStyleSheets="false" > 
    <apex:form >
        <apex:pageBlock >
            <apex:outputPanel id="panel" layout="block">
                <apex:outputText value="{!comment.CommentBody}"/>           
                <apex:commandButton value="Rerender" action="{!myAction}" rerender="panel"/>     
3) Navigate to /apex/PostbackIssue and click Rerender, line breaks and consecutive spaces will be removed.

Is this due to this Tidy's bug ( ?
If multiple spaces are required for some reason (e.g. formatting purposes), the &nbsp; entity must be used. However, in order for this entity to not itself be HTML escaped by apex, a proper "escape" option must be specified. Examples:
a) For the outputText tag, the "escape" attribute must be set to false. e.g.:
comment = new CaseComment(CommentBody = 'There&nbsp;&nbsp;&nbsp;are&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;spaces&nbsp;&nbsp;&nbsp;between&nbsp;&nbsp;&nbsp;each&nbsp;&nbsp;&nbsp;word.\r\n\r\nThere is a blank line before this one.');
Apex page:
<apex:outputText escape="false" value="{!comment.CommentBody}"/>
b) selectList / selectOptions / selectOption:
If you wish to render non-breakable spaces in options in a select list, the attribute to set is a bit different; either by setting "itemEscaped" to "false" when using apex:selectOption, or, if creating SelectOption objects in the controller, call setEscapeItem(false) on each SelectOption that has the values with the &nbsp; entities.

Having said that, the issue reproducible by following the steps outlined in the description of this article is due to Tidy (, a tool used by Visualforce to fix invalid HTML, but it is not due to the Tidy RF-1710 bug.
RF-1710 is not relevant because Tidy is not working on the VF version of the page in which the content is specified in attributes, but is rather working on the resultant HTML generated from the VF tags. So the text in question ends up being inside the content of a tag, not in an attribute. In the case of outputText, it ends up in a SPAN's content. In this case, the Tidy parser will strip away whitespace and newlines unless the content is wrapped in a PRE tag. The Tidy parser/lexer does not have the ability to honor the "white-space: pre;" CSS style, being that it is an HTML/XML-parser, but not a CSS engine.

To work around this, as stated, you may use PRE tags, or <apex:outputField> instead of <apex:outputText>.

promote demote