Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 16, 2012
1 parent b48685d commit 417ceaf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

3.0 2012-06-16
3.0 2012-06-17
- Code name "Rainbow", this is a major release.
- Removed Mojolicious::Plugin::I18N so it can be maintained as a separate
distribution.
Expand Down
42 changes: 14 additions & 28 deletions lib/Mojo/DOM.pm
Expand Up @@ -72,12 +72,7 @@ sub attrs {
return $self;
}

sub charset {
my $self = shift;
return $self->[0]->charset unless @_;
$self->[0]->charset(shift);
return $self;
}
sub charset { shift->_parser(charset => @_) }

# "Oh boy! Sleep! That's when I'm a Viking!"
sub children {
Expand Down Expand Up @@ -139,8 +134,7 @@ sub namespace {

# Prefix
return if (my $current = $self->tree)->[0] eq 'root';
my $prefix = '';
if ($current->[1] =~ /^(.*?)\:/) { $prefix = $1 }
my $prefix = $current->[1] =~ /^(.*?)\:/ ? $1 : '';

# Walk tree
while ($current) {
Expand All @@ -149,9 +143,7 @@ sub namespace {

# Namespace for prefix
if ($prefix) {
for my $key (keys %$attrs) {
return $attrs->{$key} if $key =~ /^xmlns\:$prefix$/;
}
/^xmlns\:$prefix$/ and return $attrs->{$_} for keys %$attrs;
}

# Namespace attribute
Expand Down Expand Up @@ -194,11 +186,8 @@ sub replace {

# Parse
my $tree = $self->tree;
if ($tree->[0] eq 'root') {
$self->xml(undef);
return $self->parse($new);
}
else { $new = $self->_parse("$new") }
if ($tree->[0] eq 'root') { return $self->xml(undef)->parse($new) }
else { $new = $self->_parse("$new") }

# Find and replace
my $parent = $tree->[3];
Expand Down Expand Up @@ -285,12 +274,7 @@ sub text_before {

sub to_xml { shift->[0]->render }

sub tree {
my $self = shift;
return $self->[0]->tree unless @_;
$self->[0]->tree(shift);
return $self;
}
sub tree { shift->_parser(tree => @_) }

sub type {
my ($self, $type) = @_;
Expand All @@ -308,12 +292,7 @@ sub type {
}

# "I want to set the record straight, I thought the cop was a prostitute."
sub xml {
my $self = shift;
return $self->[0]->xml unless @_;
$self->[0]->xml(shift);
return $self;
}
sub xml { shift->_parser(xml => @_) }

sub _add {
my ($self, $offset, $new) = @_;
Expand Down Expand Up @@ -362,6 +341,13 @@ sub _parse {
->parse(shift)->tree;
}

sub _parser {
my ($self, $method) = (shift, shift);
return $self->[0]->$method unless @_;
$self->[0]->$method(shift);
return $self;
}

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

Expand Down

0 comments on commit 417ceaf

Please sign in to comment.