Debian Codegraph @

html/pod/HTML.pod

raw
1 # Copyright 2022 Jeffrey Kegler
2 # This file is part of Marpa::R2. Marpa::R2 is free software: you can
3 # redistribute it and/or modify it under the terms of the GNU Lesser
4 # General Public License as published by the Free Software Foundation,
5 # either version 3 of the License, or (at your option) any later version.
6 #
7 # Marpa::R2 is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 # Lesser General Public License for more details.
11 #
12 # You should have received a copy of the GNU Lesser
13 # General Public License along with Marpa::R2. If not, see
14 # http://www.gnu.org/licenses/.
15
16 =head1 NAME
17
18 Marpa::R2::HTML - High-level HTML Parser
19
20 =head1 SYNOPSIS
21
22 B<Delete all tables>:
23
24 =for Marpa::R2::Display
25 name: 'HTML Synopsis: Delete Tables'
26 normalize-whitespace: 1
27
28 use Marpa::R2::HTML qw(html);
29
30 my $with_table = 'Text<table><tr><td>I am a cell</table> More Text';
31 my $no_table = html( \$with_table,
32 { table => sub { return q{} } });
33
34 =for Marpa::R2::Display::End
35
36 B<Delete everything but tables>:
37
38 =for Marpa::R2::Display
39 name: 'HTML Synopsis: Delete Everything But Tables'
40 perltidy: '-dcsc -sil=0'
41
42 my %handlers_to_keep_only_tables = (
43 table => sub { return Marpa::R2::HTML::original() },
44 ':TOP' => sub { return \( join q{}, @{ Marpa::R2::HTML::values() } ) }
45 );
46 my $only_table = html( \$with_table, \%handlers_to_keep_only_tables );
47
48 =for Marpa::R2::Display::End
49
50 The above works by turning the original text of the HTML into values
51 and concatenating the values at the top of the parse.
52 The same logic works even if a B<table is very defective>:
53
54 =for Marpa::R2::Display
55 name: 'HTML Synopsis: Defective Tables'
56 perltidy: '-dcsc -sil=0'
57
58 my $with_bad_table = 'Text<tr>I am a cell</table> More Text';
59 my $only_bad_table =
60 html( \$with_bad_table, \%handlers_to_keep_only_tables );
61
62 =for Marpa::R2::Display::End
63
64 B<Delete all comments>:
65
66 =for Marpa::R2::Display
67 name: 'HTML Synopsis: Delete Comments'
68 normalize-whitespace: 1
69
70 my $with_comment = 'Text <!-- I am a comment --> I am not a comment';
71 my $no_comment = html( \$with_comment,
72 { ':COMMENT' => sub { return q{} } }
73 );
74
75 =for Marpa::R2::Display::End
76
77 By default, text is passed through unchanged, so that the user
78 need only specify semantic actions for those components she
79 wants changed. To B<change the title of a document>:
80
81 =for Marpa::R2::Display
82 name: 'HTML Synopsis: Change Title'
83 perltidy: '-dcsc -sil=0'
84
85 my $old_title = '<title>Old Title</title>A little html text';
86 my $new_title = html(
87 \$old_title,
88 { 'title' => sub { return '<title>New Title</title>' }
89 }
90 );
91
92 =for Marpa::R2::Display::End
93
94 B<Delete all elements with a class attribute
95 of "C<delete_me>">:
96
97 =for Marpa::R2::Display
98 name: 'HTML Synopsis: Delete by Class'
99 normalize-whitespace: 1
100
101 my $stuff_to_be_edited = '<p>A<p class="delete_me">B<p>C';
102 my $edited_stuff = html( \$stuff_to_be_edited,
103 { '.delete_me' => sub { return q{} } });
104
105 =for Marpa::R2::Display::End
106
107 L<Marpa::R2::HTML> recognizes elements even if they have missing
108 start and/or end tags.
109 L<Marpa::R2::HTML> can B<supply missing tags>:
110
111 =for Marpa::R2::Display
112 name: 'HTML Synopsis: Supply Missing Tags'
113 perltidy: '-dcsc -sil=0'
114
115 sub supply_missing_tags {
116 my $tagname = Marpa::R2::HTML::tagname();
117 return if Marpa::R2::HTML::is_empty_element($tagname);
118 return ( Marpa::R2::HTML::start_tag() // "<$tagname>\n" )
119 . Marpa::R2::HTML::contents() .
120 ( Marpa::R2::HTML::end_tag() // "</$tagname>\n" );
121 }
122 my $html_with_just_a_title = '<title>I am a title and That is IT!';
123 my $valid_html_with_all_tags =
124 html( \$html_with_just_a_title, { q{*} => \&supply_missing_tags } );
125
126 =for Marpa::R2::Display::End
127
128 L<Marpa::R2::HTML> understands the hierarchical structure of an HTML document.
129 B<Finding the maximum nesting depth in elements> is straightforward:
130
131 =for Marpa::R2::Display
132 name: 'HTML Synopsis: Maximum Element Depth'
133 perltidy: '-dcsc -sil=0'
134
135 sub depth_below_me {
136 return List::Util::max( 0, @{ Marpa::R2::HTML::values() } );
137 }
138 my %handlers_to_calculate_maximum_element_depth = (
139 q{*} => sub { return 1 + depth_below_me() },
140 ':TOP' => sub { return depth_below_me() },
141 );
142 my $maximum_depth_with_just_a_title = html( \$html_with_just_a_title,
143 \%handlers_to_calculate_maximum_element_depth );
144
145 =for Marpa::R2::Display::End
146
147 L<Marpa::R2::HTML> tracks actual elements, however tagged.
148 The above code returns the same depth for C<$valid_html_with_all_tags>
149 as for C<$html_with_just_a_title>.
150
151 =head1 DESCRIPTION
152
153 L<Marpa::R2::HTML> does "high-level" parsing of HTML.
154 It allows handlers to be specified for elements, terminals and other
155 components in the hierarchical structure of an HTML document.
156 L<Marpa::R2::HTML> is an extremely liberal HTML parser.
157 L<Marpa::R2::HTML> does not reject any documents,
158 no mater how poorly they fit the HTML standards.
159
160 =head1 THE Marpa::R2::HTML::html STATIC METHOD
161
162 The interface to
163 L<Marpa::R2::HTML> is through the
164 C<Marpa::R2::HTML::html>
165 static method.
166 It is
167 the only
168 L<Marpa::R2::HTML>
169 method not part of the API for the
170 semantic actions.
171
172 C<html> takes one or more arguments.
173 The first argument is required, and must be a reference to
174 a string to be parsed as HTML.
175 The second and
176 subsequent arguments (all optional) are hash references
177 with handler descriptions.
178 (See L<the synopsis|/"SYNOPSIS"> for several examples of
179 calls using the C<html> method.)
180
181 =head2 CSS-style Handler Options
182
183 Handler descriptions in L<Marpa::R2::HTML> are key-value pairs
184 in a hash.
185 In each pair, the key is a CSS-style handler specifier,
186 and the value is a closure,
187 which is called the B<action> for the
188 handler.
189
190 Specifiers are "CSS-style" --
191 their syntax imitates some of the basic cases
192 of CSS specifiers.
193 No attempt is planned
194 to implement the full CSS specifier syntax.
195
196 Supported specifier syntaxes are as follows:
197
198 =over
199
200 =item Tagname Specifiers
201
202 =for Marpa::R2::Display
203 name: 'HTML Synopsis: Delete Everything But Tables'
204 partial: 1
205 normalize-whitespace: 1
206 flatten: 1
207
208 table => sub { return Marpa::R2::HTML::original() },
209
210 =for Marpa::R2::Display::End
211
212 If a specifier contains no special characters
213 it is taken
214 as the name of an element.
215 (A "special" character is
216 anything except an alphanumeric, a hyphen or an underscore.)
217 Consistent with L<HTML::Parser>'s default behavior,
218 element names must be specified in lowercase.
219
220 =item Class Specifiers
221
222 A specifier which is a dot or period followed by a name
223 will match any element whose class attribute is the same as the specified name.
224 For example, the specifier "C<.delete_me>" will match any element whose
225 class attribute is "C<delete_me>".
226
227 =item Tagname-Class Pair Specifiers
228
229 A specifier which contains a dot or period somewhere other than
230 the first position (such as "C<span.label>")
231 is treated as a dotted tagname-class pair.
232 Its action will be called for
233 any component whose tagname and class attribute both match
234 the specifiers.
235
236 =item The Tagname Wildcard Specifier
237
238 A specifier of just an asterisk ("C<*>") matches all elements.
239 Be careful to note that matching all elements is B<not> the same
240 as matching all components.
241 The element wildcard specifier will not match any pseudoclasses.
242
243 =item Pseudoclass Specifiers
244
245 =for Marpa::R2::Display
246 name: 'HTML Synopsis: Delete Comments'
247 partial: 1
248 normalize-whitespace: 1
249 flatten: 1
250
251 ':COMMENT' => sub { return q{} }
252
253 =for Marpa::R2::Display::End
254
255 A specifier which begins with a colon ("C<:>") matches a pseudoclass.
256 L<Marpa::R2::HTML> defines
257 pseudoclasses to deal with terminals and other non-element
258 components of the HTML hierarchy.
259
260 =back
261
262 =head2 Conflicting Specifiers
263
264 At most one semantic action is called for each component.
265 Where an element component matches several specifiers,
266 the action is picked based on the B<most specific match>.
267
268 =over
269
270 =item 1. Matches by tagname-class pair are the most specific.
271
272 =item 2. Matches by class are the next most specific.
273
274 =item 3. Matches by tagname are considered less specific than matches by class.
275
276 =item 4. The wildcard match is the least specific.
277
278 =back
279
280 Here's an example:
281
282 =for Marpa::R2::Display
283 name: 'HTML Pod: Handler Precedence'
284 perltidy: '-dcsc -sil=0'
285 remove-display-indent: 1
286
287 my $html = <<'END_OF_HTML';
288 <span class="high">High Span</span>
289 <span class="low">Low Span</span>
290 <div class="high">High Div</div>
291 <div class="low">Low Div</div>
292 <div class="oddball">Oddball Div</div>
293 END_OF_HTML
294
295 our @RESULTS = ();
296 Marpa::R2::HTML::html(
297 \$html,
298 { q{*} => sub {
299 push @RESULTS, 'wildcard handler: ' . Marpa::R2::HTML::contents();
300 },
301 'div' => sub {
302 push @RESULTS, '"div" handler: ' . Marpa::R2::HTML::contents();
303 },
304 '.high' => sub {
305 push @RESULTS, '".high" handler: ' . Marpa::R2::HTML::contents();
306 },
307 'div.high' => sub {
308 push @RESULTS,
309 '"div.high" handler: ' . Marpa::R2::HTML::contents();
310 },
311 '.oddball' => sub {
312 push @RESULTS,
313 '".oddball" handler: ' . Marpa::R2::HTML::contents();
314 },
315 'body' => sub {undef},
316 'head' => sub {undef},
317 'html' => sub {undef},
318 'p' => sub {undef},
319 }
320 );
321
322 =for Marpa::R2::Display::End
323
324 Here is what C<$result> would contain after the above code was run:
325
326 =for Marpa::R2::Display
327 name: 'HTML Pod: Handler Precedence Result'
328 normalize-whitespace: 1
329
330 ".high" handler: High Span
331 wildcard handler: Low Span
332 "div.high" handler: High Div
333 "div" handler: Low Div
334 ".oddball" handler: Oddball Div
335
336 =for Marpa::R2::Display::End
337
338 =head2 Details of the Specifier Syntax
339
340 For elements and class names only alphanumerics, hyphens and underscores are supported.
341 Elements must be specified in lowercase, but they will match tagnames in the original
342 document on a case-insensitive basis.
343
344 Forcing element names to be lowercase follows the default behavior of
345 L<HTML::Parser>, which coerces all tagnames to lowercase.
346 This is consistent with the HTML standards.
347 It is B<not> consistent with the XML standards,
348 and an option to configure this behavior may be added in
349 the future.
350
351 Pseudoclass names special to
352 L<Marpa::R2::HTML> are case-sensitive, and must be all uppercase.
353 Lowercase is reserved for CSS pseudoclasses.
354 The CSS standard specifies that its pseudoclass names are case-indifferent.
355 No CSS pseudoclasses are supported at this writing.
356
357 =head1 PSEUDOCLASSES
358
359 L<Marpa::R2::HTML> uses
360 L<HTML::Parser> to do its low-level parsing.
361 L<HTML::Parser> "events"
362 become the terminals for L<Marpa::R2::HTML>.
363
364 Besides terminals and elements,
365 three other HTML components are recognized:
366 the SGML prolog (C<:PROLOG>),
367 the SGML trailer (C<:TRAILER>),
368 and the HTML document as a whole (C<:TOP>).
369
370 =head2 :CDATA
371
372 The C<:CDATA> pseudoclass specifies the action for
373 CDATA terminals.
374 Its action is called once for each non-whitespace raw C<text> event
375 that is not reclassed as cruft.
376 (B<Raw text>
377 is text in which any markup and entities should be left as is.)
378
379 More precisely,
380 a C<:CDATA> terminal is created from any
381 L<HTML::Parser> C<text> event that has the C<is_cdata> flag on;
382 that contains a non-whitespace character
383 as defined in the HTML 4.01 specification
384 (L<http://www.w3.org/TR/html4/struct/text.html#h-9.1>);
385 and that is not reclassed as cruft.
386
387 =head2 :COMMENT
388
389 The C<:COMMENT> pseudoclass specifies the action for HTML comments.
390 Its action is called once for every C<HTML::Parser> C<comment> event that
391 is not reclassed as cruft.
392
393 =head2 :CRUFT
394
395 The C<:CRUFT> pseudoclass specifies the action for cruft.
396 Its action is called once for every C<HTML::Parser> event that
397 L<Marpa::R2::HTML> reclasses as cruft.
398
399 L<Marpa::R2::HTML> reclasses terminals as cruft when
400 they do not fit the structure of an HTML document.
401 One example of a terminal that
402 L<Marpa::R2::HTML> would reclass as cruft is a
403 C<< </head> >> end tag in the HTML body.
404
405 Reclassing terminals as cruft is only done as the last resort.
406 When it can,
407 L<HTML::Parser> forgives
408 violations of the HTML standards and accepts terminals as non-cruft.
409
410 Cruft is treated in much the same way as comments.
411 It is preserved, untouched, in the original text view.
412
413 =head2 :DECL
414
415 The C<:DECL> pseudoclass specifies the action for SGML declarations.
416 Its action is called once for every C<HTML::Parser> C<declaration> event that
417 is not reclassed as cruft.
418
419 =head2 :PCDATA
420
421 The C<:PCDATA> pseudoclass specifies the action for
422 PCDATA terminals.
423 Its action is called once for each non-whitespace non-raw C<text> event
424 that is not reclassed as cruft.
425
426 More precisely,
427 a C<:PCDATA> terminal is created from any
428 L<HTML::Parser> C<text> event that has the C<is_cdata> flag B<off>;
429 that contains a non-whitespace character
430 as defined in the HTML 4.01 specification
431 (L<http://www.w3.org/TR/html4/struct/text.html#h-9.1>);
432 and that is not reclassed as cruft.
433
434 Markup and entities in
435 C<:PCDATA> text
436 are expected to be interpreted eventually,
437 but it can be counter-productive to do this
438 during parsing.
439 An application may, for example,
440 be rewriting a document for display on the web.
441 In that case it will often
442 want to leave markup and entities for the client's browser
443 to interpret.
444
445 L<Marpa::R2::HTML> leaves interpretation of markup and entities entirely to
446 the application.
447 An application which chooses to do the interpretation itself
448 may do it in the actions,
449 or deal with it in post-processing.
450 CPAN has excellent tools for this,
451 some of which are part of L<HTML::Parser>.
452
453 =head2 :PI
454
455 The C<:PI> pseudoclass specifies the action for SGML processing instructions.
456 Its action is called once for every L<HTML::Parser> C<process> event that
457 is not reclassed as cruft.
458
459 =head2 :PROLOG
460
461 The C<:PROLOG> pseudoclass specifies the action for SGML prolog.
462 This is the part of the HTML document which precedes the HTML root element.
463 Components valid in the
464 prolog include SGML comments, processing instructions and whitespace.
465
466 =head2 :TOP
467
468 The action specified for the C<:TOP> pseudoclass will be called
469 once and only once in every parse,
470 and will be the last action called in every parse.
471 The C<:TOP> component is the entire
472 physical document, including
473 the SGML prolog,
474 the root element,
475 and the SGML trailer.
476 All the other HTML components in a document
477 will be descendants of the C<:TOP> component.
478
479 The C<:TOP> action is unique, in that there is always an action
480 for it, even if one is not specified.
481 The C<html> method returns the value
482 returned by the C<:TOP> action.
483 The default C<:TOP> action returns a B<reference> to a string
484 with the literal text value of all
485 of its descendants.
486
487 =head2 :TRAILER
488
489 The C<:TRAILER> pseudoclass specifies the action for SGML trailer.
490 This is the part of the HTML document which follows the HTML root element.
491 Components valid in the
492 trailer include SGML comments, processing instructions, and whitespace.
493 Cruft can also be found here, though for L<Marpa::R2::HTML> that is a
494 last resort.
495
496 =head2 :WHITESPACE
497
498 A L<Marpa::R2::HTML> C<:WHITESPACE> terminal is created for every
499 L<HTML::Parser> C<text> event that is entirely whitespace
500 as defined in the HTML 4.01 specification
501 (L<http://www.w3.org/TR/html4/struct/text.html#h-9.1>)
502 and that is not reclassed as cruft.
503 Whitespace is acceptable in places where non-whitespace is not,
504 and the difference can be very significant structurally.
505
506 =head1 VIEWS
507
508 I hope L<the synopsis|/"SYNOPSIS"> convinces the reader
509 that the action semantics of L<Marpa::R2::HTML> are natural.
510 This naturalness is achieved at the price of some novelty.
511 This section explains the ideas behind the semantic action API.
512 Depending on taste,
513 readers may want to skip this section and go straight to
514 the API.
515
516 The components of an HTML document form a hierarchy,
517 with the C<:TOP> component on top, and the terminals on the bottom.
518 The traditional syntax tree method requires semantic actions
519 to know precisely
520 what children every component will have.
521 This processing model is not a good fit to HTML.
522 L<Marpa::R2::HTML> gives the writer of semantic actions
523 "views" of each component that better fit situations
524 where the number and type of
525 children is unknown or vaguely defined.
526
527 L<Marpa::R2::HTML>'s semantics
528 focus more widely --
529 on a component's descendants instead of
530 just its direct children.
531 (The terms ancestor and descendant are used in the standard way:
532 If a component X is
533 above Y in the hierarchy,
534 X is an B<ancestor> of Y; and
535 Y is a B<descendant> of the X.)
536
537 =head2 The Original View
538
539 The B<original view> sees the text of a component as it was
540 originally passed to the parser.
541 The original view never changes.
542 The original view is seen
543 through the L</"Marpa::R2::HTML::original"> API method.
544
545 =head2 The Terminals View
546
547 The B<terminals view> sees the terminals corresponding to the
548 original text of a component.
549 The terminals view never changes.
550 The terminals view is usually seen as part of other views.
551
552 At this writing the API does not contain a "pure" terminals view method.
553 For a terminals view of the whole HTML document,
554 L<HTML::Parser> does the job with significantly lower overhead.
555 For views and sections of views with no values defined,
556 the descendants view (L<described below|/"The Descendants View">)
557 is equivalent to the terminals view.
558
559 =head2 The Values View
560
561 When actions are called, they return a value.
562 If that value is defined, it becomes visible to the B<values view> of
563 its ancestors.
564 The values view of a component sees the visible values for its descendants.
565
566 The B<values view> is an array, with the values ordered according to the
567 lexical order of the components whose actions returned them.
568 If no descendants have visible values,
569 then the values view is a zero-length array.
570
571 The values view is hierarchical.
572 When a component produces a visible value,
573 it makes the values of its descendants disappear.
574 That is, whenever the semantic action for a component X
575 returns anything other than a Perl C<undef>, it has two effects:
576
577 =over
578
579 =item * That return value becomes the visible value associated with component X.
580
581 =item * All the values previously visible due to semantic actions
582 for the descendants of component X disappear.
583
584 =back
585
586 Values which disappear are gone forever.
587 There is no mechanism to make them "reappear".
588
589 As a special case, if an action for a component returns a Perl C<undef>,
590 not only do the values of all its descendants disappear,
591 the component for the action also will not appear in the values view.
592 When its semantic action returns C<undef>, a component permanently "drops out" of the values view
593 taking all descendants with it.
594 The original view is seen
595 through the L</"Marpa::R2::HTML::values"> API method.
596
597 =head2 The Literal View
598
599 The literal view can be thought of as a mix between the original view
600 and the values view.
601 It sees a text string, like the original view.
602 But unlike the original view, the literal view includes the visible values.
603
604 Values appear in the B<literal view> in stringized form.
605 For sections of the original text without visible values,
606 the literal view is the same as the original
607 view.
608 In all L<Marpa::R2::HTML>'s views,
609 whether descendants are seen as text or values,
610 they
611 are seen in the original lexical order.
612 The literal view is seen
613 through the L</"Marpa::R2::HTML::literal"> API method.
614
615 =head2 The Descendants View
616
617 Just as the literal view can be thought of as a mix between the original view
618 and the values view,
619 the descendants view can be thought of a mix between the terminals view and the
620 values view.
621
622 The B<descendants view> sees an array of elements
623 with data for each
624 of the component's descendants,
625 in lexical order.
626 Where a value is visible, the descendants view sees data for the component with the
627 visible value.
628 Where no value is visible, the descendants view sees data for the terminals.
629 This means that
630 when no values are visible, the descendants view is the same as the terminals view.
631
632 The descendants view is implemented via the L</"Marpa::R2::HTML::descendants"> method.
633 It is the most fine-grained and detailed way to look at the descendants of a component.
634 The descendants view can do anything that the other views can do,
635 but the other views should be preferred when they fit the application.
636 Other views are typically more intuitive and efficient.
637
638 =head2 Views versus Syntax Trees
639
640 Views are a generalization of the traditional method
641 for processing semantics: syntax trees.
642 The values view is the view that most
643 closely resembles a syntax tree.
644 But there are important differences.
645
646 In its purest form,
647 the syntax tree model
648 required the semantic actions to
649 define exactly how many and what kind of immediate children
650 each node had.
651 Each node in a syntax tree worked with its immediate children.
652 Children in a syntax tree appeared as values.
653
654 The values view, on the other hand, sees all its descendants,
655 not just its immediate children, but only if
656 they make themselves visible.
657 Because of this,
658 the values view lends itself to being mixed with other views.
659 The values view allows pieces of the tree to decide when they will
660 come into sight
661 and when they will fall out of view.
662
663 =head2 Views and Efficiency
664
665 In most applications,
666 views are more efficient than syntax trees.
667 In terms of L<Marpa::R2::HTML> views,
668 traditional syntax tree processing
669 corresponds most closely to the values view
670 when every component in the parse has a visible value.
671 For L<Marpa::R2::HTML> this is close to the worst case.
672
673 L<Marpa::R2::HTML> optimizes for unvalued components.
674 Unvalued components are represented as terminal spans.
675 Adjacent descendant spans are automatically merged.
676 This means the size and time required do not increase as
677 processing rises up the component hierarchy.
678
679 Terminals views are calculated on a just-in-time basis
680 when they are requested through the action API.
681 The terminals view is produced quickly from the merged terminal span.
682
683 Original views are also calculated on a just-in-time basis
684 as requested.
685 Each terminal tracks the text it represents as a
686 character span.
687 The original text can be quickly reconstructed
688 as the text in the source document from
689 the first character location of its component's first terminal
690 to the last character location of the component's last terminal.
691
692 When a handler does not need to return a value,
693 the most efficient thing to do is to return C<undef>.
694 This reverts that component and all its descendants to
695 the efficient unvalued representation.
696
697 =head1 THE SEMANTIC ACTION API
698
699 L<Marpa::R2::HTML>'s semantic action API is implemented
700 mainly through context-aware static methods.
701 No arguments are passed to the
702 user's semantics action callbacks.
703 Instead the semantic actions get whatever data they need
704 by calling these static methods.
705
706 =head2 API Static Methods
707
708 =over
709
710 =item Marpa::R2::HTML::attributes
711
712 Returns a hash ref to the attributes of the start tag.
713 This hash ref is exactly the hash ref returned
714 for the C<attr> arg specification of L<HTML::Parser>.
715 The C<attributes> API method
716 returns an empty hash
717 if there were no attributes,
718 if there was no start tag for this element,
719 or if the current component is not an element.
720
721 =item Marpa::R2::HTML::contents
722
723 For an element, returns the literal view of the contents.
724 The contents of an element are its entire text
725 except for its start tag and its end tag.
726 For an non-element component, returns undef.
727
728 =item Marpa::R2::HTML::descendants
729
730 This static method implements the descendants view.
731 It takes one argument, the "dataspec".
732 The B<dataspec> is a string specifying
733 the data to be returned for each descendant.
734 The C<descendants> method
735 returns a reference to an array with one element per descendant,
736 in lexical order.
737 Each element in the array is a reference to an array whose
738 elements are
739 the per-descendant data requested in the string.
740
741 The descendant data specification string has a syntax
742 similar to that of the C<argspec> strings of L<HTML::Parser>.
743 Details of that syntax L<are given below|/"Dataspecs">
744
745 =item Marpa::R2::HTML::end_tag
746
747 For an element with an explicit end tag,
748 returns the original text of the end tag.
749 For non-element components, returns undef.
750 For elements with no end tag, returns undef.
751
752 =item Marpa::R2::HTML::is_empty_element
753
754 For an element, returns a Perl true value if the element
755 is empty,
756 a defined Perl false value otherwise.
757 For non-element components, returns undef.
758
759 =item Marpa::R2::HTML::literal
760
761 The C<Marpa::R2::HTML::literal> method implements the literal view.
762 Returns a string containing the literal view of the component --
763 its text as modified by any the visible values of its
764 descendants.
765
766 =item Marpa::R2::HTML::literal_ref
767
768 Returns a reference to a string containing the literal view of the
769 component. This can be useful for very long strings.
770
771 =item Marpa::R2::HTML::offset
772
773 Returns the start offset of the component.
774 This is a zero-based location in the source document.
775 Some components are zero-length,
776 containing none of the tokens in the physical input.
777 The C<Marpa::R2::HTML::offset> method return C<undef>
778 for these.
779
780 =item Marpa::R2::HTML::original
781
782 The C<Marpa::R2::HTML::original> method implements the original view.
783 Returns a string containing the original view of the component --
784 its text unchanged from the source document.
785
786 =item Marpa::R2::HTML::start_tag
787
788 For an element with an explicit start tag,
789 returns the original text of the start tag.
790 For non-element components, returns undef.
791 For elements with no explicit start tag, returns undef.
792
793 =item Marpa::R2::HTML::tagname
794
795 For an element component,
796 returns its tagname.
797 There is a tagname even if there are no
798 explicit tags.
799 Tagname is determined based on structure.
800 For non-element components, returns undef.
801
802 =item Marpa::R2::HTML::title
803
804 Returns the value of the title attribute.
805 For a non-element component, returns undef.
806 If there was no explicit start tag, returns undef.
807 If there was no title attribute, returns undef.
808
809 =item Marpa::R2::HTML::token_type
810
811 For a token, returns the token type.
812 The token types
813 are the event types from L<HTML::Parser>:
814 "C<T>" for text,
815 "C<S>" for a start tag,
816 "C<E>" for an end tag,
817 "C<PI>" for a processing instruction,
818 "C<D>" for an SGML declaration,
819 and "C<C>" for a comment.
820 If the component is an element or some other
821 non-token, returns undef.
822
823 =item Marpa::R2::HTML::values
824
825 The C<Marpa::R2::HTML::values> method implements the values view.
826 It returns a reference to an array of the descendant
827 values visible from this component,
828 in lexical order.
829 No elements of this array will be undefined.
830 The array will be zero length if no descendant
831 has a visible value.
832
833 =back
834
835 =head2 Dataspecs
836
837 =for Marpa::R2::Display
838 name: dataspec example
839 perltidy: '-dcsc -sil=0'
840
841 Marpa::R2::HTML::descendants('token_type,literal,element')
842
843 =for Marpa::R2::Display::End
844
845 The data specification string, or dataspec,
846 is a comma separated list of B<descendant data specifiers>.
847 The C<Marpa::R2::HTML::descendants> method takes a dataspec
848 as its argument.
849 The C<Marpa::R2::HTML::descendants> method returns a reference
850 to an array of references to arrays of per-descendant data.
851 The contents of the per-descendant data arrays
852 and their order is as specified
853 by the dataspec.
854 These are the valid descendant data specifiers:
855
856 =over
857
858 =item C<element>
859
860 For an element descendant, returns the tagname.
861 A valid tagname is returned even if there were no explicit tags.
862 For non-element descendants, returns undef.
863
864 =item C<literal>
865
866 Returns a string containing the literal view of the
867 descendant.
868
869 =item C<original>
870
871 Returns a string containing the original view of the
872 descendant.
873
874 =item C<token_type>
875
876 If the descendant is a terminal, returns the token type.
877 Token types are as described for the
878 L</"Marpa::R2::HTML::token_type"> API method.
879 For components with visible values, returns undef.
880
881 =item C<value>
882
883 For element descendants with a value, returns that
884 value.
885 In all other cases, returns undef.
886
887 =back
888
889 =head2 The Instance Hash
890
891 Each L<Marpa::R2::HTML> instance
892 makes available
893 a per-instance variable
894 as a scratchpad for the application:
895 C<$Marpa::R2::HTML::INSTANCE>.
896 Each call to
897 L<Marpa::R2::HTML::html|/"THE Marpa::R2::HTML::html STATIC METHOD">
898 creates a C<$Marpa::R2::HTML::INSTANCE>
899 variable which is
900 reserved for that application using the C<local> keyword.
901 L<Marpa::R2::HTML::html|/"THE Marpa::R2::HTML::html STATIC METHOD">
902 initializes it to an empty hash,
903 but after that does not touch it.
904 When programming via side effects
905 is more natural than
906 passing data up the parse
907 tree (and it often is),
908 C<$Marpa::R2::HTML::INSTANCE> can be used to
909 store the data.
910
911 Ordinarily, C<$Marpa::R2::HTML::INSTANCE> is destroyed,
912 with the rest of the parse instance,
913 when C<Marpa::R2::HTML::html> returns.
914 But it can be useful
915 for the C<:TOP> semantic action to
916 return
917 C<$Marpa::R2::HTML::INSTANCE> as the value of the parse.
918
919 =head2 Undefined Actions versus Actions Which Return C<undef>
920
921 It is worth emphasizing that
922 the effect of not defining a semantic action for a component
923 is different from the effect of defining a semantic action which
924 returns a Perl C<undef>.
925 The difference lies in what happens to any visible values
926 of the descendants of that component.
927
928 Where no action is defined for a component,
929 it leaves all that component's views as they were before.
930 That is, all values which were visible remain visible and
931 no new values become visible.
932 When an action is defined for a component, but that action returns undef,
933 no new values become visible, and
934 all descendant values which were visible B<disappear>.
935
936 =head2 Root Element versus :TOP Pseudoclass
937
938 It is important to understand the very
939 special function of the C<:TOP>
940 component,
941 and to avoid confusing it with the HTML root element.
942 The most important distinctions are that
943
944 =over
945
946 =item *
947
948 The semantic action
949 for C<:TOP> pseudoclass is always the last action
950 to be called in a parse.
951
952 =item *
953
954 The C<:TOP> component is B<always> the entire HTML document.
955 This can be true of the root element, but it is not true in
956 all cases.
957
958 =item *
959
960 The value that the action for the C<:TOP> component
961 returns becomes the value that
962 the
963 L<Marpa::R2::HTML::html|/"THE Marpa::R2::HTML::html STATIC METHOD">
964 method returns.
965
966 =back
967
968 The root element is the HTML element whose tagname is "C<html>", though
969 its start and end tags are optional
970 and can be omitted even in strictly valid HTML.
971 Tags or no tags, every HTML document has a
972 root element.
973 (The C<:TOP> component is not an element, so it does not have a tagname and
974 never has tags.)
975
976 The root element is always a descendant of the C<:TOP>
977 component.
978 The SGML prolog and SGML trailer are always descendants of the C<:TOP>
979 component.
980 The SGML prolog and SGML trailer
981 are never descendants of the root element.
982
983 If an action for the root element is specified,
984 it will also be called
985 once and only once in every parse.
986 An action for the root element can be specified in same way as actions
987 for other elements, using its tagname of "C<html>".
988 An element wildcard action also becomes the action for the root element,
989 if no more specific handler declaration takes precedence.
990
991 A C<:TOP> action will be called once and only once in every parse.
992 The C<:TOP> action is unique in that there is a default action.
993 No other component has a default action.
994
995 =head2 Tags versus Structure
996
997 Where tags conflict with structure,
998 L<HTML::Parser> follows structure.
999 "Following structure" means that, for example,
1000 if semantic actions for the C<html>, C<head>,
1001 and C<body> elements exist,
1002 they will be called once and only once during every parse.
1003
1004 Consider this short and very defective HTML document:
1005
1006 =for Marpa::R2::Display
1007 name: 'HTML Pod: Structure vs. Element Example'
1008 normalize-whitespace: 1
1009
1010 <title>Short</title><p>Text</head><head>
1011
1012 =for Marpa::R2::Display::End
1013
1014 L<HTML::Parser> starts the HTML document's body
1015 when it encounters the C<< <p> >> start tag.
1016 That means that, even if they were in the right order,
1017 the two C<head> tags cannot be fit into any reasonable parse
1018 structure.
1019
1020 If an action is specified for the C<head> element,
1021 it will be called for the actual header,
1022 and the original view of the C<head> element component
1023 will be the text "C<< <title>Short</title> >>".
1024 The action for the C<head> element will not be called again.
1025 The two stray tags, C<< </head> >> and C<< <head> >>,
1026 will be treated as descendants of
1027 the C<body> element, and reclassed as
1028 "cruft" terminals.
1029
1030 =head2 Explicit and Implicit Elements
1031
1032 If a semantic action
1033 is specified for a tagname, it is called
1034 whenever an element is found with that tagname,
1035 even if there are no explicit tags for
1036 that element.
1037 The HTML standards allow both start and end tags
1038 to be missing
1039 for
1040 C<html>,
1041 C<head>,
1042 C<body> and
1043 C<tbody> elements.
1044 L<Marpa::R2::HTML> is more liberal,
1045 and will recognize virtual tags for
1046 C<table>, C<tr>, and C<td> elements
1047 as required to repair a defective table.
1048
1049 L<Marpa::R2::HTML> is more even
1050 liberal about recognizing virtual end tags
1051 than it is about start tags.
1052 Virtual start tags are recognized only for the specific
1053 elements listed above.
1054 For any non-empty HTML element, there is some circumstance
1055 under which
1056 L<Marpa::R2::HTML> will recognize a virtual end tag.
1057 At end of file,
1058 as one example,
1059 L<Marpa::R2::HTML> will do its best to produce a balanced
1060 HTML structure by
1061 creating a virtual end tag for every element
1062 in the stack of
1063 currently active elements.
1064
1065 =head1 EXPORTS
1066
1067 L<Marpa::R2::HTML> exports nothing by default.
1068 Optionally,
1069 L<Marpa::R2::HTML::html|/"THE Marpa::R2::HTML::html STATIC METHOD">
1070 may be exported.
1071
1072 =head1 Copyright and License
1073
1074 =for Marpa::R2::Display
1075 ignore: 1
1076
1077 Copyright 2022 Jeffrey Kegler
1078 This file is part of Marpa::R2. Marpa::R2 is free software: you can
1079 redistribute it and/or modify it under the terms of the GNU Lesser
1080 General Public License as published by the Free Software Foundation,
1081 either version 3 of the License, or (at your option) any later version.
1082
1083 Marpa::R2 is distributed in the hope that it will be useful,
1084 but WITHOUT ANY WARRANTY; without even the implied warranty of
1085 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1086 Lesser General Public License for more details.
1087
1088 You should have received a copy of the GNU Lesser
1089 General Public License along with Marpa::R2. If not, see
1090 http://www.gnu.org/licenses/.
1091
1092 =for Marpa::R2::Display::End
1093
1094 =cut
1095
1096 # Local Variables:
1097 # mode: cperl
1098 # cperl-indent-level: 4
1099 # fill-column: 100
1100 # End:
1101 # vim: expandtab shiftwidth=4: