Skip to content

Commit 912536a

Browse files
author
Jonathan Stewmon
committed
fix: html entities in hrefs should not trigger noAnchorUrl
1 parent 1dbebe1 commit 912536a

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/formatter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ function formatAnchor(elem, fn, options) {
7373
if (!options.ignoreHref) {
7474
// Get the href, if present
7575
if (elem.attribs && elem.attribs.href) {
76-
href = elem.attribs.href.replace(/^mailto\:/, '');
76+
href = elem.attribs.href.replace(/^mailto:/, '');
7777
}
7878
if (href) {
79-
if ((!options.noAnchorUrl) || (options.noAnchorUrl && href.indexOf('#') === -1)) {
79+
if ((!options.noAnchorUrl) || (options.noAnchorUrl && href[0] !== '#')) {
8080
if (options.linkHrefBaseUrl && href.indexOf('/') === 0) {
8181
href = options.linkHrefBaseUrl + href;
8282
}

test/html-to-text.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ describe('html-to-text', function() {
195195
});
196196

197197
describe('a', function () {
198+
it('should decode html attribute entities from href', function () {
199+
var result = htmlToText.fromString('<a href="/foo?a&#x3D;b">test</a>');
200+
expect(result).to.equal('test [/foo?a=b]');
201+
});
202+
203+
it('should strip mailto: from email links', function () {
204+
var result = htmlToText.fromString('<a href="mailto:foo@example.com">email me</a>');
205+
expect(result).to.equal('email me [foo@example.com]');
206+
});
207+
198208
it('should return link with brackets', function () {
199209
var result = htmlToText.fromString('<a href="http://my.link">test</a>');
200210
expect(result).to.equal('test [http://my.link]');

0 commit comments

Comments
 (0)