Libdoc: Fix default value escaping with internal variables
See
@@ -21,7 +21,7 @@ from robot.errors import DataError
from robot.running import (TestLibrary, UserLibrary, UserErrorHandler,
ResourceFileBuilder)
from robot.utils import split_tags_from_doc, unescape, is_string
-from robot.variables import VariableIterator, contains_variable
+from robot.variables import VariableIterator, search_variable
from .model import LibraryDoc, KeywordDoc
@@ -107,7 +107,7 @@ class KeywordDocBuilder(object):
def build_keyword(self, kw):
doc, tags = self._get_doc_and_tags(kw)
if not self._resource:
- self._escape_strings_in_defaults(kw)
+ self._escape_strings_in_defaults(kw.arguments.defaults)
return KeywordDoc(name=kw.name,
args=kw.arguments,
doc=doc,
@@ -115,22 +115,23 @@ class KeywordDocBuilder(object):
source=kw.source,
lineno=kw.lineno)
- def _escape_strings_in_defaults(self, kw):
- for name, default in kw.arguments.defaults.items():
- if is_string(default):
- default = re.sub(r'[\\\r\n\t]', lambda x: repr(str(x.group()))[1:-1], default)
- default = self._escape_variables(default)
- kw.arguments.defaults[name] = re.sub('^(?= )|(?<= )$|(?<= )(?= )', r'\\', default)
-
- def _escape_variables(self, default):
- if contains_variable(default, '$&@%'):
- escaped_var = ''
- for before, variable, remaining in VariableIterator(default, ignore_errors=True):
- if contains_variable(remaining, '$&@%'):
- remaining = ''
- escaped_var = '%s%s\\%s%s' % (escaped_var, before, variable, remaining)
- default = escaped_var
- return default
+ def _escape_strings_in_defaults(self, defaults):
+ for name, value in defaults.items():
+ if is_string(value):
+ value = re.sub(r'[\\\r\n\t]', lambda x: repr(str(x.group()))[1:-1], value)
+ value = self._escape_variables(value)
+ defaults[name] = re.sub('^(?= )|(?<= )$|(?<= )(?= )', r'\\', value)
+
+ def _escape_variables(self, value):
+ result = ''
+ match = search_variable(value)
+ while match:
+ result += r'%s\%s{%s}' % (match.before, match.identifier,
+ self._escape_variables(match.base))
+ for item in match.items:
+ result += '[%s]' % self._escape_variables(item)
+ match = search_variable(match.after)
+ return result + match.string
def _get_doc_and_tags(self, kw):
doc = self._get_doc(kw)
