Skip to content

Commit

Permalink
Added Base() for constructing the base tag <base>; Closes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
bharadwajyarlagadda committed Aug 21, 2016
1 parent e53e0fc commit 24c64cb
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 4 deletions.
13 changes: 13 additions & 0 deletions korona/html/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,18 @@
'document.',
'values': None
}
},
'base':
{
'href': {
'description': 'Specifies the base URL for all relative URLs '
'in the page',
'values': None
},
'target': {
'description': 'Specifies the default target for all '
'hyperlinks and forms in the page',
'values': None
}
}
}
40 changes: 39 additions & 1 deletion korona/html/construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
anchor_tag,
abbr_tag,
acronym_tag,
bold_tag
bold_tag,
base_tag
)

RECTANGLE_SHAPE_COORDINATES = 4
Expand Down Expand Up @@ -211,3 +212,40 @@ def __init__(self, text=None):
def construct_tag(self):
"""Returns the constructed bold tag <b></b>."""
return bold_tag.render(self.values)


class Base(object):
"""Class for constructing base tag.
Args:
href (str): Specifies the base URL for all relative URLs in the page.
target (str): Specifies the default target for all hyperlinks and
forms in the page.
"""
def __init__(self, href=None, target=None):
# TODO: Add in the main api method where it can check that there
# should be only one base tag in the whole html document.
self.tag = 'base'
self.validate_values(href=href, target=target)
self.values = {'href': href, 'target': target}

def construct_tag(self):
"""Returns the constructed base tag <base>."""
return base_tag.render(self.values)

def validate_values(self, href, target):
"""Validates the following:
- Either of href or target attribute value is given.
- CHeck whether both href and target attribute values are strings
or not.
"""
if not href and not target:
raise AttributeError('<base>: base tag must have either a href '
'attribute or a target attribute, or both.')

if href and not isinstance(href, str):
raise ValueError('<base>: href attribute value should be string')

if target and not isinstance(target, str):
raise ValueError('<base>: target attribute value should be '
'string')
3 changes: 2 additions & 1 deletion korona/templates/html/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
anchor_tag,
abbr_tag,
acronym_tag,
bold_tag
bold_tag,
base_tag
)
5 changes: 5 additions & 0 deletions korona/templates/html/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@
bold_tag = env.from_string("""
<b>{% if text -%} {{ text }} {% endif -%}</b>
""")

base_tag = env.from_string("""
<base {% if href -%} href="{{ href }}" {% endif -%}
{% if target -%} target="{{ target }}" {% endif -%}>
""")
34 changes: 32 additions & 2 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
A,
Abbr,
Acronym,
B
B,
Base
)
from korona.templates.html import (
anchor_tag,
abbr_tag,
acronym_tag,
bold_tag
bold_tag,
base_tag
)
from korona.lib.utils import validate_tag

Expand Down Expand Up @@ -152,3 +154,31 @@ def test_construct_bold_tag(attributes):
"""
bold = B(**attributes)
assert bold.construct_tag() == bold_tag.render(attributes)


@parametrize('attributes', [
({'href': 'www.google.com'}),
({'target': 'abc'}),
({'href': 'www.google.com', 'target': 'abc'})
])
def test_construct_base_tag(attributes):
"""Test for validating whether the base tag is constructed correctly or
not.
"""
base = Base(**attributes)
assert base.construct_tag() == base_tag.render(attributes)


@parametrize('attributes,exception,error_msg', [
({'href': 123}, ValueError, 'value should be string'),
({'target': 123}, ValueError, 'value should be string'),
({'href': None, 'target': None},
AttributeError,
'either a href attribute or a target attribute, or both')
])
def test_construct_base_tag_error(attributes, exception, error_msg):
"""Test for validating base tag's attributes."""
with pytest.raises(exception) as exc:
Base(**attributes)

assert error_msg in str(exc)

0 comments on commit 24c64cb

Please sign in to comment.