Forcing rails to output HTML4 rather then XHTML?

Hi all,

Is there any way to make functions like stylesheet_link_tag and
image_tag output HTML4 rather then XHTML? (i.e. without the trailing
slash in the tags). Both functions are very helpful in keeping the
site portable and such, but if they're making my HTML code invalid I
just cannot use them.

And no, switching to XHTML is not an option.

Thanks for your time,
Mathias.

Hmm, either I'm missing something or I think you misunderstood my
problem. tag() also returns XHTML tags (such as "<br />"). I just want
the functionality of image_tag and stylesheet_link_tag, but I want the
produced HTML code to end with just ">" and not "/>" (the former being
valid HTML 4.01 and the latter valid XHTML 1.0+).

I was wondering if there perhaps was some application level setting
that would tell Rails that I want HTML 4.01 output rather then XHTML
1.0 from those two functions. Or some undocumented option or
something?

Thanks for the link though,
Mathias.

1) check out rails into vendor/rails.

2) edit the one-liner in tag as pointed out in response to your message.

Voila, you have an application-level setting.

Better idea than mine. Must drink coffee before reading list in the morning.

Thank you! Sometimes you just need someone to put up that big yellow
arrow for you to find what's really right in front of you. That worked
like a charm!

Thanks a bunch! You too Roderick!

Mathias.

I tried that one first but I didn't see how I could pass the :open =>
true bit through stylesheet_link_tag, but I'm starting to think I
could use some coffee too (but it's evening here ;). I'm a bit rusty
as well...

Mathias.

I tried that one first but I didn't see how I could pass the :open =>
true bit through stylesheet_link_tag, but I'm starting to think I
could use some coffee too (but it's evening here ;). I'm a bit rusty
as well...

Last time I checked there was no simple, unhacky way... Which is sad.
Knowing thing or two about HTML and XHTML I am firmly in HTML4.01 Strict camp
and that I miss is one-line configuration option ir environemnt.rb:
config.html_mode :html | :xhtml...

I realize that this is an old thread, but for anyone looking, here’s the solution I just used.

in application_helper.rb

def stylesheet_link_tag_html4( _n )
return stylesheet_link_tag( _n ).gsub( ’ />’, ‘>’ )

end

Not exactly elegant, but minimally-invasive and easy to understand.
It spares you from having to edit your checked out version of rails (as mentioned above), which you might upgrade at some point.

Trevor

Hi Trevor,

I almost missed out on your reply there. Even though it's a bit
"hack-y" it still beats having to freeze Rails and do the change
there. Could you perhaps briefly explain exactly what you're doing? I
understand the gsub bit, but exactly how does this tie into Rails'
methods? Might be useful both in case I need to "patch" something
later and also to do a similar fix for image_tag which also outputs
XHTML by default.

Thanks for the tip though,
Mathias.

If you want to make _html4 versions of many helpers then this might
help:

module ApplicationHelper

  class << self
    # Supports making alternative versions of Rails helpers that output
HTML
    # 4.01 markup as opposed to XHTML. Example:

Mathias Wittlock wrote in post #170114:

Is there any way to make functions like stylesheet_link_tag and
image_tag output HTML4 rather then XHTML? (i.e. without the trailing
slash in the tags). Both functions are very helpful in keeping the
site portable and such, but if they're making my HTML code invalid I
just cannot use them.

This is definitely something that needs to be resolved for Rails (if
it's not already and I just don't know about it).

However, I say this not for HTML 4 compliance. I couldn't care less
about HTML 4. I left that in the dust quite a while back. I say this
because I want HTML 5 compliance. HTML 5 will officially support both
HTML and XHTML formats. Rails needs to be configureable to work with
either IMHO.

I have no idea how difficult of a proposition that is though.

Robert Walker wrote in post #956422:

However, I say this not for HTML 4 compliance. I couldn't care less
about HTML 4. I left that in the dust quite a while back. I say this
because I want HTML 5 compliance. HTML 5 will officially support both
HTML and XHTML formats. Rails needs to be configureable to work with
either IMHO.

Actually, on second thought I think that HTML 5 validates with either
<br /> or <br> when using HTML format. The short tag format would, of
course, be necessary in the XHTML format since it must validate as XML.

Rails 3 (last time I fooled with it, which was pre-release) shipped with a completely HTML5 scaffold system, encouraging you to write your own bits in HTML5 as well.

Walter

Walter Davis wrote in post #956434:

Rails 3 (last time I fooled with it, which was pre-release) shipped
with a completely HTML5 scaffold system, encouraging you to write your
own bits in HTML5 as well.

Yes, Item #6 below confirms my earlier thoughts about the optional
self-closing tags.

Start tags must have the following format:

1. The first character of a start tag must be a U+003C LESS-THAN SIGN
character (<).

2. The next few characters of a start tag must be the element's tag
name.

3. If there are to be any attributes in the next step, there must first
be one or more space characters.

4. Then, the start tag may have a number of attributes, the syntax for
which is described below. Attributes may be separated from each other by
one or more space characters.

5. After the attributes, or after the tag name if there are no
attributes, there may be one or more space characters. (Some attributes
are required to be followed by a space. See the attributes section
below.)

6. Then, if the element is one of the void elements, or if the element
is a foreign element, then there may be a single U+002F SOLIDUS
character (/). This character has no effect on void elements, but on
foreign elements it marks the start tag as self-closing.

7. Finally, start tags must be closed by a U+003E GREATER-THAN SIGN
character (>).

Robert Walker wrote in post #956422:

Mathias Wittlock wrote in post #170114:

Is there any way to make functions like stylesheet_link_tag and
image_tag output HTML4 rather then XHTML? (i.e. without the trailing
slash in the tags). Both functions are very helpful in keeping the
site portable and such, but if they're making my HTML code invalid I
just cannot use them.

This is definitely something that needs to be resolved for Rails (if
it's not already and I just don't know about it).

It's been resolved for years. Try the html_output plugin.

However, I say this not for HTML 4 compliance. I couldn't care less
about HTML 4. I left that in the dust quite a while back.

Why? I thought it was the best format to use for older browsers that
don't know from HTML 5.

Best,

Marnen Laibow-Koser wrote in post #956459:

Why? I thought it was the best format to use for older browsers that
don't know from HTML 5.

Only because I don't have any sites build in Rails that I care about
supporting older browser. I use HTML 5 markup and do my best to make it
work well for all reasonably modern browsers.

If the sites I currently have look bad for someone visiting with with
older browsers I really don't care. If I were building commercial sites
where it actually mattered then I might care more.

I don't think it's such as bad thing to not perfectly support older
browsers. The sooner we can get people to stop using them the better off
we are. I am quite aware that not everyone is in a position to be able
to do this, but thankfully for my sites it's not a major concern.

Robert Walker wrote in post #956772:
[...]

I don't think it's such as bad thing to not perfectly support older
browsers.

Not perfectly, perhaps, but as well as possible. I am not sure that
providing HTML 5 markup meets that goal. However, I haven't yet done
much research about older browsers' support of HTML 5.

The sooner we can get people to stop using them the better off
we are.

That's true, but kind of beside the point.

I am quite aware that not everyone is in a position to be able
to do this,

Exactly.

but thankfully for my sites it's not a major concern.

How could it ever not be one?

Best,

I think the poster meant something like: I use some of the newer CSS features and maybe some HTML5 attributes. Older browsers will simply ignore those and move on. Trying to get the same result across all browsers and versions is not necessary anyway. If you use IE6, you accept that you might miss out on some of the new goodies. As long as the page renders and is usable, that’s fine imo.

Best regards

Peter De Berdt

Peter De Berdt wrote in post #956885:

I don't think it's such as bad thing to not perfectly support older
browsers.

Not perfectly, perhaps, but as well as possible. I am not sure that
providing HTML 5 markup meets that goal. However, I haven't yet done
much research about older browsers' support of HTML 5.

I think the poster meant something like: I use some of the newer CSS
features and maybe some HTML5 attributes. Older browsers will simply
ignore those and move on.

...or not. Yes, of course it's fine to use new attributes that older
browsers will ignore. However, HTML 5 differs from HTML 4 not just in
its repertoire of attributes and elements, but also *in its basic
syntax* -- HTML 5 is no longer a subset of SGML as HTML ≤4 is. That's
the part that (potentially) breaks graceful degradation.

Trying to get the same result across all
browsers and versions is not necessary anyway. If you use IE6, you
accept that you might miss out on some of the new goodies. As long as
the page renders and is usable, that's fine imo.

Right. But when the syntax -- not just the semantics -- changes, then I
start worrying. Time for me to do more research.

Best regards

Peter De Berdt

Best,

Marnen Laibow-Koser wrote in post #956924:

Right. But when the syntax -- not just the semantics -- changes, then I
start worrying. Time for me to do more research.

Okay, so let's talk specifics. Assuming valid HTML 5 markup with no HTML
5 specific features, what breaks on what browsers.

Is this speculation that things might break on older browsers? Or does
anyone have concrete examples for certain browsers and versions?

For example, does anyone know if the use of self closing tags (e.g. <br
/>) actually breaks any of the reasonably modern browsers (i.e. IE
6.0+), of course assuming validating HTML 5 markup?