Skip to content

Commit

Permalink
Moved classes IFrame() and Img() from html/construct.py to html/tags/
Browse files Browse the repository at this point in the history
  • Loading branch information
bharadwajyarlagadda committed Sep 16, 2016
1 parent e823133 commit c61355b
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 160 deletions.
4 changes: 2 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ API Reference
:members:


.. autoclass:: korona.html.construct.IFrame
.. autoclass:: korona.html.tags.IFrame
:members:


.. autoclass:: korona.html.construct.Img
.. autoclass:: korona.html.tags.Img
:members:

4 changes: 2 additions & 2 deletions docs/guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ Korona can help you build <iframe> tag.

.. code-block:: python
from korona.html.construct import IFrame
from korona.html.tags import IFrame
attributes = {'src': '/demo.asp', 'height': '100', 'width': '200'}
Expand Down Expand Up @@ -1053,7 +1053,7 @@ Korona can help you build <img tag.

.. code-block:: python
from korona.html.construct import Img
from korona.html.tags import Img
attributes = {'height': '30', 'width': '30', 'hspace': '20', 'vspace': '20'}
Expand Down
2 changes: 2 additions & 0 deletions korona/html/tags/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,7 @@
from .html import Html

from .italics import I
from .iframe import IFrame
from .image import Img

from .heading import H1, H2, H3, H4, H5, H6
74 changes: 3 additions & 71 deletions korona/html/construct.py → korona/html/tags/iframe.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
# -*- coding: utf-8 -*-
"""Module for constructing Html Tags."""
"""Module for constructing <iframe> tag."""


from __future__ import absolute_import

from ..lib.utils import (
validate_attribute_values,
validate_url
)

from ..templates.html import (
iframe_tag,
img_tag
)
from ...lib.utils import validate_attribute_values, validate_url
from ...templates.html.tags import iframe_tag


class IFrame(object):
Expand Down Expand Up @@ -101,64 +94,3 @@ def validate_sandbox(self, sandbox):
validate_attribute_values(tag=self.tag,
attribute_name='sandbox',
value=part)


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)
69 changes: 69 additions & 0 deletions korona/html/tags/image.py
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)
54 changes: 54 additions & 0 deletions tests/test_html_tags/test_iframe_tag.py
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)
37 changes: 37 additions & 0 deletions tests/test_html_tags/test_image_tag.py
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)
87 changes: 2 additions & 85 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@

import pytest

from korona.html.construct import (
IFrame,
Img
)
from korona.templates.html import (
iframe_tag,
img_tag
)
from korona.lib.utils import validate_tag

from .fixtures import parametrize

from korona.lib.utils import validate_tag


@parametrize('tag,error,error_msg', [
('htmle', ValueError, 'tag is not supported'),
Expand All @@ -25,78 +17,3 @@ def test_validate_invalid_tags(tag, error, error_msg):
validate_tag(tag)

assert error_msg in str(exc)


@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)


@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)

0 comments on commit c61355b

Please sign in to comment.