Chromium Code Reviews| 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): |