1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
=head1 NAME
Weasel::WidgetHandlers - Mapping elements to widget handlers
=head1 VERSION
version 0.32
=head1 SYNOPSIS
use Weasel::WidgetHandlers qw( register_widget_handler );
register_widget_handler(
'Weasel::Widgets::HTML::Radio', # Perl class handler
'HTML', # Widget group
tag_name => 'input',
attributes => {
type => 'radio',
});
register_widget_handler(
'Weasel::Widgets::Dojo::FilteringSelect',
'Dojo',
tag_name => 'span',
classes => ['dijitFilteringSelect'],
attributes => {
role => 'presentation',
...
});
=cut
=head1 DESCRIPTION
Widget handlers map HTML elements to Perl class instances based on the
HTML tag and its attributes. The Perl class can be used to encapsulate
interaction with the HTML element and its children, abstracting HTML DOM
interactions into functional behaviours.
A widget may itself be composed of one or more child-widget. Eg, a form
could be composed of several input fields and a button, all mapped to
widgets to handle the interactions with the specific element type.
Classes can be developed and handlers registered for widgets which
perform the same behaviours as basic HTML widgets, like the C<select>
tag; eg., a Dojo Toolkit, Vue or React widgets could be mapped to
specific classes which encapsulate the DOM interactions required to make
these widgets transparent to the Perl code which is interacting with them.
An example of a widget mimicing to be a C<select> tag is
L<Weasel::Widgets::Dojo::Select>.
=cut
=head1 DEPENDENCIES
=cut
package Weasel::WidgetHandlers 0.32;
use strict;
use warnings;
use base 'Exporter';
use Module::Runtime qw(use_module);
use List::Util qw(max);
our @EXPORT_OK = qw| register_widget_handler best_match_handler_class |;
=head1 SUBROUTINES/METHODS
=over
=item register_widget_handler($handler_class_name, $group_name, %conditions)
Registers C<$handler_class_name> to be the instantiated widget returned
for an element matching C<%conditions> into C<$group_name>.
C<Weasel::Session> can select a subset of widgets to be applicable to that
session by adding a subset of available groups to that session.
=cut
# Stores handlers as arrays per group
my %widget_handlers;
sub register_widget_handler {
my ($class, $group, %conditions) = @_;
# make sure we can use the module by pre-loading it
use_module $class;
return push @{$widget_handlers{$group}}, {
class => $class,
conditions => \%conditions,
};
}
=item best_match_handler_class($driver, $_id, $groups)
Returns the best matching handler's class name, within the groups
listed in the arrayref C<$groups>, or C<undef> in case of no match.
When C<$groups> is undef, all registered handlers will be searched.
When multiple handlers are considered "best match", the one last added
to the group last mentioned in C<$groups> is selected.
=cut
sub _cached_elem_att {
my ($cache, $driver, $_id, $att) = @_;
return (exists $cache->{$att})
? $cache->{$att}
: ($cache->{$att} = $driver->get_attribute($_id, $att));
}
sub _att_eq {
my ($att1, $att2) = @_;
return ($att1 // '') eq ($att2 // '');
}
sub best_match_handler_class {
my ($driver, $_id, $groups) = @_;
$groups //= [ keys %widget_handlers ]; # undef --> unrestricted
my @matches;
my $elem_att_cache = {};
my $elem_classes;
my $tag = $driver->tag_name($_id);
for my $group (@{$groups}) {
my $handlers = $widget_handlers{$group};
HANDLER:
for my $handler (@{$handlers}) {
my $conditions = $handler->{conditions};
next unless $tag eq $conditions->{tag_name};
my $match_count = 1;
if (exists $conditions->{classes}) {
%{$elem_classes} =
map { $_ => 1 }
split /\s+/x, ($driver->get_attribute($_id, 'class')
// '')
unless defined $elem_classes;
for my $class (@{$conditions->{classes}}) {
next HANDLER
unless exists $elem_classes->{$class};
$match_count++;
}
}
for my $att (keys %{$conditions->{attributes}}) {
next HANDLER
unless _att_eq(
$conditions->{attributes}->{$att},
_cached_elem_att(
$elem_att_cache, $driver, $_id, $att));
$match_count++;
}
push @matches, {
count => $match_count,
class => $handler->{class},
};
}
}
my $max_count = max map { $_->{count} } @matches;
@matches = grep { $_->{count} == $max_count } @matches;
warn "multiple matching handlers for element\n"
if scalar(@matches) > 1;
my $best_match = pop @matches;
return $best_match ? $best_match->{class} : undef;
}
=back
=cut
=head1 AUTHOR
Erik Huelsmann
=head1 CONTRIBUTORS
Erik Huelsmann
Yves Lavoie
=head1 MAINTAINERS
Erik Huelsmann
=head1 BUGS AND LIMITATIONS
Bugs can be filed in the GitHub issue tracker for the Weasel project:
https://github.com/perl-weasel/weasel/issues
=head1 SOURCE
The source code repository for Weasel is at
https://github.com/perl-weasel/weasel
=head1 SUPPORT
Community support is available through
L<perl-weasel@googlegroups.com|mailto:perl-weasel@googlegroups.com>.
=head1 LICENSE AND COPYRIGHT
(C) 2016-2023 Erik Huelsmann
Licensed under the same terms as Perl.
=cut
1;
|