1

I've got the following <apex:page> tag in a Visualforce page:

<apex:page docType="html-5.0" showHeader="false" applyHtmlTag="false" applyBodyTag="false" standardStylesheets="false" expires="900" controller="CensoredController">
    <html> ...stuff... </html>
</apex:page>

This generates the correct HTML5 doctype I need. And while there's no newline or whitespace before it, somehow Salesforce adds a newline before the HTML5 doctype.

How do I get rid of all the whitespace before the doctype?

Any whitespace (newline/spaces/tabs) put old Internet Explorers in quirksmode, and while I know of the X-UA-Compatible header, I still would like to remove the whitespace as well.

I would imagine in the JSP-world, this would accomplish what I want:

<%@ page trimDirectiveWhitespaces="true" %>
3
  • Since you've disabled standard stylesheets, you'll need to show us your styling and html to be able to answer your question.
    – crmprogdev
    Jul 30, 2014 at 12:41
  • @crmprogdev What exactly do you need? My styling is quite a bit of CSS, and so is the HTML. But I don't see how that's relevant to the question of removing all whitespace before the doctype. Jul 30, 2014 at 13:04
  • I've never noticed it creating a newline char and understood it to only establish the "visualforce container" or wrapper Salesforce uses to generate html from any code contained therein. Thus, any headers and your outer html or relevant JS/CSS related to both would seem important in order to understand your issue. From Jason's post, apparently that's not the case.(shrug) Good luck!
    – crmprogdev
    Jul 30, 2014 at 18:28

2 Answers 2

2

You have to connect the angle brackets to avoid the extra whitespace:

<apex:page docType="html-5.0" showHeader="false" applyHtmlTag="false" 
           applyBodyTag="false" standardStylesheets="false" expires="900"
           controller="CensoredController"><html>
    ...stuff...
</html>
</apex:page>

This is true in many situations when mixing namespaced elements and HTML elements.

1
  • This does not work. It only removes the whitespace between the doctype and the <html> element, not the whitespace before the doctype. Sep 16, 2014 at 14:07
0

I don't believe you can remove the whitespace before the doctype; in general Visualforce doesn't provide that level of control over the generated HTML. Empirically, I've found that removing all whitespace (including the new line) between the apex:page element and the html element removes the extra line after the doctype, but the blank line before the doctype remains.

However, I don't believe this is a problem. According to the current html 5 editor's draft, section 8.1, Writing HTML Documents (emphasis mine):

Documents must consist of the following parts, in the given order:

  1. Optionally, a single "BOM" (U+FEFF) character.
  2. Any number of comments and space characters.
  3. A DOCTYPE.
  4. Any number of comments and space characters.
  5. The root element, in the form of an html element.
  6. Any number of comments and space characters.

In the doc, "space characters" is a link to the definition:

The space characters, for the purposes of this specification, are U+0020 SPACE, "tab" (U+0009), "LF" (U+000A), "FF" (U+000C), and "CR" (U+000D).

A quick test using an HTML 5 Validator confirms that whitespace before the doctype does not affect validity.

2
  • 1
    All correct, but that's only theory. In practice, IE does go into quirksmode when there's whitespace before the doctype. This was fixed somewhere in the new versions of IE. But this is besides my question, I'd still like to remove the whitespace. Jul 30, 2014 at 13:02
  • 1
    "In theory, theory and practice are the same. In practice, they are not."
    – sfdcfox
    Aug 29, 2014 at 17:55

You must log in to answer this question.

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