| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/Literal.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/04/22 | ||
| 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::Literal; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( Module::Generic::Scalar ); | ||
| 21 | use vars qw( $VERSION ); | ||
| 22 | our $VERSION = 'v0.2.0'; | ||
| 23 | }; | ||
| 24 | |||
| 25 | use strict; | ||
| 26 | use warnings; | ||
| 27 | |||
| 28 | sub as_number { return( shift->to_number( @_ ) ); } | ||
| 29 | |||
| 30 | sub as_string | ||
| 31 | { | ||
| 32 | my $self = shift( @_ ); | ||
| 33 | my $txt = $self->SUPER::as_string; | ||
| 34 | # $txt =~ s/'/'/g; | ||
| 35 | $txt =~ s/(?<!\\)\'/\\\'/g; | ||
| 36 | return( "'$txt'" ); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub as_xml | ||
| 40 | { | ||
| 41 | my $self = shift( @_ ); | ||
| 42 | my $txt = $self->SUPER::as_string; | ||
| 43 | return( "<Literal>$txt</Literal>\n" ); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub evaluate { return( shift( @_ ) ); } | ||
| 47 | |||
| 48 | sub getAttributes { return( shift->error( 'Cannot get attributes of a literal' ) ); } | ||
| 49 | |||
| 50 | sub getChildNodes { return( shift->error( 'Cannot get child nodes of a literal' ) ); } | ||
| 51 | |||
| 52 | sub getParentNode { return( shift->error( 'Cannot get parent node of a literal' ) ); } | ||
| 53 | |||
| 54 | sub string_value { return( shift->value ); } | ||
| 55 | |||
| 56 | # sub to_boolean { return( shift->as_boolean ); } | ||
| 57 | sub to_boolean | ||
| 58 | { | ||
| 59 | require HTML::Object::Boolean; | ||
| 60 | return( shift->as_string ? HTML::Object::Boolean->True : HTML::Object::Boolean->False ); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub to_literal { return( shift( @_ ) ); } | ||
| 64 | |||
| 65 | # sub to_number { return( shift->as_number ); } | ||
| 66 | sub to_number | ||
| 67 | { | ||
| 68 | require HTML::Object::Number; | ||
| 69 | return( HTML::Object::Number->new( shift->value ) ); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub value { return( shift->SUPER::as_string ); } | ||
| 73 | |||
| 74 | sub value_as_number { return( shift->as_number ); } | ||
| 75 | |||
| 76 | 1; | ||
| 77 | # NOTE: POD | ||
| 78 | __END__ | ||
| 79 | |||
| 80 | =encoding utf-8 | ||
| 81 | |||
| 82 | =head1 NAME | ||
| 83 | |||
| 84 | HTML::Object::Literal - Simple string values | ||
| 85 | |||
| 86 | =head1 SYNOPSIS | ||
| 87 | |||
| 88 | use HTML::Object::Literal; | ||
| 89 | my $this = HTML::Object::Literal->new || die( HTML::Object::Literal->error, "\n" ); | ||
| 90 | |||
| 91 | =head1 VERSION | ||
| 92 | |||
| 93 | v0.2.0 | ||
| 94 | |||
| 95 | =head1 DESCRIPTION | ||
| 96 | |||
| 97 | In XPath terms a Literal is what we know as a string. | ||
| 98 | |||
| 99 | This module inherits from L<Module::Generic::Scalar> | ||
| 100 | |||
| 101 | =head1 METHODS | ||
| 102 | |||
| 103 | =head2 new | ||
| 104 | |||
| 105 | Provided with a C<string> and this will create a new Literal object with the value in C<string>. Note that " and ' will be converted to " and ' respectively. That is not part of the XPath specification, but I consider it useful. Note though that you have to go to extraordinary lengths in an XML template file (be it XSLT or whatever) to make use of this: | ||
| 106 | |||
| 107 | <xsl:value-of select=""I am feeling &quot;sad&quot;""/> | ||
| 108 | |||
| 109 | Which produces a Literal of: | ||
| 110 | |||
| 111 | I am feeling "sad" | ||
| 112 | |||
| 113 | =head2 as_string | ||
| 114 | |||
| 115 | Returns a string representation of this literal. | ||
| 116 | |||
| 117 | =head2 as_xml | ||
| 118 | |||
| 119 | Returns a string representation as xml. | ||
| 120 | |||
| 121 | =head2 evaluate | ||
| 122 | |||
| 123 | Does nothing. Returns the current object. | ||
| 124 | |||
| 125 | =head2 getAttributes | ||
| 126 | |||
| 127 | If called, this would return an L<error|Module::Generic/error> | ||
| 128 | |||
| 129 | =head2 getChildNodes | ||
| 130 | |||
| 131 | If called, this would return an L<error|Module::Generic/error> | ||
| 132 | |||
| 133 | =head2 getParentNode | ||
| 134 | |||
| 135 | If called, this would return an L<error|Module::Generic/error> | ||
| 136 | |||
| 137 | =head2 string_value | ||
| 138 | |||
| 139 | Returns the current value as-is. | ||
| 140 | |||
| 141 | =head2 to_boolean | ||
| 142 | |||
| 143 | Returns the current value as a L<boolean|Module::Generic::Boolean> | ||
| 144 | |||
| 145 | =head2 to_literal | ||
| 146 | |||
| 147 | Returns the current object. | ||
| 148 | |||
| 149 | =head2 to_number | ||
| 150 | |||
| 151 | Returns the current value as a L<number|Module::Generic::Number> | ||
| 152 | |||
| 153 | =head2 value | ||
| 154 | |||
| 155 | Also overloaded as stringification, simply returns the literal string value. | ||
| 156 | |||
| 157 | =head2 value_as_number | ||
| 158 | |||
| 159 | Returns the current value as a L<number|Module::Generic::Number> | ||
| 160 | |||
| 161 | =head1 AUTHOR | ||
| 162 | |||
| 163 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 164 | |||
| 165 | =head1 SEE ALSO | ||
| 166 | |||
| 167 | L<HTML::Object>, L<HTML::Object::Attribute>, L<HTML::Object::Boolean>, L<HTML::Object::Closing>, L<HTML::Object::Collection>, L<HTML::Object::Comment>, L<HTML::Object::Declaration>, L<HTML::Object::Document>, L<HTML::Object::Element>, L<HTML::Object::Exception>, L<HTML::Object::Literal>, L<HTML::Object::Number>, L<HTML::Object::Root>, L<HTML::Object::Space>, L<HTML::Object::Text>, L<HTML::Object::XQuery> | ||
| 168 | |||
| 169 | =head1 COPYRIGHT & LICENSE | ||
| 170 | |||
| 171 | Copyright (c) 2021 DEGUEST Pte. Ltd. | ||
| 172 | |||
| 173 | All rights reserved | ||
| 174 | |||
| 175 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 176 | |||
| 177 | =cut |