| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/ElementDataMap.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/12/12 | ||
| 7 | ## Modified 2022/09/18 | ||
| 8 | ## All rights reserved | ||
| 9 | ## | ||
| 10 | ## | ||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||
| 12 | ## under the same terms as Perl itself. | ||
| 13 | ##---------------------------------------------------------------------------- | ||
| 14 | package HTML::Object::ElementDataMap; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( Module::Generic ); | ||
| 21 | use vars qw( $VERSION ); | ||
| 22 | our $VERSION = 'v0.2.0'; | ||
| 23 | }; | ||
| 24 | |||
| 25 | use strict; | ||
| 26 | use warnings; | ||
| 27 | |||
| 28 | sub init | ||
| 29 | { | ||
| 30 | my $self = shift( @_ ); | ||
| 31 | my $elem = shift( @_ ) || return( $self->error( "No HTML::Object::Element was provided." ) ); | ||
| 32 | return( $self->error( "Element object provided is not a HTML::Object::Element" ) ) if( !$self->_is_a( $elem => 'HTML::Object::Element' ) ); | ||
| 33 | $self->{element} = $elem; | ||
| 34 | $self->{_init_strict_use_sub} = 1; | ||
| 35 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 36 | return( $self ); | ||
| 37 | } | ||
| 38 | |||
| 39 | AUTOLOAD | ||
| 40 | { | ||
| 41 | my( $name ) = our $AUTOLOAD =~ /([^:]+)$/; | ||
| 42 | my $self = shift( @_ ); | ||
| 43 | my $elem = $self->{element} || die( "No element object set!\n" ); | ||
| 44 | ( my $copy = $name ) =~ s/^([A-Z])/\L$1\E/; | ||
| 45 | my $att = 'data-' . join( '-', map( lc( $_ ), split( /(?=[A-Z])/, $copy ) ) ); | ||
| 46 | if( @_ ) | ||
| 47 | { | ||
| 48 | my $val = shift( @_ ); | ||
| 49 | if( !$elem->attributes->has( $att ) ) | ||
| 50 | { | ||
| 51 | $elem->setAttribute( $att => $val ); | ||
| 52 | } | ||
| 53 | else | ||
| 54 | { | ||
| 55 | $elem->attributes->set( $att => $val ); | ||
| 56 | } | ||
| 57 | $elem->reset(1); | ||
| 58 | return( $self ); | ||
| 59 | } | ||
| 60 | else | ||
| 61 | { | ||
| 62 | return( $elem->attributes->get( $att ) ); | ||
| 63 | } | ||
| 64 | }; | ||
| 65 | |||
| 66 | |||
| 67 | 1; | ||
| 68 | # NOTE: POD | ||
| 69 | __END__ | ||
| 70 | |||
| 71 | =encoding utf-8 | ||
| 72 | |||
| 73 | =head1 NAME | ||
| 74 | |||
| 75 | HTML::Object::ElementDataMap - HTML Object Element Data Map Class | ||
| 76 | |||
| 77 | =head1 SYNOPSIS | ||
| 78 | |||
| 79 | use HTML::Object::ElementDataMap; | ||
| 80 | my $map = HTML::Object::ElementDataMap->new( $html_element_object ) || | ||
| 81 | die( HTML::Object::ElementDataMap->error, "\n" ); | ||
| 82 | $map->dateOfBirth( '1989-12-01' ); | ||
| 83 | # Related html element would now have an attribute data-date-of-birth set to 1989-12-01 | ||
| 84 | |||
| 85 | =head1 VERSION | ||
| 86 | |||
| 87 | v0.2.0 | ||
| 88 | |||
| 89 | =head1 DESCRIPTION | ||
| 90 | |||
| 91 | This module implements a L<data map|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset> for L<HTML element|HTML::Object::Element> objects. | ||
| 92 | |||
| 93 | It provides read/write access to custom data attributes (data-*) on L<elements|HTML::Object::Element>. It provides a map of strings with an entry for each data-* attribute. | ||
| 94 | |||
| 95 | Those class objects are instantiated from L<HTML::Object::Element/dataset> | ||
| 96 | |||
| 97 | =head1 Name conversion | ||
| 98 | |||
| 99 | =head2 dash-style to camelCase conversion | ||
| 100 | |||
| 101 | A custom data attribute name is transformed to a key for the element data entry by the following: | ||
| 102 | |||
| 103 | =over 4 | ||
| 104 | |||
| 105 | =item 1. Lowercase all ASCII capital letters (A to Z); | ||
| 106 | |||
| 107 | =item 2. Remove the prefix data- (including the dash); | ||
| 108 | |||
| 109 | =item 3. For any dash (U+002D) followed by an ASCII lowercase letter a to z, remove the dash and uppercase the letter; | ||
| 110 | |||
| 111 | =item 4. Other characters (including other dashes) are left unchanged. | ||
| 112 | |||
| 113 | =back | ||
| 114 | |||
| 115 | =head2 camelCase to dash-style conversion | ||
| 116 | |||
| 117 | The opposite transformation, which maps a key to an attribute name, uses the following: | ||
| 118 | |||
| 119 | =over 4 | ||
| 120 | |||
| 121 | =item 1. Restriction: Before transformation, a dash must not be immediately followed by an ASCII lowercase letter a to z; | ||
| 122 | |||
| 123 | =item 2. Add the data- prefix; | ||
| 124 | |||
| 125 | =item 3. Add a dash before any ASCII uppercase letter A to Z, then lowercase the letter; | ||
| 126 | |||
| 127 | =item 4. Other characters are left unchanged. | ||
| 128 | |||
| 129 | =back | ||
| 130 | |||
| 131 | For example, a data-abc-def attribute corresponds to dataset.abcDef. | ||
| 132 | |||
| 133 | =head1 Accessing values | ||
| 134 | |||
| 135 | =over 4 | ||
| 136 | |||
| 137 | =item * Attributes can be set and read by the camelCase name/key as an object property of the dataset: C<$element->dataset->keyname> | ||
| 138 | |||
| 139 | =item * Attributes can also be set and read using variable: C<$element->dataset->$keyname> | ||
| 140 | |||
| 141 | =item * You can check if a given attribute exists like so: C<$element->attributes->exists( 'data-keyname' )> | ||
| 142 | |||
| 143 | =back | ||
| 144 | |||
| 145 | =head1 Setting values | ||
| 146 | |||
| 147 | =over 4 | ||
| 148 | |||
| 149 | =item * When the attribute is set, its value is always stored as is, which means you can set reference and access them later. | ||
| 150 | |||
| 151 | =item * To remove an attribute, you can use the L<HTML::Object::Element/removeAttribute> method: C<$element->removeAttribute( 'data-keyname' )> | ||
| 152 | |||
| 153 | =back | ||
| 154 | |||
| 155 | =head1 AUTHOR | ||
| 156 | |||
| 157 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 158 | |||
| 159 | =head1 SEE ALSO | ||
| 160 | |||
| 161 | L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset> | ||
| 162 | |||
| 163 | =head1 COPYRIGHT & LICENSE | ||
| 164 | |||
| 165 | Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 166 | |||
| 167 | All rights reserved | ||
| 168 | |||
| 169 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 170 | |||
| 171 | =cut |