Bladeren bron

fix(tables): raw html inside code tags in tables no longer breaks tables

Under certains conditions, raw html inside code tags in tables would break
table parsing. This commit fixes that.

Closes #471
Estevao Soares dos Santos 7 jaren geleden
bovenliggende
commit
4ef4c5e674

+ 7 - 20
dist/showdown.js

@@ -1,4 +1,4 @@
-;/*! showdown v 1.8.3 - 28-11-2017 */
+;/*! showdown v 1.8.3 - 05-12-2017 */
 (function(){
 /**
  * Created by Tivie on 13-07-2015.
@@ -3812,7 +3812,7 @@ showdown.subParser('lists', function (text, options, globals) {
             style = styleStartNumber(list, listType);
         if (pos !== -1) {
           // slice
-          result += '\n<' + listType + style + '>\n' + processListItems(txt.slice(0, pos), !!trimTrailing) + '</' + listType + '>\n';
+          result += '\n\n<' + listType + style + '>\n' + processListItems(txt.slice(0, pos), !!trimTrailing) + '</' + listType + '>\n';
 
           // invert counterType and listType
           listType = (listType === 'ul') ? 'ol' : 'ul';
@@ -3821,12 +3821,12 @@ showdown.subParser('lists', function (text, options, globals) {
           //recurse
           parseCL(txt.slice(pos));
         } else {
-          result += '\n<' + listType + style + '>\n' + processListItems(txt, !!trimTrailing) + '</' + listType + '>\n';
+          result += '\n\n<' + listType + style + '>\n' + processListItems(txt, !!trimTrailing) + '</' + listType + '>\n';
         }
       })(list);
     } else {
       var style = styleStartNumber(list, listType);
-      result = '\n<' + listType + style + '>\n' + processListItems(list, !!trimTrailing) + '</' + listType + '>\n';
+      result = '\n\n<' + listType + style + '>\n' + processListItems(list, !!trimTrailing) + '</' + listType + '>\n';
     }
 
     return result;
@@ -4100,9 +4100,9 @@ showdown.subParser('tables', function (text, options, globals) {
     return text;
   }
 
-  var tableRgx       = /^ {0,3}\|?.+\|.+\n {0,3}\|?[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:[-=]){2,}[\s\S]+?(?:\n\n|<ol|<ul|¨0)/gm,
+  var tableRgx       = /^ {0,3}\|?.+\|.+\n {0,3}\|?[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:[-=]){2,}[\s\S]+?(?:\n\n|¨0)/gm,
     //singeColTblRgx = /^ {0,3}\|.+\|\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*\n(?: {0,3}\|.+\|\n)+(?:\n\n|¨0)/gm;
-      singeColTblRgx = /^ {0,3}\|.+\|[ \t]*\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*\n( {0,3}\|.+\|[ \t]*\n)*(?:\n|<ol|<ul|¨0)/gm;
+      singeColTblRgx = /^ {0,3}\|.+\|[ \t]*\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*\n( {0,3}\|.+\|[ \t]*\n)*(?:\n|¨0)/gm;
 
   function parseStyles (sLine) {
     if (/^:[ \t]*--*$/.test(sLine)) {
@@ -4119,7 +4119,7 @@ showdown.subParser('tables', function (text, options, globals) {
   function parseHeaders (header, style) {
     var id = '';
     header = header.trim();
-    // support both tablesHeaderId and tableHeaderId due to error in documention so we don't break backwards compatibility
+    // support both tablesHeaderId and tableHeaderId due to error in documentation so we don't break backwards compatibility
     if (options.tablesHeaderId || options.tableHeaderId) {
       id = ' id="' + header.replace(/ /g, '_').toLowerCase() + '"';
     }
@@ -4220,24 +4220,11 @@ showdown.subParser('tables', function (text, options, globals) {
     return buildTable(headers, cells);
   }
 
-  function hackFixTableFollowedByList (rawTable) {
-    var lastChars = rawTable.slice(-3);
-    if (lastChars === '<ol' || lastChars === '<ul') {
-      rawTable = rawTable.slice(0, -3) + '\n\n' + rawTable.slice(-3);
-    }
-    return rawTable;
-  }
-
   text = globals.converter._dispatch('tables.before', text, options, globals);
 
   // find escaped pipe characters
   text = text.replace(/\\(\|)/g, showdown.helper.escapeCharactersCallback);
 
-  // hackfix issue #443. Due to lists only having a linebreak before them, we need to manually insert a linebreak to prevent
-  // tables not being parsed when followed by a list
-  text = text.replace(tableRgx, hackFixTableFollowedByList);
-  text = text.replace(singeColTblRgx, hackFixTableFollowedByList);
-
   // parse multi column tables
   text = text.replace(tableRgx, parseTable);
 

File diff suppressed because it is too large
+ 0 - 0
dist/showdown.js.map


File diff suppressed because it is too large
+ 0 - 1
dist/showdown.min.js


File diff suppressed because it is too large
+ 0 - 0
dist/showdown.min.js.map


+ 3 - 3
src/subParsers/lists.js

@@ -154,7 +154,7 @@ showdown.subParser('lists', function (text, options, globals) {
             style = styleStartNumber(list, listType);
         if (pos !== -1) {
           // slice
-          result += '\n<' + listType + style + '>\n' + processListItems(txt.slice(0, pos), !!trimTrailing) + '</' + listType + '>\n';
+          result += '\n\n<' + listType + style + '>\n' + processListItems(txt.slice(0, pos), !!trimTrailing) + '</' + listType + '>\n';
 
           // invert counterType and listType
           listType = (listType === 'ul') ? 'ol' : 'ul';
@@ -163,12 +163,12 @@ showdown.subParser('lists', function (text, options, globals) {
           //recurse
           parseCL(txt.slice(pos));
         } else {
-          result += '\n<' + listType + style + '>\n' + processListItems(txt, !!trimTrailing) + '</' + listType + '>\n';
+          result += '\n\n<' + listType + style + '>\n' + processListItems(txt, !!trimTrailing) + '</' + listType + '>\n';
         }
       })(list);
     } else {
       var style = styleStartNumber(list, listType);
-      result = '\n<' + listType + style + '>\n' + processListItems(list, !!trimTrailing) + '</' + listType + '>\n';
+      result = '\n\n<' + listType + style + '>\n' + processListItems(list, !!trimTrailing) + '</' + listType + '>\n';
     }
 
     return result;

+ 3 - 16
src/subParsers/tables.js

@@ -5,9 +5,9 @@ showdown.subParser('tables', function (text, options, globals) {
     return text;
   }
 
-  var tableRgx       = /^ {0,3}\|?.+\|.+\n {0,3}\|?[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:[-=]){2,}[\s\S]+?(?:\n\n|<ol|<ul|¨0)/gm,
+  var tableRgx       = /^ {0,3}\|?.+\|.+\n {0,3}\|?[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:[-=]){2,}[\s\S]+?(?:\n\n|¨0)/gm,
     //singeColTblRgx = /^ {0,3}\|.+\|\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*\n(?: {0,3}\|.+\|\n)+(?:\n\n|¨0)/gm;
-      singeColTblRgx = /^ {0,3}\|.+\|[ \t]*\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*\n( {0,3}\|.+\|[ \t]*\n)*(?:\n|<ol|<ul|¨0)/gm;
+      singeColTblRgx = /^ {0,3}\|.+\|[ \t]*\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*\n( {0,3}\|.+\|[ \t]*\n)*(?:\n|¨0)/gm;
 
   function parseStyles (sLine) {
     if (/^:[ \t]*--*$/.test(sLine)) {
@@ -24,7 +24,7 @@ showdown.subParser('tables', function (text, options, globals) {
   function parseHeaders (header, style) {
     var id = '';
     header = header.trim();
-    // support both tablesHeaderId and tableHeaderId due to error in documention so we don't break backwards compatibility
+    // support both tablesHeaderId and tableHeaderId due to error in documentation so we don't break backwards compatibility
     if (options.tablesHeaderId || options.tableHeaderId) {
       id = ' id="' + header.replace(/ /g, '_').toLowerCase() + '"';
     }
@@ -125,24 +125,11 @@ showdown.subParser('tables', function (text, options, globals) {
     return buildTable(headers, cells);
   }
 
-  function hackFixTableFollowedByList (rawTable) {
-    var lastChars = rawTable.slice(-3);
-    if (lastChars === '<ol' || lastChars === '<ul') {
-      rawTable = rawTable.slice(0, -3) + '\n\n' + rawTable.slice(-3);
-    }
-    return rawTable;
-  }
-
   text = globals.converter._dispatch('tables.before', text, options, globals);
 
   // find escaped pipe characters
   text = text.replace(/\\(\|)/g, showdown.helper.escapeCharactersCallback);
 
-  // hackfix issue #443. Due to lists only having a linebreak before them, we need to manually insert a linebreak to prevent
-  // tables not being parsed when followed by a list
-  text = text.replace(tableRgx, hackFixTableFollowedByList);
-  text = text.replace(singeColTblRgx, hackFixTableFollowedByList);
-
   // parse multi column tables
   text = text.replace(tableRgx, parseTable);
 

+ 14 - 0
test/features/tables/#471.ol-is-not-rendered-correctly-inside-table.html

@@ -0,0 +1,14 @@
+<table>
+    <thead>
+    <tr>
+        <th style="text-align:right;">h1</th>
+        <th style="text-align:left;">h2</th>
+    </tr>
+    </thead>
+    <tbody>
+    <tr>
+        <td style="text-align:right;">asdf</td>
+        <td style="text-align:left;">one <code>two &lt;ol&gt; three</code></td>
+    </tr>
+    </tbody>
+</table>

+ 3 - 0
test/features/tables/#471.ol-is-not-rendered-correctly-inside-table.md

@@ -0,0 +1,3 @@
+|  h1     |  h2                  |
+|--------:|:---------------------|
+| asdf    | one `two <ol> three` |

Some files were not shown because too many files changed in this diff