Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use single quotes for attriabutes to support hx-vals json data #4

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
uses: actions/cache@v1
with:
path: .venv
key: venv-${{ matrix.python-version }}-${{ hashFiles('poetry.lock') }}
key: cached-venv-${{ matrix.python-version }}-${{ hashFiles('poetry.lock') }}
- name: Install dependencies
run: |
poetry install
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@

## Latest Changes


### Breaking

* rename base_models.py -> models.py
* Use single quotes for attribute values.

### Feature

* Using single quotes for attributes lets us support `hx-vals`, since it expects a doubled quoted json string.

## Version 1.0.0 Hypermedia released

Hypermedia is an opinionated way to work with HTML in python and FastAPI. Hypermedia is designed to work with htmx.
Expand Down
2 changes: 1 addition & 1 deletion hypermedia/audio_and_video.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hypermedia.base_models import BaseElement, VoidElement
from hypermedia.models import BaseElement, VoidElement


class Audio(BaseElement):
Expand Down
2 changes: 1 addition & 1 deletion hypermedia/basic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hypermedia.base_models import BaseElement, Element, VoidElement
from hypermedia.models import BaseElement, Element, VoidElement

"""
All basic html tags as defined by W3Schools.
Expand Down
2 changes: 1 addition & 1 deletion hypermedia/formatting.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hypermedia.base_models import BaseElement, VoidElement
from hypermedia.models import BaseElement, VoidElement


class Abbr(BaseElement):
Expand Down
2 changes: 1 addition & 1 deletion hypermedia/forms_and_input.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hypermedia.base_models import BaseElement, VoidElement
from hypermedia.models import BaseElement, VoidElement


class Form(BaseElement):
Expand Down
2 changes: 1 addition & 1 deletion hypermedia/frames.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hypermedia.base_models import BaseElement
from hypermedia.models import BaseElement


class IFrame(BaseElement):
Expand Down
2 changes: 1 addition & 1 deletion hypermedia/htmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
TypeVar,
)

from hypermedia.base_models import Element
from hypermedia.models import Element

Param = ParamSpec("Param")
ReturnType = TypeVar("ReturnType")
Expand Down
2 changes: 1 addition & 1 deletion hypermedia/images.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hypermedia.base_models import BaseElement, VoidElement
from hypermedia.models import BaseElement, VoidElement


class Img(VoidElement):
Expand Down
2 changes: 1 addition & 1 deletion hypermedia/links.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hypermedia.base_models import BaseElement, VoidElement
from hypermedia.models import BaseElement, VoidElement


class A(BaseElement):
Expand Down
2 changes: 1 addition & 1 deletion hypermedia/lists.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hypermedia.base_models import BaseElement
from hypermedia.models import BaseElement


class Menu(BaseElement):
Expand Down
2 changes: 1 addition & 1 deletion hypermedia/meta_info.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hypermedia.base_models import VoidElement
from hypermedia.models import VoidElement


class Meta(VoidElement):
Expand Down
3 changes: 1 addition & 2 deletions hypermedia/base_models.py → hypermedia/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def get_child_slots(
if duplicate_keys := [
key for key in child.slots.keys() if key in slot_keys
]:
print("Extended child contains duplicate key", duplicate_keys)
raise ValueError(
f"All slot names must be unique: {duplicate_keys}"
)
Expand Down Expand Up @@ -98,7 +97,7 @@ def _render_attributes(self) -> str:
if value is True:
result.append(key)
continue
result.append(f'{key}="{value}"')
result.append(f"{key}='{value}'")
if result:
return " " + " ".join(result)
return ""
Expand Down
2 changes: 1 addition & 1 deletion hypermedia/programming.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hypermedia.base_models import BaseElement, VoidElement
from hypermedia.models import BaseElement, VoidElement


class Script(BaseElement):
Expand Down
2 changes: 1 addition & 1 deletion hypermedia/styles_and_semantics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hypermedia.base_models import BaseElement
from hypermedia.models import BaseElement


class Style(BaseElement):
Expand Down
2 changes: 1 addition & 1 deletion hypermedia/tables.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hypermedia.base_models import BaseElement, VoidElement
from hypermedia.models import BaseElement, VoidElement


class Table(BaseElement):
Expand Down
138 changes: 0 additions & 138 deletions tests/base_models/test_element.py

This file was deleted.

37 changes: 37 additions & 0 deletions tests/models/element/test_attributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from tests.utils import TestElement


def test_kwargs_stored_in_attributes() -> None:
element = TestElement(test="green")

assert element.attributes["test"] == "green"


def test_render_attributes() -> None:
element = TestElement(test="green")

assert element._render_attributes() == " test='green'"


def test_render_multiple_attributes_separated_by_space() -> None:
element = TestElement(one="one", two="two")

assert element._render_attributes() == " one='one' two='two'"


def test_false_value_is_skipped() -> None:
element = TestElement(test=False)

assert element._render_attributes() == ""


def test_true_value_is_added_as_key_only() -> None:
element = TestElement(test=True)

assert element._render_attributes() == " test"


def test_htmx_keys_added_with_hyphen() -> None:
element = TestElement(hx_get="url")

assert element._render_attributes() == " hx-get='url'"
13 changes: 13 additions & 0 deletions tests/models/element/test_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from tests.utils import TestElement


def test_with_dump_allowed() -> None:
assert TestElement()


def test_can_have_children() -> None:
child = TestElement()

parent = TestElement(child)

assert parent.children == [child]
30 changes: 30 additions & 0 deletions tests/models/element/test_extend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from tests.utils import TestElement


def test_extend_adds_child_to_slot() -> None:
element = TestElement(slot="my_slot")
child = TestElement()

element.extend("my_slot", child)

assert element.children == [child]


def test_extend_adds_children_to_slot() -> None:
element = TestElement(slot="my_slot")
child_1 = TestElement()
child_2 = TestElement()

element.extend("my_slot", child_1, child_2)

assert element.children == [child_1, child_2]


def test_extend_adds_child_to_any_descendant_slot() -> None:
child = TestElement(slot="descendant_slot")
parent = TestElement(child)
element = TestElement()

parent.extend("descendant_slot", element)

assert child.children == [element]
15 changes: 15 additions & 0 deletions tests/models/element/test_render_children.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from tests.utils import TestElement


def test_render_children_calls_dump() -> None:
"""
Test that all children are rendered.

Our custom WithDump().dump() just returns str(self) so we can
expect str(child_1) + str(child_2) as output.
"""
child_1 = TestElement()
child_2 = TestElement()
element = TestElement(child_1, child_2)

assert element._render_children() == str(child_1) + str(child_2)
Loading
Loading