Gestionnaire de fichiers - Editer - /usr/local/share/man/man3/HTML::Element.3pm
Arrière
.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "HTML::Element 3" .TH HTML::Element 3 "2020-03-05" "perl v5.16.3" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" HTML::Element \- Class for objects that represent HTML elements .SH "VERSION" .IX Header "VERSION" This document describes version 5.07 of HTML::Element, released August 31, 2017 as part of HTML-Tree. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 3 \& use HTML::Element; \& $a = HTML::Element\->new(\*(Aqa\*(Aq, href => \*(Aqhttp://www.perl.com/\*(Aq); \& $a\->push_content("The Perl Homepage"); \& \& $tag = $a\->tag; \& print "$tag starts out as:", $a\->starttag, "\en"; \& print "$tag ends as:", $a\->endtag, "\en"; \& print "$tag\e\*(Aqs href attribute is: ", $a\->attr(\*(Aqhref\*(Aq), "\en"; \& \& $links_r = $a\->extract_links(); \& print "Hey, I found ", scalar(@$links_r), " links.\en"; \& \& print "And that, as HTML, is: ", $a\->as_HTML, "\en"; \& $a = $a\->delete; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" (This class is part of the HTML::Tree dist.) .PP Objects of the HTML::Element class can be used to represent elements of \s-1HTML\s0 document trees. These objects have attributes, notably attributes that designates each element's parent and content. The content is an array of text segments and other HTML::Element objects. A tree with HTML::Element objects as nodes can represent the syntax tree for a \s-1HTML\s0 document. .SH "HOW WE REPRESENT TREES" .IX Header "HOW WE REPRESENT TREES" Consider this \s-1HTML\s0 document: .PP .Vb 9 \& <html lang=\*(Aqen\-US\*(Aq> \& <head> \& <title>Stuff</title> \& <meta name=\*(Aqauthor\*(Aq content=\*(AqJojo\*(Aq> \& </head> \& <body> \& <h1>I like potatoes!</h1> \& </body> \& </html> .Ve .PP Building a syntax tree out of it makes a tree-structure in memory that could be diagrammed as: .PP .Vb 11 \& html (lang=\*(Aqen\-US\*(Aq) \& / \e \& / \e \& / \e \& head body \& /\e \e \& / \e \e \& / \e \e \& title meta h1 \& | (name=\*(Aqauthor\*(Aq, | \& "Stuff" content=\*(AqJojo\*(Aq) "I like potatoes" .Ve .PP This is the traditional way to diagram a tree, with the \*(L"root\*(R" at the top, and it's this kind of diagram that people have in mind when they say, for example, that \*(L"the meta element is under the head element instead of under the body element\*(R". (The same is also said with \&\*(L"inside\*(R" instead of \*(L"under\*(R" \*(-- the use of \*(L"inside\*(R" makes more sense when you're looking at the \s-1HTML\s0 source.) .PP Another way to represent the above tree is with indenting: .PP .Vb 8 \& html (attributes: lang=\*(Aqen\-US\*(Aq) \& head \& title \& "Stuff" \& meta (attributes: name=\*(Aqauthor\*(Aq content=\*(AqJojo\*(Aq) \& body \& h1 \& "I like potatoes" .Ve .PP Incidentally, diagramming with indenting works much better for very large trees, and is easier for a program to generate. The \f(CW\*(C`$tree\->dump\*(C'\fR method uses indentation just that way. .PP However you diagram the tree, it's stored the same in memory \*(-- it's a network of objects, each of which has attributes like so: .PP .Vb 4 \& element #1: _tag: \*(Aqhtml\*(Aq \& _parent: none \& _content: [element #2, element #5] \& lang: \*(Aqen\-US\*(Aq \& \& element #2: _tag: \*(Aqhead\*(Aq \& _parent: element #1 \& _content: [element #3, element #4] \& \& element #3: _tag: \*(Aqtitle\*(Aq \& _parent: element #2 \& _content: [text segment "Stuff"] \& \& element #4 _tag: \*(Aqmeta\*(Aq \& _parent: element #2 \& _content: none \& name: author \& content: Jojo \& \& element #5 _tag: \*(Aqbody\*(Aq \& _parent: element #1 \& _content: [element #6] \& \& element #6 _tag: \*(Aqh1\*(Aq \& _parent: element #5 \& _content: [text segment "I like potatoes"] .Ve .PP The \*(L"treeness\*(R" of the tree-structure that these elements comprise is not an aspect of any particular object, but is emergent from the relatedness attributes (_parent and _content) of these element-objects and from how you use them to get from element to element. .PP While you could access the content of a tree by writing code that says "access the 'src' attribute of the root's \fIfirst\fR child's \fIseventh\fR child's \fIthird\fR child\*(L", you're more likely to have to scan the contents of a tree, looking for whatever nodes, or kinds of nodes, you want to do something with. The most straightforward way to look over a tree is to \*(R"traverse" it; an HTML::Element method (\f(CW\*(C`$h\->traverse\*(C'\fR) is provided for this purpose; and several other HTML::Element methods are based on it. .PP (For everything you ever wanted to know about trees, and then some, see Niklaus Wirth's \fIAlgorithms + Data Structures = Programs\fR or Donald Knuth's \fIThe Art of Computer Programming, Volume 1\fR.) .SS "Weak References" .IX Subsection "Weak References" \&\s-1TL\s0;DR summary: \f(CW\*(C`use\ HTML::TreeBuilder\ 5\ \-weak;\*(C'\fR and forget about the \f(CW\*(C`delete\*(C'\fR method (except for pruning a node from a tree). .PP Because HTML::Element stores a reference to the parent element, Perl's reference-count garbage collection doesn't work properly with HTML::Element trees. Starting with version 5.00, HTML::Element uses weak references (if available) to prevent that problem. Weak references were introduced in Perl 5.6.0, but you also need a version of Scalar::Util that provides the \f(CW\*(C`weaken\*(C'\fR function. .PP Weak references are enabled by default. If you want to be certain they're in use, you can say \f(CW\*(C`use\ HTML::Element\ 5\ \-weak;\*(C'\fR. You must include the version number; previous versions of HTML::Element ignored the import list entirely. .PP To disable weak references, you can say \f(CW\*(C`use\ HTML::Element\ \-noweak;\*(C'\fR. This is a global setting. \fBThis feature is deprecated\fR and is provided only as a quick fix for broken code. If your code does not work properly with weak references, you should fix it immediately, as weak references may become mandatory in a future version. Generally, all you need to do is keep a reference to the root of the tree until you're done working with it. .PP Because HTML::TreeBuilder is a subclass of HTML::Element, you can also import \f(CW\*(C`\-weak\*(C'\fR or \f(CW\*(C`\-noweak\*(C'\fR from HTML::TreeBuilder: e.g. \&\f(CW\*(C`use\ HTML::TreeBuilder:\ 5\ \-weak;\*(C'\fR. .SH "BASIC METHODS" .IX Header "BASIC METHODS" .SS "new" .IX Subsection "new" .Vb 1 \& $h = HTML::Element\->new(\*(Aqtag\*(Aq, \*(Aqattrname\*(Aq => \*(Aqvalue\*(Aq, ... ); .Ve .PP This constructor method returns a new HTML::Element object. The tag name is a required argument; it will be forced to lowercase. Optionally, you can specify other initial attributes at object creation time. .SS "attr" .IX Subsection "attr" .Vb 2 \& $value = $h\->attr(\*(Aqattr\*(Aq); \& $old_value = $h\->attr(\*(Aqattr\*(Aq, $new_value); .Ve .PP Returns (optionally sets) the value of the given attribute of \f(CW$h\fR. The attribute name (but not the value, if provided) is forced to lowercase. If trying to read the value of an attribute not present for this element, the return value is undef. If setting a new value, the old value of that attribute is returned. .PP If methods are provided for accessing an attribute (like \f(CW\*(C`$h\->tag\*(C'\fR for \&\*(L"_tag\*(R", \f(CW\*(C`$h\->content_list\*(C'\fR, etc. below), use those instead of calling attr \f(CW\*(C`$h\->attr\*(C'\fR, whether for reading or setting. .PP Note that setting an attribute to \f(CW\*(C`undef\*(C'\fR (as opposed to "", the empty string) actually deletes the attribute. .SS "tag" .IX Subsection "tag" .Vb 2 \& $tagname = $h\->tag(); \& $h\->tag(\*(Aqtagname\*(Aq); .Ve .PP Returns (optionally sets) the tag name (also known as the generic identifier) for the element \f(CW$h\fR. In setting, the tag name is always converted to lower case. .PP There are four kinds of \*(L"pseudo-elements\*(R" that show up as HTML::Element objects: .IP "Comment pseudo-elements" 4 .IX Item "Comment pseudo-elements" These are element objects with a \f(CW\*(C`$h\->tag\*(C'\fR value of \*(L"~comment\*(R", and the content of the comment is stored in the \*(L"text\*(R" attribute (\f(CW\*(C`$h\->attr("text")\*(C'\fR). For example, parsing this code with HTML::TreeBuilder... .Sp .Vb 3 \& <!\-\- I like Pie. \& Pie is good \& \-\-> .Ve .Sp produces an HTML::Element object with these attributes: .Sp .Vb 4 \& "_tag", \& "~comment", \& "text", \& " I like Pie.\en Pie is good\en " .Ve .IP "Declaration pseudo-elements" 4 .IX Item "Declaration pseudo-elements" Declarations (rarely encountered) are represented as HTML::Element objects with a tag name of \*(L"~declaration\*(R", and content in the \*(L"text\*(R" attribute. For example, this: .Sp .Vb 1 \& <!DOCTYPE foo> .Ve .Sp produces an element whose attributes include: .Sp .Vb 1 \& "_tag", "~declaration", "text", "DOCTYPE foo" .Ve .IP "Processing instruction pseudo-elements" 4 .IX Item "Processing instruction pseudo-elements" PIs (rarely encountered) are represented as HTML::Element objects with a tag name of \*(L"~pi\*(R", and content in the \*(L"text\*(R" attribute. For example, this: .Sp .Vb 1 \& <?stuff foo?> .Ve .Sp produces an element whose attributes include: .Sp .Vb 1 \& "_tag", "~pi", "text", "stuff foo?" .Ve .Sp (assuming a recent version of HTML::Parser) .IP "~literal pseudo-elements" 4 .IX Item "~literal pseudo-elements" These objects are not currently produced by HTML::TreeBuilder, but can be used to represent a \*(L"super-literal\*(R" \*(-- i.e., a literal you want to be immune from escaping. (Yes, I just made that term up.) .Sp That is, this is useful if you want to insert code into a tree that you plan to dump out with \f(CW\*(C`as_HTML\*(C'\fR, where you want, for some reason, to suppress \f(CW\*(C`as_HTML\*(C'\fR's normal behavior of amp-quoting text segments. .Sp For example, this: .Sp .Vb 6 \& my $literal = HTML::Element\->new(\*(Aq~literal\*(Aq, \& \*(Aqtext\*(Aq => \*(Aqx < 4 & y > 7\*(Aq \& ); \& my $span = HTML::Element\->new(\*(Aqspan\*(Aq); \& $span\->push_content($literal); \& print $span\->as_HTML; .Ve .Sp prints this: .Sp .Vb 1 \& <span>x < 4 & y > 7</span> .Ve .Sp Whereas this: .Sp .Vb 4 \& my $span = HTML::Element\->new(\*(Aqspan\*(Aq); \& $span\->push_content(\*(Aqx < 4 & y > 7\*(Aq); \& # normal text segment \& print $span\->as_HTML; .Ve .Sp prints this: .Sp .Vb 1 \& <span>x < 4 & y > 7</span> .Ve .Sp Unless you're inserting lots of pre-cooked code into existing trees, and dumping them out again, it's not likely that you'll find \&\f(CW\*(C`~literal\*(C'\fR pseudo-elements useful. .SS "parent" .IX Subsection "parent" .Vb 2 \& $parent = $h\->parent(); \& $h\->parent($new_parent); .Ve .PP Returns (optionally sets) the parent (aka \*(L"container\*(R") for this element. The parent should either be undef, or should be another element. .PP You \fBshould not\fR use this to directly set the parent of an element. Instead use any of the other methods under \*(L"Structure-Modifying Methods\*(R", below. .PP Note that \f(CW\*(C`not($h\->parent)\*(C'\fR is a simple test for whether \f(CW$h\fR is the root of its subtree. .SS "content_list" .IX Subsection "content_list" .Vb 2 \& @content = $h\->content_list(); \& $num_children = $h\->content_list(); .Ve .PP Returns a list of the child nodes of this element \*(-- i.e., what nodes (elements or text segments) are inside/under this element. (Note that this may be an empty list.) .PP In a scalar context, this returns the count of the items, as you may expect. .SS "content" .IX Subsection "content" .Vb 1 \& $content_array_ref = $h\->content(); # may return undef .Ve .PP This somewhat deprecated method returns the content of this element; but unlike content_list, this returns either undef (which you should understand to mean no content), or a \fIreference to the array\fR of content items, each of which is either a text segment (a string, i.e., a defined non-reference scalar value), or an HTML::Element object. Note that even if an arrayref is returned, it may be a reference to an empty array. .PP While older code should feel free to continue to use \f(CW\*(C`$h\->content\*(C'\fR, new code should use \f(CW\*(C`$h\->content_list\*(C'\fR in almost all conceivable cases. It is my experience that in most cases this leads to simpler code anyway, since it means one can say: .PP .Vb 1 \& @children = $h\->content_list; .Ve .PP instead of the inelegant: .PP .Vb 1 \& @children = @{$h\->content || []}; .Ve .PP If you do use \f(CW\*(C`$h\->content\*(C'\fR (or \f(CW\*(C`$h\->content_array_ref\*(C'\fR), you should not use the reference returned by it (assuming it returned a reference, and not undef) to directly set or change the content of an element or text segment! Instead use content_refs_list or any of the other methods under \*(L"Structure-Modifying Methods\*(R", below. .SS "content_array_ref" .IX Subsection "content_array_ref" .Vb 1 \& $content_array_ref = $h\->content_array_ref(); # never undef .Ve .PP This is like \f(CW\*(C`content\*(C'\fR (with all its caveats and deprecations) except that it is guaranteed to return an array reference. That is, if the given node has no \f(CW\*(C`_content\*(C'\fR attribute, the \f(CW\*(C`content\*(C'\fR method would return that undef, but \f(CW\*(C`content_array_ref\*(C'\fR would set the given node's \&\f(CW\*(C`_content\*(C'\fR value to \f(CW\*(C`[]\*(C'\fR (a reference to a new, empty array), and return that. .SS "content_refs_list" .IX Subsection "content_refs_list" .Vb 1 \& @content_refs = $h\->content_refs_list; .Ve .PP This returns a list of scalar references to each element of \f(CW$h\fR's content list. This is useful in case you want to in-place edit any large text segments without having to get a copy of the current value of that segment value, modify that copy, then use the \&\f(CW\*(C`splice_content\*(C'\fR to replace the old with the new. Instead, here you can in-place edit: .PP .Vb 4 \& foreach my $item_r ($h\->content_refs_list) { \& next if ref $$item_r; \& $$item_r =~ s/honour/honor/g; \& } .Ve .PP You \fIcould\fR currently achieve the same affect with: .PP .Vb 5 \& foreach my $item (@{ $h\->content_array_ref }) { \& # deprecated! \& next if ref $item; \& $item =~ s/honour/honor/g; \& } .Ve .PP \&...except that using the return value of \f(CW\*(C`$h\->content\*(C'\fR or \&\f(CW\*(C`$h\->content_array_ref\*(C'\fR to do that is deprecated, and just might stop working in the future. .SS "implicit" .IX Subsection "implicit" .Vb 2 \& $is_implicit = $h\->implicit(); \& $h\->implicit($make_implicit); .Ve .PP Returns (optionally sets) the \*(L"_implicit\*(R" attribute. This attribute is a flag that's used for indicating that the element was not originally present in the source, but was added to the parse tree (by HTML::TreeBuilder, for example) in order to conform to the rules of \&\s-1HTML\s0 structure. .SS "pos" .IX Subsection "pos" .Vb 2 \& $pos = $h\->pos(); \& $h\->pos($element); .Ve .PP Returns (and optionally sets) the \*(L"_pos\*(R" (for "current \fIpos\fRition") pointer of \f(CW$h\fR. This attribute is a pointer used during some parsing operations, whose value is whatever HTML::Element element at or under \f(CW$h\fR is currently \*(L"open\*(R", where \f(CW\*(C`$h\->insert_element(NEW)\*(C'\fR will actually insert a new element. .PP (This has nothing to do with the Perl function called \f(CW\*(C`pos\*(C'\fR, for controlling where regular expression matching starts.) .PP If you set \f(CW\*(C`$h\->pos($element)\*(C'\fR, be sure that \f(CW$element\fR is either \f(CW$h\fR, or an element under \f(CW$h\fR. .PP If you've been modifying the tree under \f(CW$h\fR and are no longer sure \f(CW\*(C`$h\->pos\*(C'\fR is valid, you can enforce validity with: .PP .Vb 1 \& $h\->pos(undef) unless $h\->pos\->is_inside($h); .Ve .SS "all_attr" .IX Subsection "all_attr" .Vb 1 \& %attr = $h\->all_attr(); .Ve .PP Returns all this element's attributes and values, as key-value pairs. This will include any \*(L"internal\*(R" attributes (i.e., ones not present in the original element, and which will not be represented if/when you call \f(CW\*(C`$h\->as_HTML\*(C'\fR). Internal attributes are distinguished by the fact that the first character of their key (not value! key!) is an underscore (\*(L"_\*(R"). .PP Example output of \f(CW\*(C`$h\->all_attr()\*(C'\fR : \&\f(CW\*(C`\*(Aq_parent\*(Aq, \*(C'\fR\fI[object_value]\fR\f(CW\*(C` , \*(Aq_tag\*(Aq, \*(Aqem\*(Aq, \*(Aqlang\*(Aq, \*(Aqen\-US\*(Aq, \&\*(Aq_content\*(Aq, \*(C'\fR\fI[array\-ref value]\fR. .SS "all_attr_names" .IX Subsection "all_attr_names" .Vb 2 \& @names = $h\->all_attr_names(); \& $num_attrs = $h\->all_attr_names(); .Ve .PP Like \f(CW\*(C`all_attr\*(C'\fR, but only returns the names of the attributes. In scalar context, returns the number of attributes. .PP Example output of \f(CW\*(C`$h\->all_attr_names()\*(C'\fR : \&\f(CW\*(C`\*(Aq_parent\*(Aq, \*(Aq_tag\*(Aq, \*(Aqlang\*(Aq, \*(Aq_content\*(Aq, \*(C'\fR. .SS "all_external_attr" .IX Subsection "all_external_attr" .Vb 1 \& %attr = $h\->all_external_attr(); .Ve .PP Like \f(CW\*(C`all_attr\*(C'\fR, except that internal attributes are not present. .SS "all_external_attr_names" .IX Subsection "all_external_attr_names" .Vb 2 \& @names = $h\->all_external_attr_names(); \& $num_attrs = $h\->all_external_attr_names(); .Ve .PP Like \f(CW\*(C`all_attr_names\*(C'\fR, except that internal attributes' names are not present (or counted). .SS "id" .IX Subsection "id" .Vb 2 \& $id = $h\->id(); \& $h\->id($string); .Ve .PP Returns (optionally sets to \f(CW$string\fR) the \*(L"id\*(R" attribute. \&\f(CW\*(C`$h\->id(undef)\*(C'\fR deletes the \*(L"id\*(R" attribute. .PP \&\f(CW\*(C`$h\->id(...)\*(C'\fR is basically equivalent to \f(CW\*(C`$h\->attr(\*(Aqid\*(Aq, ...)\*(C'\fR, except that when setting the attribute, this method returns the new value, not the old value. .SS "idf" .IX Subsection "idf" .Vb 2 \& $id = $h\->idf(); \& $h\->idf($string); .Ve .PP Just like the \f(CW\*(C`id\*(C'\fR method, except that if you call \f(CW\*(C`$h\->idf()\*(C'\fR and no \*(L"id\*(R" attribute is defined for this element, then it's set to a likely-to-be-unique value, and returned. (The \*(L"f\*(R" is for \*(L"force\*(R".) .SH "STRUCTURE-MODIFYING METHODS" .IX Header "STRUCTURE-MODIFYING METHODS" These methods are provided for modifying the content of trees by adding or changing nodes as parents or children of other nodes. .SS "push_content" .IX Subsection "push_content" .Vb 1 \& $h\->push_content($element_or_text, ...); .Ve .PP Adds the specified items to the \fIend\fR of the content list of the element \f(CW$h\fR. The items of content to be added should each be either a text segment (a string), an HTML::Element object, or an arrayref. Arrayrefs are fed thru \f(CW\*(C`$h\->new_from_lol(that_arrayref)\*(C'\fR to convert them into elements, before being added to the content list of \f(CW$h\fR. This means you can say things concise things like: .PP .Vb 6 \& $body\->push_content( \& [\*(Aqbr\*(Aq], \& [\*(Aqul\*(Aq, \& map [\*(Aqli\*(Aq, $_], qw(Peaches Apples Pears Mangos) \& ] \& ); .Ve .PP See the \*(L"new_from_lol\*(R" method's documentation, far below, for more explanation. .PP Returns \f(CW$h\fR (the element itself). .PP The push_content method will try to consolidate adjacent text segments while adding to the content list. That's to say, if \f(CW$h\fR's \f(CW\*(C`content_list\*(C'\fR is .PP .Vb 1 \& (\*(Aqfoo bar \*(Aq, $some_node, \*(Aqbaz!\*(Aq) .Ve .PP and you call .PP .Vb 1 \& $h\->push_content(\*(Aqquack?\*(Aq); .Ve .PP then the resulting content list will be this: .PP .Vb 1 \& (\*(Aqfoo bar \*(Aq, $some_node, \*(Aqbaz!quack?\*(Aq) .Ve .PP and not this: .PP .Vb 1 \& (\*(Aqfoo bar \*(Aq, $some_node, \*(Aqbaz!\*(Aq, \*(Aqquack?\*(Aq) .Ve .PP If that latter is what you want, you'll have to override the feature of consolidating text by using splice_content, as in: .PP .Vb 1 \& $h\->splice_content(scalar($h\->content_list),0,\*(Aqquack?\*(Aq); .Ve .PP Similarly, if you wanted to add 'Skronk' to the beginning of the content list, calling this: .PP .Vb 1 \& $h\->unshift_content(\*(AqSkronk\*(Aq); .Ve .PP then the resulting content list will be this: .PP .Vb 1 \& (\*(AqSkronkfoo bar \*(Aq, $some_node, \*(Aqbaz!\*(Aq) .Ve .PP and not this: .PP .Vb 1 \& (\*(AqSkronk\*(Aq, \*(Aqfoo bar \*(Aq, $some_node, \*(Aqbaz!\*(Aq) .Ve .PP What you'd to do get the latter is: .PP .Vb 1 \& $h\->splice_content(0,0,\*(AqSkronk\*(Aq); .Ve .SS "unshift_content" .IX Subsection "unshift_content" .Vb 1 \& $h\->unshift_content($element_or_text, ...) .Ve .PP Just like \f(CW\*(C`push_content\*(C'\fR, but adds to the \fIbeginning\fR of the \f(CW$h\fR element's content list. .PP The items of content to be added should each be either a text segment (a string), an HTML::Element object, or an arrayref (which is fed thru \f(CW\*(C`new_from_lol\*(C'\fR). .PP The unshift_content method will try to consolidate adjacent text segments while adding to the content list. See above for a discussion of this. .PP Returns \f(CW$h\fR (the element itself). .SS "splice_content" .IX Subsection "splice_content" .Vb 2 \& @removed = $h\->splice_content($offset, $length, \& $element_or_text, ...); .Ve .PP Detaches the elements from \f(CW$h\fR's list of content-nodes, starting at \&\f(CW$offset\fR and continuing for \f(CW$length\fR items, replacing them with the elements of the following list, if any. Returns the elements (if any) removed from the content-list. If \f(CW$offset\fR is negative, then it starts that far from the end of the array, just like Perl's normal \f(CW\*(C`splice\*(C'\fR function. If \f(CW$length\fR and the following list is omitted, removes everything from \f(CW$offset\fR onward. .PP The items of content to be added (if any) should each be either a text segment (a string), an arrayref (which is fed thru \*(L"new_from_lol\*(R"), or an HTML::Element object that's not already a child of \f(CW$h\fR. .SS "detach" .IX Subsection "detach" .Vb 1 \& $old_parent = $h\->detach(); .Ve .PP This unlinks \f(CW$h\fR from its parent, by setting its 'parent' attribute to undef, and by removing it from the content list of its parent (if it had one). The return value is the parent that was detached from (or undef, if \f(CW$h\fR had no parent to start with). Note that neither \f(CW$h\fR nor its parent are explicitly destroyed. .SS "detach_content" .IX Subsection "detach_content" .Vb 1 \& @old_content = $h\->detach_content(); .Ve .PP This unlinks all of \f(CW$h\fR's children from \f(CW$h\fR, and returns them. Note that these are not explicitly destroyed; for that, you can just use \f(CW\*(C`$h\->delete_content\*(C'\fR. .SS "replace_with" .IX Subsection "replace_with" .Vb 1 \& $h\->replace_with( $element_or_text, ... ) .Ve .PP This replaces \f(CW$h\fR in its parent's content list with the nodes specified. The element \f(CW$h\fR (which by then may have no parent) is returned. This causes a fatal error if \f(CW$h\fR has no parent. The list of nodes to insert may contain \f(CW$h\fR, but at most once. Aside from that possible exception, the nodes to insert should not already be children of \f(CW$h\fR's parent. .PP Also, note that this method does not destroy \f(CW$h\fR if weak references are turned off \*(-- use \f(CW\*(C`$h\->replace_with(...)\->delete\*(C'\fR if you need that. .SS "preinsert" .IX Subsection "preinsert" .Vb 1 \& $h\->preinsert($element_or_text...); .Ve .PP Inserts the given nodes right \s-1BEFORE \s0\f(CW$h\fR in \f(CW$h\fR's parent's content list. This causes a fatal error if \f(CW$h\fR has no parent. None of the given nodes should be \f(CW$h\fR or other children of \f(CW$h\fR. Returns \f(CW$h\fR. .SS "postinsert" .IX Subsection "postinsert" .Vb 1 \& $h\->postinsert($element_or_text...) .Ve .PP Inserts the given nodes right \s-1AFTER \s0\f(CW$h\fR in \f(CW$h\fR's parent's content list. This causes a fatal error if \f(CW$h\fR has no parent. None of the given nodes should be \f(CW$h\fR or other children of \f(CW$h\fR. Returns \&\f(CW$h\fR. .SS "replace_with_content" .IX Subsection "replace_with_content" .Vb 1 \& $h\->replace_with_content(); .Ve .PP This replaces \f(CW$h\fR in its parent's content list with its own content. The element \f(CW$h\fR (which by then has no parent or content of its own) is returned. This causes a fatal error if \f(CW$h\fR has no parent. Also, note that this does not destroy \f(CW$h\fR if weak references are turned off \*(-- use \&\f(CW\*(C`$h\->replace_with_content\->delete\*(C'\fR if you need that. .SS "delete_content" .IX Subsection "delete_content" .Vb 2 \& $h\->delete_content(); \& $h\->destroy_content(); # alias .Ve .PP Clears the content of \f(CW$h\fR, calling \f(CW\*(C`$h\->delete\*(C'\fR for each content element. Compare with \f(CW\*(C`$h\->detach_content\*(C'\fR. .PP Returns \f(CW$h\fR. .PP \&\f(CW\*(C`destroy_content\*(C'\fR is an alias for this method. .SS "delete" .IX Subsection "delete" .Vb 2 \& $h\->delete(); \& $h\->destroy(); # alias .Ve .PP Detaches this element from its parent (if it has one) and explicitly destroys the element and all its descendants. The return value is the empty list (or \f(CW\*(C`undef\*(C'\fR in scalar context). .PP Before version 5.00 of HTML::Element, you had to call \f(CW\*(C`delete\*(C'\fR when you were finished with the tree, or your program would leak memory. This is no longer necessary if weak references are enabled, see \&\*(L"Weak References\*(R". .SS "destroy" .IX Subsection "destroy" An alias for \*(L"delete\*(R". .SS "destroy_content" .IX Subsection "destroy_content" An alias for \*(L"delete_content\*(R". .SS "clone" .IX Subsection "clone" .Vb 1 \& $copy = $h\->clone(); .Ve .PP Returns a copy of the element (whose children are clones (recursively) of the original's children, if any). .PP The returned element is parentless. Any '_pos' attributes present in the source element/tree will be absent in the copy. For that and other reasons, the clone of an HTML::TreeBuilder object that's in mid-parse (i.e, the head of a tree that HTML::TreeBuilder is elaborating) cannot (currently) be used to continue the parse. .PP You are free to clone HTML::TreeBuilder trees, just as long as: 1) they're done being parsed, or 2) you don't expect to resume parsing into the clone. (You can continue parsing into the original; it is never affected.) .SS "clone_list" .IX Subsection "clone_list" .Vb 1 \& @copies = HTML::Element\->clone_list(...nodes...); .Ve .PP Returns a list consisting of a copy of each node given. Text segments are simply copied; elements are cloned by calling \f(CW\*(C`$it\->clone\*(C'\fR on each of them. .PP Note that this must be called as a class method, not as an instance method. \f(CW\*(C`clone_list\*(C'\fR will croak if called as an instance method. You can also call it like so: .PP .Vb 1 \& ref($h)\->clone_list(...nodes...) .Ve .SS "normalize_content" .IX Subsection "normalize_content" .Vb 1 \& $h\->normalize_content .Ve .PP Normalizes the content of \f(CW$h\fR \*(-- i.e., concatenates any adjacent text nodes. (Any undefined text segments are turned into empty-strings.) Note that this does not recurse into \f(CW$h\fR's descendants. .SS "delete_ignorable_whitespace" .IX Subsection "delete_ignorable_whitespace" .Vb 1 \& $h\->delete_ignorable_whitespace() .Ve .PP This traverses under \f(CW$h\fR and deletes any text segments that are ignorable whitespace. You should not use this if \f(CW$h\fR is under a \f(CW\*(C`<pre>\*(C'\fR element. .SS "insert_element" .IX Subsection "insert_element" .Vb 1 \& $h\->insert_element($element, $implicit); .Ve .PP Inserts (via push_content) a new element under the element at \&\f(CW\*(C`$h\->pos()\*(C'\fR. Then updates \f(CW\*(C`$h\->pos()\*(C'\fR to point to the inserted element, unless \f(CW$element\fR is a prototypically empty element like \&\f(CW\*(C`<br>\*(C'\fR, \f(CW\*(C`<hr>\*(C'\fR, \f(CW\*(C`<img>\*(C'\fR, etc. The new \f(CW\*(C`$h\->pos()\*(C'\fR is returned. This method is useful only if your particular tree task involves setting \&\f(CW\*(C`$h\->pos()\*(C'\fR. .SH "DUMPING METHODS" .IX Header "DUMPING METHODS" .SS "dump" .IX Subsection "dump" .Vb 2 \& $h\->dump() \& $h\->dump(*FH) ; # or *FH{IO} or $fh_obj .Ve .PP Prints the element and all its children to \s-1STDOUT \s0(or to a specified filehandle), in a format useful only for debugging. The structure of the document is shown by indentation (no end tags). .SS "as_HTML" .IX Subsection "as_HTML" .Vb 4 \& $s = $h\->as_HTML(); \& $s = $h\->as_HTML($entities); \& $s = $h\->as_HTML($entities, $indent_char); \& $s = $h\->as_HTML($entities, $indent_char, \e%optional_end_tags); .Ve .PP Returns a string representing in \s-1HTML\s0 the element and its descendants. The optional argument \f(CW$entities\fR specifies a string of the entities to encode. For compatibility with previous versions, specify \f(CW\*(Aq<>&\*(Aq\fR here. If omitted or undef, \fIall\fR unsafe characters are encoded as \s-1HTML\s0 entities. See HTML::Entities for details. If passed an empty string, no entities are encoded. .PP If \f(CW$indent_char\fR is specified and defined, the \s-1HTML\s0 to be output is intented, using the string you specify (which you probably should set to \*(L"\et\*(R", or some number of spaces, if you specify it). .PP If \f(CW\*(C`\e%optional_end_tags\*(C'\fR is specified and defined, it should be a reference to a hash that holds a true value for every tag name whose end tag is optional. Defaults to \&\f(CW\*(C`\e%HTML::Element::optionalEndTag\*(C'\fR, which is an alias to \&\f(CW%HTML::Tagset::optionalEndTag\fR, which, at time of writing, contains true values for \f(CW\*(C`p, li, dt, dd\*(C'\fR. A useful value to pass is an empty hashref, \f(CW\*(C`{}\*(C'\fR, which means that no end-tags are optional for this dump. Otherwise, possibly consider copying \f(CW%HTML::Tagset::optionalEndTag\fR to a hash of your own, adding or deleting values as you like, and passing a reference to that hash. .SS "as_text" .IX Subsection "as_text" .Vb 2 \& $s = $h\->as_text(); \& $s = $h\->as_text(skip_dels => 1); .Ve .PP Returns a string consisting of only the text parts of the element's descendants. Any whitespace inside the element is included unchanged, but whitespace not in the tree is never added. But remember that whitespace may be ignored or compacted by HTML::TreeBuilder during parsing (depending on the value of the \f(CW\*(C`ignore_ignorable_whitespace\*(C'\fR and \f(CW\*(C`no_space_compacting\*(C'\fR attributes). Also, since whitespace is never added during parsing, .PP .Vb 2 \& HTML::TreeBuilder\->new_from_content("<p>a</p><p>b</p>") \& \->as_text; .Ve .PP returns \f(CW"ab"\fR, not \f(CW"a b"\fR or \f(CW"a\enb"\fR. .PP Text under \f(CW\*(C`<script>\*(C'\fR or \f(CW\*(C`<style>\*(C'\fR elements is never included in what's returned. If \f(CW\*(C`skip_dels\*(C'\fR is true, then text content under \f(CW\*(C`<del>\*(C'\fR nodes is not included in what's returned. .SS "as_trimmed_text" .IX Subsection "as_trimmed_text" .Vb 3 \& $s = $h\->as_trimmed_text(...); \& $s = $h\->as_trimmed_text(extra_chars => \*(Aq\exA0\*(Aq); # remove \& $s = $h\->as_text_trimmed(...); # alias .Ve .PP This is just like \f(CW\*(C`as_text(...)\*(C'\fR except that leading and trailing whitespace is deleted, and any internal whitespace is collapsed. .PP This will not remove non-breaking spaces, Unicode spaces, or any other non-ASCII whitespace unless you supply the extra characters as a string argument (e.g. \f(CW\*(C`$h\->as_trimmed_text(extra_chars => \*(Aq\exA0\*(Aq)\*(C'\fR). \&\f(CW\*(C`extra_chars\*(C'\fR may be any string that can appear inside a character class, including ranges like \f(CW\*(C`a\-z\*(C'\fR, \s-1POSIX\s0 character classes like \&\f(CW\*(C`[:alpha:]\*(C'\fR, and character class escapes like \f(CW\*(C`\ep{Zs}\*(C'\fR. .SS "as_XML" .IX Subsection "as_XML" .Vb 1 \& $s = $h\->as_XML() .Ve .PP Returns a string representing in \s-1XML\s0 the element and its descendants. .PP The \s-1XML\s0 is not indented. .SS "as_Lisp_form" .IX Subsection "as_Lisp_form" .Vb 1 \& $s = $h\->as_Lisp_form(); .Ve .PP Returns a string representing the element and its descendants as a Lisp form. Unsafe characters are encoded as octal escapes. .PP The Lisp form is indented, and contains external (\*(L"href\*(R", etc.) as well as internal attributes (\*(L"_tag\*(R", \*(L"_content\*(R", \*(L"_implicit\*(R", etc.), except for \*(L"_parent\*(R", which is omitted. .PP Current example output for a given element: .PP .Vb 1 \& ("_tag" "img" "border" "0" "src" "pie.png" "usemap" "#main.map") .Ve .SS "format" .IX Subsection "format" .Vb 2 \& $s = $h\->format; # use HTML::FormatText \& $s = $h\->format($formatter); .Ve .PP Formats text output. Defaults to HTML::FormatText. .PP Takes a second argument that is a reference to a formatter. .SS "starttag" .IX Subsection "starttag" .Vb 2 \& $start = $h\->starttag(); \& $start = $h\->starttag($entities); .Ve .PP Returns a string representing the complete start tag for the element. I.e., leading \*(L"<\*(R", tag name, attributes, and trailing \*(L">\*(R". All values are surrounded with double-quotes, and appropriate characters are encoded. If \f(CW$entities\fR is omitted or undef, \fIall\fR unsafe characters are encoded as \s-1HTML\s0 entities. See HTML::Entities for details. If you specify some value for \f(CW$entities\fR, remember to include the double-quote character in it. (Previous versions of this module would basically behave as if \&\f(CW\*(Aq&">\*(Aq\fR were specified for \f(CW$entities\fR.) If \f(CW$entities\fR is an empty string, no entity is escaped. .SS "starttag_XML" .IX Subsection "starttag_XML" .Vb 1 \& $start = $h\->starttag_XML(); .Ve .PP Returns a string representing the complete start tag for the element. .SS "endtag" .IX Subsection "endtag" .Vb 1 \& $end = $h\->endtag(); .Ve .PP Returns a string representing the complete end tag for this element. I.e., \*(L"</\*(R", tag name, and \*(L">\*(R". .SS "endtag_XML" .IX Subsection "endtag_XML" .Vb 1 \& $end = $h\->endtag_XML(); .Ve .PP Returns a string representing the complete end tag for this element. I.e., \*(L"</\*(R", tag name, and \*(L">\*(R". .SH "SECONDARY STRUCTURAL METHODS" .IX Header "SECONDARY STRUCTURAL METHODS" These methods all involve some structural aspect of the tree; either they report some aspect of the tree's structure, or they involve traversal down the tree, or walking up the tree. .SS "is_inside" .IX Subsection "is_inside" .Vb 1 \& $inside = $h\->is_inside(\*(Aqtag\*(Aq, $element, ...); .Ve .PP Returns true if the \f(CW$h\fR element is, or is contained anywhere inside an element that is any of the ones listed, or whose tag name is any of the tag names listed. You can use any mix of elements and tag names. .SS "is_empty" .IX Subsection "is_empty" .Vb 1 \& $empty = $h\->is_empty(); .Ve .PP Returns true if \f(CW$h\fR has no content, i.e., has no elements or text segments under it. In other words, this returns true if \f(CW$h\fR is a leaf node, \s-1AKA\s0 a terminal node. Do not confuse this sense of \*(L"empty\*(R" with another sense that it can have in \s-1SGML/HTML/XML\s0 terminology, which means that the element in question is of the type (like \s-1HTML\s0's \f(CW\*(C`<hr>\*(C'\fR, \&\f(CW\*(C`<br>\*(C'\fR, \f(CW\*(C`<img>\*(C'\fR, etc.) that \fIcan't\fR have any content. .PP That is, a particular \f(CW\*(C`<p>\*(C'\fR element may happen to have no content, so \&\f(CW$that_p_element\fR\->is_empty will be true \*(-- even though the prototypical \&\f(CW\*(C`<p>\*(C'\fR element isn't \*(L"empty\*(R" (not in the way that the prototypical \&\f(CW\*(C`<hr>\*(C'\fR element is). .PP If you think this might make for potentially confusing code, consider simply using the clearer exact equivalent: \f(CW\*(C`not($h\->content_list)\*(C'\fR. .SS "pindex" .IX Subsection "pindex" .Vb 1 \& $index = $h\->pindex(); .Ve .PP Return the index of the element in its parent's contents array, such that \f(CW$h\fR would equal .PP .Vb 3 \& $h\->parent\->content\->[$h\->pindex] \& # or \& ($h\->parent\->content_list)[$h\->pindex] .Ve .PP assuming \f(CW$h\fR isn't root. If the element \f(CW$h\fR is root, then \&\f(CW\*(C`$h\->pindex\*(C'\fR returns \f(CW\*(C`undef\*(C'\fR. .SS "left" .IX Subsection "left" .Vb 2 \& $element = $h\->left(); \& @elements = $h\->left(); .Ve .PP In scalar context: returns the node that's the immediate left sibling of \f(CW$h\fR. If \f(CW$h\fR is the leftmost (or only) child of its parent (or has no parent), then this returns undef. .PP In list context: returns all the nodes that're the left siblings of \f(CW$h\fR (starting with the leftmost). If \f(CW$h\fR is the leftmost (or only) child of its parent (or has no parent), then this returns an empty list. .PP (See also \f(CW\*(C`$h\->preinsert(LIST)\*(C'\fR.) .SS "right" .IX Subsection "right" .Vb 2 \& $element = $h\->right(); \& @elements = $h\->right(); .Ve .PP In scalar context: returns the node that's the immediate right sibling of \f(CW$h\fR. If \f(CW$h\fR is the rightmost (or only) child of its parent (or has no parent), then this returns \f(CW\*(C`undef\*(C'\fR. .PP In list context: returns all the nodes that're the right siblings of \&\f(CW$h\fR, starting with the leftmost. If \f(CW$h\fR is the rightmost (or only) child of its parent (or has no parent), then this returns an empty list. .PP (See also \f(CW\*(C`$h\->postinsert(LIST)\*(C'\fR.) .SS "address" .IX Subsection "address" .Vb 2 \& $address = $h\->address(); \& $element_or_text = $h\->address($address); .Ve .PP The first form (with no parameter) returns a string representing the location of \f(CW$h\fR in the tree it is a member of. The address consists of numbers joined by a '.', starting with '0', and followed by the pindexes of the nodes in the tree that are ancestors of \f(CW$h\fR, starting from the top. .PP So if the way to get to a node starting at the root is to go to child 2 of the root, then child 10 of that, and then child 0 of that, and then you're there \*(-- then that node's address is \*(L"0.2.10.0\*(R". .PP As a bit of a special case, the address of the root is simply \*(L"0\*(R". .PP I forsee this being used mainly for debugging, but you may find your own uses for it. .PP .Vb 1 \& $element_or_text = $h\->address($address); .Ve .PP This form returns the node (whether element or text-segment) at the given address in the tree that \f(CW$h\fR is a part of. (That is, the address is resolved starting from \f(CW\*(C`$h\->root\*(C'\fR.) .PP If there is no node at the given address, this returns \f(CW\*(C`undef\*(C'\fR. .PP You can specify \*(L"relative addressing\*(R" (i.e., that indexing is supposed to start from \f(CW$h\fR and not from \f(CW\*(C`$h\->root\*(C'\fR) by having the address start with a period \*(-- e.g., \f(CW\*(C`$h\->address(".3.2")\*(C'\fR will look at child 3 of \f(CW$h\fR, and child 2 of that. .SS "depth" .IX Subsection "depth" .Vb 1 \& $depth = $h\->depth(); .Ve .PP Returns a number expressing \f(CW$h\fR's depth within its tree, i.e., how many steps away it is from the root. If \f(CW$h\fR has no parent (i.e., is root), its depth is 0. .SS "root" .IX Subsection "root" .Vb 1 \& $root = $h\->root(); .Ve .PP Returns the element that's the top of \f(CW$h\fR's tree. If \f(CW$h\fR is root, this just returns \f(CW$h\fR. (If you want to test whether \f(CW$h\fR \&\fIis\fR the root, instead of asking what its root is, just test \&\f(CW\*(C`not($h\->parent)\*(C'\fR.) .SS "lineage" .IX Subsection "lineage" .Vb 1 \& @lineage = $h\->lineage(); .Ve .PP Returns the list of \f(CW$h\fR's ancestors, starting with its parent, and then that parent's parent, and so on, up to the root. If \f(CW$h\fR is root, this returns an empty list. .PP If you simply want a count of the number of elements in \f(CW$h\fR's lineage, use \f(CW\*(C`$h\->depth\*(C'\fR. .SS "lineage_tag_names" .IX Subsection "lineage_tag_names" .Vb 1 \& @names = $h\->lineage_tag_names(); .Ve .PP Returns the list of the tag names of \f(CW$h\fR's ancestors, starting with its parent, and that parent's parent, and so on, up to the root. If \f(CW$h\fR is root, this returns an empty list. Example output: \f(CW\*(C`(\*(Aqem\*(Aq, \*(Aqtd\*(Aq, \*(Aqtr\*(Aq, \*(Aqtable\*(Aq, \*(Aqbody\*(Aq, \*(Aqhtml\*(Aq)\*(C'\fR .PP Equivalent to: .PP .Vb 1 \& map { $_\->tag } $h\->lineage; .Ve .SS "descendants" .IX Subsection "descendants" .Vb 1 \& @descendants = $h\->descendants(); .Ve .PP In list context, returns the list of all \f(CW$h\fR's descendant elements, listed in pre-order (i.e., an element appears before its content-elements). Text segments \s-1DO NOT\s0 appear in the list. In scalar context, returns a count of all such elements. .SS "descendents" .IX Subsection "descendents" This is just an alias to the \f(CW\*(C`descendants\*(C'\fR method, for people who can't spell. .SS "find_by_tag_name" .IX Subsection "find_by_tag_name" .Vb 2 \& @elements = $h\->find_by_tag_name(\*(Aqtag\*(Aq, ...); \& $first_match = $h\->find_by_tag_name(\*(Aqtag\*(Aq, ...); .Ve .PP In list context, returns a list of elements at or under \f(CW$h\fR that have any of the specified tag names. In scalar context, returns the first (in pre-order traversal of the tree) such element found, or undef if none. .SS "find" .IX Subsection "find" This is just an alias to \f(CW\*(C`find_by_tag_name\*(C'\fR. (There was once going to be a whole find_* family of methods, but then \f(CW\*(C`look_down\*(C'\fR filled that niche, so there turned out not to be much reason for the verboseness of the name \*(L"find_by_tag_name\*(R".) .SS "find_by_attribute" .IX Subsection "find_by_attribute" .Vb 2 \& @elements = $h\->find_by_attribute(\*(Aqattribute\*(Aq, \*(Aqvalue\*(Aq); \& $first_match = $h\->find_by_attribute(\*(Aqattribute\*(Aq, \*(Aqvalue\*(Aq); .Ve .PP In a list context, returns a list of elements at or under \f(CW$h\fR that have the specified attribute, and have the given value for that attribute. In a scalar context, returns the first (in pre-order traversal of the tree) such element found, or undef if none. .PP This method is \fBdeprecated\fR in favor of the more expressive \&\f(CW\*(C`look_down\*(C'\fR method, which new code should use instead. .SS "look_down" .IX Subsection "look_down" .Vb 2 \& @elements = $h\->look_down( ...criteria... ); \& $first_match = $h\->look_down( ...criteria... ); .Ve .PP This starts at \f(CW$h\fR and looks thru its element descendants (in pre-order), looking for elements matching the criteria you specify. In list context, returns all elements that match all the given criteria; in scalar context, returns the first such element (or undef, if nothing matched). .PP There are three kinds of criteria you can specify: .IP "(attr_name, attr_value)" 4 .IX Item "(attr_name, attr_value)" This means you're looking for an element with that value for that attribute. Example: \f(CW"alt", "pix!"\fR. Consider that you can search on internal attribute values too: \f(CW"_tag", "p"\fR. .IP "(attr_name, qr/.../)" 4 .IX Item "(attr_name, qr/.../)" This means you're looking for an element whose value for that attribute matches the specified Regexp object. .IP "a coderef" 4 .IX Item "a coderef" This means you're looking for elements where coderef\->(each_element) returns true. Example: .Sp .Vb 5 \& my @wide_pix_images = $h\->look_down( \& _tag => "img", \& alt => "pix!", \& sub { $_[0]\->attr(\*(Aqwidth\*(Aq) > 350 } \& ); .Ve .PP Note that \f(CW\*(C`(attr_name, attr_value)\*(C'\fR and \f(CW\*(C`(attr_name, qr/.../)\*(C'\fR criteria are almost always faster than coderef criteria, so should presumably be put before them in your list of criteria. That is, in the example above, the sub ref is called only for elements that have already passed the criteria of having a \*(L"_tag\*(R" attribute with value \*(L"img\*(R", and an \*(L"alt\*(R" attribute with value \*(L"pix!\*(R". If the coderef were first, it would be called on every element, and \&\fIthen\fR what elements pass that criterion (i.e., elements for which the coderef returned true) would be checked for their \*(L"_tag\*(R" and \*(L"alt\*(R" attributes. .PP Note that comparison of string attribute-values against the string value in \f(CW\*(C`(attr_name, attr_value)\*(C'\fR is case-INsensitive! A criterion of \f(CW\*(C`(\*(Aqalign\*(Aq, \*(Aqright\*(Aq)\*(C'\fR \fIwill\fR match an element whose \*(L"align\*(R" value is \*(L"\s-1RIGHT\*(R",\s0 or \*(L"right\*(R" or \*(L"rIGhT\*(R", etc. .PP Note also that \f(CW\*(C`look_down\*(C'\fR considers "" (empty-string) and undef to be different things, in attribute values. So this: .PP .Vb 1 \& $h\->look_down("alt", "") .Ve .PP will find elements \fIwith\fR an \*(L"alt\*(R" attribute, but where the value for the \*(L"alt\*(R" attribute is "". But this: .PP .Vb 1 \& $h\->look_down("alt", undef) .Ve .PP is the same as: .PP .Vb 1 \& $h\->look_down(sub { !defined($_[0]\->attr(\*(Aqalt\*(Aq)) } ) .Ve .PP That is, it finds elements that do not have an \*(L"alt\*(R" attribute at all (or that do have an \*(L"alt\*(R" attribute, but with a value of undef \*(-- which is not normally possible). .PP Note that when you give several criteria, this is taken to mean you're looking for elements that match \fIall\fR your criterion, not just \fIany\fR of them. In other words, there is an implicit \*(L"and\*(R", not an \*(L"or\*(R". So if you wanted to express that you wanted to find elements with a \&\*(L"name\*(R" attribute with the value \*(L"foo\*(R" \fIor\fR with an \*(L"id\*(R" attribute with the value \*(L"baz\*(R", you'd have to do it like: .PP .Vb 7 \& @them = $h\->look_down( \& sub { \& # the lcs are to fold case \& lc($_[0]\->attr(\*(Aqname\*(Aq)) eq \*(Aqfoo\*(Aq \& or lc($_[0]\->attr(\*(Aqid\*(Aq)) eq \*(Aqbaz\*(Aq \& } \& ); .Ve .PP Coderef criteria are more expressive than \f(CW\*(C`(attr_name, attr_value)\*(C'\fR and \f(CW\*(C`(attr_name, qr/.../)\*(C'\fR criteria, and all \f(CW\*(C`(attr_name, attr_value)\*(C'\fR and \f(CW\*(C`(attr_name, qr/.../)\*(C'\fR criteria could be expressed in terms of coderefs. However, \f(CW\*(C`(attr_name, attr_value)\*(C'\fR and \f(CW\*(C`(attr_name, qr/.../)\*(C'\fR criteria are a convenient shorthand. (In fact, \f(CW\*(C`look_down\*(C'\fR itself is basically \*(L"shorthand\*(R" too, since anything you can do with \f(CW\*(C`look_down\*(C'\fR you could do by traversing the tree, either with the \f(CW\*(C`traverse\*(C'\fR method or with a routine of your own. However, \f(CW\*(C`look_down\*(C'\fR often makes for very concise and clear code.) .SS "look_up" .IX Subsection "look_up" .Vb 2 \& @elements = $h\->look_up( ...criteria... ); \& $first_match = $h\->look_up( ...criteria... ); .Ve .PP This is identical to \f(CW\*(C`$h\->look_down\*(C'\fR, except that whereas \&\f(CW\*(C`$h\->look_down\*(C'\fR basically scans over the list: .PP .Vb 1 \& ($h, $h\->descendants) .Ve .PP \&\f(CW\*(C`$h\->look_up\*(C'\fR instead scans over the list .PP .Vb 1 \& ($h, $h\->lineage) .Ve .PP So, for example, this returns all ancestors of \f(CW$h\fR (possibly including \&\f(CW$h\fR itself) that are \f(CW\*(C`<td>\*(C'\fR elements with an \*(L"align\*(R" attribute with a value of \*(L"right\*(R" (or \*(L"\s-1RIGHT\*(R",\s0 etc.): .PP .Vb 1 \& $h\->look_up("_tag", "td", "align", "right"); .Ve .SS "traverse" .IX Subsection "traverse" .Vb 1 \& $h\->traverse(...options...) .Ve .PP Lengthy discussion of HTML::Element's unnecessary and confusing \&\f(CW\*(C`traverse\*(C'\fR method has been moved to a separate file: HTML::Element::traverse .SS "attr_get_i" .IX Subsection "attr_get_i" .Vb 2 \& @values = $h\->attr_get_i(\*(Aqattribute\*(Aq); \& $first_value = $h\->attr_get_i(\*(Aqattribute\*(Aq); .Ve .PP In list context, returns a list consisting of the values of the given attribute for \f(CW$h\fR and for all its ancestors starting from \f(CW$h\fR and working its way up. Nodes with no such attribute are skipped. (\*(L"attr_get_i\*(R" stands for \*(L"attribute get, with inheritance\*(R".) In scalar context, returns the first such value, or undef if none. .PP Consider a document consisting of: .PP .Vb 10 \& <html lang=\*(Aqi\-klingon\*(Aq> \& <head><title>Pati Pata</title></head> \& <body> \& <h1 lang=\*(Aqla\*(Aq>Stuff</h1> \& <p lang=\*(Aqes\-MX\*(Aq align=\*(Aqcenter\*(Aq> \& Foo bar baz <cite>Quux</cite>. \& </p> \& <p>Hooboy.</p> \& </body> \& </html> .Ve .PP If \f(CW$h\fR is the \f(CW\*(C`<cite>\*(C'\fR element, \f(CW\*(C`$h\->attr_get_i("lang")\*(C'\fR in list context will return the list \f(CW\*(C`(\*(Aqes\-MX\*(Aq, \*(Aqi\-klingon\*(Aq)\*(C'\fR. In scalar context, it will return the value \f(CW\*(Aqes\-MX\*(Aq\fR. .PP If you call with multiple attribute names... .PP .Vb 2 \& @values = $h\->attr_get_i(\*(Aqa1\*(Aq, \*(Aqa2\*(Aq, \*(Aqa3\*(Aq); \& $first_value = $h\->attr_get_i(\*(Aqa1\*(Aq, \*(Aqa2\*(Aq, \*(Aqa3\*(Aq); .Ve .PP \&...in list context, this will return a list consisting of the values of these attributes which exist in \f(CW$h\fR and its ancestors. In scalar context, this returns the first value (i.e., the value of the first existing attribute from the first element that has any of the attributes listed). So, in the above example, .PP .Vb 1 \& $h\->attr_get_i(\*(Aqlang\*(Aq, \*(Aqalign\*(Aq); .Ve .PP will return: .PP .Vb 3 \& (\*(Aqes\-MX\*(Aq, \*(Aqcenter\*(Aq, \*(Aqi\-klingon\*(Aq) # in list context \& or \& \*(Aqes\-MX\*(Aq # in scalar context. .Ve .PP But note that this: .PP .Vb 1 \& $h\->attr_get_i(\*(Aqalign\*(Aq, \*(Aqlang\*(Aq); .Ve .PP will return: .PP .Vb 3 \& (\*(Aqcenter\*(Aq, \*(Aqes\-MX\*(Aq, \*(Aqi\-klingon\*(Aq) # in list context \& or \& \*(Aqcenter\*(Aq # in scalar context. .Ve .SS "tagname_map" .IX Subsection "tagname_map" .Vb 1 \& $hash_ref = $h\->tagname_map(); .Ve .PP Scans across \f(CW$h\fR and all its descendants, and makes a hash (a reference to which is returned) where each entry consists of a key that's a tag name, and a value that's a reference to a list to all elements that have that tag name. I.e., this method returns: .PP .Vb 6 \& { \& # Across $h and all descendants... \& \*(Aqa\*(Aq => [ ...list of all <a> elements... ], \& \*(Aqem\*(Aq => [ ...list of all <em> elements... ], \& \*(Aqimg\*(Aq => [ ...list of all <img> elements... ], \& } .Ve .PP (There are entries in the hash for only those tagnames that occur at/under \f(CW$h\fR \*(-- so if there's no \f(CW\*(C`<img>\*(C'\fR elements, there'll be no \&\*(L"img\*(R" entry in the returned hashref.) .PP Example usage: .PP .Vb 7 \& my $map_r = $h\->tagname_map(); \& my @heading_tags = sort grep m/^h\ed$/s, keys %$map_r; \& if(@heading_tags) { \& print "Heading levels used: @heading_tags\en"; \& } else { \& print "No headings.\en" \& } .Ve .SS "extract_links" .IX Subsection "extract_links" .Vb 2 \& $links_array_ref = $h\->extract_links(); \& $links_array_ref = $h\->extract_links(@wantedTypes); .Ve .PP Returns links found by traversing the element and all of its children and looking for attributes (like \*(L"href\*(R" in an \f(CW\*(C`<a>\*(C'\fR element, or \*(L"src\*(R" in an \f(CW\*(C`<img>\*(C'\fR element) whose values represent links. The return value is a \&\fIreference\fR to an array. Each element of the array is reference to an array with \fIfour\fR items: the link-value, the element that has the attribute with that link-value, and the name of that attribute, and the tagname of that element. (Example: \f(CW\*(C`[\*(Aqhttp://www.suck.com/\*(Aq,\*(C'\fR \fI\f(CI$elem_obj\fI\fR \f(CW\*(C`, \*(Aqhref\*(Aq, \*(Aqa\*(Aq]\*(C'\fR.) You may or may not end up using the element itself \*(-- for some purposes, you may use only the link value. .PP You might specify that you want to extract links from just some kinds of elements (instead of the default, which is to extract links from \&\fIall\fR the kinds of elements known to have attributes whose values represent links). For instance, if you want to extract links from only \f(CW\*(C`<a>\*(C'\fR and \f(CW\*(C`<img>\*(C'\fR elements, you could code it like this: .PP .Vb 7 \& for (@{ $e\->extract_links(\*(Aqa\*(Aq, \*(Aqimg\*(Aq) }) { \& my($link, $element, $attr, $tag) = @$_; \& print \& "Hey, there\*(Aqs a $tag that links to ", \& $link, ", in its $attr attribute, at ", \& $element\->address(), ".\en"; \& } .Ve .SS "simplify_pres" .IX Subsection "simplify_pres" .Vb 1 \& $h\->simplify_pres(); .Ve .PP In text bits under \s-1PRE\s0 elements that are at/under \f(CW$h\fR, this routine nativizes all newlines, and expands all tabs. .PP That is, if you read a file with lines delimited by \f(CW\*(C`\ecm\ecj\*(C'\fR's, the text under \s-1PRE\s0 areas will have \f(CW\*(C`\ecm\ecj\*(C'\fR's instead of \f(CW\*(C`\en\*(C'\fR's. Calling \&\f(CW\*(C`$h\->simplify_pres\*(C'\fR on such a tree will turn \f(CW\*(C`\ecm\ecj\*(C'\fR's into \&\f(CW\*(C`\en\*(C'\fR's. .PP Tabs are expanded to however many spaces it takes to get to the next 8th column \*(-- the usual way of expanding them. .SS "same_as" .IX Subsection "same_as" .Vb 1 \& $equal = $h\->same_as($i) .Ve .PP Returns true if \f(CW$h\fR and \f(CW$i\fR are both elements representing the same tree of elements, each with the same tag name, with the same explicit attributes (i.e., not counting attributes whose names start with \*(L"_\*(R"), and with the same content (textual, comments, etc.). .PP Sameness of descendant elements is tested, recursively, with \&\f(CW\*(C`$child1\->same_as($child_2)\*(C'\fR, and sameness of text segments is tested with \f(CW\*(C`$segment1 eq $segment2\*(C'\fR. .SS "new_from_lol" .IX Subsection "new_from_lol" .Vb 2 \& $h = HTML::Element\->new_from_lol($array_ref); \& @elements = HTML::Element\->new_from_lol($array_ref, ...); .Ve .PP Resursively constructs a tree of nodes, based on the (non-cyclic) data structure represented by each \f(CW$array_ref\fR, where that is a reference to an array of arrays (of arrays (of arrays (etc.))). .PP In each arrayref in that structure, different kinds of values are treated as follows: .IP "\(bu" 4 Arrayrefs .Sp Arrayrefs are considered to designate a sub-tree representing children for the node constructed from the current arrayref. .IP "\(bu" 4 Hashrefs .Sp Hashrefs are considered to contain attribute-value pairs to add to the element to be constructed from the current arrayref .IP "\(bu" 4 Text segments .Sp Text segments at the start of any arrayref will be considered to specify the name of the element to be constructed from the current arrayref; all other text segments will be considered to specify text segments as children for the current arrayref. .IP "\(bu" 4 Elements .Sp Existing element objects are either inserted into the treelet constructed, or clones of them are. That is, when the lol-tree is being traversed and elements constructed based what's in it, if an existing element object is found, if it has no parent, then it is added directly to the treelet constructed; but if it has a parent, then \f(CW\*(C`$that_node\->clone\*(C'\fR is added to the treelet at the appropriate place. .PP An example will hopefully make this more obvious: .PP .Vb 10 \& my $h = HTML::Element\->new_from_lol( \& [\*(Aqhtml\*(Aq, \& [\*(Aqhead\*(Aq, \& [ \*(Aqtitle\*(Aq, \*(AqI like stuff!\*(Aq ], \& ], \& [\*(Aqbody\*(Aq, \& {\*(Aqlang\*(Aq, \*(Aqen\-JP\*(Aq, _implicit => 1}, \& \*(Aqstuff\*(Aq, \& [\*(Aqp\*(Aq, \*(Aqum, p < 4!\*(Aq, {\*(Aqclass\*(Aq => \*(Aqpar123\*(Aq}], \& [\*(Aqdiv\*(Aq, {foo => \*(Aqbar\*(Aq}, \*(Aq123\*(Aq], \& ] \& ] \& ); \& $h\->dump; .Ve .PP Will print this: .PP .Vb 10 \& <html> @0 \& <head> @0.0 \& <title> @0.0.0 \& "I like stuff!" \& <body lang="en\-JP"> @0.1 (IMPLICIT) \& "stuff" \& <p class="par123"> @0.1.1 \& "um, p < 4!" \& <div foo="bar"> @0.1.2 \& "123" .Ve .PP And printing \f(CW$h\fR\->as_HTML will give something like: .PP .Vb 3 \& <html><head><title>I like stuff!</title></head> \& <body lang="en\-JP">stuff<p class="par123">um, p < 4! \& <div foo="bar">123</div></body></html> .Ve .PP You can even do fancy things with \f(CW\*(C`map\*(C'\fR: .PP .Vb 10 \& $body\->push_content( \& # push_content implicitly calls new_from_lol on arrayrefs... \& [\*(Aqbr\*(Aq], \& [\*(Aqblockquote\*(Aq, \& [\*(Aqh2\*(Aq, \*(AqPictures!\*(Aq], \& map [\*(Aqp\*(Aq, $_], \& $body2\->look_down("_tag", "img"), \& # images, to be copied from that other tree. \& ], \& # and more stuff: \& [\*(Aqul\*(Aq, \& map [\*(Aqli\*(Aq, [\*(Aqa\*(Aq, {\*(Aqhref\*(Aq=>"$_.png"}, $_ ] ], \& qw(Peaches Apples Pears Mangos) \& ], \& ); .Ve .PP In scalar context, you must supply exactly one arrayref. In list context, you can pass a list of arrayrefs, and new_from_lol will return a list of elements, one for each arrayref. .PP .Vb 5 \& @elements = HTML::Element\->new_from_lol( \& [\*(Aqhr\*(Aq], \& [\*(Aqp\*(Aq, \*(AqAnd there, on the door, was a hook!\*(Aq], \& ); \& # constructs two elements. .Ve .SS "objectify_text" .IX Subsection "objectify_text" .Vb 1 \& $h\->objectify_text(); .Ve .PP This turns any text nodes under \f(CW$h\fR from mere text segments (strings) into real objects, pseudo-elements with a tag-name of \*(L"~text\*(R", and the actual text content in an attribute called \*(L"text\*(R". (For a discussion of pseudo-elements, see the \*(L"tag\*(R" method, far above.) This method is provided because, for some purposes, it is convenient or necessary to be able, for a given text node, to ask what element is its parent; and clearly this is not possible if a node is just a text string. .PP Note that these \*(L"~text\*(R" objects are not recognized as text nodes by methods like \*(L"as_text\*(R". Presumably you will want to call \&\f(CW\*(C`$h\->objectify_text\*(C'\fR, perform whatever task that you needed that for, and then call \f(CW\*(C`$h\->deobjectify_text\*(C'\fR before calling anything like \&\f(CW\*(C`$h\->as_text\*(C'\fR. .SS "deobjectify_text" .IX Subsection "deobjectify_text" .Vb 1 \& $h\->deobjectify_text(); .Ve .PP This undoes the effect of \f(CW\*(C`$h\->objectify_text\*(C'\fR. That is, it takes any \&\*(L"~text\*(R" pseudo-elements in the tree at/under \f(CW$h\fR, and deletes each one, replacing each with the content of its \*(L"text\*(R" attribute. .PP Note that if \f(CW$h\fR itself is a \*(L"~text\*(R" pseudo-element, it will be destroyed \*(-- a condition you may need to treat specially in your calling code (since it means you can't very well do anything with \f(CW$h\fR after that). So that you can detect that condition, if \f(CW$h\fR is itself a \&\*(L"~text\*(R" pseudo-element, then this method returns the value of the \&\*(L"text\*(R" attribute, which should be a defined value; in all other cases, it returns undef. .PP (This method assumes that no \*(L"~text\*(R" pseudo-element has any children.) .SS "number_lists" .IX Subsection "number_lists" .Vb 1 \& $h\->number_lists(); .Ve .PP For every \s-1UL, OL, DIR,\s0 and \s-1MENU\s0 element at/under \f(CW$h\fR, this sets a \&\*(L"_bullet\*(R" attribute for every child \s-1LI\s0 element. For \s-1LI\s0 children of an \&\s-1OL,\s0 the \*(L"_bullet\*(R" attribute's value will be something like \*(L"4.\*(R", \*(L"d.\*(R", \&\*(L"D.\*(R", \*(L"\s-1IV.\*(R",\s0 or \*(L"iv.\*(R", depending on the \s-1OL\s0 element's \*(L"type\*(R" attribute. \&\s-1LI\s0 children of a \s-1UL, DIR,\s0 or \s-1MENU\s0 get their \*(L"_bullet\*(R" attribute set to \*(L"*\*(R". There should be no other LIs (i.e., except as children of \s-1OL, UL, DIR,\s0 or \s-1MENU\s0 elements), and if there are, they are unaffected. .SS "has_insane_linkage" .IX Subsection "has_insane_linkage" .Vb 1 \& $h\->has_insane_linkage .Ve .PP This method is for testing whether this element or the elements under it have linkage attributes (_parent and _content) whose values are deeply aberrant: if there are undefs in a content list; if an element appears in the content lists of more than one element; if the _parent attribute of an element doesn't match its actual parent; or if an element appears as its own descendant (i.e., if there is a cyclicity in the tree). .PP This returns empty list (or false, in scalar context) if the subtree's linkage methods are sane; otherwise it returns two items (or true, in scalar context): the element where the error occurred, and a string describing the error. .PP This method is provided is mainly for debugging and troubleshooting \*(-- it should be \fIquite impossible\fR for any document constructed via HTML::TreeBuilder to parse into a non-sane tree (since it's not the content of the tree per se that's in question, but whether the tree in memory was properly constructed); and it \fIshould\fR be impossible for you to produce an insane tree just thru reasonable use of normal documented structure-modifying methods. But if you're constructing your own trees, and your program is going into infinite loops as during calls to \fItraverse()\fR or any of the secondary structural methods, as part of debugging, consider calling \&\f(CW\*(C`has_insane_linkage\*(C'\fR on the tree. .SS "element_class" .IX Subsection "element_class" .Vb 1 \& $classname = $h\->element_class(); .Ve .PP This method returns the class which will be used for new elements. It defaults to HTML::Element, but can be overridden by subclassing or esoteric means best left to those will will read the source and then not complain when those esoteric means change. (Just subclass.) .SH "CLASS METHODS" .IX Header "CLASS METHODS" .SS "Use_Weak_Refs" .IX Subsection "Use_Weak_Refs" .Vb 2 \& $enabled = HTML::Element\->Use_Weak_Refs; \& HTML::Element\->Use_Weak_Refs( $enabled ); .Ve .PP This method allows you to check whether weak reference support is enabled, and to enable or disable it. For details, see \*(L"Weak References\*(R". \&\f(CW$enabled\fR is true if weak references are enabled. .PP You should not switch this in the middle of your program, and you probably shouldn't use it at all. Existing trees are not affected by this method (until you start modifying nodes in them). .PP Throws an exception if you attempt to enable weak references and your Perl or Scalar::Util does not support them. .PP Disabling weak reference support is deprecated. .SH "SUBROUTINES" .IX Header "SUBROUTINES" .SS "Version" .IX Subsection "Version" This subroutine is deprecated. Please use the standard \s-1VERSION\s0 method (e.g. \f(CW\*(C`HTML::Element\->VERSION\*(C'\fR) instead. .SS "\s-1ABORT OK PRUNE PRUNE_SOFTLY PRUNE_UP\s0" .IX Subsection "ABORT OK PRUNE PRUNE_SOFTLY PRUNE_UP" Constants for signalling back to the traverser .SH "BUGS" .IX Header "BUGS" * If you want to free the memory associated with a tree built of HTML::Element nodes, and you have disabled weak references, then you will have to delete it explicitly using the \*(L"delete\*(R" method. See \*(L"Weak References\*(R". .PP * There's almost nothing to stop you from making a \*(L"tree\*(R" with cyclicities (loops) in it, which could, for example, make the traverse method go into an infinite loop. So don't make cyclicities! (If all you're doing is parsing \s-1HTML\s0 files, and looking at the resulting trees, this will never be a problem for you.) .PP * There's no way to represent comments or processing directives in a tree with HTML::Elements. Not yet, at least. .PP * There's (currently) nothing to stop you from using an undefined value as a text segment. If you're running under \f(CW\*(C`perl \-w\*(C'\fR, however, this may make HTML::Element's code produce a slew of warnings. .SH "NOTES ON SUBCLASSING" .IX Header "NOTES ON SUBCLASSING" You are welcome to derive subclasses from HTML::Element, but you should be aware that the code in HTML::Element makes certain assumptions about elements (and I'm using \*(L"element\*(R" to mean \s-1ONLY\s0 an object of class HTML::Element, or of a subclass of HTML::Element): .PP * The value of an element's _parent attribute must either be undef or otherwise false, or must be an element. .PP * The value of an element's _content attribute must either be undef or otherwise false, or a reference to an (unblessed) array. The array may be empty; but if it has items, they must \s-1ALL\s0 be either mere strings (text segments), or elements. .PP * The value of an element's _tag attribute should, at least, be a string of printable characters. .PP Moreover, bear these rules in mind: .PP * Do not break encapsulation on objects. That is, access their contents only thru \f(CW$obj\fR\->attr or more specific methods. .PP * You should think twice before completely overriding any of the methods that HTML::Element provides. (Overriding with a method that calls the superclass method is not so bad, though.) .SH "SEE ALSO" .IX Header "SEE ALSO" HTML::Tree; HTML::TreeBuilder; HTML::AsSubs; HTML::Tagset; and, for the morbidly curious, HTML::Element::traverse. .SH "ACKNOWLEDGEMENTS" .IX Header "ACKNOWLEDGEMENTS" Thanks to Mark-Jason Dominus for a \s-1POD\s0 suggestion. .SH "AUTHOR" .IX Header "AUTHOR" Current maintainers: .IP "\(bu" 4 Christopher J. Madsen \f(CW\*(C`<perl\ AT\ cjmweb.net>\*(C'\fR .IP "\(bu" 4 Jeff Fearn \f(CW\*(C`<jfearn\ AT\ cpan.org>\*(C'\fR .PP Original HTML-Tree author: .IP "\(bu" 4 Gisle Aas .PP Former maintainers: .IP "\(bu" 4 Sean M. Burke .IP "\(bu" 4 Andy Lester .IP "\(bu" 4 Pete Krawczyk \f(CW\*(C`<petek\ AT\ cpan.org>\*(C'\fR .PP You can follow or contribute to HTML-Tree's development at <https://github.com/kentfredric/HTML\-Tree>. .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright 1995\-1998 Gisle Aas, 1999\-2004 Sean M. Burke, 2005 Andy Lester, 2006 Pete Krawczyk, 2010 Jeff Fearn, 2012 Christopher J. Madsen. .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .PP The programs in this library are distributed in the hope that they will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.
| ver. 1.4 |
Github
|
.
| PHP 8.0.30 | Génération de la page: 0.01 |
proxy
|
phpinfo
|
Réglages