The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

HTML::Object::ElementDataMap - HTML Object Element Data Map Class

SYNOPSIS

    use HTML::Object::ElementDataMap;
    my $map = HTML::Object::ElementDataMap->new( $html_element_object ) || 
        die( HTML::Object::ElementDataMap->error, "\n" );
    $map->dateOfBirth( '1989-12-01' );
    # Related html element would now have an attribute data-date-of-birth set to 1989-12-01

VERSION

    v0.2.0

DESCRIPTION

This module implements a data map for HTML element objects.

It provides read/write access to custom data attributes (data-*) on elements. It provides a map of strings with an entry for each data-* attribute.

Those class objects are instantiated from "dataset" in HTML::Object::Element

Name conversion

dash-style to camelCase conversion

A custom data attribute name is transformed to a key for the element data entry by the following:

1. Lowercase all ASCII capital letters (A to Z);
2. Remove the prefix data- (including the dash);
3. For any dash (U+002D) followed by an ASCII lowercase letter a to z, remove the dash and uppercase the letter;
4. Other characters (including other dashes) are left unchanged.

camelCase to dash-style conversion

The opposite transformation, which maps a key to an attribute name, uses the following:

1. Restriction: Before transformation, a dash must not be immediately followed by an ASCII lowercase letter a to z;
2. Add the data- prefix;
3. Add a dash before any ASCII uppercase letter A to Z, then lowercase the letter;
4. Other characters are left unchanged.

For example, a data-abc-def attribute corresponds to dataset.abcDef.

Accessing values

  • Attributes can be set and read by the camelCase name/key as an object property of the dataset: $element-dataset->keyname>

  • Attributes can also be set and read using variable: $element-dataset->$keyname>

  • You can check if a given attribute exists like so: $element-attributes->exists( 'data-keyname' )>

Setting values

  • When the attribute is set, its value is always stored as is, which means you can set reference and access them later.

  • To remove an attribute, you can use the "removeAttribute" in HTML::Object::Element method: $element-removeAttribute( 'data-keyname' )>

AUTHOR

Jacques Deguest <jack@deguest.jp>

SEE ALSO

Mozilla documentation

COPYRIGHT & LICENSE

Copyright(c) 2021 DEGUEST Pte. Ltd.

All rights reserved

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.