-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an argument to allow arbitrary html (#100)
* add argument to allow arbitrary html in email * version bump * added tests * added a library and code to validate that email template content contains valid html, added tests * add validation of html if passthrough option is used in get_html_email_body
- Loading branch information
Showing
7 changed files
with
96 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from py_w3c.validators.html.validator import HTMLValidator | ||
|
||
|
||
def check_if_string_contains_valid_html(content: str) -> list: | ||
""" | ||
Check if html snippet is valid - returns [] if html is valid. | ||
This is only a partial document, so we expect the Doctype and title to be missing. | ||
""" | ||
|
||
allowed_errors = [ | ||
"Start tag seen without seeing a doctype first. Expected “<!DOCTYPE html>”.", | ||
"Element “head” is missing a required instance of child element “title”.", | ||
] | ||
|
||
# the content can contain markdown as well as html - wrap the content in a div so it has a chance of being valid html | ||
content_in_div = f"<div>{content}</div>" | ||
|
||
val = HTMLValidator() | ||
val.validate_fragment(content_in_div) | ||
|
||
significant_errors = [] | ||
for error in val.errors: | ||
if error["message"] in allowed_errors: | ||
continue | ||
significant_errors.append(error) | ||
|
||
return significant_errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
__version__ = "45.0.0" | ||
__version__ = "46.0.0" | ||
# GDS version '34.0.1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import pytest | ||
|
||
from notifications_utils.validate_html import check_if_string_contains_valid_html | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"good_content", | ||
( | ||
"<div>abc</div>", | ||
'<div style="display: none;">abc</div>', | ||
"""<div style="margin: 20px auto 30px auto;"> | ||
<img | ||
src="http://google.com" | ||
alt="alt text" | ||
height="10" | ||
width="10" | ||
/> | ||
</div>""", | ||
"abc<div>abc</div>xyz", | ||
), | ||
) | ||
def test_good_content_is_valid(good_content: str): | ||
assert check_if_string_contains_valid_html(good_content) == [] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"bad_content", ("<div>abc<div>", '<img src="http://google.com">', "abc<div>abc<div>xyz", '<div style=">abc</div>') | ||
) | ||
def test_bad_content_is_invalid(bad_content: str): | ||
assert check_if_string_contains_valid_html(bad_content) != [] |