Skip to content
This repository was archived by the owner on Feb 11, 2020. It is now read-only.

Commit fe691ac

Browse files
committed
Restricted allowed_tags to real cases
1 parent 52a5719 commit fe691ac

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

bootstrap_ui/templatetags/bootstrap_ui_tags.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class HtmlTagNode(BaseNode):
1616
min_args = None
1717

1818
# Additional constants, overwrite these when inheriting for customisation
19-
allowed_tags = []
19+
allowed_tags = [tag.__name__ for tag in tags.html_tag.__subclasses__()]
2020
default_css_classes = []
2121
default_tag = 'div'
2222

@@ -25,31 +25,40 @@ def render_tag(self, context, safe=True, *tag_args, **tag_kwargs):
2525
if self not in context.render_context:
2626
scope = {'nodelist': self.nodelist}
2727

28+
# Preserve all kwargs
2829
for keyword in tag_kwargs:
2930
scope[keyword] = tag_kwargs[keyword]
3031

32+
# Decide to use either given tag or default
3133
if 'use_tag' not in scope:
3234
scope['use_tag'] = self.default_tag
33-
elif scope['use_tag'] not in self.allowed_tags and self.allowed_tags:
35+
36+
# Check if given tag is allowed
37+
if scope['use_tag'] not in self.allowed_tags and self.allowed_tags:
3438
raise TemplateSyntaxError(
3539
'%r tag only allows %r for %r' % (self.tag_name, ', '.join(self.allowed_tags), 'use_tag')
3640
)
3741

42+
# Collect css classes to apply
3843
scope['use_css_classes'] = self.default_css_classes[:]
3944

4045
if 'add_css_classes' in scope:
4146
scope['use_css_classes'] += scope['add_css_classes'].split()
4247

48+
# Add scope to context identified by self
4349
context.render_context[self] = scope
4450

51+
# Retrieve scope from context as shortcut
4552
scope = context.render_context[self]
4653

47-
# Instantiate a html tag from the given scope
54+
# Instantiate a html tag from the given tag
4855
htmltag = getattr(tags, scope['use_tag'])()
4956

57+
# Apply css classes
5058
if scope['use_css_classes']:
5159
htmltag.set_attribute('class', ' '.join(scope['use_css_classes']))
5260

61+
# Render inner html content
5362
htmltag.add_raw_string(scope['nodelist'].render(context))
5463

5564
return mark_safe(htmltag.render()) if safe else htmltag
@@ -63,7 +72,12 @@ class BootstrapNode(HtmlTagNode):
6372
end_tag_name = 'endbootstraptag'
6473

6574
# Overwrite HtmlTagNode attributes
66-
allowed_tags = []
75+
allowed_tags = [
76+
'a',
77+
'div',
78+
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
79+
'ul', 'li',
80+
]
6781
default_css_classes = ['bs']
6882
default_tag = 'div'
6983

0 commit comments

Comments
 (0)