Skip to content

Commit

Permalink
Ignore whitespace when checking for content in fields with idrefs.
Browse files Browse the repository at this point in the history
Fixes #50.
  • Loading branch information
Bryan Worrell committed Apr 15, 2015
1 parent 3c926e3 commit bf7ee6e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
28 changes: 28 additions & 0 deletions sdv/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,31 @@ def has_tzinfo(timestamp):
"""
ts = parse_timestamp(timestamp)
return ts and bool(ts.tzinfo)


def strip_whitespace(string):
"""Returns a copy of `string` with all whitespace removed.
"""
if string is None:
return None

return ''.join(string.split())


def has_content(node):
"""Returns ``True`` if the `node` has children or text nodes.
Note:
This will ignore whitespace and XML comments.
"""
if node is None:
return False

if len(node.findall('*')) > 0:
return True

stripped = strip_whitespace(node.text)
return bool(stripped)

5 changes: 2 additions & 3 deletions sdv/validators/stix/best_practice.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ def _check_idref_with_content(self, root, namespaces, version): # noqa
def is_invalid(node):
if common.is_idref_content_exception(node):
return False
return bool(node.text) or len(node.findall('*')) > 0

return utils.has_content(node)

nodes = root.xpath("//*[@idref]")
warnings = [BestPracticeWarning(x) for x in nodes if is_invalid(x)]
Expand Down Expand Up @@ -575,8 +576,6 @@ def _check_timestamp_usage(self, root, namespaces, **kwargs): # noqa
warning['timestamp'] = timestamp
results.append(warning)

warning = None # overwritten below

if id_ and not timestamp:
warning = BestPracticeWarning(
node=node,
Expand Down

0 comments on commit bf7ee6e

Please sign in to comment.