# HG changeset patch # User paulb # Date 1138148808 0 # Node ID 129565ae4d6a1ea8472861ae36e3569de15e9e2d # Parent 761ef670c5a937c87c8069b669cf5d4e2d620bea [project @ 2006-01-25 00:26:48 by paulb] Changed selector initialisation to discard references which are no longer valid. This helps in cases where selectors are reinitialised in the context of a new document. diff -r 761ef670c5a9 -r 129565ae4d6a XSLForms/Fields.py --- a/XSLForms/Fields.py Fri Jan 20 17:37:33 2006 +0000 +++ b/XSLForms/Fields.py Wed Jan 25 00:26:48 2006 +0000 @@ -213,18 +213,16 @@ if index < 0: break - # NOTE: Controversial creation of potentially non-existent - # NOTE: nodes. + # Where a node cannot be found, do not create a selector. - try: - node = self._enter_element(node, name, index) - except FieldsError, exc: - raise FieldsError, "In field '%s', name '%s' and index '%s' could not be added, since '%s' was found." % ( - field, name, index, exc.args[0]) + node = self._find_element(node, name, index) + if node is None: + break if not selectors.has_key(selector_name): selectors[selector_name] = [] - selectors[selector_name].append(node) + if node is not None: + selectors[selector_name].append(node) def _append_element(self, node, name): @@ -259,6 +257,23 @@ return new_node + def _find_element(self, node, name, index): + + """ + From 'node' find the element with the given 'name' at the + given 'index' position amongst the child elements. Return + None if no such element exists. + """ + + elements = node.xpath("*") + try: + new_node = elements[index] + if new_node.localName != name: + return None + except IndexError: + return None + return new_node + def _get_model_name_and_components(self, field): """ @@ -433,6 +448,7 @@ items = [ ("_action_update", "Some value"), ("_action_delete=/zoo$1/cage$2", "Some value"), + ("_action_nasty=/zoo$1/cage$3", "Some value"), ("/actions$1/update$1/selected", "Some value"), # Not actually used in output documents or input. ("/zoo$1/name", "The Zoo זרו"), ("/zoo$1/cage$1/name", "reptiles"),