From 18ebac8077c53537b4811b0cf9179dd5094b7e3a Mon Sep 17 00:00:00 2001 From: Timo Virtanen Date: Mon, 8 Oct 2018 16:10:20 -0700 Subject: [PATCH 1/2] make sure that element is li (list item) --- lib/formatter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/formatter.js b/lib/formatter.js index ef2e82d..687a3fb 100644 --- a/lib/formatter.js +++ b/lib/formatter.js @@ -125,7 +125,7 @@ function formatUnorderedList(elem, fn, options) { var result = ''; var prefix = options.unorderedListItemPrefix; var nonWhiteSpaceChildren = (elem.children || []).filter(function(child) { - return child.type !== 'text' || !whiteSpaceRegex.test(child.data); + return child.name === 'li' && (child.type !== 'text' || !whiteSpaceRegex.test(child.data)); }); nonWhiteSpaceChildren.forEach(function(elem) { result += formatListItem(prefix, elem, fn, options); @@ -136,7 +136,7 @@ function formatUnorderedList(elem, fn, options) { function formatOrderedList(elem, fn, options) { var result = ''; var nonWhiteSpaceChildren = (elem.children || []).filter(function(child) { - return child.type !== 'text' || !whiteSpaceRegex.test(child.data); + return child.name === 'li' && (child.type !== 'text' || !whiteSpaceRegex.test(child.data)); }); // Return different functions for different OL types var typeFunction = (function() { From e459173266a497258677b60e1c80e415aa5f6bb3 Mon Sep 17 00:00:00 2001 From: Timo Virtanen Date: Tue, 9 Oct 2018 12:10:59 -0700 Subject: [PATCH 2/2] add tests --- test/html-to-text.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/html-to-text.js b/test/html-to-text.js index af7f7aa..13db820 100644 --- a/test/html-to-text.js +++ b/test/html-to-text.js @@ -244,6 +244,11 @@ describe('html-to-text', function() { expect(htmlToText.fromString(testString)).to.equal(' * foo\n * bar'); }); + it('should handle an unordered list with line break', function() { + var testString = ''; + expect(htmlToText.fromString(testString)).to.equal(' * foo\n * bar'); + }); + it('should handle an unordered list prefix option', function() { var testString = ''; var options = {unorderedListItemPrefix: ' test '}; @@ -262,6 +267,11 @@ describe('html-to-text', function() { expect(htmlToText.fromString(testString)).to.equal(' 1. foo\n 2. bar'); }); + it('should handle an ordered list with line break', function() { + var testString = '

  1. foo
  2. bar
'; + expect(htmlToText.fromString(testString)).to.equal(' 1. foo\n 2. bar'); + }); + it('should support the ordered list type="1" attribute', function() { var testString = '
  1. foo
  2. bar
'; expect(htmlToText.fromString(testString)).to.equal(' 1. foo\n 2. bar');