Skip to content

Commit a31daaa

Browse files
authored
Skip empty JSON-LD scripts by trimming and skipping empty input (#240)
1 parent 2fd175e commit a31daaa

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

extruct/jsonld.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ def extract_items(self, document, base_url=None):
3333
]
3434

3535
def _extract_items(self, node):
36-
script = node.xpath("string()")
36+
script = node.xpath("string()").strip()
37+
if not script:
38+
return
3739
try:
3840
# TODO: `strict=False` can be configurable if needed
3941
data = json.loads(script, strict=False)

tests/test_jsonld.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,9 @@ def test_null(self):
6363
jsonlde = JsonLdExtractor()
6464
data = jsonlde.extract(body)
6565
self.assertEqual(data, expected)
66+
67+
def test_empty_jsonld_script(self):
68+
jsonlde = JsonLdExtractor()
69+
body = '<script type="application/ld+json"> \n\n </script>'
70+
data = jsonlde.extract(body)
71+
self.assertEqual(data, [])

0 commit comments

Comments
 (0)