diff --git a/app/assets/javascripts/app/helpers/text_formatter.js b/app/assets/javascripts/app/helpers/text_formatter.js index 57c7a5d57ee9d017a83f79bb536be50a6ad17ea1..3e122d87a29f810bb0a9e7dfde9aba9d835b75f0 100644 --- a/app/assets/javascripts/app/helpers/text_formatter.js +++ b/app/assets/javascripts/app/helpers/text_formatter.js @@ -43,6 +43,12 @@ $(function() { unicodeUrl += ')'; urlSuffix = '>'; } + + // url*DE*code as much as possible + while( unicodeUrl.indexOf("%") !== -1 && unicodeUrl != decodeURI(unicodeUrl) ) { + unicodeUrl = decodeURI(unicodeUrl); + } + // markdown doesn't like '(' or ')' anywhere, except where it wants var workingUrl = unicodeUrl.replace(/\(/, "%28").replace(/\)/, "%29"); diff --git a/spec/javascripts/app/helpers/text_formatter_spec.js b/spec/javascripts/app/helpers/text_formatter_spec.js index 1a95c0c3ad5e52c6f28388af2415d474436dcf65..29aec511e90808e413dff91f81d827ca93846629 100644 --- a/spec/javascripts/app/helpers/text_formatter_spec.js +++ b/spec/javascripts/app/helpers/text_formatter_spec.js @@ -158,6 +158,24 @@ describe("app.helpers.textFormatter", function(){ expect(parsed).toContain(text); }); + context("percent-encoded input url", function() { + beforeEach(function() { + this.input = "http://www.soilandhealth.org/01aglibrary/010175.tree%20crops.pdf" // #4507 + this.correctHref = 'href="'+this.input+'"'; + }); + + it("doesn't get double-encoded", function(){ + var parsed = this.formatter.markdownify(this.input); + expect(parsed).toContain(this.correctHref); + }); + + it("gets correctly decoded, even when multiply encoded", function() { + var uglyUrl = encodeURI(encodeURI(encodeURI(this.input))); + var parsed = this.formatter.markdownify(uglyUrl); + expect(parsed).toContain(this.correctHref); + }); + }); + it("tests a bunch of benchmark urls", function(){ var self = this; $.ajax({