Skip to content

Commit

Permalink
remove support for smart whitespace trimming from all_text and text m…
Browse files Browse the repository at this point in the history
…ethods in Mojo::DOM
  • Loading branch information
kraih committed Jun 21, 2016
1 parent 425cb74 commit 3e1051a
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 219 deletions.
7 changes: 6 additions & 1 deletion Changes
@@ -1,5 +1,10 @@

6.67 2016-06-21
7.0 2016-06-21
- Code name "Nerd Face", this is a major release.
- Removed squish method from Mojo::ByteStream.
- Removed squish function from Mojo::Util.
- Removed support for smart whitespace trimming from all_text and text methods
in Mojo::DOM.
- Fix a few whitespace bugs in Mojo::DOM.

6.66 2016-06-16
Expand Down
14 changes: 3 additions & 11 deletions lib/Mojo/ByteStream.pm
Expand Up @@ -12,8 +12,8 @@ our @EXPORT_OK = ('b');
my @UTILS = (
qw(b64_decode b64_encode camelize decamelize hmac_sha1_sum html_unescape),
qw(md5_bytes md5_sum punycode_decode punycode_encode quote sha1_bytes),
qw(sha1_sum slurp spurt squish term_escape trim unindent unquote),
qw(url_escape url_unescape xml_escape xor_encode)
qw(sha1_sum slurp spurt term_escape trim unindent unquote url_escape),
qw(url_unescape xml_escape xor_encode)
);
for my $name (@UTILS) {
my $sub = Mojo::Util->can($name);
Expand Down Expand Up @@ -266,7 +266,7 @@ Read all data at once from file into bytestream with L<Mojo::Util/"slurp">.
Write all data from bytestream at once to file with L<Mojo::Util/"spurt">.
# Remove unnecessary whitespace from file
b('/home/sri/foo.txt')->slurp->squish->spurt('/home/sri/bar.txt');
b('/home/sri/foo.txt')->slurp->trim->spurt('/home/sri/bar.txt');
=head2 split
Expand All @@ -278,14 +278,6 @@ objects.
# "One,Two,Three"
b("one,two,three")->split(',')->map('camelize')->join(',');
=head2 squish
$stream = $stream->squish;
Trim whitespace characters from both ends of bytestream and then change all
consecutive groups of whitespace into one space each with
L<Mojo::Util/"squish">.
=head2 tap
$stream = $stream->tap(sub {...});
Expand Down
52 changes: 13 additions & 39 deletions lib/Mojo/DOM.pm
Expand Up @@ -13,10 +13,9 @@ use Carp 'croak';
use Mojo::Collection;
use Mojo::DOM::CSS;
use Mojo::DOM::HTML;
use Mojo::Util 'squish';
use Scalar::Util qw(blessed weaken);

sub all_text { shift->_all_text(1, @_) }
sub all_text { _text([_nodes(shift->tree)], 1) }

sub ancestors { _select($_[0]->_collect($_[0]->_ancestors), $_[1]) }

Expand Down Expand Up @@ -151,7 +150,7 @@ sub tag {

sub tap { shift->Mojo::Base::tap(@_) }

sub text { shift->_all_text(0, @_) }
sub text { _text([_nodes(shift->tree)], 0) }

sub to_string { shift->_delegate('render') }

Expand Down Expand Up @@ -199,17 +198,6 @@ sub _all {
map { $_->[0] eq 'tag' ? ($_, _all(_nodes($_))) : ($_) } @_;
}

sub _all_text {
my ($self, $recurse, $trim) = (shift, shift, shift // 1);

# Detect "pre" tag
my $tree = $self->tree;
map { $_->[1] eq 'pre' and $trim = 0 } $self->_ancestors, $tree
if $trim && $tree->[0] ne 'root';

return _text([_nodes($tree)], $recurse, $trim);
}

sub _ancestors {
my ($self, $root) = @_;

Expand Down Expand Up @@ -316,26 +304,22 @@ sub _siblings {
sub _start { $_[0][0] eq 'root' ? 1 : 4 }

sub _text {
my ($nodes, $recurse, $trim) = @_;
my ($nodes, $recurse) = @_;

my $text = '';
for my $node (@$nodes) {
my $type = $node->[0];

# Text
my $chunk = '';
if ($type eq 'text') { $chunk = $trim ? squish $node->[1] : $node->[1] }

# CDATA or raw text
elsif ($type eq 'cdata' || $type eq 'raw') { $chunk = $node->[1] }
if ($type eq 'text' || $type eq 'cdata' || $type eq 'raw') {
$text .= $node->[1];
}

# Nested tag
elsif ($type eq 'tag' && $recurse) {
no warnings 'recursion';
$chunk = _text([_nodes($node)], 1, $node->[1] eq 'pre' ? 0 : $trim);
$text .= _text([_nodes($node)], 1);
}

$text .= $chunk;
}

return $text;
Expand Down Expand Up @@ -465,17 +449,12 @@ L<Mojo::DOM> implements the following methods.
=head2 all_text
my $trimmed = $dom->all_text;
my $untrimmed = $dom->all_text(0);
Extract text content from all descendant nodes of this element, smart
whitespace trimming is enabled by default.
my $text = $dom->all_text;
# "foo bar baz"
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text;
Extract text content from all descendant nodes of this element.
# "foo\nbarbaz\n"
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text(0);
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text;
=head2 ancestors
Expand Down Expand Up @@ -882,17 +861,12 @@ Alias for L<Mojo::Base/"tap">.
=head2 text
my $trimmed = $dom->text;
my $untrimmed = $dom->text(0);
my $text = $dom->text;
Extract text content from this element only (not including child elements),
smart whitespace trimming is enabled by default.
# "foo baz"
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text;
Extract text content from this element only (not including child elements).
# "foo\nbaz\n"
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text(0);
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text;
=head2 to_string
Expand Down
20 changes: 2 additions & 18 deletions lib/Mojo/Util.pm
Expand Up @@ -57,8 +57,8 @@ our @EXPORT_OK = (
qw(decode deprecated dumper encode files hmac_sha1_sum html_unescape),
qw(md5_bytes md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp split_cookie_header),
qw(split_header spurt squish steady_time tablify term_escape trim unindent),
qw(unquote url_escape url_unescape xml_escape xor_encode)
qw(split_header spurt steady_time tablify term_escape trim unindent unquote),
qw(url_escape url_unescape xml_escape xor_encode)
);

# Aliases
Expand Down Expand Up @@ -264,12 +264,6 @@ sub spurt {
return $content;
}

sub squish {
my $str = trim(@_);
$str =~ s/\s+/ /g;
return $str;
}

sub tablify {
my $rows = shift;

Expand Down Expand Up @@ -730,16 +724,6 @@ its own array reference, and keys without a value get C<undef> assigned.
Write all data at once to file.
=head2 squish
my $squished = squish $str;
Trim whitespace characters from both ends of string and then change all
consecutive groups of whitespace into one space each.
# "foo bar"
squish ' foo bar ';
=head2 steady_time
my $time = steady_time;
Expand Down
6 changes: 4 additions & 2 deletions lib/Mojolicious.pm
Expand Up @@ -42,8 +42,8 @@ has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };
has validator => sub { Mojolicious::Validator->new };

our $CODENAME = 'Clinking Beer Mugs';
our $VERSION = '6.67';
our $CODENAME = 'Nerd Face';
our $VERSION = '7.0';

sub AUTOLOAD {
my $self = shift;
Expand Down Expand Up @@ -718,6 +718,8 @@ L<http://www.apache.org/licenses/LICENSE-2.0>.
Every major release of L<Mojolicious> has a code name, these are the ones that
have been used in the past.
7.0, C<Nerd Face> (U+1F913)
6.0, C<Clinking Beer Mugs> (U+1F37B)
5.0, C<Tiger Face> (U+1F42F)
Expand Down
3 changes: 0 additions & 3 deletions t/mojo/bytestream.t
Expand Up @@ -61,9 +61,6 @@ is b('"foo 23 \"bar"')->unquote, 'foo 23 "bar', 'right unquoted result';
# trim
is b(' la la la ')->trim, 'la la la', 'right trimmed result';

# squish
is b("\n la\nla la \n")->squish, 'la la la', 'right squished result';

# md5_bytes
is unpack('H*', b('foo bar baz ♥')->encode->md5_bytes),
'a740aeb6e066f158cbf19fd92e890d2d', 'right binary md5 checksum';
Expand Down

0 comments on commit 3e1051a

Please sign in to comment.