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.

Mathias W. wrote:

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.

You’ll want to take a look at
ActionView::Helpers::TagHelper.

  • Roderick

On 11/20/06, Roderick van Domburg [email protected]
wrote:

You’ll want to take a look at
ActionView::Helpers::TagHelper.

  • Roderick

Hmm, either I’m missing something or I think you misunderstood my
problem. tag() also returns XHTML tags (such as “
”). 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.

On 11/20/06, Mathias W. [email protected]
wrote:

Hmm, either I’m missing something or I think you misunderstood my
problem. tag() also returns XHTML tags (such as “
”). 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?

  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.

On 11/20/06, Roderick van Domburg [email protected]
wrote:

If you look at the source, you’ll notice that setting :open => true
results in HTML-compliant closing tags.

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


_Deirdre http://deirdre.net/

Mathias W. wrote:

You’ll want to take a look at
ActionView::Helpers::TagHelper.

Hmm, either I’m missing something or I think you misunderstood my
problem. tag() also returns XHTML tags (such as “
”). 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?

If you look at the source, you’ll notice that setting :open => true
results in HTML-compliant closing tags.

  • Roderick

On 11/20/06, Deirdre Saoirse M. [email protected] wrote:

Voila, you have an application-level setting.

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.

On 11/20/06, Deirdre Saoirse M. [email protected] wrote:

On 11/20/06, Roderick van Domburg [email protected] wrote:

If you look at the source, you’ll notice that setting :open => true
results in HTML-compliant closing tags.

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


_Deirdre http://deirdre.net/

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…


Regards,
Rimantas

http://rimantas.com/

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

On 1/16/07, Trevor S. [email protected] wrote:

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:
#
# html4_version_of :submit_tag
#
# …which creates a submit_tag_html4 helper that takes the same
options as
# submit_tag but which renders HTML 4.01.
#
def html4_version_of helper_name
define_method “#{helper_name}_html4” do |*args|
html = send helper_name, *args
html.gsub! " />", “>”
html
end
end

def html4_versions_of *helper_names
  helper_names.each do |helper_name|
    html4_version_of  helper_name
  end
end

end

html4_versions_of :stylesheet_link_tag, :image_tag, :text_field,
:submit_tag

end

Robert W. 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

or
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 D. 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 (>).

Mathias W. 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.

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 W. wrote in post #956422:

Mathias W. 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
http://www.marnen.org
[email protected]

On 25 Oct 2010, at 15:34, Marnen Laibow-Koser wrote:

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. 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

Robert W. 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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]