Skip to content

Commit

Permalink
Merge pull request #237 from Gallaecio/query-input
Browse files Browse the repository at this point in the history
Add a minimal action I/O implementation to improve SearchAction support
  • Loading branch information
kmike authored Nov 8, 2024
2 parents 3bf7546 + 1000c83 commit 9b937ad
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions extruct/w3cmicrodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,17 @@ 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"}}}}}]
14 changes: 14 additions & 0 deletions tests/test_microdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ 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 +74,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 9b937ad

Please sign in to comment.