Skip to content

Commit

Permalink
Fixed HTML-to-native conversion of unsupported HTML elements
Browse files Browse the repository at this point in the history
The conversion of HTML elements that have no native representation was
flawed because they were processed like elements with a native
representation.
  • Loading branch information
gettalong committed May 5, 2011
1 parent b598a80 commit 3317b5e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 47 deletions.
3 changes: 2 additions & 1 deletion doc/news/release_0_X_X.page
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ modified_at: 2011-02-21 18:01:00 +02:00

- Added support for correctly parsing more HTML5 elements (requested by Bernt Carstenschulz)

* 8 bug fixes:
* 9 bug fixes:

- Table line `` |a|`b` `` was parsed as `` |a`b` `` (patch by Masahiro Kitajima)
- Table line `` |`a` `` lead to error condition (patch by Masahiro Kitajima)
Expand All @@ -27,6 +27,7 @@ modified_at: 2011-02-21 18:01:00 +02:00
- Fixed problem with footnotes in LaTeX tables -- now using `longtable` instead of `tabular`
environment (reported by Michael Franzl)
- The `style` attribute is now used for outputting table cell alignments in HTML
- Fixed HTML-to-native conversion of unsupported HTML elements

* 1 documentation fix:

Expand Down
31 changes: 18 additions & 13 deletions lib/kramdown/parser/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,28 +217,32 @@ def process(el, do_conversion = true, preserve_text = false, parent = nil)
else return
end

type = el.value
remove_text_children(el) if REMOVE_TEXT_CHILDREN.include?(type)

mname = "convert_#{el.value}"
if do_conversion && self.class.method_defined?(mname)
send(mname, el)
elsif do_conversion && SIMPLE_ELEMENTS.include?(type)
set_basics(el, type.intern)
process_children(el, do_conversion, preserve_text)
else
process_html_element(el, do_conversion, preserve_text)
end
type = el.value
remove_text_children(el) if do_conversion && REMOVE_TEXT_CHILDREN.include?(type)

strip_whitespace(el) if STRIP_WHITESPACE.include?(type)
remove_whitespace_children(el) if REMOVE_WHITESPACE_CHILDREN.include?(type)
wrap_text_children(el) if WRAP_TEXT_CHILDREN.include?(type)
if do_conversion && SIMPLE_ELEMENTS.include?(type)
set_basics(el, type.intern)
process_children(el, do_conversion, preserve_text)
else
process_html_element(el, do_conversion, preserve_text)
end

if do_conversion
strip_whitespace(el) if STRIP_WHITESPACE.include?(type)
remove_whitespace_children(el) if REMOVE_WHITESPACE_CHILDREN.include?(type)
wrap_text_children(el) if WRAP_TEXT_CHILDREN.include?(type)
end
end
end

def process_children(el, do_conversion = true, preserve_text = false)
el.children.map! do |c|
if c.type == :text
process_text(c.value, preserve_text)
process_text(c.value, preserve_text || !do_conversion)
else
process(c, do_conversion, preserve_text, el)
c
Expand Down Expand Up @@ -279,7 +283,7 @@ def process_text(raw, preserve = false)

def process_html_element(el, do_conversion = true, preserve_text = false)
el.options.replace(:category => HTML_SPAN_ELEMENTS.include?(el.value) ? :span : :block,
:content_model => HTML_CONTENT_MODEL[el.value])
:content_model => (do_conversion ? HTML_CONTENT_MODEL[el.value] : :raw))
process_children(el, do_conversion, preserve_text)
end

Expand Down Expand Up @@ -413,6 +417,7 @@ def convert_table(el)
process_html_element(el, false)
return
end
remove_text_children(el)
process_children(el)
set_basics(el, :table)

Expand Down
16 changes: 8 additions & 8 deletions test/testcases/block/09_html/content_model/tables.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<table class="examples">
<tr>
<th><em>Usage</em></th>
<th>
<tr>
<th><em>Usage</em></th>
<th>
Output
</th>
</tr>
<tr>
<td>Some <em>data</em></td>
<td>
</tr>
<tr>
<td>Some <em>data</em></td>
<td>
<h1 id="some-more">Some more</h1>
</td>
</tr>
</tr>
</table>
2 changes: 1 addition & 1 deletion test/testcases/block/09_html/content_model/tables.text
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Output
</tr>
<tr>
<td markdown="span">Some *data*</td>
<td>
<td markdown="1">
# Some more
</td>
</tr>
Expand Down
3 changes: 2 additions & 1 deletion test/testcases/block/09_html/html_to_native/emphasis.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

<p>This is <strong>strong<em>italic</em>, yes!</strong>.</p>

<p>This is <em> not</em> converted, as <em>is </em> this.</p>
<p>This is <em> not</em> converted, as <em>is
</em> this.</p>
22 changes: 10 additions & 12 deletions test/testcases/block/09_html/html_to_native/table_normal.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<table class="examples">
<tr>
<th>Usage</th>
<th>Other</th>
</tr>
<tr>
<td>
Some *data*
</td>
<td>
<p>Some more</p>
</td>
</tr>
<tr>
<th>Usage</th>
<th>Other</th>
</tr>
<tr>
<td>Some *data*</td>
<td>
<p>Some more</p>
</td>
</tr>
</table>
22 changes: 11 additions & 11 deletions test/testcases/block/09_html/html_to_native/table_simple.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@
</table>

<table class="examples">
<tr>
<th>Usage</th>
<th>Output</th>
</tr>
<tr>
<td>
Some *data*
</td>
<td>
<tr>
<th>Usage</th>
<th>
Output
</th>
</tr>
<tr>
<td>Some *data*</td>
<td>
Some more
</td>
</tr>
</td>
</tr>
</table>

0 comments on commit 3317b5e

Please sign in to comment.