Skip to content

Commit

Permalink
Add defaults for Element() attrs/children.
Browse files Browse the repository at this point in the history
This makes it less verbose to create Elements manually.
  • Loading branch information
pelme committed Aug 12, 2024
1 parent 1f0d597 commit 2845c54
Showing 1 changed file with 119 additions and 117 deletions.
236 changes: 119 additions & 117 deletions htpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def _get_element(name: str) -> Element:
raise AttributeError(
f"{name} is not a valid element name. html elements must have all lowercase names"
)
return Element(name.replace("_", "-"), {}, None)
return Element(name.replace("_", "-"))


def __getattr__(name: str) -> Element:
Expand All @@ -143,9 +143,11 @@ def __getattr__(name: str) -> Element:
class BaseElement:
__slots__ = ("_name", "_attrs", "_children")

def __init__(self, name: str, attrs: dict[str, Attribute], children: Node) -> None:
def __init__(
self, name: str, attrs: dict[str, Attribute] | None = None, children: Node = None
) -> None:
self._name = name
self._attrs = attrs
self._attrs = attrs or {}
self._children = children

def __str__(self) -> _Markup:
Expand Down Expand Up @@ -240,124 +242,124 @@ def __html__(self) -> str: ...
Attribute: TypeAlias = None | bool | str | _HasHtml | _ClassNames

# https://developer.mozilla.org/en-US/docs/Glossary/Doctype
html = HTMLElement("html", {}, None)
html = HTMLElement("html")

# https://developer.mozilla.org/en-US/docs/Glossary/Void_element
area = VoidElement("area", {}, None)
base = VoidElement("base", {}, None)
br = VoidElement("br", {}, None)
col = VoidElement("col", {}, None)
embed = VoidElement("embed", {}, None)
hr = VoidElement("hr", {}, None)
img = VoidElement("img", {}, None)
input = VoidElement("input", {}, None)
link = VoidElement("link", {}, None)
meta = VoidElement("meta", {}, None)
param = VoidElement("param", {}, None)
source = VoidElement("source", {}, None)
track = VoidElement("track", {}, None)
wbr = VoidElement("wbr", {}, None)
area = VoidElement("area")
base = VoidElement("base")
br = VoidElement("br")
col = VoidElement("col")
embed = VoidElement("embed")
hr = VoidElement("hr")
img = VoidElement("img")
input = VoidElement("input")
link = VoidElement("link")
meta = VoidElement("meta")
param = VoidElement("param")
source = VoidElement("source")
track = VoidElement("track")
wbr = VoidElement("wbr")

# Non-deprecated HTML elements, extracted from
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element
# Located via the inspector with:
# Array.from($0.querySelectorAll('li')).filter(x=>!x.querySelector('.icon-deprecated')).map(x => x.querySelector('code').textContent) # noqa: E501
a = Element("a", {}, None)
abbr = Element("abbr", {}, None)
abc = Element("abc", {}, None)
address = Element("address", {}, None)
article = Element("article", {}, None)
aside = Element("aside", {}, None)
audio = Element("audio", {}, None)
b = Element("b", {}, None)
bdi = Element("bdi", {}, None)
bdo = Element("bdo", {}, None)
blockquote = Element("blockquote", {}, None)
body = Element("body", {}, None)
button = Element("button", {}, None)
canvas = Element("canvas", {}, None)
caption = Element("caption", {}, None)
cite = Element("cite", {}, None)
code = Element("code", {}, None)
colgroup = Element("colgroup", {}, None)
data = Element("data", {}, None)
datalist = Element("datalist", {}, None)
dd = Element("dd", {}, None)
del_ = Element("del_", {}, None)
details = Element("details", {}, None)
dfn = Element("dfn", {}, None)
dialog = Element("dialog", {}, None)
div = Element("div", {}, None)
dl = Element("dl", {}, None)
dt = Element("dt", {}, None)
em = Element("em", {}, None)
fieldset = Element("fieldset", {}, None)
figcaption = Element("figcaption", {}, None)
figure = Element("figure", {}, None)
footer = Element("footer", {}, None)
form = Element("form", {}, None)
h1 = Element("h1", {}, None)
h2 = Element("h2", {}, None)
h3 = Element("h3", {}, None)
h4 = Element("h4", {}, None)
h5 = Element("h5", {}, None)
h6 = Element("h6", {}, None)
head = Element("head", {}, None)
header = Element("header", {}, None)
hgroup = Element("hgroup", {}, None)
i = Element("i", {}, None)
iframe = Element("iframe", {}, None)
ins = Element("ins", {}, None)
kbd = Element("kbd", {}, None)
label = Element("label", {}, None)
legend = Element("legend", {}, None)
li = Element("li", {}, None)
main = Element("main", {}, None)
map = Element("map", {}, None)
mark = Element("mark", {}, None)
menu = Element("menu", {}, None)
meter = Element("meter", {}, None)
nav = Element("nav", {}, None)
noscript = Element("noscript", {}, None)
object = Element("object", {}, None)
ol = Element("ol", {}, None)
optgroup = Element("optgroup", {}, None)
option = Element("option", {}, None)
output = Element("output", {}, None)
p = Element("p", {}, None)
picture = Element("picture", {}, None)
portal = Element("portal", {}, None)
pre = Element("pre", {}, None)
progress = Element("progress", {}, None)
q = Element("q", {}, None)
rp = Element("rp", {}, None)
rt = Element("rt", {}, None)
ruby = Element("ruby", {}, None)
s = Element("s", {}, None)
samp = Element("samp", {}, None)
script = Element("script", {}, None)
search = Element("search", {}, None)
section = Element("section", {}, None)
select = Element("select", {}, None)
slot = Element("slot", {}, None)
small = Element("small", {}, None)
span = Element("span", {}, None)
strong = Element("strong", {}, None)
style = Element("style", {}, None)
sub = Element("sub", {}, None)
summary = Element("summary", {}, None)
sup = Element("sup", {}, None)
table = Element("table", {}, None)
tbody = Element("tbody", {}, None)
td = Element("td", {}, None)
template = Element("template", {}, None)
textarea = Element("textarea", {}, None)
tfoot = Element("tfoot", {}, None)
th = Element("th", {}, None)
thead = Element("thead", {}, None)
time = Element("time", {}, None)
title = Element("title", {}, None)
tr = Element("tr", {}, None)
u = Element("u", {}, None)
ul = Element("ul", {}, None)
var = Element("var", {}, None)
a = Element("a")
abbr = Element("abbr")
abc = Element("abc")
address = Element("address")
article = Element("article")
aside = Element("aside")
audio = Element("audio")
b = Element("b")
bdi = Element("bdi")
bdo = Element("bdo")
blockquote = Element("blockquote")
body = Element("body")
button = Element("button")
canvas = Element("canvas")
caption = Element("caption")
cite = Element("cite")
code = Element("code")
colgroup = Element("colgroup")
data = Element("data")
datalist = Element("datalist")
dd = Element("dd")
del_ = Element("del_")
details = Element("details")
dfn = Element("dfn")
dialog = Element("dialog")
div = Element("div")
dl = Element("dl")
dt = Element("dt")
em = Element("em")
fieldset = Element("fieldset")
figcaption = Element("figcaption")
figure = Element("figure")
footer = Element("footer")
form = Element("form")
h1 = Element("h1")
h2 = Element("h2")
h3 = Element("h3")
h4 = Element("h4")
h5 = Element("h5")
h6 = Element("h6")
head = Element("head")
header = Element("header")
hgroup = Element("hgroup")
i = Element("i")
iframe = Element("iframe")
ins = Element("ins")
kbd = Element("kbd")
label = Element("label")
legend = Element("legend")
li = Element("li")
main = Element("main")
map = Element("map")
mark = Element("mark")
menu = Element("menu")
meter = Element("meter")
nav = Element("nav")
noscript = Element("noscript")
object = Element("object")
ol = Element("ol")
optgroup = Element("optgroup")
option = Element("option")
output = Element("output")
p = Element("p")
picture = Element("picture")
portal = Element("portal")
pre = Element("pre")
progress = Element("progress")
q = Element("q")
rp = Element("rp")
rt = Element("rt")
ruby = Element("ruby")
s = Element("s")
samp = Element("samp")
script = Element("script")
search = Element("search")
section = Element("section")
select = Element("select")
slot = Element("slot")
small = Element("small")
span = Element("span")
strong = Element("strong")
style = Element("style")
sub = Element("sub")
summary = Element("summary")
sup = Element("sup")
table = Element("table")
tbody = Element("tbody")
td = Element("td")
template = Element("template")
textarea = Element("textarea")
tfoot = Element("tfoot")
th = Element("th")
thead = Element("thead")
time = Element("time")
title = Element("title")
tr = Element("tr")
u = Element("u")
ul = Element("ul")
var = Element("var")

0 comments on commit 2845c54

Please sign in to comment.