Skip to content

Commit

Permalink
typing for head elements
Browse files Browse the repository at this point in the history
  • Loading branch information
xoudini committed Jul 1, 2024
1 parent 3cca2c6 commit 2797efb
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 21 deletions.
5 changes: 4 additions & 1 deletion mjml/elements/head/_head_base.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import typing as t

from mjml.core import Component, initComponent


__all__ = ['HeadComponent']


class HeadComponent(Component):
# TODO typing: figure out proper type annotations
def handlerChildren(self):
def handle_children(children):
def handle_children(children: t.Dict[str, t.Any]) -> t.Optional[str]:
tagName = children['tagName']
component = initComponent(
name = tagName,
Expand Down
12 changes: 9 additions & 3 deletions mjml/elements/head/mj_attributes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import typing as t

import typing_extensions as te

from mjml.helpers import omit

Expand All @@ -6,12 +9,15 @@

__all__ = ['MjAttributes']


class MjAttributes(HeadComponent):
component_name = 'mj-attributes'
component_name: t.ClassVar[str] = 'mj-attributes'

def handler(self):
@te.override
def handler(self) -> None:
add = self.context['add']
_children = self.props.get("children")
if (_children := self.props.get("children")) is None:
return None

for child in _children:
tagName = child['tagName']
Expand Down
11 changes: 8 additions & 3 deletions mjml/elements/head/mj_breakpoint.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import typing as t

import typing_extensions as te

from ._head_base import HeadComponent

Expand All @@ -6,14 +9,16 @@


class MjBreakpoint(HeadComponent):
component_name = 'mj-breakpoint'
component_name: t.ClassVar[str] = 'mj-breakpoint'

@te.override
@classmethod
def allowed_attrs(cls):
def allowed_attrs(cls) -> t.Dict[str, str]:
return {
'width': 'unit(px)',
}

def handler(self):
@te.override
def handler(self) -> None:
add = self.context['add']
add('breakpoint', self.getAttribute('width'))
11 changes: 8 additions & 3 deletions mjml/elements/head/mj_font.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import typing as t

import typing_extensions as te

from ._head_base import HeadComponent

Expand All @@ -6,15 +9,17 @@


class MjFont(HeadComponent):
component_name = 'mj-font'
component_name: t.ClassVar[str] = 'mj-font'

@te.override
@classmethod
def allowed_attrs(cls):
def allowed_attrs(cls) -> t.Dict[str, str]:
return {
'href' : 'string',
'name' : 'string',
}

def handler(self):
@te.override
def handler(self) -> None:
add = self.context['add']
add('fonts', self.getAttribute('name'), self.getAttribute('href'))
9 changes: 7 additions & 2 deletions mjml/elements/head/mj_head.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import typing as t

import typing_extensions as te

from ._head_base import HeadComponent


__all__ = ['MjHead']

class MjHead(HeadComponent):
component_name = 'mj-head'
component_name: t.ClassVar[str] = 'mj-head'

def handler(self):
@te.override
def handler(self) -> t.Optional[str]:
# TODO typing: fix
return self.handlerChildren()
8 changes: 6 additions & 2 deletions mjml/elements/head/mj_html_attributes.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import typing as t

import typing_extensions as te

from ._head_base import HeadComponent


__all__ = ['MjHtmlAttributes']

class MjHtmlAttributes(HeadComponent):
component_name = 'mj-html-attributes'
component_name: t.ClassVar[str] = 'mj-html-attributes'

def handler(self):
@te.override
def handler(self) -> None:
add = self.context['add']
_children = self.props.get("children")

Expand Down
8 changes: 6 additions & 2 deletions mjml/elements/head/mj_preview.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import typing as t

import typing_extensions as te

from ._head_base import HeadComponent

Expand All @@ -6,8 +9,9 @@


class MjPreview(HeadComponent):
component_name = 'mj-preview'
component_name: t.ClassVar[str] = 'mj-preview'

def handler(self):
@te.override
def handler(self) -> None:
add = self.context['add']
add('preview', self.getContent())
11 changes: 8 additions & 3 deletions mjml/elements/head/mj_style.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import typing as t

import typing_extensions as te

from ._head_base import HeadComponent


__all__ = ['MjStyle']

class MjStyle(HeadComponent):
component_name = 'mj-style'
component_name: t.ClassVar[str] = 'mj-style'

@te.override
@classmethod
def default_attrs(cls):
def default_attrs(cls) -> t.Dict[str, str]:
return {
'inline': '',
}

def handler(self):
@te.override
def handler(self) -> None:
add = self.context['add']
inline_attr = 'inlineStyle' if (self.get_attr('inline') == 'inline') else 'style'
html_str = self.getContent()
Expand Down
8 changes: 6 additions & 2 deletions mjml/elements/head/mj_title.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import typing as t

import typing_extensions as te

from ._head_base import HeadComponent


__all__ = ['MjTitle']

class MjTitle(HeadComponent):
component_name = 'mj-title'
component_name: t.ClassVar[str] = 'mj-title'

def handler(self):
@te.override
def handler(self) -> None:
add = self.context['add']
add('title', self.getContent())

0 comments on commit 2797efb

Please sign in to comment.