Permalink
Browse files

Restricted root element parsing. Added and changed some tests.

  • Loading branch information...
1 parent 60a74e5 commit b2f414c60fce2e7bc55c30bc2cf4ad2454595276 @supernovus committed Oct 9, 2010
Showing with 38 additions and 6 deletions.
  1. +5 −0 README
  2. +8 −3 lib/Flower.pm
  3. +23 −1 t/01-basics.t
  4. +2 −2 t/06-utils-text.t
View
5 README
@@ -50,6 +50,11 @@ Status: Flower is currently usable, but there are still plenty of planned
'lt xx' the number is less than xx.
'eq xx' the number is equal to xx.
'ne xx' the number is not equal to xx.
+ * I'm not sure how Petal, PHPTAL or Zope deal with TAL attributes in the
+ root element (the top-most element of the document), but to avoid death,
+ destruction and mayhem, Flower only processes 'define', 'attributes'
+ and 'content' tags on the root element. The rest of the tags are only
+ processed on children of the root.
The above list will be updated as this project is developed, as I'm sure
other changes will be introduced that will be a gotchya for users of Petal,
View
@@ -13,8 +13,13 @@ has $!tal-ns = 'http://xml.zope.org/namespaces/tal';
has $!metal-ns = 'http://xml.zope.org/namespaces/metal';
has $!i18n-ns = 'http://xml.zope.org/namespaces/i18n';
+## a restricted set of tags for the root element.
+has @!root-tal-tags = 'define', 'attributes', 'content';
+
+## the normal set of TAL tags for every other element.
has @!tal-tags = 'define', 'condition', 'repeat', 'attributes', 'content', 'replace', 'omit-tag';
+## the tags for METAL processing, not yet implemented.
has @!metal-tags = 'define-macro', 'use-macro', 'define-slot', 'fill-slot';
## Override find with a subroutine that can find templates based off
@@ -120,7 +125,7 @@ method parse (*%data) {
}
## Okay, now let's parse the elements.
- self!parse-element($.template.root);
+ self!parse-element($.template.root, @!root-tal-tags);
return ~$.template;
}
@@ -153,8 +158,8 @@ method !parse-elements ($xml is rw) {
}
}
-method !parse-element($element is rw) {
- for @!tal-tags -> $tal {
+method !parse-element($element is rw, @tal-tags = @!tal-tags) {
+ for @tal-tags -> $tal {
my $tag = $!tal~':'~$tal;
self!parse-tag($element, $tag, $tal);
if $element !~~ Exemel::Element { last; } # skip if we changed type.
View
@@ -5,31 +5,43 @@ BEGIN { @*INC.unshift: './lib' }
use Test;
use Flower;
-plan 8;
+plan 9;
my $xml = '<?xml version="1.0"?>';
+## test 1
+
my $template = '<test><item tal:define="test my_test_var" tal:content="test"/></test>';
my $flower = Flower.new(:template($template));
is $flower.parse(my_test_var => 'Hello World'), $xml~'<test><item>Hello World</item></test>', 'tal:define and tal:content';
+## test 2
+
$template = '<test><item tal:define="test1 my_test_var1; test2 my_test_var2" tal:content="string:${test1} = ${test2}"/></test>';
$flower.=another(:template($template));
is $flower.parse(my_test_var1 => 'Hello', my_test_var2 => 'World'), $xml~'<test><item>Hello = World</item></test>', 'tal:define with multiple definitions.';
+## test 3
+
$template = '<test><replaced tal:replace="hello">This will be replaced</replaced></test>';
$flower.=another(:template($template));
is $flower.parse(hello => 'Hello World'), $xml~'<test>Hello World</test>', 'tal:replace';
+## test 4
+
$template = '<test><true tal:condition="hello">This is true</true><false tal:condition="notreal">This is false</false><right tal:condition="true:hello"/><wrong tal:condition="false:hello"/></test>';
$flower.=another(:template($template));
is $flower.parse(hello => 'Hello World'), $xml~'<test><true>This is true</true><right/></test>', 'tal:condition';
+## test 5
+
$template = '<test><div tal:omit-tag="">Good <b tal:omit-tag="hello">Day</b> Mate</div></test>';
#$template = '<test><div tal:omit-tag="string:1">Good Day Mate</div></test>';
$flower.=another(:template($template));
is $flower.parse(hello => 'hello world'), $xml~'<test>Good Day Mate</test>', 'tal:omit-tag';
+## test 6
+
$template = '<test><attrib tal:attributes="hello hello; cya goodbye"/></test>';
$flower.=another(:template($template));
my $matched = False;
@@ -38,11 +50,21 @@ if $output eq $xml~'<test><attrib hello="Hello World" cya="Goodbye Universe"/></
elsif $output eq $xml~'<test><attrib cya="Goodbye Universe" hello="Hello World"/></test>' { $matched = True; }
ok $matched, 'tal:attributes';
+## test 7
+
$template = '<test><hello/><goodbye tal:replace=""/></test>';
$flower.=another(:template($template));
is $flower.parse(), $xml~'<test><hello/></test>', 'tal:replace when empty.';
+## test 8
+
$template = '<test xmlns:petal="http://xml.zope.org/namespaces/tal"><div petal:replace="test"/></test>';
$flower.=another(:template($template));
is $flower.parse(test=>'Hello World'), $xml~'<test xmlns:petal="http://xml.zope.org/namespaces/tal">Hello World</test>', 'tal:replace with custom namespace.';
+## test 9
+
+$template = '<test tal:attributes="id id">Test document</test>';
+$flower.=another(:template($template));
+is $flower.parse(id=>'first'), $xml~'<test id="first">Test document</test>', 'attributes on root document';
+
View
@@ -20,15 +20,15 @@ is $flower.parse(name => 'Flower'), $xml~'<test><upper>A TEST OF FLOWER, IN UPPE
## test 2
-$template = '<test><lower tal:content="lc:string:I AM NOT YELLING"/></test>';
+$template = '<test><lower tal:content="lc:default">I AM NOT YELLING</lower></test>';
$flower.=another(:template($template));
is $flower.parse(), $xml~'<test><lower>i am not yelling</lower></test>', 'lc: modifier';
## test 3
-$template = '<test><ucfirst tal:replace="ucfirst:string:bob"/></test>';
+$template = '<test><ucfirst tal:replace="ucfirst:\'bob\'"/></test>';
$flower.=another(:template($template));

0 comments on commit b2f414c

Please sign in to comment.