Skip to content

Commit

Permalink
Added recursive queries to elements() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
supernovus committed Oct 22, 2010
1 parent 10869e5 commit f28c7c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/TODO.txt
@@ -1,3 +1,5 @@
- Add tests for the RECURSE and NEST options to the elements() method.

- Add Exemel::Query, jQuery/CSS and XPath selectors.
Create: my $xquery = Exemel::Query.new($xml);
Where $xml must be an Exemel::Element object.
Expand Down
23 changes: 22 additions & 1 deletion lib/Exemel.pm
Expand Up @@ -167,12 +167,27 @@ class Exemel::Element does Exemel {
#
# Eg. @items = $form.elements(:TAG<input>, :type<checkbox>);
#
method elements (*%query) {
# There are two other 'special' tags that don't match attributes, but
# set rules for the elements query:
#
# RECURSE If set to a non-zero digit, child elements will also be
# searched for elements matching the queries. By default
# only non-matching elements will be searched (so only the
# top-most matching elements will be returned.)
#
# NEST If set to a positive value, the RECURSE option will apply to
# ALL child elements, including ones that have already matched
# the query and been added to the results.
#
method elements (*%query is copy) {
my @elements;
for @.nodes -> $node {
if $node ~~ Exemel::Element {
my $matched = True;
for %query.kv -> $key, $val {
if $key eq 'RECURSE' { next; } # Skip recurse setting.
if $key eq 'NEST' { next; } # Skip nesting recurse setting.

if $key eq 'TAG' {
if $node.name ne $val { $matched = False; }
}
Expand All @@ -183,6 +198,12 @@ class Exemel::Element does Exemel {
if $matched {
@elements.push: $node;
}
if ( %query<RECURSE> && (%query<NEST> || !$matched ) ) {
my %opts = %query.clone;
%opts<RECURSE> = %query<RECURSE> - 1;
my @subelements = $node.elements(|%opts);
@elements.push: |@subelements;
}
}
}
return @elements;
Expand Down

0 comments on commit f28c7c9

Please sign in to comment.