Skip to content

Commit

Permalink
Allow icons with unquoted/single-quoted ids in the styleguide
Browse files Browse the repository at this point in the history
  • Loading branch information
laymonage committed Apr 30, 2024
1 parent afbafd6 commit 452fe54
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/advanced_topics/icons.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Draw or download an icon and save it in a template folder:

The `svg` tag should:

- Set the `id="icon-<name>"` attribute, icons are referenced by this name.
- Set the `id="icon-<name>"` attribute, icons are referenced by this `name`. The `name` should only contain lowercase letters, numbers, and hyphens.
- Set the `xmlns="http://www.w3.org/2000/svg"` attribute.
- Set the `viewBox="..."` attribute, and no `width` and `height` attributes.
- If the icon should be mirrored in right-to-left (RTL) languages, set the `class="icon--directional"` attribute.
Expand Down
44 changes: 44 additions & 0 deletions wagtail/contrib/styleguide/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,47 @@ def test_styleguide(self):
self.assertContains(response, custom_css)
self.assertContains(response, widget_css)
self.assertContains(response, widget_js)

def test_icons(self):
def register_icons(icons):
return icons + [
"tests/icons/no-quotes.svg", # id=icon-no-quotes
"tests/icons/single-quotes.svg", # id='icon-single-quotes'
]

with self.register_hook("register_icons", register_icons):
response = self.client.get(reverse("wagtailstyleguide"))

self.assertEqual(response.status_code, 200)
# Should render the icons in the table
self.assertContains(
response,
'<use href="#icon-no-quotes"></use>',
html=True,
)
self.assertContains(
response,
"<td>Custom icon with no quotes for the id</td>",
html=True,
)
self.assertContains(
response,
'<use href="#icon-single-quotes"></use>',
html=True,
)
self.assertContains(
response,
"<td>Custom icon with single quotes for the id</td>",
html=True,
)
# Built-in icon, not from the above hook
self.assertContains(
response,
'<use href="#icon-h1"></use>',
html=True,
)
self.assertContains(
response,
"<td>Custom icon</td>",
html=True,
)
5 changes: 4 additions & 1 deletion wagtail/contrib/styleguide/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ def media(self):
)


icon_id_pattern = re.compile(r"id=\"icon-([a-z0-9-]+)\"")
# Allow no quotes, single quotes, and double quotes for the ID.
# For simplicity and readability, we don't enforce the opening
# and closing quotes to match.
icon_id_pattern = re.compile(r"""id=["']?icon-([a-z0-9-]+)["']?""")
icon_comment_pattern = re.compile(r"<!--!(.*?)-->")


Expand Down
1 change: 1 addition & 0 deletions wagtail/test/testapp/templates/tests/icons/no-quotes.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 452fe54

Please sign in to comment.