Skip to content

Commit 98aafc7

Browse files
Support default property values
1 parent 3f7de9e commit 98aafc7

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

meta/generate_tag_defs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def generate_tag_class(output: TextIO, tag: TagInfo):
4343
prop_unions_gen = []
4444
for prop in tag.properties:
4545
# Yucky hard-coded spaces, I can't be bothered to fix this
46-
prop_args_gen.append(f" {prop.name}: Any = None,")
46+
prop_args_gen.append(f" {prop.name}: Any = {prop.default!r},")
4747
prop_unions_gen.append(f" '{prop.name}': {prop.name},")
4848

4949
prop_args = '\n'.join(prop_args_gen).strip()

meta/scrape_tags.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,23 @@ def domXrefReplace(lookup: str, presentation: Optional[str] = None) -> str:
9898
"""Type definition for info grabbed from MDN docs"""
9999

100100

101+
class PropYmlItem(TypedDict):
102+
"""
103+
Properties of a tag, defined in tags.yml
104+
"""
105+
106+
doc: str
107+
"""Documentation for the property"""
108+
109+
default: NotRequired[str]
110+
"""Default value of the property"""
111+
112+
101113
class TagsYmlItem(TypedDict):
102114
"""
103115
A tag which has suggested keys
104116
"""
105-
properties: NotRequired[dict[str, str]]
117+
properties: NotRequired[dict[str, str | PropYmlItem]]
106118
"""Mapping of properties used by the tag (name: description)"""
107119

108120
base: NotRequired[str]
@@ -127,9 +139,14 @@ class Prop:
127139
Name of the property
128140
"""
129141

130-
description: Optional[str]
142+
doc: Optional[str]
131143
"""
132-
Description of the property if applicable
144+
Documentation of the property if applicable
145+
"""
146+
147+
default: Optional[str]
148+
"""
149+
Default value for the property
133150
"""
134151

135152

@@ -347,8 +364,14 @@ def prop_entries_to_object(
347364
return []
348365

349366
props = []
350-
for name, description in tag_data['properties'].items():
351-
props.append(Prop(name, description))
367+
for name, value in tag_data['properties'].items():
368+
if isinstance(value, str):
369+
doc: Optional[str] = value
370+
default: Optional[str] = None
371+
else:
372+
doc = value.get("doc")
373+
default = value.get("default")
374+
props.append(Prop(name, doc, default))
352375
return props
353376

354377

meta/tags.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ a:
2323
href: URL of page to link to
2424
target: Use "_blank" to open in a new tab
2525

26+
script:
27+
properties:
28+
type:
29+
doc: Type of script to use
30+
default: text/javascript
31+
2632
p:
2733
base: StylableTag
2834

pyhtml/__tags/generated.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3340,7 +3340,7 @@ class script(Tag):
33403340
def __init__(
33413341
self,
33423342
*children: Any,
3343-
3343+
type: Any = 'text/javascript',
33443344
**properties: Any,
33453345
) -> None:
33463346
"""
@@ -3349,14 +3349,14 @@ def __init__(
33493349
[View full documentation](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script)
33503350
"""
33513351
properties |= {
3352-
3352+
'type': type,
33533353
}
33543354
super().__init__(*children, **properties)
33553355

33563356
def __call__(
33573357
self,
33583358
*children: Any,
3359-
3359+
type: Any = 'text/javascript',
33603360
**properties: Any,
33613361
):
33623362
"""
@@ -3365,7 +3365,7 @@ def __call__(
33653365
[View full documentation](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script)
33663366
"""
33673367
properties |= {
3368-
3368+
'type': type,
33693369
}
33703370
return super().__call__(*children, **properties)
33713371

tests/basic_rendering_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ def test_larger_page():
103103
' <title>',
104104
' Hello, world!',
105105
' </title>',
106-
' <script src="http://example.com/script.js"></script>',
106+
' <script src="http://example.com/script.js" '
107+
'type="text/javascript"></script>',
107108
' </head>',
108109
' <body>',
109110
' <h1>',

0 commit comments

Comments
 (0)