| 1 | =head1 NAME | ||||||||||||
| 2 | |||||||||||||
| 3 | HTML::Tested::JavaScript::Serializer - Serialize HTML::Tested to/from JavaScript. | ||||||||||||
| 4 | |||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||
| 6 | |||||||||||||
| 7 | package MyClass; | ||||||||||||
| 8 | use base 'HTML::Tested'; | ||||||||||||
| 9 | use HTML::Tested::JavaScript::Serializer; | ||||||||||||
| 10 | use HTML::Tested::JavaScript::Serializer::Value; | ||||||||||||
| 11 | |||||||||||||
| 12 | use constant HTJS => "HTML::Tested::JavaScript::Serializer"; | ||||||||||||
| 13 | |||||||||||||
| 14 | # add JS Value named "val". | ||||||||||||
| 15 | __PACKAGE__->ht_add_widget(HTJS . "::Value", "val"); | ||||||||||||
| 16 | |||||||||||||
| 17 | # add serializer "ser" and bind "val" to it. | ||||||||||||
| 18 | __PACKAGE__->ht_add_widget(HTJS, "ser", "val"); | ||||||||||||
| 19 | |||||||||||||
| 20 | # now MyClass->ht_render produces ser javascript variable | ||||||||||||
| 21 | |||||||||||||
| 22 | # in your HTML file serialize back | ||||||||||||
| 23 | ht_serializer_submit(ser, url, callback); | ||||||||||||
| 24 | |||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||
| 26 | |||||||||||||
| 27 | This module serializes data to/from JavaScript data structures. | ||||||||||||
| 28 | It also produces script tags to include necessary JavaScript files. | ||||||||||||
| 29 | |||||||||||||
| 30 | =head1 AUTHOR | ||||||||||||
| 31 | |||||||||||||
| 32 | Boris Sukholitko | ||||||||||||
| 33 | boriss@gmail.com | ||||||||||||
| 34 | |||||||||||||
| 35 | |||||||||||||
| 36 | =head1 COPYRIGHT | ||||||||||||
| 37 | |||||||||||||
| 38 | This program is free software; you can redistribute | ||||||||||||
| 39 | it and/or modify it under the same terms as Perl itself. | ||||||||||||
| 40 | |||||||||||||
| 41 | The full text of the license can be found in the | ||||||||||||
| 42 | LICENSE file included with this module. | ||||||||||||
| 43 | =head1 SEE ALSO | ||||||||||||
| 44 | |||||||||||||
| 45 | HTML::Tested, HTML::Tested::JavaScript::Serializer::Value, | ||||||||||||
| 46 | HTML::Tested::JavaScript::Serializer::List. | ||||||||||||
| 47 | |||||||||||||
| 48 | Tests for HTML::Tested::JavaScript. | ||||||||||||
| 49 | |||||||||||||
| 50 | =cut | ||||||||||||
| 51 | |||||||||||||
| 52 | 8 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 53 | 8 | ▶ | use warnings FATAL => 'all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 54 | |||||||||||||
| 55 | package HTML::Tested::JavaScript::Serializer; | ||||||||||||
| 56 | 8 | ▶ | use base 'HTML::Tested::Value'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 57 | 8 | ▶ | use HTML::Tested::JavaScript; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 58 | 8 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 59 | |||||||||||||
| 60 | 6 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 61 | 6 | my ($class, $parent, $name, @jses) = @_; | |||||||||||
| 62 | 6 | my $self = bless({ name => $name, _jses => \@jses | |||||||||||
| 63 | , _options => {} }, $class); | ||||||||||||
| 64 | 6 | my @unknowns = grep { !$parent->ht_find_widget($_) } @jses; | |||||||||||
| 65 | 6 | ▶ | confess "$class: Unable to find js controls: " . join(', ', @unknowns) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | if @unknowns; | ||||||||||||
| 67 | 5 | return $self; | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | 33 | ▶ | sub Wrap { | ||||||||||
|
Subroutine
Wrap: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 71 | 33 | my ($n, $v) = @_; | |||||||||||
| 72 | 33 | $v =~ s#/#\\/#gs; # is needed for </script> | |||||||||||
| 73 | 33 | return "<script>//<![CDATA[\nvar $n = $v;//]]>\n</script>"; | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | 21 | ▶ | sub render { | ||||||||||
|
Subroutine
render: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 77 | 21 | my ($self, $caller, $stash, $id) = @_; | |||||||||||
| 78 | 21 | my $n = $self->name; | |||||||||||
| 79 | 21 | ▶ | my $res = $caller->ht_get_widget_option($n, "no_script") ? "" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | : HTML::Tested::JavaScript::Script_Include(); | ||||||||||||
| 81 | 60 | $res .= Wrap($n, "{\n\t" . join(",\n\t", grep { $_ } map { | |||||||||||
| 82 | 60 | my $r = $stash->{$_}; | |||||||||||
| 83 | 60 | ▶ | ref($r) ? $stash->{$_ . "_js"} : $r | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | 21 | } @{ $self->{_jses} }) | |||||||||||
| 85 | . "\n}"); | ||||||||||||
| 86 | 21 | $stash->{ $n } = $res; | |||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | 1 | ▶ | sub validate { return (); } | ||||||||||
|
Subroutine
validate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 90 | |||||||||||||
| 91 | 12 | ▶ | sub Extract_Text { | ||||||||||
|
Subroutine
Extract_Text: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 92 | 12 | my ($n, $str) = @_; | |||||||||||
| 93 | 12 | my ($res) = ($str =~ m#<script>//<!\[CDATA\[\nvar $n = (.*)#s); | |||||||||||
| 94 | 12 | ▶ | ($res =~ s#;//\]\]>\n</script>.*##s) if $res; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 12 | return $res; | |||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | 4 | ▶ | sub Extract_JSON { | ||||||||||
|
Subroutine
Extract_JSON: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 99 | 4 | my ($n, $str) = @_; | |||||||||||
| 100 | 4 | ▶ | my $et = Extract_Text($n, $str) // return; | ||||||||||
|
Condition100%2/2
Extract_Text($n, $str) // (return)
MC/DC100%1/1
Extract_Text($n, $str) // (return)
Extract_Text($n, $str)
Truth table100%2/2
A
A= Extract_Text($n, $str)
| |||||||||||||
| 101 | 3 | return JSON::XS->new->allow_nonref->decode($et); | |||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | 8 | 1; | |||||||||||