Permalink
Browse files

Changed how 'default' works, and root tag processed too.

 * My original method of 'default' was lame and totally broken.
   The new version actually works properly.
 * Due to changes above, there's also a new array called @.elements, of which
   the first item is always the element currently being processed.
 * Prior to this release, the root tag of the document was not processed
   for TAL attributes. Now it is. May not be common, but useful nonetheless.
 * Oh, and re-organized the tests yet again. Now they are grouped into
   categories, each category starting with a certain number.
  • Loading branch information...
1 parent 6da6654 commit 60a74e51d305335f970f5352a4b630bf82e89802 @supernovus committed Oct 9, 2010
View
@@ -27,6 +27,10 @@ has %!modifiers;
## Data, is used to store the replacement data. Is available for modifiers.
has %.data is rw;
+## Default stores the elements in order of parsing.
+## Used to get the 'default' value, and other such stuff.
+has @.elements;
+
## Internal class, used for the 'repeat' object.
class Flower::Repeat {
has $.index;
@@ -116,7 +120,7 @@ method parse (*%data) {
}
## Okay, now let's parse the elements.
- self!parse-elements($.template.root);
+ self!parse-element($.template.root);
return ~$.template;
}
@@ -133,8 +137,9 @@ method !parse-elements ($xml is rw) {
if $i == $xml.nodes.elems { last; }
my $element = $xml.nodes[$i];
if $element !~~ Exemel::Element { next; } # skip non-elements.
- %.data<default> = $element.nodes; # the 'default' keyword.
+ @.elements.unshift: $element; ## Stuff the newest element into place.
self!parse-element($element);
+ @.elements.shift; ## and remove it again.
## Now we clean up removed elements, and insert replacements.
if ! defined $element {
$xml.nodes.splice($i--, 1);
@@ -195,10 +200,13 @@ method !parse-condition ($xml is rw, $tag) {
}
method !parse-content ($xml is rw, $tag) {
- $xml.nodes.splice;
my $node = self.query($xml.attribs{$tag}, :forcexml);
if defined $node {
- $xml.nodes.push: $node;
+ if $node === $xml.nodes {} # special case for 'default'.
+ else {
+ $xml.nodes.splice;
+ $xml.nodes.push: $node;
+ }
}
$xml.unset: $tag;
}
@@ -275,6 +283,10 @@ method query ($query is copy, :$noxml, :$forcexml, :$bool, :$noescape is copy) {
if ($bool) { return False; }
else { return ''; }
}
+ if $query eq 'default' {
+ my $default = @.elements[0].nodes;
+ return $default;
+ }
if $query ~~ /^ structure \s+ / {
$query.=subst(/^ structure \s+ /, '');
$noescape = True;
View
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl6
+
+BEGIN { @*INC.unshift: './lib' }
+
+use Test;
+use Flower;
+
+plan 2;
+
+my $xml = '<?xml version="1.0"?>';
+
+## test 1
+
+my $template = '<test><i tal:content="default">The default text</i></test>';
+my $flower = Flower.new(:template($template));
+
+is $flower.parse(), $xml~'<test><i>The default text</i></test>', 'tal:content with default';
+
+## test 2
+
+$template = '<test><i tal:replace="default">The default text</i></test>';
+$flower.=another(:template($template));
+
+is $flower.parse(), $xml~'<test>The default text</test>', 'tal:replace with default';
+
+
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 60a74e5

Please sign in to comment.