| 1 | package HTML::StickyQuery::DoCoMoGUID; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | our $VERSION = '0.01'; |
|---|
| 6 | |
|---|
| 7 | use HTML::StickyQuery; |
|---|
| 8 | |
|---|
| 9 | sub new { |
|---|
| 10 | my $class = shift; |
|---|
| 11 | bless { |
|---|
| 12 | sticky => HTML::StickyQuery->new( regexp => qr/./ ), |
|---|
| 13 | }, $class; |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | sub sticky { |
|---|
| 17 | my($self, %args) = @_; |
|---|
| 18 | $args{param} = {}; |
|---|
| 19 | $args{param}->{guid} = 'ON'; |
|---|
| 20 | |
|---|
| 21 | local $self->{sticky}->{use_xhtml} = exists $args{xhtml} ? $args{xhtml} : 1; |
|---|
| 22 | |
|---|
| 23 | local *_start = *HTML::StickyQuery::start; |
|---|
| 24 | local *HTML::StickyQuery::start = *start; |
|---|
| 25 | $self->{sticky}->sticky( %args ); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | # sticky for FORM tag. original code is HTML::StickyQuery |
|---|
| 29 | sub start { |
|---|
| 30 | my($self, $tagname, $attr, $attrseq, $orig) = @_; |
|---|
| 31 | |
|---|
| 32 | if ($tagname ne 'form') { |
|---|
| 33 | # goto original code |
|---|
| 34 | goto &_start; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | unless(exists $attr->{action}) { |
|---|
| 38 | $self->{output} .= $orig; |
|---|
| 39 | return; |
|---|
| 40 | } |
|---|
| 41 | my $u = URI->new($attr->{action}); |
|---|
| 42 | |
|---|
| 43 | # skip absolute URI |
|---|
| 44 | if (!$self->{abs} && $u->scheme) { |
|---|
| 45 | $self->{output} .= $orig; |
|---|
| 46 | return; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | # when URI has other scheme (ie. mailto ftp ..) |
|---|
| 50 | if(defined($u->scheme) && $u->scheme !~ m/^https?/) { |
|---|
| 51 | $self->{output} .= $orig; |
|---|
| 52 | return; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | if (!$self->{regexp} || $u->path =~ m/$self->{regexp}/) { |
|---|
| 56 | # get method |
|---|
| 57 | unless (($attr->{method} || '') =~ /^post$/i) { |
|---|
| 58 | $self->{output} .= $orig; |
|---|
| 59 | while (my($key, $value) = each %{ $self->{param} }) { |
|---|
| 60 | $self->{output} .= sprintf '<input type="hidden" name="%s" value="%s"%s>', |
|---|
| 61 | $key, $value, ($self->{use_xhtml} ? ' /' : ''); |
|---|
| 62 | } |
|---|
| 63 | return; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | # post method |
|---|
| 67 | if ($self->{keep_original}) { |
|---|
| 68 | my %original; |
|---|
| 69 | my @original = $u->query_form; |
|---|
| 70 | while (my ($key, $val) = splice(@original, 0, 2)) { |
|---|
| 71 | if (exists $original{$key}) { |
|---|
| 72 | if (ref $original{$key} eq 'ARRAY') { |
|---|
| 73 | push @{ $original{$key} }, $val; |
|---|
| 74 | } else { |
|---|
| 75 | $original{$key} = [ $original{$key}, $val ]; |
|---|
| 76 | } |
|---|
| 77 | } else { |
|---|
| 78 | $original{$key} = $val; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | $u->query_form(%original, %{ $self->{param} }); |
|---|
| 82 | } else { |
|---|
| 83 | $u->query_form(%{$self->{param}}); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | $self->{output} .= "<$tagname"; |
|---|
| 88 | |
|---|
| 89 | # save attr order. |
|---|
| 90 | for my $key (@{ $attrseq }) { |
|---|
| 91 | if ($key eq 'action'){ |
|---|
| 92 | $self->{output} .= sprintf ' action="%s"', $self->escapeHTML($u->as_string); |
|---|
| 93 | } elsif ($attr->{$key} eq '__BOOLEAN__') { |
|---|
| 94 | $self->{output} .= " $key"; |
|---|
| 95 | } else { |
|---|
| 96 | $self->{output} .= sprintf qq{ $key="%s"}, $self->escapeHTML($attr->{$key}); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | $self->{output} .= '>'; |
|---|
| 100 | return; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | $self->{output} .= $orig; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | 1; |
|---|
| 107 | __END__ |
|---|
| 108 | |
|---|
| 109 | =encoding utf8 |
|---|
| 110 | |
|---|
| 111 | =head1 NAME |
|---|
| 112 | |
|---|
| 113 | HTML::StickyQuery::DoCoMoGUID - add guid query for DoCoMo imode |
|---|
| 114 | |
|---|
| 115 | =head1 SYNOPSIS |
|---|
| 116 | |
|---|
| 117 | use HTML::StickyQuery::DoCoMoGUID; |
|---|
| 118 | |
|---|
| 119 | my $guid = HTML::StickyQuery::DoCoMoGUID->new; |
|---|
| 120 | print $guid->sticky( scalarref => \$html ); |
|---|
| 121 | |
|---|
| 122 | =head1 DESCRIPTION |
|---|
| 123 | |
|---|
| 124 | 主に HTML::StickyQuery を使って DoCoMo用の guid=ON をつけるフィルタリングをするよ。 |
|---|
| 125 | |
|---|
| 126 | =head1 AUTHOR |
|---|
| 127 | |
|---|
| 128 | Kazuhiro Osawa E<lt>ko@yappo.ne.jpE<gt> |
|---|
| 129 | |
|---|
| 130 | =head1 SEE ALSO |
|---|
| 131 | |
|---|
| 132 | L<HTML::StickyQuery> |
|---|
| 133 | |
|---|
| 134 | =head1 REPOSITORY |
|---|
| 135 | |
|---|
| 136 | svn co http://svn.coderepos.org/share/lang/perl/HTML-StickyQuery-DoCoMoGUID/trunk HTML-StickyQuery-DoCoMoGUID |
|---|
| 137 | |
|---|
| 138 | HTML::StickyQuery::DoCoMoGUID is Subversion repository is hosted at L<http://coderepos.org/share/>. |
|---|
| 139 | patches and collaborators are welcome. |
|---|
| 140 | |
|---|
| 141 | =head1 LICENSE |
|---|
| 142 | |
|---|
| 143 | This library is free software; you can redistribute it and/or modify |
|---|
| 144 | it under the same terms as Perl itself. |
|---|
| 145 | |
|---|
| 146 | =cut |
|---|