getting things synced up for a release
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / FilterBuilder.pm
index 74b7c14..ed7fef1 100644 (file)
@@ -42,9 +42,9 @@ sub set_attribute {
 sub _parse_attribute_args {
   my $self = shift;
 
-  warn "WARNING: Long form arg (name => 'class', value => 'x') is deprecated. This may not do what you originally intended..."
+  die "Long form arg (name => 'class', value => 'x') is no longer supported"
     if(@_ == 1 && $_[0]->{'name'} && $_[0]->{'value'});
-    
+
   my $opts = ref($_[0]) eq 'HASH' ? $_[0] : {$_[0] => $_[1]};
   for (values %{$opts}) { $self->_zconfig->parser->html_escape($_); }
   return $opts;
@@ -56,7 +56,7 @@ sub add_attribute {
 
 sub add_class { shift->add_to_attribute('class',@_) }
 
-sub remove_class { shift->remove_attribute('class',@_) }
+sub remove_class { shift->remove_from_attribute('class',@_) }
 
 sub set_class { shift->set_attribute('class',@_) }
 
@@ -71,7 +71,7 @@ sub add_to_attribute {
     +{ %$evt, raw => undef, raw_attrs => undef,
        attrs => {
          %$a,
-         map {$_ => join(' ', (exists $a->{$_} ? $a->{$_} : ()), $attr->{$_}) } 
+         map {$_ => join(' ', (exists $a->{$_} ? $a->{$_} : ()), $attr->{$_}) }
           keys %$attr
       },
       @kadd ? (attr_names => [ @{$evt->{attr_names}}, @kadd ]) : ()
@@ -79,6 +79,23 @@ sub add_to_attribute {
   };
 }
 
+sub remove_from_attribute {
+  my $self = shift;
+  my $attr = $self->_parse_attribute_args(@_);
+  sub {
+    my $a = (my $evt = $_[0])->{attrs};
+    +{ %$evt, raw => undef, raw_attrs => undef,
+       attrs => {
+         %$a,
+         #TODO needs to support multiple removes
+         map { my $tar = $_; $_ => join ' ',
+          map {$attr->{$tar} ne $_} split ' ', $a->{$_} }
+            grep {exists $a->{$_}} keys %$attr
+      },
+    }
+  };
+}
+
 sub remove_attribute {
   my ($self, $args) = @_;
   my $name = (ref($args) eq 'HASH') ? $args->{name} : $args;
@@ -460,8 +477,7 @@ Sets an attribute of a given name to a given value for all matching selections.
       ->select('p')
       ->set_attribute(class=>'paragraph')
       ->select('div')
-      ->set_attribute({name=>'class', value=>'divider'});
-
+      ->set_attribute({class=>'paragraph', name=>'divider'});
 
 Overrides existing values, if such exist.  When multiple L</set_attribute>
 calls are made against the same or overlapping selection sets, the final
@@ -472,19 +488,9 @@ call wins.
 Adds a value to an existing attribute, or creates one if the attribute does not
 yet exist.  You may call this method with either an Array or HashRef of Args.
 
-Here's the 'long form' HashRef:
-
-    $html_zoom
-      ->select('p')
-      ->set_attribute(class=>'paragraph')
-      ->then
-      ->add_to_attribute({name=>'class', value=>'divider'});
-
-And the exact same effect using the 'short form' Array:
-
     $html_zoom
       ->select('p')
-      ->set_attribute(class=>'paragraph')
+      ->set_attribute({class => 'paragraph', name => 'test'})
       ->then
       ->add_to_attribute(class=>'divider');
 
@@ -500,8 +506,26 @@ Removes an attribute and all its values.
       ->then
       ->remove_attribute('class');
 
+=head2 remove_from_attribute
+
+Removes a value from existing attribute
+
+    $html_zoom
+      ->select('p')
+      ->set_attribute(class=>'paragraph lead')
+      ->then
+      ->remove_from_attribute('class' => 'lead');
+
 Removes attributes from the original stream or events already added.
 
+=head2 add_class
+
+Add to a class attribute
+
+=head2 remove_class
+
+Remove from a class attribute
+
 =head2 transform_attribute
 
 Transforms (or creates or deletes) an attribute by running the passed
@@ -653,9 +677,9 @@ Similar to add_before, but adds the content to the match.
     ->select('p')
     ->prepend_content("Hello ")
     ->to_html
-    
+
   ## <p>Hello World</p>
-  
+
 Acceptable values are strings, scalar refs and L<HTML::Zoom> objects
 
 =head2 append_content
@@ -667,7 +691,7 @@ Similar to add_after, but adds the content to the match.
     ->select('p')
     ->prepend_content("World")
     ->to_html
-    
+
   ## <p>Hello World</p>
 
 Acceptable values are strings, scalar refs and L<HTML::Zoom> objects
@@ -704,7 +728,7 @@ pre-generated)
         }
       } @list
     ]);
-    
+
 Subroutines would be run with $_ localized to result of L<HTML::Zoom/select> (of
 collected elements), and with said result passed as parameter to subroutine.