Loading

Salesforce Visualforce: Line Breaks and Consecutive Spaces Removed from apex:outputText on Postback

Fecha de publicación: Jun 2, 2026
Descripción

In Salesforce Visualforce, the apex:outputText component renders Apex controller variable values as HTML text. When a Visualforce page performs a partial postback (using the rerender attribute on a commandButton or other action component), the Visualforce engine processes the output using an HTML parsing library called Tidy. Tidy is designed to fix invalid HTML — however, it also strips multiple consecutive spaces and line break characters (such as \r\n) from text content inside HTML tags such as <span> (which apex:outputText renders as).

As a result, if a controller variable contains multiple consecutive spaces between words or blank line characters, these whitespace elements appear correctly on the initial page load. However, after any partial postback that rerenders the component, the spaces and line breaks are stripped, causing the displayed text to lose its formatting.


To reproduce this behavior: Bind an apex:outputText component to a String variable in the controller that contains multiple spaces between words and newline characters. Wrap it in an apex:outputPanel with an apex:commandButton that rerenders the panel. On the initial load the spacing appears correctly. After clicking the button to trigger a rerender, the extra spaces and line breaks are removed.


Note: This behavior is caused by the Tidy parser operating on the generated HTML output, not by a Tidy bug related to attribute-level whitespace (such as JBoss RF-1710). The Tidy parser does not preserve whitespace inside SPAN tag content unless the content is wrapped in a PRE tag.

Solución

Solution 1: Use Non-Breaking Space Entities (&nbsp;) with escape="false" on apex:outputText

Replace regular space characters in the controller string with HTML non-breaking space entities (&nbsp;). Set the escape attribute on apex:outputText to "false" to prevent Visualforce from HTML-escaping these entities before output.


Important: Use escape="false" only with fully trusted and sanitized content. Setting escape to false allows raw HTML to be rendered, which creates a cross-site scripting (XSS) risk if the content contains user-supplied data.
Example: In the controller, replace spaces between words with &nbsp; entities. In the Visualforce page, use <apex:outputText escape="false" value="{!yourVariable}"/>.

Solution 2: Use apex:outputField Instead of apex:outputText

The apex:outputField component renders SObject field values using the field's native data type formatting, which can preserve spacing for certain field types such as Long Text Area fields. If your use case involves displaying SObject field values, switching from apex:outputText to apex:outputField may resolve the whitespace stripping issue.

Solution 3: Wrap Content in an HTML PRE Tag

The Tidy parser preserves whitespace inside HTML <pre> (preformatted text) tags. Wrapping the content rendered by apex:outputText inside a <pre> tag in the Visualforce page prevents Tidy from stripping spaces and line breaks on postback.

Solution 4: Use selectOption with setEscapeItem(false)

If the whitespace issue occurs within apex:selectList / apex:selectOptions / apex:selectOption components displaying options with multiple spaces, set the itemEscaped attribute to "false" on apex:selectOption, or call setEscapeItem(false) on each SelectOption object in the controller that contains &nbsp; entities.

Número del artículo de conocimiento

000387212

 
Cargando
Salesforce Help | Article