pure XHTML

Railsers:

I have this friend who keeps claiming that all HTML in a website
should be pure XHTML.

He claims this ideal follows a general and a specific guideline. The
general guideline is that all code should be of the highest possible
quality, with no compromises. Specifically, HTML should never rely on
"browser forgiveness", and get by with mis-matched tags, ill-formed
markup, missing required attributes, etc. No more programming websites
in Notepad.

The specific guideline is that if your code is pure XHTML, you can
parse it with XPath at test time. XPath makes a great way to query
into a page and chop out details, competitive with assert_select() or
assert_tag().

So I don't want to tell my friend that Rails doesn't produce pure
XHTML. For example, here's a generated script tag:

<script
  type='text/javascript'
  src='/javascripts/dragdrop.js?1162442777' >

"What's the hell is wrong with that???" I hear you scream (clear
across the 'net).

It's the first problem XPath finds: the > should be />

Any chance of fixing this, for all of Rails? To appease my friend??

Alex Wayne wrote:

according to my uses, javascript_include_tag(:foo) produces:

  <script src="/javascripts/foo.js" type="text/javascript"></script>

Uh, Edge?

My standard layout sez:

<%= stylesheet_link_tag('application') +
      javascript_include_tag('prototype') +
      javascript_include_tag(:defaults) %>

(Someone got creative with the style there.)

Curiously, look what it's producing:

<link href='/stylesheets/application.css?1163738349' rel='Stylesheet'
type='text/css' media='screen' />
<script type='text/javascript'
src='/javascripts/prototype.js?1162442777'>

The LINK has a /> on the end and the SCRIPT

And javascript_tag("alert('foo')") produces:

  <script type="text/javascript">
  //<![CDATA[

<script type='text/javascript'>
//<![CDATA[<![CDATA[

Alex Wayne wrote:

according to my uses, javascript_include_tag(:foo) produces:

  <script src="/javascripts/foo.js" type="text/javascript"></script>

Uh, Edge?

My standard layout sez:

<%= stylesheet_link_tag('application') +
      javascript_include_tag('prototype') +
      javascript_include_tag(:defaults) %>

(Someone got creative with the style there;)

Curiously, look what it produces:

<link href='/stylesheets/application.css?1163738349' rel='Stylesheet'
type='text/css' media='screen' />
<script type='text/javascript'
src='/javascripts/prototype.js?1162442777'>

The LINK has a /> on the end and the SCRIPT tag doesn't!

And javascript_tag("alert('foo')") produces:

  <script type="text/javascript">
  //<![CDATA[

One of my JavaScript generators, periodically_call_remote(), produces
this:

<script type='text/javascript'>
//<![CDATA[<![CDATA[

That SCRIPT contents sure is insulated from decade-old browsers!

All this is from 'puts html_document.root', in a functional test. I'm
not concerned about the specific details so long as XHTML is the
general goal...

Hi --

My standard layout sez:

<%= stylesheet_link_tag('application') +
     javascript_include_tag('prototype') +
     javascript_include_tag(:defaults) %>

(Someone got creative with the style there;)

Curiously, look what it produces:

<link href='/stylesheets/application.css?1163738349' rel='Stylesheet'
type='text/css' media='screen' />
<script type='text/javascript'
src='/javascripts/prototype.js?1162442777'>

I guess it's fixed in edge; I dropped your lines into a layout and
got:

<link href="/stylesheets/application.css" media="screen"
rel="Stylesheet" type="text/css" />
<script src="/javascripts/prototype.js?1163852948"
type="text/javascript"></script>
<script src="/javascripts/prototype.js?1163852948"
type="text/javascript"></script>
<script src="/javascripts/effects.js?1163852948"
type="text/javascript"></script>
<script src="/javascripts/dragdrop.js?1163852948"
type="text/javascript"></script>
<script src="/javascripts/controls.js?1163852948"
type="text/javascript"></script>
<script src="/javascripts/application.js?1163852948"
type="text/javascript"></script>

David

} Phlip wrote:
} > Railsers:
} >
} > I have this friend who keeps claiming that all HTML in a website
} > should be pure XHTML.
} >
} > So I don't want to tell my friend that Rails doesn't produce pure
} > XHTML. For example, here's a generated script tag:
} >
} > <script
} > type='text/javascript'
} > src='/javascripts/dragdrop.js?1162442777' >
} >
} > "What's the hell is wrong with that???" I hear you scream (clear
} > across the 'net).
} >
} > It's the first problem XPath finds: the > should be />
} >
} > Any chance of fixing this, for all of Rails? To appease my friend??
}
} Your friend is a smart dude. But so is Rails.
[...]

I haven't tried this on edge, but I will mention that current stable Rails
actually strips out the / on empty XML tags. For example, if I have <wbr />
in a .rhtml file, when it gets to the browser it has become <wbr>. I hope
this is fixed in edge, but I don't have time to check.

--Greg

Hi --

Gregory Seidman wrote:

I haven't tried this on edge, but I will mention that current stable Rails
actually strips out the / on empty XML tags. For example, if I have <wbr />

Parenthetically, what's <wbr/>?

in a .rhtml file, when it gets to the browser it has become <wbr>. I hope
this is fixed in edge, but I don't have time to check.

This gets us close to the FAQ "should I switch to Edge?" The answer is,
as usual, "If you have to ask, you shouldn't!" Any project should use
that metric as the definition for its Edge version! :wink:

Now here's why we like XPath. I have a simple assert_xpath that works
like this:

@xdoc = Document.new(someFragment)

  assert_xpath '//span[@id="defeat_myspace"]' do
    assert_xpath '/img[@src="goForIt.png"]'
  end

The source is below my sig. Currently, I can't drop an entire
(non-Edge) page into someFragment, so I use assert_tag or assert_select
to extract one, or I get one directly from one of my own
XHTML-generating methods.

The assertion passes a node to the sought path into its block. This
allows the inner block to assert as if all the outer block's path were
prepended to its own path. So in just a few lines of code I compete
with assert_select(), and blow assert_tag() away.

XPath's query system is awesome enough to let me embed the = != < >
criteria right into its selection string.

For my next magical trick, I will create an XPath generator that lets
you go assert_xpath.span(:id=>'defeat_myspace) etc. Ruby is at the
forefront of the languages that make dicking with your test rig much
more fun that working on your smegging project! :wink:

There's talk here of "fixed on edge", but I'm *not* running edge, and
my code generates perfectly good XHTML. No stripping of trailing
slashes, and script tags are closed.

I'm running rails 1.1.6, ruby 1.8.4 on win32.

-Brian

For browsers, it won’t really make a difference. In fact, some of the current browsers don’t understand XHTML at all (Internet Exploder 6 for example). Other browsers like Safari do, but only if the header coming from the server tells it it’s XHTML (application/xhtml+xml). You should really read this blog article from “Surfin Safari”. It’s an eye-opener and is valid for all current browsers.

http://webkit.org/blog/?p=68

That said, I like to develop my sites using XHTML and validating at W3C (the validator doesn’t need the http header, it valides according to the doctype set), just to refrain myself from mixing in presentation tags (which belong in the CSS file).

Best regards

Peter De Berdt

The spectre of &nbsp; and its ilk raise their ugly head.

I suspect that some XMLs deny the existence of &nbsp;, so you have to
add some hack, such as the matching Unicode point.

But maybe I'm only experienced with REXML idiosyncracies.

}
} The spectre of &nbsp; and its ilk raise their ugly head.
}
} I suspect that some XMLs deny the existence of &nbsp;, so you have to
} add some hack, such as the matching Unicode point.
}
} But maybe I'm only experienced with REXML idiosyncracies.

See http://www.tbray.org/ongoing/When/200x/2006/11/09/Optimizing-Ruby for
some discussion of XML parsing in Ruby.

--Greg