Update: Saw GrandFather's reply. There is some misunderstanding. I guess that that was my fault, and I could have expressed myself more clearly. I have never doubted the merit of his code. The intention of this reply was/is not about whether his orginal HTML was valid. What I am saying is that, as_html converts a valid document to an invalid document, and that is a bug. To prove that, I needed a valid HTML document (in a more strict sense) that can pass the validation service, and that's all why I modified the original code. Otherwise, both HTML's before and after convertion fail the validation, and I cannot prove my point. No worries, GrandFather ;-)

=========================================

This is a bug, by HTML 4.01 specification. You do not need to be familiar with the specification, we can use W3C validate service to verify those HTML documents in this reply.

I modified your code a little bit to contain a valid HTML document. The HTML document passed W3C validation as tentatively valid.

use strict; use warnings; use HTML::TreeBuilder; my $data = do {local $/ = ""; <DATA>}; my $tree = HTML::TreeBuilder->new; $tree->store_comments(1); $tree->store_declarations(1); $tree->parse ($data); $tree->eof (); print $tree->as_HTML(); __DATA__ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <HTML> <HEAD> <TITLE>My first HTML document</TITLE> </HEAD> <BODY> <P>Hello world! </BODY> </HTML>

Run this program it generates:

<html><head><title>My first HTML document</title></head><body><p>Hello + world! </body><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"></html>

And this generated HTML does not pass the validate service. It complains that DOCTYPE cannot be found and is misplaced.


In reply to Re: HTML::TreeBuilder bug or feature? by pg
in thread HTML::TreeBuilder bug or feature? by GrandFather

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.