Skip to content

Presence of new line characters in inline attribute list throws exception  #482

@rkrp

Description

@rkrp

Presence of new line characters \n (without any whitespace around) in markdown attribute list causes exception while converting to HTML. The problem is in attr_list extension.

Case 1

from markdown import markdown

md = """
![text](http://link){foo="a"\nbar="b"}
"""
print markdown(md, extensions=['attr_list'])

This results in ValueError: too many values to unpack in _handle_double_quote

Case 2

This produces incorrect results when there is a class name or id present in the attribute list.

For example,

from markdown import markdown

md = """
![text](http://link){foo="a"\n.nomedia bar="b"}
"""
print markdown(md, extensions=['attr_list'])

produces the following output:

<p><img _.nomedia="
.nomedia" alt="text" bar="b" foo="a" src="http://link" /></p>

Possible Fix

Replacing the new line characters with a single space fixes the problem. It also provides the expected output for the two cases.

diff --git a/markdown/extensions/attr_list.py b/markdown/extensions/attr_list.py

index 683bdf8..83c2c01 100644
--- a/markdown/extensions/attr_list.py
+++ b/markdown/extensions/attr_list.py
@@ -139,6 +139,7 @@ class AttrListTreeprocessor(Treeprocessor):
             else:
                 # inline: check for attrs at start of tail
                 if elem.tail:
+                    elem.tail = elem.tail.replace('\n', ' ')
                     m = self.INLINE_RE.match(elem.tail)
                     if m:
                         self.assign_attrs(elem, m.group(1))    

After applying the above patch, the results are obtained as

Case 1

![text](http://link){foo="a"\nbar="b"}
<p><img alt="text" bar="b" foo="a" src="http://link" /></p>

Case 2

![text](http://link){foo="a"\n.nomedia bar="b"}
<p><img alt="text" bar="b" class="nomedia" foo="a" src="http://link" /></p>

EDIT: Added code highlighting

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions