-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved classes IFrame() and Img() from html/construct.py to html/tags/
- Loading branch information
1 parent
e823133
commit c61355b
Showing
8 changed files
with
171 additions
and
160 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
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,69 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Module for constructing <img> tag.""" | ||
|
||
|
||
from __future__ import absolute_import | ||
|
||
from ...lib.utils import validate_attribute_values, validate_url | ||
from ...templates.html.tags import img_tag | ||
|
||
|
||
class Img(object): | ||
"""Class for constructing <img> tag. | ||
Args: | ||
align (str): Specifies the alignment of an image according to | ||
surrounding elements. | ||
alt (str): Specifies an alternate text for an image. | ||
border (str): Specifies the width of the border around an image. | ||
crossorigin (str): Allow images from third-party sites that allow | ||
cross-origin access to be used with canvas. | ||
height (str): Specifies the height of an image. | ||
hspace (str): Specifies the whitespace on left and right side of an | ||
image. | ||
ismap (bool): Specifies an image as a server-side image-map. | ||
longdesc (str): Specifies a URL to a detailed description of an image. | ||
src (str): Specifies the URL of an image. | ||
usemap (str): Specifies an image as a client-side image-map. | ||
vspace (str): Specifies the whitespace on top and bottom of an image. | ||
width (str): Specifies the width of an image. | ||
.. versionadded:: 0.4.0-dev | ||
""" | ||
def __init__(self, | ||
align=None, | ||
alt=None, | ||
border=None, | ||
crossorigin=None, | ||
height=None, | ||
hspace=None, | ||
ismap=False, | ||
longdesc=None, | ||
src=None, | ||
usemap=None, | ||
vspace=None, | ||
width=None): | ||
self.tag = 'img' | ||
validate_attribute_values(tag=self.tag, | ||
attribute_name='align', | ||
value=align) | ||
# TODO: Add validation for ismap attribute. | ||
validate_url(attribute_name='longdesc', url=longdesc) | ||
validate_url(attribute_name='src', url=src) | ||
# TODO: Add validation for usemap attribute. | ||
self.values = {'align': align, | ||
'alt': alt, | ||
'border': border, | ||
'crossorigin': crossorigin, | ||
'height': height, | ||
'hspace': hspace, | ||
'ismap': ismap, | ||
'longdesc': longdesc, | ||
'src': src, | ||
'usemap': usemap, | ||
'vspace': vspace, | ||
'width': width} | ||
|
||
def construct(self): | ||
"""Returns the constructed image tag <img>.""" | ||
return img_tag.render(self.values) |
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,54 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import pytest | ||
|
||
from ..fixtures import parametrize | ||
|
||
from korona.html.tags import IFrame | ||
from korona.templates.html.tags import iframe_tag | ||
|
||
|
||
@parametrize('attributes', [ | ||
({'align': 'left', 'frameborder': '1'}), | ||
({'src': '/demo.asp', 'height': '100', 'width': '200'}), | ||
({'longdesc': 'demo.txt'}), | ||
({'marginheight': '50', 'marginwidth': '50', 'name': 'example'}), | ||
({'sandbox': 'allow-forms allow-scripts', 'scrolling': 'yes'}), | ||
({'srcdoc': '<p>hello</p>'}) | ||
]) | ||
def test_construct_iframe_tag(attributes): | ||
"""Test for validating whether the iframe tag is constructed correctly or | ||
not. | ||
""" | ||
iframe = IFrame(**attributes) | ||
assert iframe.construct() == iframe_tag.render(attributes) | ||
|
||
|
||
@parametrize('attributes,exception,error_msg', [ | ||
({'frameborder': '4'}, | ||
AttributeError, | ||
'attribute values should be one of these'), | ||
({'align': 'left-center'}, | ||
AttributeError, | ||
'attribute values should be one of these'), | ||
({'sandbox': 'abc'}, | ||
AttributeError, | ||
'attribute values should be one of these'), | ||
({'sandbox': 'allow-forms abc'}, | ||
AttributeError, | ||
'attribute values should be one of these'), | ||
({'scrolling': 'mount'}, | ||
AttributeError, | ||
'attribute values should be one of these'), | ||
({'sandbox': 'allow-forms allow-pointer-lock abc'}, | ||
AttributeError, | ||
'attribute values should be one of these'), | ||
({'longdesc': 123}, ValueError, 'is not a valid url'), | ||
({'src': 123}, ValueError, 'is not a valid url') | ||
]) | ||
def test_construct_iframe_tag_error(attributes, exception, error_msg): | ||
"""Test for validating iframe tag's attributes.""" | ||
with pytest.raises(exception) as exc: | ||
IFrame(**attributes) | ||
|
||
assert error_msg in str(exc) |
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,37 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import pytest | ||
|
||
from ..fixtures import parametrize | ||
|
||
from korona.html.tags import Img | ||
from korona.templates.html.tags import img_tag | ||
|
||
|
||
@parametrize('attributes', [ | ||
({'align': 'left', 'alt': 'Smiley text', 'border': '4'}), | ||
({'crossorigin': 'anonymous'}), | ||
({'height': '30', 'width': '30', 'hspace': '20', 'vspace': '20'}), | ||
({'ismap': True}), | ||
({'longdesc': 'explained', 'src': '/demo.asp', 'usemap': 'planets'}) | ||
]) | ||
def test_construct_img_tag(attributes): | ||
"""Test for validating whether the img tag is constructed correctly or not. | ||
""" | ||
img = Img(**attributes) | ||
assert img.construct() == img_tag.render(attributes) | ||
|
||
|
||
@parametrize('attributes,exception,error_msg', [ | ||
({'align': 'left-top'}, | ||
AttributeError, | ||
'attribute values should be one of these'), | ||
({'longdesc': 123}, ValueError, 'is not a valid url'), | ||
({'src': 123}, ValueError, 'is not a valid url') | ||
]) | ||
def test_construct_img_tag_error(attributes, exception, error_msg): | ||
"""Test for validating img tag's attributes.""" | ||
with pytest.raises(exception) as exc: | ||
Img(**attributes) | ||
|
||
assert error_msg in str(exc) |
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