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:

"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?? -- Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!

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:

And javascript_tag(“alert(‘foo’)”) produces:

Both of which are perfectly valid XHTML

Alex W. wrote:

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

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:

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

And javascript_tag(“alert(‘foo’)”) produces:

//<![CDATA[
//<![CDATA[<![CDATA[

Hi –

On Sun, 19 Nov 2006, Phlip wrote:

David


David A. Black | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

On Sun, Nov 19, 2006 at 06:49:28AM +0100, Alex W. wrote:
} 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
in a .rhtml file, when it gets to the browser it has become . I
hope
this is fixed in edge, but I don’t have time to check.

–Greg

Alex W. wrote:

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

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:

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

And javascript_tag(“alert(‘foo’)”) produces:

//<![CDATA[

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

//<![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... -- Phlip

Hi –

On Sun, 19 Nov 2006, Gregory S. 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
in a .rhtml file, when it gets to the browser it has become . I hope
this is fixed in edge, but I don’t have time to check.

A brief test (sticking
into my default layout) suggests that
it’s been fixed in edge.

David


David A. Black | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

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

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

Parenthetically, what’s ?

in a .rhtml file, when it gets to the browser it has become . 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:


Phlip

class Test::Unit::TestCase

def assert_xpath(path, &block)
node = XPath.first(@xdoc, path)
assert_not_nil node
was_xdoc = @xdoc
@xdoc = node
block.call(@xdoc) if block
return node
ensure
@xdoc = was_xdoc
end # CONSIDER better diagnostics?

def assert_no_xpath path
node = XPath.first(@xdoc, path)
assert_nil node
end

end

On 19 Nov 2006, at 05:21, Phlip wrote:

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.

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.

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   and its ilk raise their ugly head.

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

But maybe I’m only experienced with REXML idiosyncracies.

On Sun, Nov 19, 2006 at 06:10:14PM -0000, Phlip wrote:
}
} The spectre of   and its ilk raise their ugly head.
}
} I suspect that some XMLs deny the existence of  , so you have to
} add some hack, such as the matching Unicode point.
}
} But maybe I’m only experienced with REXML idiosyncracies.

See ongoing by Tim Bray · An RX for Ruby Performance
for
some discussion of XML parsing in Ruby.

–Greg