Skip to content

Commit

Permalink
Only strip spaces in tables (Python-Markdown#644)
Browse files Browse the repository at this point in the history
Strip only the space character and not things like nbsp in tables. Fixes Python-Markdown#635.
  • Loading branch information
facelessuser authored and waylan committed Feb 22, 2018
1 parent b0e993a commit cab4b69
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
10 changes: 5 additions & 5 deletions markdown/extensions/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test(self, parent, block):
Keep border check and separator row do avoid repeating the work.
"""
is_table = False
rows = [row.strip() for row in block.split('\n')]
rows = [row.strip(' ') for row in block.split('\n')]
if len(rows) > 1:
header0 = rows[0]
self.border = PIPE_NONE
Expand Down Expand Up @@ -76,13 +76,13 @@ def test(self, parent, block):
def run(self, parent, blocks):
""" Parse a table block and build table. """
block = blocks.pop(0).split('\n')
header = block[0].strip()
header = block[0].strip(' ')
rows = [] if len(block) < 3 else block[2:]

# Get alignment of columns
align = []
for c in self.separator:
c = c.strip()
c = c.strip(' ')
if c.startswith(':') and c.endswith(':'):
align.append('center')
elif c.startswith(':'):
Expand All @@ -102,7 +102,7 @@ def run(self, parent, blocks):
self._build_empty_row(tbody, align)
else:
for row in rows:
self._build_row(row.strip(), tbody, align)
self._build_row(row.strip(' '), tbody, align)

def _build_empty_row(self, parent, align):
"""Build an empty row."""
Expand All @@ -124,7 +124,7 @@ def _build_row(self, row, parent, align):
for i, a in enumerate(align):
c = etree.SubElement(tr, tag)
try:
c.text = cells[i].strip()
c.text = cells[i].strip(' ')
except IndexError: # pragma: no cover
c.text = ""
if a:
Expand Down
Empty file.
43 changes: 43 additions & 0 deletions tests/test_syntax/extensions/test_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from markdown.test_tools import TestCase


class TestTableBlocks(TestCase):

def test_empty_cells(self):
"""Empty cells (nbsp)."""

text = """
  | Second Header
------------- | -------------
  | Content Cell
Content Cell |  
"""

self.assertMarkdownRenders(
text,
self.dedent(
"""
<table>
<thead>
<tr>
<th> </th>
<th>Second Header</th>
</tr>
</thead>
<tbody>
<tr>
<td> </td>
<td>Content Cell</td>
</tr>
<tr>
<td>Content Cell</td>
<td> </td>
</tr>
</tbody>
</table>
"""
),
extensions=['tables']
)

0 comments on commit cab4b69

Please sign in to comment.