Skip to content

Commit

Permalink
added remove_class
Browse files Browse the repository at this point in the history
  • Loading branch information
mirod committed Sep 24, 2010
1 parent b5e82b5 commit 5eaaaae
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -20,6 +20,7 @@ added: can use the tag[nested] form in twig handlers that
triggers on elements 'tag' that include a child 'nested'
added: aliased the add_to_class XML::Twig::Elt method to add_class,
which seems more natural
added: the remove_class method
added: made att and class lvalues (in perl 5.6 and up)
fixed: copy did not copy the empty status of an element
RT#31664 spotted by Roland Minner
Expand Down
23 changes: 21 additions & 2 deletions Twig.pm
Expand Up @@ -5525,6 +5525,15 @@ sub add_to_class
$elt->set_class( join( ' ', sort keys %class));
}

sub remove_class
{ my( $elt, $class_to_remove)= @_;
return $elt unless $class_to_remove;
my $class= $elt->class;
my %class= $class ? map { $_ => 1 } split /\s+/, $class : ();
delete $class{$class_to_remove};
$elt->set_class( join( ' ', sort keys %class));
}

sub att_to_class { my( $elt, $att)= @_; $elt->set_class( $elt->{'att'}->{$att}); }
sub add_att_to_class { my( $elt, $att)= @_; $elt->add_to_class( $elt->{'att'}->{$att}); }
sub move_att_to_class { my( $elt, $att)= @_; $elt->add_to_class( $elt->{'att'}->{$att});
Expand Down Expand Up @@ -12653,8 +12662,18 @@ Set the C<class> attribute for the element to C<$class>
=item add_class ($class)
Add C<$class> to the element C<class> attribute: the new class is added
only if it is not already present. Note that classes are sorted alphabetically,
so the C<class> attribute can be changed even if the class is already there
only if it is not already present.
Note that classes are then sorted alphabetically, so the C<class> attribute
can be changed even if the class is already there
=item remove_class ($class)
Remove C<$class> from the element C<class> attribute.
Note that classes are then sorted alphabetically, so the C<class> attribute can be
changed even if the class is already there
=item add_to_class ($class)
Expand Down
23 changes: 21 additions & 2 deletions Twig_pm.slow
Expand Up @@ -5526,6 +5526,15 @@ sub add_to_class
$elt->set_class( join( ' ', sort keys %class));
}

sub remove_class
{ my( $elt, $class_to_remove)= @_;
return $elt unless $class_to_remove;
my $class= $elt->class;
my %class= $class ? map { $_ => 1 } split /\s+/, $class : ();
delete $class{$class_to_remove};
$elt->set_class( join( ' ', sort keys %class));
}

sub att_to_class { my( $elt, $att)= @_; $elt->set_class( $elt->att( $att)); }
sub add_att_to_class { my( $elt, $att)= @_; $elt->add_to_class( $elt->att( $att)); }
sub move_att_to_class { my( $elt, $att)= @_; $elt->add_to_class( $elt->att( $att));
Expand Down Expand Up @@ -12655,8 +12664,18 @@ Set the C<class> attribute for the element to C<$class>
=item add_class ($class)

Add C<$class> to the element C<class> attribute: the new class is added
only if it is not already present. Note that classes are sorted alphabetically,
so the C<class> attribute can be changed even if the class is already there
only if it is not already present.

Note that classes are then sorted alphabetically, so the C<class> attribute
can be changed even if the class is already there

=item remove_class ($class)

Remove C<$class> from the element C<class> attribute.

Note that classes are then sorted alphabetically, so the C<class> attribute can be
changed even if the class is already there


=item add_to_class ($class)

Expand Down
16 changes: 14 additions & 2 deletions t/test_3_36.t
Expand Up @@ -12,7 +12,7 @@ my $DEBUG=0;

use XML::Twig;

my $TMAX=61;
my $TMAX=66;
print "1..$TMAX\n";

{ my $doc=q{<d><s id="s1"><t>title 1</t><s id="s2"><t>title 2</t></s><s id="s3"></s></s><s id="s4"></s></d>};
Expand Down Expand Up @@ -358,7 +358,19 @@ score: anchored: 0 predicates: 3 steps: 1 type: 3
is( $triggered_foo , 'e1.1', 'handler condition on valued private attribute');
}


{ my $t= XML::Twig->parse( '<d class="foo"><e class="bar baz"/></d>');
$t->root->remove_class( 'foo');
is( $t->root->class, '', 'empty class after remove_class');
my $e= $t->first_elt( 'e');
$e->remove_class( 'foo');
is( $e->class, 'bar baz', 'remove_class on non-existent class');
$e->remove_class( 'baz');
is( $e->class, 'bar', 'remove_class');
$e->remove_class( 'foo');
is( $e->class, 'bar', 'remove_class on non-existent class (again)');
$e->remove_class( 'bar');
is( $e->class, '', 'remove_class until no class is left');
}

{ if( XML::Twig::_use( 'Text::Wrap'))
{ my $out= "t/test_wrapped.xml";
Expand Down
2 changes: 1 addition & 1 deletion t/test_xml_split_g.t
Expand Up @@ -18,7 +18,7 @@ if( $] < 5.006) { print "1..1\nok 1\n"; warn "skipping, xml_merge runs only on p

print "1..13\n";

$perl = used_perl();
my $perl = used_perl();
my $xml_split = File::Spec->catfile( "tools", "xml_split", "xml_split");
my $xml_merge = File::Spec->catfile( "tools", "xml_merge", "xml_merge");

Expand Down

0 comments on commit 5eaaaae

Please sign in to comment.