Skip to content

Commit

Permalink
added next and previous methods to Mojo::DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 31, 2012
1 parent 9354ce2 commit f6305c5
Show file tree
Hide file tree
Showing 63 changed files with 46 additions and 61 deletions.
4 changes: 4 additions & 0 deletions Changes
@@ -1,4 +1,8 @@

3.54 2012-11-01
- Added next and previous methods to Mojo::DOM.
- Improved tests.

3.53 2012-10-31
- Replaced Mojolicious::Guides::CodingGuidelines with
Mojolicious::Guides::Contributing.
Expand Down
34 changes: 34 additions & 0 deletions lib/Mojo/DOM.pm
Expand Up @@ -151,6 +151,8 @@ sub namespace {
return '';
}

sub next { shift->_sibling(1) }

sub parent {
my $self = shift;

Expand Down Expand Up @@ -178,6 +180,8 @@ sub prepend_content {
return $self;
}

sub previous { shift->_sibling(0) }

sub remove { shift->replace('') }

sub replace {
Expand Down Expand Up @@ -344,6 +348,24 @@ sub _parser {
return $self;
}

sub _sibling {
my ($self, $next) = @_;

# Root
return undef unless my $parent = $self->parent;

# Find previous or next sibling
my ($candidate, $current);
for my $child ($parent->children->each) {
++$current and next if $child->tree eq $self->tree;
return $next ? $child : $candidate if $current;
$candidate = $child;
}

# No siblings
return undef;
}

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

Expand Down Expand Up @@ -570,6 +592,12 @@ Find element namespace.
# Find namespace for an element that may or may not have a namespace prefix
my $namespace = $dom->at('svg > circle')->namespace;
=head2 C<next>
my $sibling = $dom->next;
Next sibling of element.
=head2 C<parent>
my $parent = $dom->parent;
Expand Down Expand Up @@ -603,6 +631,12 @@ Prepend to element content.
# "<div><h2>AB</h2></div>"
$dom->parse('<div><h2>B</h2></div>')->at('h2')->prepend_content('A')->root;
=head2 C<previous>
my $sibling = $dom->previous;
Previous sibling of element.
=head2 C<remove>
my $old = $dom->remove;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -38,7 +38,7 @@ has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };

our $CODENAME = 'Rainbow';
our $VERSION = '3.53';
our $VERSION = '3.54';

sub AUTOLOAD {
my $self = shift;
Expand Down
1 change: 0 additions & 1 deletion lib/Mojolicious/Command/generate/plugin.pm
Expand Up @@ -79,7 +79,6 @@ L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
use Mojo::Base -strict;
use Test::More;
use Mojolicious::Lite;
use Test::Mojo;
Expand Down
1 change: 0 additions & 1 deletion t/mojo/app.t
Expand Up @@ -7,7 +7,6 @@ BEGIN {
}

use Test::More;

use Mojo;
use Mojo::IOLoop;
use Mojo::Server::Daemon;
Expand Down
1 change: 0 additions & 1 deletion t/mojo/asset.t
@@ -1,7 +1,6 @@
use Mojo::Base -strict;

use Test::More;

use Mojo::Asset::File;
use Mojo::Asset::Memory;

Expand Down
1 change: 0 additions & 1 deletion t/mojo/bytestream.t
Expand Up @@ -3,7 +3,6 @@ use Mojo::Base -strict;
use utf8;

use Test::More;

use File::Spec::Functions qw(catfile splitdir);
use File::Temp 'tempdir';
use FindBin;
Expand Down
1 change: 0 additions & 1 deletion t/mojo/cache.t
@@ -1,7 +1,6 @@
use Mojo::Base -strict;

use Test::More;

use Mojo::Cache;

my $cache = Mojo::Cache->new(max_keys => 2);
Expand Down
1 change: 0 additions & 1 deletion t/mojo/cgi.t
@@ -1,7 +1,6 @@
use Mojo::Base -strict;

use Test::More;

use Mojo::Message::Response;
use Mojo::Server::CGI;
use Mojolicious::Command::cgi;
Expand Down
1 change: 0 additions & 1 deletion t/mojo/collection.t
@@ -1,7 +1,6 @@
use Mojo::Base -strict;

use Test::More;

use Mojo::Collection 'c';

# Array
Expand Down
1 change: 0 additions & 1 deletion t/mojo/content.t
@@ -1,7 +1,6 @@
use Mojo::Base -strict;

use Test::More;

use Mojo::Content::MultiPart;
use Mojo::Content::Single;

Expand Down
1 change: 0 additions & 1 deletion t/mojo/cookie.t
@@ -1,7 +1,6 @@
use Mojo::Base -strict;

use Test::More;

use Mojo::Cookie::Request;
use Mojo::Cookie::Response;

Expand Down
1 change: 0 additions & 1 deletion t/mojo/cookiejar.t
@@ -1,7 +1,6 @@
use Mojo::Base -strict;

use Test::More;

use Mojo::Cookie::Response;
use Mojo::URL;
use Mojo::UserAgent::CookieJar;
Expand Down
1 change: 0 additions & 1 deletion t/mojo/date.t
@@ -1,7 +1,6 @@
use Mojo::Base -strict;

use Test::More;

use Mojo::Date;

# RFC 822/1123
Expand Down
1 change: 0 additions & 1 deletion t/mojo/delay.t
Expand Up @@ -7,7 +7,6 @@ BEGIN {
}

use Test::More;

use Mojo::IOLoop;
use Mojo::IOLoop::Delay;

Expand Down
8 changes: 7 additions & 1 deletion t/mojo/dom.t
Expand Up @@ -3,7 +3,6 @@ use Mojo::Base -strict;
use utf8;

use Test::More;

use Mojo::DOM;
use Mojo::Util 'encode';

Expand Down Expand Up @@ -128,6 +127,13 @@ is $dom->at('[class$="ing"]')->type, 'simple', 'right type';
is $dom->at('[class="working"]')->type, 'simple', 'right type';
is $dom->at('[class$=ing]')->type, 'simple', 'right type';
is $dom->at('[class=working]')->type, 'simple', 'right type';
is $dom->at('foo > simple')->next->type, 'test', 'right type';
is $dom->at('foo > simple')->next->next->type, 'a', 'right type';
is $dom->at('foo > test')->previous->type, 'simple', 'right type';
is $dom->next, undef, 'no siblings';
is $dom->previous, undef, 'no siblings';
is $dom->at('foo > a')->next, undef, 'no next sibling';
is $dom->at('foo > simple')->previous, undef, 'no previous sibling';

# Class and ID
$dom = Mojo::DOM->new->parse('<div id="id" class="class">a</div>');
Expand Down
1 change: 0 additions & 1 deletion t/mojo/eventemitter.t
@@ -1,7 +1,6 @@
use Mojo::Base -strict;

use Test::More;

use Mojo::EventEmitter;

# Normal event
Expand Down
1 change: 0 additions & 1 deletion t/mojo/headers.t
@@ -1,7 +1,6 @@
use Mojo::Base -strict;

use Test::More;

use Mojo::Headers;

# Basic functionality
Expand Down
1 change: 0 additions & 1 deletion t/mojo/home.t
Expand Up @@ -4,7 +4,6 @@ use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }

use Test::More;

use Cwd qw(cwd realpath);
use File::Spec::Functions qw(canonpath catdir splitdir);
use FindBin;
Expand Down
1 change: 0 additions & 1 deletion t/mojo/ioloop.t
Expand Up @@ -7,7 +7,6 @@ BEGIN {
}

use Test::More;

use Mojo::IOLoop;
use Mojo::IOLoop::Client;
use Mojo::IOLoop::Delay;
Expand Down
1 change: 0 additions & 1 deletion t/mojo/json.t
Expand Up @@ -11,7 +11,6 @@ use Mojo::Base -strict;
use utf8;

use Test::More;

use Mojo::ByteStream 'b';
use Mojo::JSON;

Expand Down
1 change: 0 additions & 1 deletion t/mojo/json_pointer.t
Expand Up @@ -3,7 +3,6 @@ use Mojo::Base -strict;
use utf8;

use Test::More;

use Mojo::JSON::Pointer;

# "contains" (hash)
Expand Down
1 change: 0 additions & 1 deletion t/mojo/log.t
@@ -1,7 +1,6 @@
use Mojo::Base -strict;

use Test::More;

use File::Spec::Functions 'catdir';
use File::Temp 'tempdir';
use Mojo::Asset::File;
Expand Down
1 change: 0 additions & 1 deletion t/mojo/parameters.t
Expand Up @@ -3,7 +3,6 @@ use Mojo::Base -strict;
use utf8;

use Test::More;

use Mojo::Parameters;

# Basic functionality
Expand Down
1 change: 0 additions & 1 deletion t/mojo/path.t
Expand Up @@ -3,7 +3,6 @@ use Mojo::Base -strict;
use utf8;

use Test::More;

use Mojo::Path;

# Basic functionality
Expand Down
1 change: 0 additions & 1 deletion t/mojo/psgi.t
@@ -1,7 +1,6 @@
use Mojo::Base -strict;

use Test::More;

use Mojo::JSON;
use Mojo::Server::PSGI;
use Mojolicious::Command::psgi;
Expand Down
1 change: 0 additions & 1 deletion t/mojo/reactor_poll.t
Expand Up @@ -7,7 +7,6 @@ BEGIN {
}

use Test::More;

use IO::Socket::INET;
use Mojo::Reactor::Poll;

Expand Down
1 change: 0 additions & 1 deletion t/mojo/request.t
Expand Up @@ -3,7 +3,6 @@ use Mojo::Base -strict;
use utf8;

use Test::More;

use File::Spec::Functions 'catfile';
use File::Temp 'tempdir';
use Mojo::Content::Single;
Expand Down
1 change: 0 additions & 1 deletion t/mojo/request_cgi.t
@@ -1,7 +1,6 @@
use Mojo::Base -strict;

use Test::More;

use Mojo::Message::Request;

# Parse Lighttpd CGI environment variables and body
Expand Down
1 change: 0 additions & 1 deletion t/mojo/response.t
@@ -1,7 +1,6 @@
use Mojo::Base -strict;

use Test::More;

use Mojo::Asset::File;
use Mojo::Content::Single;
use Mojo::Content::MultiPart;
Expand Down
1 change: 0 additions & 1 deletion t/mojo/template.t
Expand Up @@ -18,7 +18,6 @@ use Mojo::Base -strict;
use utf8;

use Test::More;

use File::Spec::Functions qw(catfile splitdir);
use FindBin;
use Mojo::Template;
Expand Down
1 change: 0 additions & 1 deletion t/mojo/transactor.t
@@ -1,7 +1,6 @@
use Mojo::Base -strict;

use Test::More;

use File::Spec::Functions 'catdir';
use FindBin;
use Mojo::URL;
Expand Down
1 change: 0 additions & 1 deletion t/mojo/url.t
Expand Up @@ -3,7 +3,6 @@ use Mojo::Base -strict;
use utf8;

use Test::More;

use Mojo::URL;

# Simple
Expand Down
1 change: 0 additions & 1 deletion t/mojo/user_agent.t
Expand Up @@ -7,7 +7,6 @@ BEGIN {
}

use Test::More;

use Mojo::IOLoop;
use Mojo::UserAgent;
use Mojolicious::Lite;
Expand Down
1 change: 0 additions & 1 deletion t/mojo/util.t
Expand Up @@ -3,7 +3,6 @@ use Mojo::Base -strict;
use utf8;

use Test::More;

use File::Spec::Functions qw(catfile splitdir);
use File::Temp 'tempdir';
use FindBin;
Expand Down
1 change: 0 additions & 1 deletion t/mojo/websocket.t
Expand Up @@ -7,7 +7,6 @@ BEGIN {
}

use Test::More;

use IO::Socket::INET;
use Mojo::IOLoop;
use Mojo::Transaction::WebSocket;
Expand Down
1 change: 0 additions & 1 deletion t/mojo/websocket_frames.t
@@ -1,7 +1,6 @@
use Mojo::Base -strict;

use Test::More;

use Mojo::Transaction::WebSocket;

# Simple text frame roundtrip
Expand Down
1 change: 0 additions & 1 deletion t/mojo/websocket_proxy.t
Expand Up @@ -7,7 +7,6 @@ BEGIN {
}

use Test::More;

use Mojo::IOLoop;
use Mojo::Server::Daemon;
use Mojo::UserAgent;
Expand Down
1 change: 0 additions & 1 deletion t/mojolicious/charset_lite_app.t
Expand Up @@ -9,7 +9,6 @@ BEGIN {
}

use Test::More;

use Mojo::ByteStream 'b';
use Mojolicious::Lite;
use Test::Mojo;
Expand Down
1 change: 0 additions & 1 deletion t/mojolicious/command.t
Expand Up @@ -4,7 +4,6 @@ use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }

use Test::More;

use Cwd 'cwd';
use File::Spec::Functions 'catdir';
use File::Temp 'tempdir';
Expand Down

0 comments on commit f6305c5

Please sign in to comment.