| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | package Mojo::DOM::Role::AttrAccessors; | ||
| 4 | |||
| 5 | use Mojo::Base -role; | ||
| 6 | |||
| 7 | sub AUTOLOAD { | ||
| 8 | my ($self, $val) = @_; | ||
| 9 | our $AUTOLOAD; | ||
| 10 | my $attr = $AUTOLOAD =~ s/.*:://r; # strip package name | ||
| 11 | |||
| 12 | return if $attr eq 'DESTROY'; | ||
| 13 | return $self->attr($attr) unless defined $val; | ||
| 14 | return $self->attr($attr => $val); | ||
| 15 | } | ||
| 16 | |||
| 17 | sub data { | ||
| 18 | my ($self, $key, $val) = @_; | ||
| 19 | |||
| 20 | if (defined $val) { | ||
| 21 | my $stored = ref $val ? Mojo::JSON::encode_json($val) : $val; | ||
| 22 | return $self->attr("data-$key" => $stored); | ||
| 23 | } | ||
| 24 | |||
| 25 | my $raw = $self->attr("data-$key"); | ||
| 26 | return undef unless defined $raw; | ||
| 27 | |||
| 28 | # if (my $decoded = eval { Mojo::JSON::decode_json($raw) }) { | ||
| 29 | # return ref $decoded ? $decoded : $raw; | ||
| 30 | # } | ||
| 31 | my $decoded = eval { Mojo::JSON::decode_json($raw) }; | ||
| 32 | if (!$@) { | ||
| 33 | return ref $decoded ? $decoded : $raw; | ||
| 34 | } | ||
| 35 | return $raw; | ||
| 36 | } | ||
| 37 | |||
| 38 | 1; | ||
| 39 | |||
| 40 | |||
| 41 | =encoding utf8 | ||
| 42 | |||
| 43 | =head1 NAME | ||
| 44 | |||
| 45 | Mojo::DOM::Role::AttrAccessors - Attribute accessors for Mojo::DOM | ||
| 46 | |||
| 47 | =head1 SYNOPSIS | ||
| 48 | |||
| 49 | use Mojo::DOM; | ||
| 50 | |||
| 51 | my $dom = Mojo::DOM->with_roles('+AttrAccessors') | ||
| 52 | ->new('<a href="https://example.com">Example</a>') | ||
| 53 | ->at('a'); | ||
| 54 | |||
| 55 | # Read an attribute | ||
| 56 | my $href = $dom->href; | ||
| 57 | |||
| 58 | # Write an attribute | ||
| 59 | $dom->href('https://mojolicious.org'); | ||
| 60 | |||
| 61 | # Chain | ||
| 62 | $dom->href('https://mojolicious.org')->id('main-link'); | ||
| 63 | |||
| 64 | # Structured data via data-* attributes | ||
| 65 | $dom->data('config', { foo => 1 }); | ||
| 66 | my $config = $dom->data('config'); # returns hashref | ||
| 67 | |||
| 68 | # Plain strings pass through untouched | ||
| 69 | $dom->data('label', 'hello'); | ||
| 70 | my $label = $dom->data('label'); # returns "hello" | ||
| 71 | |||
| 72 | =head1 DESCRIPTION | ||
| 73 | |||
| 74 | L<Mojo::DOM::Role::AttrAccessors> is a L<Mojo::Role> that adds autoloaded | ||
| 75 | attribute accessors to L<Mojo::DOM> nodes. Any HTML attribute can be read or | ||
| 76 | written as a method call directly on the node, without going through | ||
| 77 | L<Mojo::DOM/attr>. | ||
| 78 | |||
| 79 | The role also provides a L</data> method for ergonomic access to C<data-*> | ||
| 80 | attributes, with automatic JSON serialisation for reference values. | ||
| 81 | |||
| 82 | =head1 METHODS | ||
| 83 | |||
| 84 | L<Mojo::DOM::Role::AttrAccessors> implements the following methods. | ||
| 85 | |||
| 86 | =head2 data | ||
| 87 | |||
| 88 | my $val = $dom->data('key'); | ||
| 89 | $dom->data('key', 'plain string'); | ||
| 90 | $dom->data('key', { structured => 'data' }); | ||
| 91 | $dom->data('key', [1, 2, 3]); | ||
| 92 | |||
| 93 | Read or write a C<data-*> attribute on the node. The following encoding | ||
| 94 | rules apply: | ||
| 95 | |||
| 96 | =over 4 | ||
| 97 | |||
| 98 | =item Inbound | ||
| 99 | |||
| 100 | If the value is a reference it is JSON-encoded before storing. Plain scalars | ||
| 101 | are stored as-is. | ||
| 102 | |||
| 103 | =item Outbound | ||
| 104 | |||
| 105 | If the stored value is valid JSON and decodes to a reference, the reference | ||
| 106 | is returned. Otherwise the raw string is returned. This means plain strings | ||
| 107 | and bare JSON scalars (C<42>, C<true>) are always returned as strings. | ||
| 108 | |||
| 109 | =head3 CAVEAT | ||
| 110 | |||
| 111 | JSON boolean values (C<true>, C<false>) are returned as | ||
| 112 | L<Mojo::JSON> boolean objects, which evaluate correctly in boolean | ||
| 113 | context. Note that storing C<\1> or C<\0> will round-trip as these | ||
| 114 | boolean objects rather than the original scalar references. | ||
| 115 | |||
| 116 | =back | ||
| 117 | |||
| 118 | =head2 AUTOLOAD | ||
| 119 | |||
| 120 | my $val = $dom->href; | ||
| 121 | $dom->href('https://mojolicious.org'); | ||
| 122 | |||
| 123 | Any method call not defined by this role or L<Mojo::DOM> is intercepted and | ||
| 124 | treated as an attribute accessor. The method name is used directly as the | ||
| 125 | attribute name. | ||
| 126 | |||
| 127 | Note that HTML attributes containing hyphens (such as C<data-foo> or | ||
| 128 | C<aria-label>) are not accessible via AUTOLOAD since hyphens are not valid | ||
| 129 | in Perl method names. Use L<Mojo::DOM/attr> directly for those: | ||
| 130 | |||
| 131 | $dom->attr('aria-label'); | ||
| 132 | $dom->attr('data-foo'); | ||
| 133 | |||
| 134 | Or use L</data> for C<data-*> attributes specifically. | ||
| 135 | |||
| 136 | =head1 CAVEATS | ||
| 137 | |||
| 138 | If a future version of L<Mojo::DOM> introduces a method whose name clashes | ||
| 139 | with an HTML attribute you are accessing via AUTOLOAD, the real method will | ||
| 140 | take precedence and AUTOLOAD will not fire. Use L<Mojo::DOM/attr> directly | ||
| 141 | in that case. | ||
| 142 | |||
| 143 | =head1 SEE ALSO | ||
| 144 | |||
| 145 | L<Mojo::DOM>, L<Mojo::Role> | ||
| 146 | |||
| 147 | =head1 AUTHOR | ||
| 148 | |||
| 149 | Simone Cesano <scesano@cpan.org> | ||
| 150 | |||
| 151 | =head1 COPYRIGHT AND LICENSE | ||
| 152 | |||
| 153 | Copyright (C) Simone Cesano. | ||
| 154 | |||
| 155 | This library is free software; you can redistribute it and/or modify | ||
| 156 | it under the same terms as Perl itself. | ||
| 157 | |||
| 158 | =cut |