Skip to content

Commit

Permalink
improved html_escape to favor lower case entities
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 27, 2012
1 parent 5c8169c commit 19b74a6
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

3.44 2012-09-27
- Improved html_escape to favor lower case entities. (judofyr)
- Improved javascript and stylesheet helpers to not generate type
attributes.
- Improved documentation.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/DOM.pm
Expand Up @@ -433,7 +433,7 @@ use it for validation.
=head1 CASE SENSITIVITY
L<Mojo::DOM> defaults to HTML semantics, that means all tags and attributes
are lowercased and selectors need to be lowercase as well.
are lowercased and selectors need to be lower case as well.
my $dom = Mojo::DOM->new('<P ID="greeting">Hi!</P>');
say $dom->at('p')->text;
Expand Down
4 changes: 3 additions & 1 deletion lib/Mojo/Util.pm
Expand Up @@ -33,7 +33,9 @@ my %ENTITIES;

# Reverse entities for html_escape (without "apos")
my %REVERSE = ("\x{0027}" => '#39;');
$REVERSE{$ENTITIES{$_}} //= $_ for sort grep {/;/} keys %ENTITIES;
$REVERSE{$ENTITIES{$_}} //= $_
for sort { split(/[A-Z]/, $a) <=> split(/[A-Z]/, $b) }
sort grep {/;/} keys %ENTITIES;

our @EXPORT_OK = (
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/bytestream.t
Expand Up @@ -33,7 +33,7 @@ is b('foo%C3%9F%C4%80bar%E2%98%BA')->url_unescape->decode('UTF-8'),
"foo\x{df}\x{0100}bar\x{263a}", 'right url unescaped result';

# html_escape
is b("foo bar'<baz>")->html_escape, 'foo bar&#39;&LT;baz&GT;',
is b("foo bar'<baz>")->html_escape, 'foo bar&#39;&lt;baz&gt;',
'right html escaped result';

# html_unescape
Expand Down
6 changes: 3 additions & 3 deletions t/mojo/util.t
Expand Up @@ -103,7 +103,7 @@ is decode('UTF-8', url_unescape 'foo%C3%9F%C4%80bar%E2%98%BA'),
"foo\x{df}\x{0100}bar\x{263a}", 'right url unescaped result';

# html_escape
is html_escape("foo bar'<baz>"), 'foo bar&#39;&LT;baz&GT;',
is html_escape("foo bar'<baz>"), 'foo bar&#39;&lt;baz&gt;',
'right html escaped result';

# html_escape (nothing to escape)
Expand Down Expand Up @@ -131,7 +131,7 @@ is html_unescape('&Ltf&amp&0oo&nbspba;&ltr'), "&Ltf&&0oo\x{00a0}ba;<r",

# UTF-8 html_escape
is html_escape("fo\nobar<baz>&\"\x{152}\x{02ae4}"),
"fo\nobar&LT;baz&GT;&AMP;&QUOT;&OElig;&Dashv;", 'right html escaped result';
"fo\nobar&lt;baz&gt;&amp;&quot;&OElig;&Dashv;", 'right html escaped result';

# UTF-8 html_unescape
is html_unescape(decode 'UTF-8', 'foo&lt;baz&gt;&#x26;&#34;&OElig;&Foo;'),
Expand All @@ -142,7 +142,7 @@ is html_escape('/home/sri/perl/site_perl/5.10.0/Mojo.pm'),
'/home/sri/perl/site_perl/5.10.0/Mojo.pm', 'right html escaped result';

# html_escape (custom pattern)
is html_escape("fo\no b<a>r", 'o<'), "f&#111;\n&#111; b&LT;a>r",
is html_escape("fo\no b<a>r", 'o<'), "f&#111;\n&#111; b&lt;a>r",
'right html escaped result';

# xml_escape
Expand Down

0 comments on commit 19b74a6

Please sign in to comment.