HTML-Element-Library-5.220000/ 0000755 0001750 0001750 00000000000 12733574630 015073 5 ustar marius marius HTML-Element-Library-5.220000/README 0000644 0001750 0001750 00000001604 12733574435 015757 0 ustar marius marius HTML-Element-Library version 5.220000
=====================================
HTML-Element-Library provides more methods for HTML::Element.
Read the POD for more information.
INSTALLATION
To install this module type the following:
perl Makefile.PL
make
make test
make install
DEPENDENCIES
This module requires these other modules and libraries:
* Array::Group
* Data::Rmap
* File::Slurp
* HTML::Element
* HTML::FillInForm
* HTML::PrettyPrinter
* List::MoreUTils
* List::Rotation::Cycle
* List::Util
* Params::Validate
* Scalar::Listify
* Test::More
* Test::XML
COPYRIGHT AND LICENCE
Coypright (C) 2014-2016 by Marius Gavrilescu
Copyright (C) 2004-2012 by Terrence Brannon
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.18.1 or,
at your option, any later version of Perl 5 you may have available.
HTML-Element-Library-5.220000/Makefile.PL 0000644 0001750 0001750 00000001767 12733574546 017066 0 ustar marius marius use ExtUtils::MakeMaker;
use strict;
use warnings;
WriteMakefile(
NAME => 'HTML::Element::Library',
VERSION_FROM => 'lib/HTML/Element/Library.pm',
ABSTRACT_FROM => 'lib/HTML/Element/Library.pm',
AUTHOR => [
'Terrence Brannon ',
'Marius Gavrilescu '
],
MIN_PERL_VERSION => '5.6.0',
LICENSE => 'perl',
SIGN => 1,
BUILD_REQUIRES => {
qw/File::Slurp 0
HTML::PrettyPrinter 0
HTML::TreeBuilder 0
Test::More 0
Test::XML 0/,
},
PREREQ_PM => {
qw/Array::Group 0
Data::Rmap 0
HTML::Element 0
HTML::FillInForm 0
List::MoreUtils 0
List::Rotation::Cycle 0
List::Util 0
Params::Validate 0
Scalar::Listify 0/,
},
META_MERGE => {
dynamic_config => 0,
resources => {
repository => 'https://git.ieval.ro/?p=html-element-library.git',
},
}
);
HTML-Element-Library-5.220000/META.yml 0000644 0001750 0001750 00000001722 12733574627 016354 0 ustar marius marius ---
abstract: 'HTML::Element convenience functions'
author:
- 'Terrence Brannon '
- 'Marius Gavrilescu '
build_requires:
File::Slurp: '0'
HTML::PrettyPrinter: '0'
HTML::TreeBuilder: '0'
Test::More: '0'
Test::XML: '0'
configure_requires:
ExtUtils::MakeMaker: '0'
dynamic_config: 0
generated_by: 'ExtUtils::MakeMaker version 7.0401, CPAN::Meta::Converter version 2.150005'
license: perl
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: '1.4'
name: HTML-Element-Library
no_index:
directory:
- t
- inc
requires:
Array::Group: '0'
Data::Rmap: '0'
HTML::Element: '0'
HTML::FillInForm: '0'
List::MoreUtils: '0'
List::Rotation::Cycle: '0'
List::Util: '0'
Params::Validate: '0'
Scalar::Listify: '0'
perl: '5.006000'
resources:
repository: https://git.ieval.ro/?p=html-element-library.git
version: '5.220000'
x_serialization_backend: 'CPAN::Meta::YAML version 0.018'
HTML-Element-Library-5.220000/t/ 0000755 0001750 0001750 00000000000 12733574627 015344 5 ustar marius marius HTML-Element-Library-5.220000/t/00system.t 0000644 0001750 0001750 00000000751 12447477635 017223 0 ustar marius marius #!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 1;
diag "Running under perl version $] for $^O", (chr(65) eq 'A') ? "\n" : " in a non-ASCII world\n";
diag "Win32::BuildNumber ", &Win32::BuildNumber(), "\n" if defined(&Win32::BuildNumber) and defined &Win32::BuildNumber();
diag "MacPerl verison $MacPerl::Version\n" if defined $MacPerl::Version;
diag sprintf "Current time local: %s\n", scalar localtime;
diag sprintf "Current time GMT: %s\n", scalar gmtime;
pass;
HTML-Element-Library-5.220000/t/tables.t 0000644 0001750 0001750 00000010652 12447477635 017012 0 ustar marius marius #!/usr/bin/perl -T
use lib '.';
use t::lib tests => 9;
my $tree;
sub data () {
[map { +{name => $_->[0], age => $_->[1], weight => $_->[2]} } (
[qw/bob 99 99/],
[qw/bill 12 52/],
[qw/brian 44 80/],
[qw/babette 52 124/],
[qw/bobo 12 120/],
[qw/bix 43 230/],
)];
}
sub data2 () {
{
'3dig' => [
['437', 'MS-DOS', 'United States', '0', '1', '1', '1', '1'],
['708', 'Arabic (ASMO 708)', '0', '1', '0', '0', '1'],
['709', 'Arabic (ASMO 449+ BCON V4)', '0', '1', '0', '0', '1'],
['710', 'Arabic (Transparent Arabic)', '0', '1', '0', '0', '1'],
['720', 'Arabic (Transparent ASMO)', '0', '1', '0', '0', '1']],
'4dig' => [
['1200', 'Unicode (BMP of ISO 10646)', '0', '0', '1', '1', '2'],
['1250', 'Windows 3.1 Eastern European', '1', '0', '1', '1', '1'],
['1251', 'Windows 3.1 Cyrillic', '1', '0', '1', '1', '1'],
['1252', 'Windows 3.1 US (ANSI)', '1', '0', '1', '1', '1'],
['1253', 'Windows 3.1 Greek', '1', '0', '1', '1', '1'],
['1254', 'Windows 3.1 Turkish', '1', '0', '1', '1', '1'],
['1255', 'Hebrew', '1', '0', '0', '0', '1'],
['1256', 'Arabic', '1', '0', '0', '0', '1'],
['1257', 'Baltic', '1', '0', '0', '0', '1'],
['1361', 'Korean (Johab)', '1', '0', '0', '3', '1']]
};
}
$tree = mktree 't/html/table.html';
$tree->table(
gi_table => 'load_data',
gi_tr => 'data_row',
table_data => data,
tr_data => sub {
my ($self, $data) = @_;
shift(@{$data}) ;
},
td_data => sub {
my ($tr_node, $tr_data) = @_;
$tr_node->content_handler($_ => $tr_data->{$_}) for qw(name age weight)
});
isxml $tree, 't/html/table-exp.html', 'table';
###
$tree = mktree 't/html/table-alt.html';
$tree->table(
gi_table => 'load_data',
gi_tr => ['iterate1', 'iterate2'],
table_data => data,
tr_data => sub {
my ($self, $data) = @_;
shift @{$data};
},
td_data => sub {
my ($tr_node, $tr_data) = @_;
$tr_node->content_handler($_ => $tr_data->{$_}) for qw(name age weight)
});
isxml $tree, 't/html/table-alt-exp.html', 'table (alternating)';
###
my $d = data2;
$tree = mktree 't/html/table2.html';
for my $dataset (keys %$d) {
my %tbody = ('4dig' => 0, '3dig' => 1);
$tree->table2 (
debug => $ENV{TEST_VERBOSE},
table_data => $d->{$dataset},
tr_base_id => $dataset,
tr_ld => sub {
my $t = shift;
my $tbody = ($t->look_down('_tag' => 'tbody'))[$tbody{$dataset}];
my @tbody_child = $tbody->content_list;
$tbody_child[$_]->detach for (1 .. $#tbody_child) ;
$tbody->content_list;
},
td_proc => sub {
my ($tr, $data) = @_;
my @td = $tr->look_down('_tag' => 'td');
for my $i (0..$#td) {
# warn $i;
$td[$i]->splice_content(0, 1, $data->[$i]);
}
}
);
}
isxml $tree, 't/html/table2-exp.html', 'table2';
###
# a - default table_ld
$tree = mktree 't/html/table2.html';
my $table = HTML::Element::Library::ref_or_ld(
$tree,
['_tag' => 'table']
);
isxml $table, 't/html/table2-table_ld-exp.html', 'table2 look_down default';
###
# b - arrayref table_ld
$table = HTML::Element::Library::ref_or_ld(
$tree,
[frame => 'hsides', rules => 'groups']
);
isxml $table, 't/html/table2-table_ld-exp.html', 'table2 look_down arrayref';
# c - coderef table_ld
$table = HTML::Element::Library::ref_or_ld(
$tree,
sub {
my ($t) = @_;
my $caption = $t->look_down('_tag' => 'caption');
$caption->parent;
}
);
isxml $table, 't/html/table2-table_ld-exp.html', 'table2 look_down coderef';
###
# a - default table_ld
my @tr = HTML::Element::Library::ref_or_ld(
$tree,
['_tag' => 'tr']
);
is (scalar @tr, 16, 'table2 tr look_down (default)');
# b - coderef tr_ld
# removes windows listings before returning @tr
HTML::Element::Library::ref_or_ld(
$tree,
sub {
my ($t) = @_;
my @trs = $t->look_down('_tag' => 'tr');
my @keep;
for my $tr (@trs) {
my @td = $tr->look_down ('_tag' => 'td') ;
my $detached;
for my $td (@td) {
if (grep { $_ =~ /Windows/ } $td->content_list) {
$tr->detach;
++$detached;
last;
}
}
push @keep, $tr unless $detached;
}
@keep;
}
);
isxml $tree, 't/html/table2-tr_ld-coderef-exp.html', 'table2 tr look_down (coderef)';
# c - arrayref tr_ld
$tree = mktree 't/html/table2-tr_ld-arrayref.html';
my $tr = HTML::Element::Library::ref_or_ld(
$tree,
[class => 'findMe']
);
isxml $tr, 't/html/table2-tr_ld-arrayref-exp.html', 'table2 tr look_down (arrayref)';
HTML-Element-Library-5.220000/t/lib.pm 0000644 0001750 0001750 00000001354 12447474267 016454 0 ustar marius marius package t::lib;
use strict;
use warnings;
use File::Slurp qw/read_file/;
use HTML::TreeBuilder;
use HTML::Element::Library;
use Test::More ();
use Test::XML;
use parent qw/Exporter/;
our @EXPORT = qw/is is_deeply is_xml slurp mktree isxml/;
our $VERSION = '0.001'; # Exporter needs a $VERSION
sub import {
my ($self, @args) = @_;
strict->import;
warnings->import;
Test::More->import(@args);
$self->export_to_level(1, $self);
}
sub slurp { scalar read_file @_ }
sub mktree {
my ($file) = @_;
HTML::TreeBuilder->new_from_file($file)->disembowel;
}
sub isxml {
my ($tree, $file, $name) = @_;
my $res = ref $tree eq 'SCALAR' ? $$tree : $tree->as_XML;
my $exp = ref $file eq 'SCALAR' ? $$file : slurp $file;
is_xml $res, $exp, $name
}
HTML-Element-Library-5.220000/t/perlcriticrc 0000644 0001750 0001750 00000002225 12447474207 017750 0 ustar marius marius severity = 1
[-BuiltinFunctions::ProhibitComplexMappings]
[-CodeLayout::RequireTidyCode]
[-ControlStructures::ProhibitPostfixControls]
[-ControlStructures::ProhibitUnlessBlocks]
[-Documentation::PodSpelling]
[-Documentation::RequirePodLinksIncludeText]
[-ErrorHandling::RequireCarping]
[-InputOutput::RequireBracedFileHandleWithPrint]
[-References::ProhibitDoubleSigils]
[-RegularExpressions::ProhibitEnumeratedClasses]
[-RegularExpressions::RequireLineBoundaryMatching]
[-Subroutines::RequireFinalReturn]
[-ValuesAndExpressions::ProhibitConstantPragma]
[-ValuesAndExpressions::ProhibitEmptyQuotes]
[-ValuesAndExpressions::ProhibitMagicNumbers]
[-ValuesAndExpressions::ProhibitNoisyQuotes]
[-Variables::ProhibitLocalVars]
[-Variables::ProhibitPackageVars]
[-Variables::ProhibitPunctuationVars]
[RegularExpressions::RequireExtendedFormatting]
minimum_regex_length_to_complain_about = 20
[Documentation::RequirePodSections]
lib_sections = NAME | SYNOPSIS | DESCRIPTION | AUTHOR | COPYRIGHT AND LICENSE
script_sections = NAME | SYNOPSIS | DESCRIPTION | AUTHOR | COPYRIGHT AND LICENSE
[Subroutines::RequireArgUnpacking]
short_subroutine_statements = 5
allow_subscripts = 1
HTML-Element-Library-5.220000/t/html/ 0000755 0001750 0001750 00000000000 12733574627 016310 5 ustar marius marius HTML-Element-Library-5.220000/t/html/iter-exp.html 0000644 0001750 0001750 00000000270 12447476540 020727 0 ustar marius marius
Here are the things I need from the store:
bread
butter
vodka
HTML-Element-Library-5.220000/t/html/highlander.html 0000644 0001750 0001750 00000000440 12447476540 021276 0 ustar marius marius
Hello, does your mother know you're using her AOL account?
Sorry, you're not old enough to enter (and too dumb to lie about your age)
Welcome
HTML-Element-Library-5.220000/t/html/iter2-exp.html 0000644 0001750 0001750 00000000466 12447476540 021020 0 ustar marius marius
Here are the type of people you meet at XYZ, inc:
Programmer
one who likes Perl and Seamstress
DBA
one who does business as
Admin
one who plays Tetris all day
Poet
A relative of Edgar Allan Poe.
HTML-Element-Library-5.220000/t/html/defmap.html 0000644 0001750 0001750 00000000144 12447476540 020426 0 ustar marius marius
Yes
No
No
HTML-Element-Library-5.220000/t/html/highlander2-27-exp.html 0000644 0001750 0001750 00000000217 12447476540 022402 0 ustar marius marius Welcome, isn't it good to be 27 years old?
HTML-Element-Library-5.220000/t/html/table2-tr_ld-coderef-exp.html 0000644 0001750 0001750 00000005715 12447476540 023655 0 ustar marius marius
Complex HTML Table
metaperl
Last modified: Fri Nov 25 08:57:41 PST 2005
HTML-Element-Library-5.220000/t/html/prune.html 0000644 0001750 0001750 00000000342 12447476540 020323 0 ustar marius marius
There was man named Jed
He did not have a head
He lived beneath a sled
Now he's afraid of Fred...
HTML-Element-Library-5.220000/t/html/fillinform.html 0000644 0001750 0001750 00000000734 12447476540 021340 0 ustar marius marius
Dialer
HTML-Element-Library-5.220000/t/html/table-exp.html 0000644 0001750 0001750 00000001327 12447476540 021057 0 ustar marius marius
name
age
weight
bob
99
99
bill
12
52
brian
44
80
babette
52
124
bobo
12
120
bix
43
230
HTML-Element-Library-5.220000/t/html/unroll_select-exp.html 0000644 0001750 0001750 00000002233 12447476540 022637 0 ustar marius marius
ctf gimble challenge ladder - signup
signup
HTML-Element-Library-5.220000/t/html/highlander2-passover-exp.html 0000644 0001750 0001750 00000000240 12447476540 024010 0 ustar marius marius Sorry, you're only AGE (and too dumb to lie about your age)
HTML-Element-Library-5.220000/t/html/table-alt-exp.html 0000644 0001750 0001750 00000001475 12447476540 021641 0 ustar marius marius
name
age
weight
bob
99
99
bill
12
52
brian
44
80
babette
52
124
bobo
12
120
bix
43
230
HTML-Element-Library-5.220000/t/html/unroll_select.html 0000644 0001750 0001750 00000001737 12447476540 022055 0 ustar marius marius
ctf gimble challenge ladder - signup
signup
HTML-Element-Library-5.220000/t/html/crunch-exp.html 0000644 0001750 0001750 00000002051 12447476540 021245 0 ustar marius marius
Item 1 Title
Item 1 Description
HTML-Element-Library-5.220000/t/html/table2.html 0000644 0001750 0001750 00000005443 12447476540 020352 0 ustar marius marius
Complex HTML Table
metaperl
Last modified: Fri Nov 25 08:57:41 PST 2005
HTML-Element-Library-5.220000/t/html/hashmap-exp.html 0000644 0001750 0001750 00000000545 12447476540 021412 0 ustar marius marius
HO HO HA HA HA
HTML-Element-Library-5.220000/t/html/highlander2.html 0000644 0001750 0001750 00000000606 12447476540 021364 0 ustar marius marius
Hello, little AGE-year old, does your mother know you're using her AOL account?
Sorry, you're only AGE (and too dumb to lie about your age)
Welcome, isn't it good to be AGE years old?
HTML-Element-Library-5.220000/t/html/hashmap.html 0000644 0001750 0001750 00000000552 12447476540 020616 0 ustar marius marius
HO HO HA HA HA
metaperl
Last modified: Fri Nov 25 08:57:41 PST 2005
HTML-Element-Library-5.220000/t/html/highlander-50-exp.html 0000644 0001750 0001750 00000000125 12447476540 022312 0 ustar marius marius Welcome
HTML-Element-Library-5.220000/t/html/defmap-exp.html 0000644 0001750 0001750 00000000145 12447476540 021221 0 ustar marius marius
Yes
arsenal rules
No
HTML-Element-Library-5.220000/t/html/dual_iter.html 0000644 0001750 0001750 00000000463 12447476540 021146 0 ustar marius marius
Artist
A person who draws blood.
Musician
A clone of Iggy Pop.
Poet
A relative of Edgar Allan Poe.
sample header
sample data
HTML-Element-Library-5.220000/t/html/iter2.html 0000644 0001750 0001750 00000000362 12447476540 020221 0 ustar marius marius
Here are the type of people you meet at XYZ, inc:
Artist
A person who draws blood.
Musician
A clone of Iggy Pop.
Poet
A relative of Edgar Allan Poe.
HTML-Element-Library-5.220000/t/html/dual_iter-exp.html 0000644 0001750 0001750 00000000645 12447476540 021742 0 ustar marius marius
the pros
never have to worry about service again
the cons
upfront extra charge on purchase
our choice
go with the extended service plan
sample header
sample data
HTML-Element-Library-5.220000/t/html/fillinform-exp.html 0000644 0001750 0001750 00000000756 12447476540 022136 0 ustar marius marius
Dialer
HTML-Element-Library-5.220000/t/html/table.html 0000644 0001750 0001750 00000000331 12447476540 020257 0 ustar marius marius
name
age
weight
HTML-Element-Library-5.220000/t/html/table2-tr_ld-arrayref.html 0000644 0001750 0001750 00000005460 12447476540 023264 0 ustar marius marius
Complex HTML Table
metaperl
Last modified: Fri Nov 25 08:57:41 PST 2005
HTML-Element-Library-5.220000/t/html/iter.html 0000644 0001750 0001750 00000000163 12447476540 020136 0 ustar marius marius
Here are the things I need from the store:
Sample item
HTML-Element-Library-5.220000/t/html/highlander-5-exp.html 0000644 0001750 0001750 00000000210 12447476540 022225 0 ustar marius marius Hello, does your mother know you're using her AOL account?
HTML-Element-Library-5.220000/t/html/table2-table_ld-exp.html 0000644 0001750 0001750 00000005411 12447476540 022703 0 ustar marius marius
CODE-PAGE SUPPORT IN MICROSOFT WINDOWS
Code-Page ID
Name
ACP
OEMCP
Windows NT 3.1
Windows NT 3.51
Windows 95
1200
Unicode (BMP of ISO/IEC-10646)
X
X
*
1250
Windows 3.1 Eastern European
X
X
X
X
1251
Windows 3.1 Cyrillic
X
X
X
X
1252
Windows 3.1 US (ANSI)
X
X
X
X
1253
Windows 3.1 Greek
X
X
X
X
1254
Windows 3.1 Turkish
X
X
X
X
1255
Hebrew
X
X
1256
Arabic
X
X
1257
Baltic
X
X
1361
Korean (Johab)
X
**
X
437
MS-DOS United States
X
X
X
X
708
Arabic (ASMO 708)
X
X
709
Arabic (ASMO 449+, BCON V4)
X
X
710
Arabic (Transparent Arabic)
X
X
720
Arabic (Transparent ASMO)
X
X
HTML-Element-Library-5.220000/t/html/highlander2-15-exp.html 0000644 0001750 0001750 00000000237 12447476540 022401 0 ustar marius marius Sorry, you're only 15 (and too dumb to lie about your age)
HTML-Element-Library-5.220000/t/html/itercb-exp.html 0000644 0001750 0001750 00000000612 12733573644 021235 0 ustar marius marius
First Name
Last Name
Option
Foo
Bar
2
Bar
Bar
3
Baz
Bar
4
HTML-Element-Library-5.220000/t/html/table-alt.html 0000644 0001750 0001750 00000000737 12447476540 021047 0 ustar marius marius
name
age
weight
NATURE BOY RIC FLAIR
35
220
NATURE BOY RIC FLAIR
35
220
HTML-Element-Library-5.220000/t/html/highlander-15-exp.html 0000644 0001750 0001750 00000000230 12447476540 022310 0 ustar marius marius Sorry, you're not old enough to enter (and too dumb to lie about your age)
HTML-Element-Library-5.220000/t/html/highlander2-5-exp.html 0000644 0001750 0001750 00000000262 12447476540 022316 0 ustar marius marius Hello, little 5-year old, does your mother know you're using her AOL account?
HTML-Element-Library-5.220000/t/html/itercb.html 0000644 0001750 0001750 00000000335 12733573635 020445 0 ustar marius marius
First Name
Last Name
Option
First
Last
1
HTML-Element-Library-5.220000/t/perlcritic.t 0000644 0001750 0001750 00000000332 12447521405 017653 0 ustar marius marius #!/usr/bin/perl
use strict;
use warnings;
use Test::More;
BEGIN { plan skip_all => '$ENV{RELEASE_TESTING} is false' unless $ENV{RELEASE_TESTING} }
use Test::Perl::Critic -profile => 't/perlcriticrc';
all_critic_ok
HTML-Element-Library-5.220000/t/misc.t 0000644 0001750 0001750 00000014771 12733572666 016477 0 ustar marius marius #!/usr/bin/perl -T
use lib '.';
use t::lib tests => 27;
##################################################
# Short tests based on mklol
sub mklol {
HTML::Element->new_from_lol(
['html',
['head',
[ 'title', 'I like stuff!' ]],
['body', {id => 'corpus'}, {'lang', 'en-JP'},
'stuff',
['p', 'um, p < 4!', {'class' => 'par123'}],
['div', {foo => 'bar'}, '123'],
['div', {jack => 'olantern'}, '456']]]);
}
my $tree_replaced = \'I like stuff!all gone!';
my $tree;
$tree = mklol;
$tree->content_handler(corpus => 'all gone!');
isxml $tree, $tree_replaced, 'content_handler';
$tree = mklol;
$tree->set_child_content(id => 'corpus', 'all gone!');
isxml $tree, $tree_replaced, 'set_child_content';
$tree = mklol;
$tree->look_down('_tag' => 'body')->replace_content('all gone!');
isxml $tree, $tree_replaced, 'replace_content';
$tree = mklol;
my $p = $tree->look_down('_tag' => 'body')->look_down(_tag => 'p');
is $p->sibdex, 1, 'p tag has 1 as its index';
$tree = mklol;
my $div = $tree->look_down('_tag' => 'body')->look_down(_tag => 'p');
my @sibs = $div->siblings;
is $sibs[0], 'stuff', "first sibling is simple text";
is $sibs[2]->tag, 'div', "3rd tag is a div tag";
is scalar @sibs, 4, "4 siblings total";
$tree = mklol;
my $bold = HTML::Element->new('b', id => 'wrapper');
my $w = $tree->look_down(_tag => 'p');
$w->wrap_content($bold);
isxml $w, \'
um, p < 4!
', 'wrap_content';
##################################################
# Short tests
$tree = mktree 't/html/crunch.html';
$tree->crunch(look_down => [ class => 'imageElement' ], leave => 1);
isxml $tree, 't/html/crunch-exp.html', 'crunch';
$tree = mktree 't/html/defmap.html';
$tree->defmap(smap => {pause => 'arsenal rules'}, $ENV{TEST_VERBOSE});
isxml $tree, 't/html/defmap-exp.html', 'defmap';
$tree = mktree 't/html/fillinform.html';
isxml \($tree->fillinform({state => 'catatonic'})), 't/html/fillinform-exp.html', 'fillinform';
$tree = mktree 't/html/hashmap.html';
$tree->hash_map(
hash => {people_id => 888, phone => '444-4444', email => 'm@xml.com'},
to_attr => 'sid',
excluding => ['email']
);
isxml $tree, 't/html/hashmap-exp.html', 'hash_map';
$tree = mktree 't/html/iter.html';
my $li = $tree->look_down(class => 'store_items');
$tree->iter($li, qw/bread butter vodka/);
isxml $tree, 't/html/iter-exp.html', 'iter';
my @list = map { [item => $_] } qw/bread butter beans/;
my $initial_lol = [ note => [ list => [ item => 'sample' ] ] ];
my ($new_lol) = HTML::Element::newchild($initial_lol, list => @list);
my $expected = [note => [list => [item => 'bread'], [item => 'butter'], [item => 'beans']]];
is_deeply $new_lol, $expected, 'newchild unrolling';
$tree = mktree 't/html/highlander2.html';
$tree->passover('under18');
isxml $tree, 't/html/highlander2-passover-exp.html', 'passover';
$tree = mktree 't/html/position.html';
my $found = $tree->look_down(id => 'findme');
my $pos = join ' ', $found->position;
is $pos, '-1 1 0 1 2', 'position';
$tree = mktree 't/html/prune.html';
$tree->prune;
isxml $tree, 't/html/prune-exp.html', 'prune';
##################################################
# Longer tests
$tree = mktree 't/html/dual_iter.html';
$tree->iter2(
wrapper_data => [
['the pros' => 'never have to worry about service again'],
['the cons' => 'upfront extra charge on purchase'],
['our choice' => 'go with the extended service plan']
],
wrapper_proc => sub {
my ($container) = @_;
# only keep the last 2 dts and dds
my @content_list = $container->content_list;
$container->splice_content(0, @content_list - 2);
},
splice => sub {
my ($container, @item_elems) = @_;
$container->unshift_content(@item_elems);
},
debug => $ENV{TEST_VERBOSE},
);
isxml $tree, 't/html/dual_iter-exp.html', 'dual_iter';
###
sub cb {
my ($data, $tr) = @_;
$tr->look_down(class => 'first')->replace_content($data->{first});
$tr->look_down(class => 'last')->replace_content($data->{last});
$tr->look_down(class => 'option')->replace_content($data->{option});
}
my @cbdata = (
{first => 'Foo', last => 'Bar', option => 2},
{first => 'Bar', last => 'Bar', option => 3},
{first => 'Baz', last => 'Bar', option => 4},
);
$tree = mktree 't/html/itercb.html';
$tree->find('table')->find('tbody')->find('tr')->itercb(\@cbdata, \&cb);
isxml $tree, 't/html/itercb-exp.html', 'itercb';
###
for my $age (qw/5 15 50/) {
$tree = mktree 't/html/highlander.html';
$tree->highlander(
age_dialog => [
under10 => sub { $_[0] < 10 },
under18 => sub { $_[0] < 18 },
welcome => sub { 1 }
],
$age
);
isxml $tree, "t/html/highlander-$age-exp.html", "highlander for $age";
}
###
sub replace_age {
my ($branch, $age) = @_;
$branch->look_down(id => 'age')->replace_content($age);
}
for my $age (qw/5 15 27/) {
$tree = mktree 't/html/highlander2.html';
my $if_then = $tree->look_down(id => 'age_dialog')->highlander2(
cond => [
under10 => [ sub { $_[0] < 10 }, \&replace_age ],
under18 => [ sub { $_[0] < 18 }, \&replace_age ],
welcome => [ sub { 1 }, \&replace_age ]
],
cond_arg => [ $age ]
);
isxml ($tree, "t/html/highlander2-$age-exp.html", "highlander2 for age $age");
}
###
$tree = mktree 't/html/iter2.html';
$tree->iter2(
# default wrapper_ld ok
wrapper_data => [
[ Programmer => 'one who likes Perl and Seamstress' ],
[ DBA => 'one who does business as' ],
[ Admin => 'one who plays Tetris all day' ]
],
wrapper_proc => sub {
my ($container) = @_;
# only keep the last 2 dts and dds
my @content_list = $container->content_list;
$container->splice_content(0, @content_list - 2);
},
# default item_ld is k00l
# default item_data is phrEsh
# default item_proc will do w0rk
splice => sub {
my ($container, @item_elems) = @_;
$container->unshift_content(@item_elems);
},
debug => $ENV{TEST_VERBOSE},
);
isxml $tree, 't/html/iter2-exp.html', 'iter2';
###
my @data = (
{ clan_name => 'janglers', clan_id => 12, selected => 1 },
{ clan_name => 'thugknights', clan_id => 14 },
{ clan_name => 'cavaliers' , clan_id => 13 }
);
$tree = mktree 't/html/unroll_select.html';
$tree->unroll_select(
select_label => 'clan_list',
option_value => sub { my $row = shift; $row->{clan_id} },
option_content => sub { my $row = shift; $row->{clan_name} },
option_selected => sub { my $row = shift; $row->{selected} },
data => \@data,
data_iter => sub { my $data = shift; shift @$data });
isxml $tree, 't/html/unroll_select-exp.html', 'unroll_select';
HTML-Element-Library-5.220000/Changes 0000644 0001750 0001750 00000002254 12733574611 016370 0 ustar marius marius Revision history for Perl extension HTML::Element::Library.
5.220000 2016-06-26T00:37+03:00
- Fix spelling errors in POD (RT #115466)
- Add itercb, fid, fclass methods
- Use https for repository link in meta.yml
5.210000 2015-01-17T17:35+02:00
- Stable release
- Update copyright years
5.200_002 2014-12-27T14:17+02:00
- Remove 'use v5.14;' from t/perlcritic.t
5.200_001 2014-12-27T12:45+02:00
- Adopt module
- Clean up/tidy/refactor everything (code, tests, pod, packaging)
- Add perlcritic test and make code compliant
- Replace dist.ini with traditional Makefile.PL
- Fix testsuite for 5.18+ (RT #87850)
- Fix dependencies (RT #73995)
- Update Changes to CPAN::Changes::Spec format
5.120100 2012-01-10
5.112170 2011-08-05
5.112060 2011-07-25
5.111890 2011-07-08
4.3 2011-03-22
4.2.c 2011-03-20
4.2.b 2010-01-23
4.2.a 2009-08-06
4.0 2009-07-31
3.53 2009-05-29
3.51 2006-04-11
- Depend on Array::Group instead of Array::Reform
3.50 2006-02-13
- Make highlander2 use Params::Validate
3.1 2006-01-21
- Add iter2
2.0 2005-11-30
1.9 2005-11-29
1.2 2005-09-24
0.06 2005-09-19
0.05 2005-02-07
0.04 2005-02-03
0.03 2005-02-02
0.002 2005-01-29
0.001 2005-01-27
HTML-Element-Library-5.220000/MANIFEST 0000644 0001750 0001750 00000002617 12733574627 016240 0 ustar marius marius Changes
lib/HTML/Element/Library.pm
MANIFEST
Makefile.PL
README
t/00system.t
t/html/crunch-exp.html
t/html/crunch.html
t/html/defmap-exp.html
t/html/defmap.html
t/html/dual_iter-exp.html
t/html/dual_iter.html
t/html/fillinform-exp.html
t/html/fillinform.html
t/html/hashmap-exp.html
t/html/hashmap.html
t/html/highlander-15-exp.html
t/html/highlander2-15-exp.html
t/html/highlander2-27-exp.html
t/html/highlander2-5-exp.html
t/html/highlander2.html
t/html/highlander2-passover-exp.html
t/html/highlander-50-exp.html
t/html/highlander-5-exp.html
t/html/highlander.html
t/html/iter2-exp.html
t/html/iter2.html
t/html/itercb-exp.html
t/html/itercb.html
t/html/iter-exp.html
t/html/iter.html
t/html/position.html
t/html/prune-exp.html
t/html/prune.html
t/html/table2-exp.html
t/html/table2.html
t/html/table2-table_ld-exp.html
t/html/table2-tr_ld-arrayref-exp.html
t/html/table2-tr_ld-arrayref.html
t/html/table2-tr_ld-coderef-exp.html
t/html/table2-tr_ld-default-exp.html
t/html/table-alt-exp.html
t/html/table-alt.html
t/html/table-exp.html
t/html/table.html
t/html/unroll_select-exp.html
t/html/unroll_select.html
t/lib.pm
t/misc.t
t/perlcriticrc
t/perlcritic.t
t/tables.t
META.yml Module YAML meta-data (added by MakeMaker)
META.json Module JSON meta-data (added by MakeMaker)
SIGNATURE Public-key signature (added by MakeMaker)
HTML-Element-Library-5.220000/SIGNATURE 0000644 0001750 0001750 00000011646 12733574630 016367 0 ustar marius marius This file contains message digests of all files listed in MANIFEST,
signed via the Module::Signature module, version 0.79.
To verify the content in this distribution, first make sure you have
Module::Signature installed, then type:
% cpansign -v
It will check each file's integrity, as well as the signature's
validity. If "==> Signature verified OK! <==" is not displayed,
the distribution may already have been compromised, and you should
not run its Makefile.PL or Build.PL.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
SHA1 300fc54150aee9a54fda9c702420ec87e24ce4d0 Changes
SHA1 7bcad13ac09dde21f84cf5ce83edeabff115528c MANIFEST
SHA1 5ed4c509817dc5ba0b6328e27f7d1547c0fbb209 META.json
SHA1 1ea29b503c9e58687727c70cb833fb5cd9c23060 META.yml
SHA1 1dda51c011486559d29bb8a7dd99d94b2807a70d Makefile.PL
SHA1 13943d9e45c3552bf6f6240303b16783bb407e3b README
SHA1 4a28a03da82ba02ff5796fea73563701e9d77aa8 lib/HTML/Element/Library.pm
SHA1 de66e070d9ee27d00c9043740a51b75672f8b70c t/00system.t
SHA1 e2193f3aaf1f01d3afe33b4246a100fd8f22a85a t/html/crunch-exp.html
SHA1 341bc3688909db8d712eb8e16d827c6c761274fb t/html/crunch.html
SHA1 f860da3833285b6aadef5f76a0e4d8b8a0c5d2b8 t/html/defmap-exp.html
SHA1 eca148d8eae0be6f1ab19928f5c3d4b85cc817ff t/html/defmap.html
SHA1 d0fa0224af178d8aa7d1ee622252d987ae119a04 t/html/dual_iter-exp.html
SHA1 66d9cdb09279f5bfff6a52155df41bb3c495acf6 t/html/dual_iter.html
SHA1 404fb46c90fd6564b119eae5d2c8d8cef1af76a4 t/html/fillinform-exp.html
SHA1 787700b033a6860c3d9e50111cc80dfd11f3081e t/html/fillinform.html
SHA1 4992e94cb0903c54c38795a41938378d4c8227b0 t/html/hashmap-exp.html
SHA1 8e228d80be1826a61e1cfe3bcc47d8cfdce73b3a t/html/hashmap.html
SHA1 7d0e6b932cec1618b5d1ddc5620674cbd7fa57c7 t/html/highlander-15-exp.html
SHA1 b8c49946bfca498d50df2548d9abe82f5894d3d4 t/html/highlander-5-exp.html
SHA1 b9971a9de8fe238a8ffa306f0ca2838149e7799b t/html/highlander-50-exp.html
SHA1 9421015a5b52d4680eb49e673e34529516516f0f t/html/highlander.html
SHA1 547a7bc5f7498cc1aed8d42d3fa6c28c120eb2a7 t/html/highlander2-15-exp.html
SHA1 8c0472e58b5ff430d461b1183fa9b3cf779fc485 t/html/highlander2-27-exp.html
SHA1 61d67c2af21f995ead218469a82aaa7ef40f67e3 t/html/highlander2-5-exp.html
SHA1 604aadd96324696c0dd033f7abf9665d37be7c5d t/html/highlander2-passover-exp.html
SHA1 b3923438fea8846e82f6cf05379ff43a774f9c90 t/html/highlander2.html
SHA1 ec25775279f28ae5cf6029b7f58c8b517c5136a1 t/html/iter-exp.html
SHA1 405cd0a0d8031e6947bee961f967184ac1f3e75c t/html/iter.html
SHA1 2d9589489b7bfa58f21938246e688e2bd5a71879 t/html/iter2-exp.html
SHA1 9311975a54f9cce63a3c5a21ecbfba14abaf46e1 t/html/iter2.html
SHA1 afc361b5a3a9edb833fb22728d96c57a56a5bf4e t/html/itercb-exp.html
SHA1 2f55b52a6b897cbe2ab5d973c7bb4af28efc79c2 t/html/itercb.html
SHA1 24e3ef64a56b047a6ddf357e192ccdf2bcdd85aa t/html/position.html
SHA1 cb85f61116828c3c1157d6bb026d1cbe7f1bfd78 t/html/prune-exp.html
SHA1 762afc24bb38d51b3b5293a6b8e7b17d5662a9b4 t/html/prune.html
SHA1 9ceec5ef96a4a9cb1227866bc78433826cfbb5a8 t/html/table-alt-exp.html
SHA1 3d21aab8c6c02e878fb3cdd3b72c63bddd8a95d4 t/html/table-alt.html
SHA1 1895da3c3322a0fa2ed4f04ce8fa0ed3cc7efed6 t/html/table-exp.html
SHA1 10815b659306e9a0c75133546bdd88a3632460ca t/html/table.html
SHA1 6c7a9138128aaa8e51332116d3adb8b11aadae8c t/html/table2-exp.html
SHA1 5b9bedb396e95d268227f1557a270b41b6dda44b t/html/table2-table_ld-exp.html
SHA1 3647c669c95141deb31dafbd7635d6248a76d75b t/html/table2-tr_ld-arrayref-exp.html
SHA1 9c6aa4dde4ee1e64915c38e139086c3c93024014 t/html/table2-tr_ld-arrayref.html
SHA1 6238218ea283a93a26360192b39fec991e4b2364 t/html/table2-tr_ld-coderef-exp.html
SHA1 c7b37bea01adbe3255e2ada86725f392e86fa9c2 t/html/table2-tr_ld-default-exp.html
SHA1 71cc9f5a0f03ca3feebe81913876eb9945fb33ff t/html/table2.html
SHA1 5cb634cca3eb0aa522e0264e10923abe4c11bf22 t/html/unroll_select-exp.html
SHA1 c1a6c5123288f163671f68e15c68588bd6f4bfd3 t/html/unroll_select.html
SHA1 aed47a4516ad8c5381e06c41a16bdfc6e7ed15b4 t/lib.pm
SHA1 79db24f58aa8c16da4644b4fc4591911fa92f508 t/misc.t
SHA1 61d0ee2ff95402d8b22f637fca777334317171f4 t/perlcritic.t
SHA1 125969baa323f3257bf675eae19b556cfb2bf958 t/perlcriticrc
SHA1 97b0d259035dd1b0eac7497ed80ff6d4ae53dccc t/tables.t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
iQIcBAEBCgAGBQJXbvmXAAoJEMoENb5ewbNi800P/A8Crj6Zuq16y/q8AnUPaOcK
6IvoUJacHyvoa1OrQbYV7WBUvRccJr9XODNlDjZS8pEvBwo2WQfeqdzoCLnuuPxn
BgNleEgr8TQoZoddZtKJ9qiis25QU6Nngo5VBgSWzZsUN6ZHw5ZZLmzkUF3FICxh
G5uNOSJmvNyJnfUu/UnYQ/zvRQBnYmcj8P1O2O+uK0exPA/vKwVsdWcY+LOXLcSU
W7/kV9r7EDtaHYwsbuxhWLvPyYhc+QWmy/d/ezqUcpXfuti8IDc1bEEk1wR0yhmS
2Z4ovJs9wAAhIA047mUUDs016z13ZpR/Qrh3O8sARDUoL+24h6asfy+pb0mq4gWm
mA9e4mB3oEe9dQWN1YS764sh1wNe5PxOgtfJbfiuFD1dj5BRHM0ZCC4Ve2l1c6HT
gKzhzylqCqdExBUYyo/Aeql7e4NuNTmcCxwG9f7QJ9RAk9DsbaIDihRbhr2JmPQa
dEbMUP5eilEBlaEtVXdXJMODAKpvi5Yy3PTNURygKABknn/6h84HUA4jCJBccKKp
5a/KMxpf6JwVx3db6V1ZJCgwl5smlv6cdUqUsSE/Et3iZ0Fp3YIeUBYBH/z+VAoF
2IM5KdXIuqKgbfUyY4pnEPuF6AC0LT1WDyPqzTW/ujnE5mff/BE9Z2FuZdrE6fqq
PtNKv5MB3d5uK/jN3lk6
=Y3Rq
-----END PGP SIGNATURE-----
HTML-Element-Library-5.220000/META.json 0000644 0001750 0001750 00000003070 12733574627 016522 0 ustar marius marius {
"abstract" : "HTML::Element convenience functions",
"author" : [
"Terrence Brannon ",
"Marius Gavrilescu "
],
"dynamic_config" : 0,
"generated_by" : "ExtUtils::MakeMaker version 7.0401, CPAN::Meta::Converter version 2.150005",
"license" : [
"perl_5"
],
"meta-spec" : {
"url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
"version" : "2"
},
"name" : "HTML-Element-Library",
"no_index" : {
"directory" : [
"t",
"inc"
]
},
"prereqs" : {
"build" : {
"requires" : {
"File::Slurp" : "0",
"HTML::PrettyPrinter" : "0",
"HTML::TreeBuilder" : "0",
"Test::More" : "0",
"Test::XML" : "0"
}
},
"configure" : {
"requires" : {
"ExtUtils::MakeMaker" : "0"
}
},
"runtime" : {
"requires" : {
"Array::Group" : "0",
"Data::Rmap" : "0",
"HTML::Element" : "0",
"HTML::FillInForm" : "0",
"List::MoreUtils" : "0",
"List::Rotation::Cycle" : "0",
"List::Util" : "0",
"Params::Validate" : "0",
"Scalar::Listify" : "0",
"perl" : "5.006000"
}
}
},
"release_status" : "stable",
"resources" : {
"repository" : {
"url" : "https://git.ieval.ro/?p=html-element-library.git"
}
},
"version" : "5.220000",
"x_serialization_backend" : "JSON::PP version 2.27400"
}
HTML-Element-Library-5.220000/lib/ 0000755 0001750 0001750 00000000000 12733574627 015647 5 ustar marius marius HTML-Element-Library-5.220000/lib/HTML/ 0000755 0001750 0001750 00000000000 12733574627 016413 5 ustar marius marius HTML-Element-Library-5.220000/lib/HTML/Element/ 0000755 0001750 0001750 00000000000 12733574627 020004 5 ustar marius marius HTML-Element-Library-5.220000/lib/HTML/Element/Library.pm 0000644 0001750 0001750 00000151540 12733574441 021746 0 ustar marius marius package HTML::Element::Library;
use strict;
use warnings;
our $VERSION = '5.220000';
our $DEBUG = 0;
use Array::Group ':all';
use Carp 'confess';
use Data::Dumper;
use Data::Rmap 'rmap_array';
use HTML::Element;
use HTML::FillInForm;
use List::MoreUtils ':all';
use List::Rotation::Cycle;
use List::Util 'first';
use Params::Validate ':all';
use Scalar::Listify;
# https://rt.cpan.org/Ticket/Display.html?id=44105
sub HTML::Element::fillinform {
my ($tree, $hashref, $return_tree, $guts) = @_;
(ref $hashref) eq 'HASH' or confess 'hashref not supplied as argument' ;
my $html = $tree->as_HTML;
my $new_html = HTML::FillInForm->fill(\$html, $hashref);
if ($return_tree) {
$tree = HTML::TreeBuilder->new_from_content($new_html);
$tree = $guts ? $tree->guts : $tree ;
} else {
$new_html;
}
}
sub HTML::Element::siblings {
my $element = shift;
my $p = $element->parent;
return () unless $p;
$p->content_list;
}
sub HTML::Element::defmap {
my($tree, $attr, $hashref, $debug) = @_;
while (my ($k, $v) = (each %$hashref)) {
warn "defmap looks for ($attr => $k)" if $debug;
my $found = $tree->look_down($attr => $k);
if ($found) {
warn "($attr => $k) was found.. replacing with '$v'" if $debug;
$found->replace_content( $v );
}
}
}
sub HTML::Element::_only_empty_content {
my ($self) = @_;
my @c = $self->content_list;
my $length = scalar @c;
scalar @c == 1 and not length $c[0];
}
sub HTML::Element::prune {
my ($self) = @_;
for my $c ($self->content_list) {
next unless ref $c;
$c->prune;
}
# post-order:
$self->delete if ($self->is_empty or $self->_only_empty_content);
$self;
}
sub HTML::Element::newchild {
my ($lol, $parent_label, @newchild) = @_;
rmap_array {
if ($_->[0] eq $parent_label) {
$_ = [ $parent_label => @newchild ];
Data::Rmap::cut($_);
} else {
$_;
}
} $lol;
}
sub HTML::Element::crunch { ## no critic (RequireArgUnpacking)
my $container = shift;
my %p = validate(@_, {
look_down => { type => ARRAYREF },
leave => { default => 1 },
});
my @look_down = @{$p{look_down}} ;
my @elem = $container->look_down(@look_down) ;
my $detached;
for my $elem (@elem) {
$elem->detach if $detached++ >= $p{leave};
}
}
sub HTML::Element::hash_map { ## no critic (RequireArgUnpacking)
my $container = shift;
my %p = validate(@_, {
hash => { type => HASHREF },
to_attr => 1,
excluding => { type => ARRAYREF , default => [] },
debug => { default => 0 },
});
warn 'The container tag is ', $container->tag if $p{debug} ;
warn 'hash' . Dumper($p{hash}) if $p{debug} ;
#warn 'at_under' . Dumper(\@_) if $p{debug} ;
my @same_as = $container->look_down( $p{to_attr} => qr/.+/s ) ;
warn 'Found ' . scalar(@same_as) . ' nodes' if $p{debug} ;
for my $same_as (@same_as) {
my $attr_val = $same_as->attr($p{to_attr}) ;
if (first { $attr_val eq $_ } @{$p{excluding}}) {
warn "excluding $attr_val" if $p{debug} ;
next;
}
warn "processing $attr_val" if $p{debug} ;
$same_as->replace_content($p{hash}->{$attr_val});
}
}
sub HTML::Element::hashmap {
my ($container, $attr_name, $hashref, $excluding, $debug) = @_;
$excluding ||= [] ;
$container->hash_map(
hash => $hashref,
to_attr => $attr_name,
excluding => $excluding,
debug => $debug);
}
sub HTML::Element::passover {
my ($tree, @to_preserve) = @_;
warn "ARGS: my ($tree, @to_preserve)" if $DEBUG;
warn $tree->as_HTML(undef, ' ') if $DEBUG;
my $exodus = $tree->look_down(id => $to_preserve[0]);
warn "E: $exodus" if $DEBUG;
my @s = HTML::Element::siblings($exodus);
for my $s (@s) {
next unless ref $s;
$s->delete unless first { $s->attr('id') eq $_ } @to_preserve;
}
return $exodus; # Goodbye Egypt! http://en.wikipedia.org/wiki/Passover
}
sub HTML::Element::sibdex {
my $element = shift;
firstidx { $_ eq $element } $element->siblings
}
sub HTML::Element::addr { goto &HTML::Element::sibdex }
sub HTML::Element::replace_content {
my $elem = shift;
$elem->delete_content;
$elem->push_content(@_);
}
sub HTML::Element::wrap_content {
my($self, $wrap) = @_;
my $content = $self->content;
if (ref $content) {
$wrap->push_content(@$content);
@$content = ($wrap);
}
else {
$self->push_content($wrap);
}
$wrap;
}
sub HTML::Element::Library::super_literal {
my($text) = @_;
HTML::Element->new('~literal', text => $text);
}
sub HTML::Element::position {
# Report coordinates by chasing addr's up the
# HTML::ElementSuper tree. We know we've reached
# the top when a) there is no parent, or b) the
# parent is some HTML::Element unable to report
# it's position.
my $p = shift;
my @pos;
while ($p) {
my $a = $p->addr;
unshift @pos, $a if defined $a;
$p = $p->parent;
}
@pos;
}
sub HTML::Element::content_handler {
my ($tree, %content_hash) = @_;
for my $k (keys %content_hash) {
$tree->set_child_content(id => $k, $content_hash{$k});
}
}
sub HTML::Element::assign { goto &HTML::Element::content_handler }
sub make_counter {
my $i = 1;
sub {
shift() . ':' . $i++
}
}
sub HTML::Element::iter {
my ($tree, $p, @data) = @_;
# warn 'P: ' , $p->attr('id') ;
# warn 'H: ' , $p->as_HTML;
# my $id_incr = make_counter;
my @item = map {
my $new_item = clone $p;
$new_item->replace_content($_);
$new_item;
} @data;
$p->replace_with(@item);
}
sub HTML::Element::itercb {
my ($self, $data, $code) = @_;
my $orig = $self;
my $prev = $orig;
for my $el (@$data) {
my $current = $orig->clone;
$code->($el, $current);
$prev->postinsert($current);
$prev = $current;
}
$orig->detach;
}
sub HTML::Element::iter2 { ## no critic (RequireArgUnpacking)
my $tree = shift;
#warn "INPUT TO TABLE2: ", Dumper \@_;
my %p = validate(
@_, {
wrapper_ld => { default => ['_tag' => 'dl'] },
wrapper_data => 1,
wrapper_proc => { default => undef },
item_ld => {
default => sub {
my $tr = shift;
[
$tr->look_down('_tag' => 'dt'),
$tr->look_down('_tag' => 'dd')
];
}},
item_data => {
default => sub {
my ($wrapper_data) = @_;
shift @{$wrapper_data};
}},
item_proc => {
default => sub {
my ($item_elems, $item_data, $row_count) = @_;
$item_elems->[$_]->replace_content($item_data->[$_]) for (0,1) ;
$item_elems;
}},
splice => {
default => sub {
my ($container, @item_elems) = @_;
$container->splice_content(0, 2, @item_elems);
}
},
debug => {default => 0}
}
);
warn 'wrapper_data: ' . Dumper $p{wrapper_data} if $p{debug} ;
my $container = ref_or_ld($tree, $p{wrapper_ld});
warn 'container: ' . $container if $p{debug} ;
warn 'wrapper_(preproc): ' . $container->as_HTML if $p{debug} ;
$p{wrapper_proc}->($container) if defined $p{wrapper_proc} ;
warn 'wrapper_(postproc): ' . $container->as_HTML if $p{debug} ;
my $_item_elems = $p{item_ld}->($container);
my $row_count;
my @item_elem;
while(1){
my $item_data = $p{item_data}->($p{wrapper_data});
last unless defined $item_data;
warn Dumper('item_data', $item_data) if $p{debug};
my $item_elems = [ map { $_->clone } @{$_item_elems} ] ;
if ($p{debug}) {
for (@{$item_elems}) {
warn 'ITEM_ELEMS ', $_->as_HTML if $p{debug};
}
}
my $new_item_elems = $p{item_proc}->($item_elems, $item_data, ++$row_count);
if ($p{debug}) {
for (@{$new_item_elems}) {
warn 'NEWITEM_ELEMS ', $_->as_HTML if $p{debug};
}
}
push @item_elem, @{$new_item_elems} ;
}
warn 'pushing ' . @item_elem . ' elems' if $p{debug} ;
$p{splice}->($container, @item_elem);
}
sub HTML::Element::dual_iter {
my ($parent, $data) = @_;
my ($prototype_a, $prototype_b) = $parent->content_list;
# my $id_incr = make_counter;
my $i;
@$data %2 == 0 or confess 'dataset does not contain an even number of members';
my @iterable_data = ngroup 2 => @$data;
my @item = map {
my ($new_a, $new_b) = map { clone $_ } ($prototype_a, $prototype_b) ;
$new_a->splice_content(0,1, $_->[0]);
$new_b->splice_content(0,1, $_->[1]);
#$_->attr('id', $id_incr->($_->attr('id'))) for ($new_a, $new_b) ;
($new_a, $new_b)
} @iterable_data;
$parent->splice_content(0, 2, @item);
}
sub HTML::Element::set_child_content { ## no critic (RequireArgUnpacking)
my $tree = shift;
my $content = pop;
my @look_down = @_;
my $content_tag = $tree->look_down(@look_down);
unless ($content_tag) {
warn "criteria [@look_down] not found";
return;
}
$content_tag->replace_content($content);
}
sub HTML::Element::highlander {
my ($tree, $local_root_id, $aref, @arg) = @_;
ref $aref eq 'ARRAY' or confess 'must supply array reference';
my @aref = @$aref;
@aref % 2 == 0 or confess 'supplied array ref must have an even number of entries';
warn __PACKAGE__ if $DEBUG;
my $survivor;
while (my ($id, $test) = splice @aref, 0, 2) {
warn $id if $DEBUG;
if ($test->(@arg)) {
$survivor = $id;
last;
}
}
my @id_survivor = (id => $survivor);
my $survivor_node = $tree->look_down(@id_survivor);
# warn $survivor;
# warn $local_root_id;
# warn $node;
warn "survivor: $survivor" if $DEBUG;
warn 'tree: ' . $tree->as_HTML if $DEBUG;
$survivor_node or die "search for @id_survivor failed in tree($tree): " . $tree->as_HTML;
my $survivor_node_parent = $survivor_node->parent;
$survivor_node = $survivor_node->clone;
$survivor_node_parent->replace_content($survivor_node);
warn 'new tree: ' . $tree->as_HTML if $DEBUG;
$survivor_node;
}
sub HTML::Element::highlander2 { ## no critic (RequireArgUnpacking)
my $tree = shift;
my %p = validate(@_, {
cond => { type => ARRAYREF },
cond_arg => {
type => ARRAYREF,
default => []
},
debug => { default => 0 }
});
my @cond = @{$p{cond}};
@cond % 2 == 0 or confess 'supplied array ref must have an even number of entries';
warn __PACKAGE__ if $p{debug};
my @cond_arg = @{$p{cond_arg}};
my $survivor; my $then;
while (my ($id, $if_then) = splice @cond, 0, 2) {
warn $id if $p{debug};
my ($if, $_then);
if (ref $if_then eq 'ARRAY') {
($if, $_then) = @$if_then;
} else {
($if, $_then) = ($if_then, sub {});
}
if ($if->(@cond_arg)) {
$survivor = $id;
$then = $_then;
last;
}
}
my @ld = (ref $survivor eq 'ARRAY') ? @$survivor : (id => $survivor);
warn 'survivor: ', $survivor if $p{debug};
warn 'survivor_ld: ', Dumper \@ld if $p{debug};
my $survivor_node = $tree->look_down(@ld);
$survivor_node or confess "search for @ld failed in tree($tree): " . $tree->as_HTML;
my $survivor_node_parent = $survivor_node->parent;
$survivor_node = $survivor_node->clone;
$survivor_node_parent->replace_content($survivor_node);
# **************** NEW FUNCTIONALITY *******************
# apply transforms on survivor node
warn 'SURV::pre_trans ' . $survivor_node->as_HTML if $p{debug};
$then->($survivor_node, @cond_arg);
warn 'SURV::post_trans ' . $survivor_node->as_HTML if $p{debug};
# **************** NEW FUNCTIONALITY *******************
$survivor_node;
}
sub overwrite_action {
my ($mute_node, %X) = @_;
$mute_node->attr($X{local_attr}{name} => $X{local_attr}{value}{new});
}
sub HTML::Element::overwrite_attr {
my $tree = shift;
$tree->mute_elem(@_, \&overwrite_action);
}
sub HTML::Element::mute_elem {
my ($tree, $mute_attr, $closures, $post_hook) = @_;
my @mute_node = $tree->look_down($mute_attr => qr/.*/s) ;
for my $mute_node (@mute_node) {
my ($local_attr,$mute_key) = split /\s+/s, $mute_node->attr($mute_attr);
my $local_attr_value_current = $mute_node->attr($local_attr);
my $local_attr_value_new = $closures->{$mute_key}->($tree, $mute_node, $local_attr_value_current);
$post_hook->(
$mute_node,
tree => $tree,
local_attr => {
name => $local_attr,
value => {
current => $local_attr_value_current,
new => $local_attr_value_new
}
}
) if ($post_hook) ;
}
}
sub HTML::Element::table {
my ($s, %table) = @_;
my $table = {};
# Get the table element
$table->{table_node} = $s->look_down(id => $table{gi_table});
$table->{table_node} or confess "table tag not found via (id => $table{gi_table}";
# Get the prototype tr element(s)
my @table_gi_tr = listify $table{gi_tr} ;
my @iter_node = map {
my $tr = $table->{table_node}->look_down(id => $_);
$tr or confess "tr with id => $_ not found";
$tr;
} @table_gi_tr;
warn 'found ' . @iter_node . ' iter nodes ' if $DEBUG;
my $iter_node = List::Rotation::Cycle->new(@iter_node);
# warn $iter_node;
warn Dumper ($iter_node, \@iter_node) if $DEBUG;
# $table->{content} = $table{content};
# $table->{parent} = $table->{table_node}->parent;
# $table->{table_node}->detach;
# $_->detach for @iter_node;
my @table_rows;
while (1) {
my $row = $table{tr_data}->($table, $table{table_data});
last unless defined $row;
# get a sample table row and clone it.
my $I = $iter_node->next;
warn "I: $I" if $DEBUG;
my $new_iter_node = $I->clone;
$table{td_data}->($new_iter_node, $row);
push @table_rows, $new_iter_node;
}
if (@table_rows) {
my $replace_with_elem = $s->look_down(id => shift @table_gi_tr) ;
$s->look_down(id => $_)->detach for @table_gi_tr;
$replace_with_elem->replace_with(@table_rows);
}
}
sub ref_or_ld {
my ($tree, $slot) = @_;
if (ref($slot) eq 'CODE') {
$slot->($tree);
} else {
$tree->look_down(@$slot);
}
}
sub HTML::Element::table2 { ## no critic (RequireArgUnpacking)
my $tree = shift;
my %p = validate(
@_, {
table_ld => { default => ['_tag' => 'table'] },
table_data => 1,
table_proc => { default => undef },
tr_ld => { default => ['_tag' => 'tr'] },
tr_data => {
default => sub {
my ($self, $data) = @_;
shift @{$data};
}},
tr_base_id => { default => undef },
tr_proc => { default => sub {} },
td_proc => 1,
debug => {default => 0}
}
);
warn 'INPUT TO TABLE2: ', Dumper \@_ if $p{debug};
warn 'table_data: ' . Dumper $p{table_data} if $p{debug} ;
my $table = {};
# Get the table element
$table->{table_node} = ref_or_ld( $tree, $p{table_ld} ) ;
$table->{table_node} or confess 'table tag not found via ' . Dumper($p{table_ld}) ;
warn 'table: ' . $table->{table_node}->as_HTML if $p{debug};
# Get the prototype tr element(s)
my @proto_tr = ref_or_ld( $table->{table_node}, $p{tr_ld} ) ;
warn 'found ' . @proto_tr . ' iter nodes' if $p{debug};
return unless @proto_tr;
if ($p{debug}) {
warn $_->as_HTML for @proto_tr;
}
my $proto_tr = List::Rotation::Cycle->new(@proto_tr);
my $tr_parent = $proto_tr[0]->parent;
warn 'parent element of trs: ' . $tr_parent->as_HTML if $p{debug};
my $row_count;
my @table_rows;
while(1) {
my $row = $p{tr_data}->($table, $p{table_data}, $row_count);
warn 'data row: ' . Dumper $row if $p{debug};
last unless defined $row;
# wont work: my $new_iter_node = $table->{iter_node}->clone;
my $new_tr_node = $proto_tr->next->clone;
warn "new_tr_node: $new_tr_node" if $p{debug};
$p{tr_proc}->($tree, $new_tr_node, $row, $p{tr_base_id}, ++$row_count) if defined $p{tr_proc};
warn 'data row redux: ' . Dumper $row if $p{debug};
$p{td_proc}->($new_tr_node, $row);
push @table_rows, $new_tr_node;
}
$_->detach for @proto_tr;
$tr_parent->push_content(@table_rows) if (@table_rows) ;
}
sub HTML::Element::unroll_select {
my ($s, %select) = @_;
my $select = {};
warn 'Select Hash: ' . Dumper(\%select) if $select{debug};
my $select_node = $s->look_down(id => $select{select_label});
warn "Select Node: $select_node" if $select{debug};
unless ($select{append}) {
for my $option ($select_node->look_down('_tag' => 'option')) {
$option->delete;
}
}
my $option = HTML::Element->new('option');
warn "Option Node: $option" if $select{debug};
$option->detach;
while (my $row = $select{data_iter}->($select{data})) {
warn 'Data Row: ' . Dumper($row) if $select{debug};
my $o = $option->clone;
$o->attr('value', $select{option_value}->($row));
$o->attr('SELECTED', 1) if (exists $select{option_selected} and $select{option_selected}->($row));
$o->replace_content($select{option_content}->($row));
$select_node->push_content($o);
warn $o->as_HTML if $select{debug};
}
}
sub HTML::Element::set_sibling_content {
my ($elt, $content) = @_;
$elt->parent->splice_content($elt->pindex + 1, 1, $content);
}
sub HTML::TreeBuilder::parse_string {
my ($package, $string) = @_;
my $h = HTML::TreeBuilder->new;
HTML::TreeBuilder->parse($string);
}
sub HTML::Element::fid { shift->look_down(id => $_[0]) }
sub HTML::Element::fclass { shift->look_down(class => qr/\b$_[0]\b/s) }
1;
__END__
=encoding utf-8
=head1 NAME
HTML::Element::Library - HTML::Element convenience functions
=head1 SYNOPSIS
use HTML::Element::Library;
use HTML::TreeBuilder;
=head1 DESCRIPTION
HTML:::Element::Library provides extra methods for HTML::Element.
=head1 METHODS
=head2 Aliases
These are short aliases for common operations:
=over
=item I<$el>->B(I<$id>)
Finds an element given its id. Equivalent to C<< $el->look_down(id => $id) >>.
=item I<$el>->B(I<$class>)
Finds one or more elements given one of their classes. Equivalent to C<< $el->look_down(class => qr/\b$class\b/s) >>
=back
=head2 Positional Querying Methods
=head3 $elem->siblings
Return a list of all nodes under the same parent.
=head3 $elem->sibdex
Return the index of C<$elem> into the array of siblings of which it is
a part. L calls this method C but I don't
think that is a descriptive name. And such naming is deceptively close
to the C function of C. HOWEVER, in the
interest of backwards compatibility, both methods are available.
=head3 $elem->addr
Same as sibdex
=head3 $elem->position()
Returns the coordinates of this element in the tree it inhabits. This
is accomplished by succesively calling addr() on ancestor elements
until either a) an element that does not support these methods is
found, or b) there are no more parents. The resulting list is the
n-dimensional coordinates of the element in the tree.
=head2 Element Decoration Methods
=head3 HTML::Element::Library::super_literal($text)
In L, Sean Burke discusses super-literals. They are
text which does not get escaped. Great for includng Javascript in
HTML. Also great for including foreign language into a document.
So, you basically toss C your text and back comes your
text wrapped in a C<~literal> element.
One of these days, I'll around to writing a nice C section.
=head2 Tree Rewriting Methods
=head3 "de-prepping" HTML
Oftentimes, the HTML to be worked with will have multiple sample rows:
bread
butter
beer
bacon
But, before you begin to rewrite the HTML with your model data, you
typically only want 1 or 2 sample rows.
Thus, you want to "crunch" the multiple sample rows to a specified
amount. Hence the C method:
$tree->crunch(look_down => [ '_tag' => 'li' ], leave => 2) ;
The C argument defaults to 1 if not given. The call above would
"crunch" the above 4 sample rows to:
bread
butter
=head3 Simplifying calls to HTML::FillInForm
Since HTML::FillInForm gets and returns strings, using HTML::Element
instances becomes tedious:
1. Seamstress has an HTML tree that it wants the form filled in on
2. Seamstress converts this tree to a string
3. FillInForm parses the string into an HTML tree and then fills in the form
4. FillInForm converts the HTML tree to a string
5. Seamstress re-parses the HTML for additional processing
I've filed a bug about this:
L
This function, fillinform, allows you to pass a tree to fillinform
(along with your data structure) and get back a tree:
my $new_tree = $html_tree->fillinform($data_structure);
=head3 Mapping a hashref to HTML elements
It is very common to get a hashref of data from some external source -
flat file, database, XML, etc. Therefore, it is important to have a
convenient way of mapping this data to HTML.
As it turns out, there are 3 ways to do this in
HTML::Element::Library. The most strict and structured way to do this
is with C. Two other methods, C and
C require less manual mapping and may prove even more easy to
use in certain cases.
As is usual with Perl, a practical example is always best. So let's
take some sample HTML:
user data
???
Now, let's say our data structure is this:
$ref = { email => 'jim@beam.com', gender => 'lots' } ;
And let's start with the most strict way to get what you want:
$tree->content_handler(email => $ref->{email} , gender => $ref->{gender}) ;
In this case, you manually state the mapping between id tags and
hashref keys and then C retrieves the hashref data
and pops it in the specified place.
Now let's look at the two (actually 2 and a half) other hash-mapping
methods.
$tree->hashmap(id => $ref);
Now, what this function does is super-destructive. It finds every
element in the tree with an attribute named id (since 'id' is a
parameter, it could find every element with some other attribute also)
and replaces the content of those elements with the hashref value.
So, in the case above, the
?
would come out as
(it would be blank) - because there is nothing in the hash with that
value, so it substituted
$ref->{name}
which was blank and emptied the contents.
Now, let's assume we want to protect name from being auto-assigned.
Here is what you do:
$tree->hashmap(id => $ref, ['name']);
That last array ref is an exclusion list.
But wouldnt it be nice if you could do a hashmap, but only assigned
things which are defined in the hashref? C<< defmap() >> to the
rescue:
$tree->defmap(id => $ref);
does just that, so
?
would be left alone.
=head4 $elem->hashmap($attr_name, \%hashref, \@excluded, $debug)
This method is designed to take a hashref and populate a series of
elements. For example:
1
(877) 255-3239
*********
In the table above, there are several attributes named C<< smap >>. If
we have a hashref whose keys are the same:
my %data = (people_id => 888, phone => '444-4444', password => 'dont-you-dare-render');
Then a single API call allows us to populate the HTML while excluding
those ones we don't:
$tree->hashmap(smap => \%data, ['password']);
Note: the other way to prevent rendering some of the hash mapping is
to not give that element the attr you plan to use for hash mapping.
Also note: the function C<< hashmap >> has a simple easy-to-type API.
Interally, it calls C<< hash_map >> (which has a more verbose keyword
calling API). Thus, the above call to C results in this
call:
$tree->hash_map(hash => \%data, to_attr => 'sid', excluding => ['password']);
=head4 $elem->defmap($attr_name, \%hashref, $debug)
C was described above.
=head3 $elem->replace_content(@new_elem)
Replaces all of C<$elem>'s content with C<@new_elem>.
=head3 $elem->wrap_content($wrapper_element)
Wraps the existing content in the provided element. If the provided
element happens to be a non-element, a push_content is performed
instead.
=head3 $elem->set_child_content(@look_down, $content)
This method looks down $tree using the criteria specified in
@look_down using the the HTML::Element look_down() method.
After finding the node, it detaches the node's content and pushes
$content as the node's content.
=head3 $tree->content_handler(%id_content)
This is a convenience method. Because the look_down criteria will
often simply be:
id => 'fixme'
to find things like:
replace_content
You can call this method to shorten your typing a bit. You can simply
type
$elem->content_handler( fixme => 'new text' )
Instead of typing:
$elem->set_child_content(sid => 'fixme', 'new text')
ALSO NOTE: you can pass a hash whose keys are Cs and whose values
are the content you want there and it will perform the replacement on
each hash member:
my %id_content = (name => "Terrence Brannon",
email => 'tbrannon@in.com',
balance => 666,
content => $main_content);
$tree->content_handler(%id_content);
=head3 $tree->highlander($subtree_span_id, $conditionals, @conditionals_args)
This allows for "if-then-else" style processing. Highlander was a
movie in which only one would survive. Well, in terms of a tree when
looking at a structure that you want to process in C
style, only one child will survive. For example, given this HTML
template:
Hello, does your mother know you're
using her AOL account?
Sorry, you're not old enough to enter
(and too dumb to lie about your age)
Welcome
We only want one child of the C tag with id C to
remain based on the age of the person visiting the page.
So, let's setup a call that will prune the subtree as a function of
age:
sub process_page {
my $age = shift;
my $tree = HTML::TreeBuilder->new_from_file('t/html/highlander.html');
$tree->highlander
(age_dialog =>
[
under10 => sub { $_[0] < 10},
under18 => sub { $_[0] < 18},
welcome => sub { 1 }
],
$age
);
And there we have it. If the age is less than 10, then the node with
id C remains. For age less than 18, the node with id
C remains. Otherwise our "else" condition fires and the child
with id C remains.
=head3 $tree->passover(@id_of_element)
In some cases, you know exactly which element(s) should survive. In
this case, you can simply call C to remove it's (their)
siblings. For the HTML above, you could delete C and
C by simply calling:
$tree->passover('under18');
Because passover takes an array, you can specify several children to
preserve.
=head3 $tree->highlander2($tree, $conditionals, @conditionals_args)
Right around the same time that C came into being,
Seamstress began to tackle tougher and tougher processing problems. It
became clear that a more powerful highlander was needed... one that
not only snipped the tree of the nodes that should not survive, but
one that allows for post-processing of the survivor node. And one that
was more flexible with how to find the nodes to snip.
Thus (drum roll) C.
So let's look at our HTML which requires post-selection processing:
Hello, little AGE-year old,
does your mother know you're using her AOL account?
Sorry, you're only AGE
(and too dumb to lie about your age)
Welcome, isn't it good to be AGE years old?
In this case, a branch survives, but it has dummy data in it. We must
take the surviving segment of HTML and rewrite the age C with
the age. Here is how we use C to do so:
sub replace_age {
my $branch = shift;
my $age = shift;
$branch->look_down(id => 'age')->replace_content($age);
}
my $if_then = $tree->look_down(id => 'age_dialog');
$if_then->highlander2(
cond => [
under10 => [
sub { $_[0] < 10} ,
\&replace_age
],
under18 => [
sub { $_[0] < 18} ,
\&replace_age
],
welcome => [
sub { 1 },
\&replace_age
]
],
cond_arg => [ $age ]
);
We pass it the tree (C<$if_then>), an arrayref of conditions (C)
and an arrayref of arguments which are passed to the Cs and to
the replacement subs.
The C, C and C are id attributes in the
tree of the siblings of which only one will survive. However, should
you need to do more complex look-downs to find the survivor, then
supply an array ref instead of a simple scalar:
$if_then->highlander2(
cond => [
[class => 'r12'] => [
sub { $_[0] < 10} ,
\&replace_age
],
[class => 'z22'] => [
sub { $_[0] < 18} ,
\&replace_age
],
[class => 'w88'] => [
sub { 1 },
\&replace_age
]
],
cond_arg => [ $age ]
);
=head3 $tree->overwrite_attr($mutation_attr => $mutating_closures)
This method is designed for taking a tree and reworking a set of nodes
in a stereotyped fashion. For instance let's say you have 3 remote
image archives, but you don't want to put long URLs in your img src
tags for reasons of abstraction, re-use and brevity. So instead you do
this:
and then when the tree of HTML is being processed, you make this call:
my %closures = (
lnc => sub { my ($tree, $mute_node, $attr_value)= @_; "http://lnc.usc.edu$attr_value" },
playboy => sub { my ($tree, $mute_node, $attr_value)= @_; "http://playboy.com$attr_value" }
foobar => sub { my ($tree, $mute_node, $attr_value)= @_; "http://foobar.info$attr_value" }
)
$tree->overwrite_attr(fixup => \%closures) ;
and the tags come out modified like so:
=head3 $tree->mute_elem($mutation_attr => $mutating_closures, [ $post_hook ] )
This is a generalization of C. C
assumes the return value of the closure is supposed overwrite an
attribute value and does it for you. C is a more general
function which does nothing but hand the closure the element and let
it mutate it as it jolly well pleases :)
In fact, here is the implementation of C to give you a
taste of how C is used:
sub overwrite_action {
my ($mute_node, %X) = @_;
$mute_node->attr($X{local_attr}{name} => $X{local_attr}{value}{new});
}
sub HTML::Element::overwrite_attr {
my $tree = shift;
$tree->mute_elem(@_, \&overwrite_action);
}
=head2 Tree-Building Methods
=head3 Unrolling an array via a single sample element (
container)
This is best described by example. Given this HTML:
Here are the things I need from the store:
Sample item
We can unroll it like so:
my $li = $tree->look_down(class => 'store_items');
my @items = qw(bread butter vodka);
$tree->iter($li => @items);
To produce this:
Here are the things I need from the store:
bread
butter
vodka
Now, you might be wondering why the API call is:
$tree->iter($li => @items)
instead of:
$li->iter(@items)
and there is no good answer. The latter would be more concise and it
is what I should have done.
=head3 Unrolling an array via a single sample element and a callback (
container)
This is a more advanced version of the previous method. Instead of
cloning the sample element several times and calling
C on the clone with the array element, a custom
callback is called with the clone and array element.
Here is the example from before.
Here are the things I need from the store:
Sample item
Code:
sub cb {
my ($data, $li) = @_;
$li->replace_content($data);
}
my $li = $tree->look_down(class => 'store_items');
my @items = qw(bread butter vodka);
$li->itercb(\@items, \&cb);
Output is as before:
Here are the things I need from the store:
bread
butter
vodka
Here is a more complex example (unrolling a table). HTML:
First Name
Last Name
Option
First
Last
1
Code:
sub tr_cb {
my ($data, $tr) = @_;
$tr->look_down(class => 'first')->replace_content($data->{first});
$tr->look_down(class => 'last')->replace_content($data->{last});
$tr->look_down(class => 'option')->replace_content($data->{option});
}
my @data = (
{first => 'Foo', last => 'Bar', option => 2},
{first => 'Bar', last => 'Bar', option => 3},
{first => 'Baz', last => 'Bar', option => 4},
);
my $tr = $tree->find('table')->find('tbody')->find('tr');
$tr->itercb(\@data, \&tr_cb);
Produces:
First Name
Last Name
Option
Foo
Bar
2
Bar
Bar
3
Baz
Bar
4
=head3 Unrolling an array via n sample elements (
container)
C was fine for awhile, but some things (e.g. definition lists)
need a more general function to make them easy to do. Hence
C. This function will be explained by example of unrolling a
simple definition list.
So here's our mock-up HTML from the designer:
Artist
A person who draws blood.
Musician
A clone of Iggy Pop.
Poet
A relative of Edgar Allan Poe.
sample header
sample data
And we want to unroll our data set:
my @items = (
['the pros' => 'never have to worry about service again'],
['the cons' => 'upfront extra charge on purchase'],
['our choice' => 'go with the extended service plan']
);
Now, let's make this problem a bit harder to show off the power of
C. Let's assume that we want only the last
and it's
accompanying
(the one with "sample data") to be used as the
sample data for unrolling with our data set. Let's further assume that
we want them to remain in the final output.
So now, the API to C will be discussed and we will explain
how our goal of getting our data into HTML fits into the API.
=over 4
=item * wrapper_ld
This is how to look down and find the container of all the elements we
will be unrolling. The
tag is the container for the dt and dd
tags we will be unrolling.
If you pass an anonymous subroutine, then it is presumed that
execution of this subroutine will return the HTML::Element
representing the container tag. If you pass an array ref, then this
will be dereferenced and passed to C.
default value: C<< ['_tag' => 'dl'] >>
Based on the mock HTML above, this default is fine for finding our
container tag. So let's move on.
=item * wrapper_data
This is an array reference of data that we will be putting into the
container. You must supply this. C<@items> above is our
C.
=item * wrapper_proc
After we find the container via C, we may want to
pre-process some aspect of this tree. In our case the first two sets
of dt and dd need to be removed, leaving the last dt and dd. So, we
supply a C which will do this.
default: undef
=item * item_ld
This anonymous subroutine returns an array ref of Cs
that will be cloned and populated with item data (item data is a "row"
of C).
default: returns an arrayref consisting of the dt and dd element
inside the container.
=item * item_data
This is a subroutine that takes C and retrieves one
"row" to be "pasted" into the array ref of Cs found via
C. I hope that makes sense.
default: shifts C.
=item * item_proc
This is a subroutine that takes the C and the
Cs found via C and produces an arrayref of
Cs which will eventually be spliced into the container.
Note that this subroutine MUST return the new items. This is done So
that more items than were passed in can be returned. This is useful
when, for example, you must return 2 dts for an input data item. And
when would you do this? When a single term has multiple spellings for
instance.
default: expects C to be an arrayref of two elements and
C to be an arrayref of two Cs. It replaces
the content of the Cs with the C.
=item * splice
After building up an array of C<@item_elems>, the subroutine passed as
C will be given the parent container HTML::Element and the
C<@item_elems>. How the C<@item_elems> end up in the container is up
to this routine: it could put half of them in. It could unshift them
or whatever.
default: C<< $container->splice_content(0, 2, @item_elems) >> In other
words, kill the 2 sample elements with the newly generated @item_elems
=back
So now that we have documented the API, let's see the call we need:
$tree->iter2(
# default wrapper_ld ok.
wrapper_data => \@items,
wrapper_proc => sub {
my ($container) = @_;
# only keep the last 2 dts and dds
my @content_list = $container->content_list;
$container->splice_content(0, @content_list - 2);
},
# default item_ld is fine.
# default item_data is fine.
# default item_proc is fine.
splice => sub {
my ($container, @item_elems) = @_;
$container->unshift_content(@item_elems);
},
debug => 1,
);
=head3 Select Unrolling
The C method has this API:
$tree->unroll_select(
select_label => $id_label,
option_value => $closure, # how to get option value from data row
option_content => $closure, # how to get option content from data row
option_selected => $closure, # boolean to decide if SELECTED
data => $data # the data to be put into the SELECT
data_iter => $closure # the thing that will get a row of data
debug => $boolean,
append => $boolean, # remove the sample