Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/extensions/attr_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,7 @@ Usage
See [Extensions](index.html) for general extension usage, specify `markdown.extensions.attr_list`
as the name of the extension.

This extension does not accept any special configuration options.
The following options are provided to configure the output:

* **`allowed_attributes`**:
List of attributes to allow setting. `['*']` means all. Defaults to `['*']`.
21 changes: 17 additions & 4 deletions markdown/extensions/attr_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ def assign_attrs(self, elem, attrs):
elem.set('class', v)
else:
# assign attr k with v
elem.set(self.sanitize_name(k), v)
k = self.sanitize_name(k)
allowed = self.config['allowed_attributes']
if '*' in allowed or k in allowed:
elem.set(k, v)

def sanitize_name(self, name):
"""
Expand All @@ -167,10 +170,20 @@ def sanitize_name(self, name):


class AttrListExtension(Extension):
def __init__(self, *args, **kwargs):
# define default configs
self.config = {
'allowed_attributes': [['*'],
"List of attributes allowed to be set. "
"['*']=All."]
}

super(AttrListExtension, self).__init__(*args, **kwargs)

def extendMarkdown(self, md, md_globals):
md.treeprocessors.add(
'attr_list', AttrListTreeprocessor(md), '>prettify'
)
processor = AttrListTreeprocessor(md)
processor.config = self.getConfigs()
md.treeprocessors.add('attr_list', processor, '>prettify')


def makeExtension(*args, **kwargs):
Expand Down
19 changes: 19 additions & 0 deletions tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ def testNestedAbbr(self):
)


class TestAtrrList(unittest.TestCase):
""" Test abbr extension. """

def testDisallowedAttr(self):
""" Test Disallowed Attributes. """
md = markdown.Markdown(extensions=['markdown.extensions.attr_list'])
text = '# Header 1 {: onclick="insecure" bar="baz" }'
self.assertEqual(
md.convert(text),
'<h1 bar="baz" onclick="insecure">Header 1</h1>'
)
md = markdown.Markdown(
extensions=[markdown.extensions.attr_list.AttrListExtension(allowed_attributes=['bar'])])
self.assertEqual(
md.convert(text),
'<h1 bar="baz">Header 1</h1>'
)


class TestCodeHilite(TestCaseWithAssertStartsWith):
""" Test codehilite extension. """

Expand Down