Skip to content

Commit

Permalink
Add a minimal action I/O implementation to improve SearchAction support
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallaecio committed Jul 25, 2024
1 parent 3bf7546 commit 2c8a3f3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions extruct/w3cmicrodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@ def _extract_property_value(self, node, items_seen, base_url, itemids, force=Fal
elif node.get("content"):
return node.get("content")

# https://schema.org/docs/actions.html#part-4
elif (
(itemprop := node.get("itemprop"))
and (itemprop.endswith("-input") or itemprop.endswith("-output"))
):
result = {}
if "required" in node.attrib:
result["valueRequired"] = True
if name := node.get("name"):
result["valueName"] = name
return result

else:
return self._extract_textContent(node)

Expand Down
8 changes: 8 additions & 0 deletions tests/samples/schema.org/SearchAction.001.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div itemscope itemtype="https://schema.org/WebSite">
<meta itemprop="url" content="https://www.example.com/"/>
<form itemprop="potentialAction" itemscope itemtype="https://schema.org/SearchAction">
<meta itemprop="target" content="https://query.example.com/search?q={search_term_string}"/>
<input itemprop="query-input" type="text" name="search_term_string" required/>
<input type="submit"/>
</form>
</div>
1 change: 1 addition & 0 deletions tests/samples/schema.org/SearchAction.001.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"type": "https://schema.org/WebSite", "properties": {"url": "https://www.example.com/", "potentialAction": {"type": "https://schema.org/SearchAction", "properties": {"target": "https://query.example.com/search?q={search_term_string}", "query-input": {"valueRequired": true, "valueName": "search_term_string"}}}}}]
15 changes: 15 additions & 0 deletions tests/test_microdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ class TestMicrodata(unittest.TestCase):

maxDiff = None

def _test_schemaorg(self, schema, indexes=None):
indexes = indexes or [1]
for i in indexes:
body = get_testdata("schema.org", f"{schema}.{i:03d}.html")
expected = json.loads(
get_testdata("schema.org", f"{schema}.{i:03d}.json").decode()
)
mde = MicrodataExtractor()
data = mde.extract(body)
self.assertEqual(data, expected)


def test_schemaorg_CreativeWork(self):
for i in [1]:
body = get_testdata("schema.org", "CreativeWork.{:03d}.html".format(i))
Expand Down Expand Up @@ -63,6 +75,9 @@ def test_schemaorg_Event(self):

self.assertEqual(data, expected)

def test_schemaorg_SearchAction(self):
self._test_schemaorg("SearchAction")

def test_w3c_textContent_values(self):
body = get_testdata("w3c", "microdata.4.2.strings.html")
expected = json.loads(
Expand Down

0 comments on commit 2c8a3f3

Please sign in to comment.