cgiffard / Downsize

Tag safe text truncation for HTML and XML!
BSD 3-Clause "New" or "Revised" License
41 stars 13 forks source link

Truncation Breaks if HTML Comment Contains Quotes #31

Open sshaw opened 5 years ago

sshaw commented 5 years ago

For example:

const html = `<!-- it's broken -->one two three`;
const truncate = require('downsize');
console.log(truncate(html, {words: 2}));

Results in:

<!-- it's broken -->one two three

This fixes it:

diff --git a/index.js b/index.js
index 03cbc9b..42e14c6 100644
--- a/index.js
+++ b/index.js
@@ -42,7 +42,7 @@ var XRegexp = require('xregexp').XRegExp;
         }

         options.keepContext     = !!options.contextualTags;
-        options.contextualTags  = 
+        options.contextualTags  =
             options.keepContext && Array.isArray(options.contextualTags) ?
                 options.contextualTags : [];

@@ -161,7 +161,8 @@ var XRegexp = require('xregexp').XRegExp;
                         // if double quote is found in a single quote string,
                         // ignore it and let the string finish
                         break;
-
+                    } else if (parseState === PARSER_COMMENT) {
+                        break;
                     } else if (parseState !== PARSER_UNINITIALISED) {
                         parseState = PARSER_TAG_STRING;
                     }
@@ -176,7 +177,8 @@ var XRegexp = require('xregexp').XRegExp;
                         // if single quote is found in a double quote string,
                         // ignore it and let the string finish
                         break;
-
+                    } else if (parseState === PARSER_COMMENT) {
+                        break;
                     } else if (parseState !== PARSER_UNINITIALISED) {
                         parseState = PARSER_TAG_STRING_SINGLE;
                     }