Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(283)

Unified Diff: grit/node/misc.py

Issue 11155024: If-then-else support for GRIT (Closed) Base URL: https://grit-i18n.googlecode.com/svn/trunk
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « grit/node/mapping.py ('k') | grit/node/misc_unittest.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: grit/node/misc.py
diff --git a/grit/node/misc.py b/grit/node/misc.py
index d13f67d6b3338c9290457422650755f41637dd01..eb5a868392a9ac099148646d3e7a08ffef22ae18 100755
--- a/grit/node/misc.py
+++ b/grit/node/misc.py
@@ -86,10 +86,34 @@ class IfNode(SplicingNode):
def MandatoryAttributes(self):
return ['expr']
+ def _IsValidChild(self, child):
+ return (isinstance(child, (ThenNode, ElseNode)) or
+ super(IfNode, self)._IsValidChild(child))
+
+ def EndParsing(self):
+ children = self.children
+ if any(isinstance(node, (ThenNode, ElseNode)) for node in children):
+ if (len(children) != 2 or not isinstance(children[0], ThenNode) or
+ not isinstance(children[1], ElseNode)):
+ raise exception.UnexpectedChild(
+ '<if> element must be <if><then>...</then><else>...</else></if>')
+
def ActiveChildren(self):
- if not self.EvaluateCondition(self.attrs['expr']):
- return []
- return super(IfNode, self).ActiveChildren()
+ cond = self.EvaluateCondition(self.attrs['expr'])
+ if len(self.children) == 2 and isinstance(self.children[0], ThenNode):
Jói 2012/10/16 11:21:11 This is slightly hard to follow; I think a comment
+ return self.children[0 if cond else 1].ActiveChildren()
+ else:
+ return super(IfNode, self).ActiveChildren() if cond else []
+
+
+class ThenNode(SplicingNode):
+ """A <then> node. Can only appear directly inside an <if> node."""
+ pass
+
+
+class ElseNode(SplicingNode):
+ """An <else> node. Can only appear directly inside an <if> node."""
+ pass
class PartNode(SplicingNode):
« no previous file with comments | « grit/node/mapping.py ('k') | grit/node/misc_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698