From adf9bcf8e80a072fd22867e34b06542dcafee37c Mon Sep 17 00:00:00 2001
From: Steffen van Bergerem <svbergerem@online.de>
Date: Wed, 25 Mar 2015 05:17:12 +0100
Subject: [PATCH] Update markdown-it

---
 Gemfile                                       | 13 ++-
 Gemfile.lock                                  | 26 +++---
 .../javascripts/app/helpers/text_formatter.js | 37 ++------
 app/assets/javascripts/main.js                |  2 -
 .../app/helpers/text_formatter_spec.js        | 14 +--
 vendor/assets/javascripts/parse_url.js        | 88 -------------------
 6 files changed, 33 insertions(+), 147 deletions(-)
 delete mode 100644 vendor/assets/javascripts/parse_url.js

diff --git a/Gemfile b/Gemfile
index 810a1f7843..8027b88172 100644
--- a/Gemfile
+++ b/Gemfile
@@ -92,14 +92,13 @@ gem "js-routes",                                        "1.0.0"
 source "https://rails-assets.org" do
   gem "rails-assets-jquery",                              "1.11.1" # Should be kept in sync with jquery-rails
 
-  gem "rails-assets-punycode",                            "1.3.2"
-  gem "rails-assets-markdown-it",                         "3.1.0"
-  gem "rails-assets-markdown-it-hashtag",                 "0.2.3"
-  gem "rails-assets-markdown-it-diaspora-mention",        "0.2.1"
-  gem "rails-assets-markdown-it-sanitizer",               "0.2.2"
+  gem "rails-assets-markdown-it",                         "4.0.3"
+  gem "rails-assets-markdown-it-hashtag",                 "0.3.0"
+  gem "rails-assets-markdown-it-diaspora-mention",        "0.3.0"
+  gem "rails-assets-markdown-it-sanitizer",               "0.3.0"
   gem "rails-assets-markdown-it--markdown-it-for-inline", "0.1.0"
-  gem "rails-assets-markdown-it-sub",                     "0.1.0"
-  gem "rails-assets-markdown-it-sup",                     "0.1.0"
+  gem "rails-assets-markdown-it-sub",                     "1.0.0"
+  gem "rails-assets-markdown-it-sup",                     "1.0.0"
 
   # jQuery plugins
 
diff --git a/Gemfile.lock b/Gemfile.lock
index ce1f2368df..bd91fd5190 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -510,15 +510,14 @@ GEM
     rails-assets-jquery.slimscroll (1.3.3)
       rails-assets-jquery (>= 1.7)
     rails-assets-markdown-it--markdown-it-for-inline (0.1.0)
-    rails-assets-markdown-it (3.1.0)
-    rails-assets-markdown-it-diaspora-mention (0.2.1)
-    rails-assets-markdown-it-hashtag (0.2.3)
-    rails-assets-markdown-it-sanitizer (0.2.2)
-    rails-assets-markdown-it-sub (0.1.0)
-    rails-assets-markdown-it-sup (0.1.0)
+    rails-assets-markdown-it (4.0.3)
+    rails-assets-markdown-it-diaspora-mention (0.3.0)
+    rails-assets-markdown-it-hashtag (0.3.0)
+    rails-assets-markdown-it-sanitizer (0.3.0)
+    rails-assets-markdown-it-sub (1.0.0)
+    rails-assets-markdown-it-sup (1.0.0)
     rails-assets-perfect-scrollbar (0.5.9)
       rails-assets-jquery (>= 1.10)
-    rails-assets-punycode (1.3.2)
     rails-deprecated_sanitizer (1.0.3)
       activesupport (>= 4.2.0.alpha)
     rails-dom-testing (1.0.6)
@@ -777,15 +776,14 @@ DEPENDENCIES
   rails-assets-jquery-idletimer (= 1.0.1)!
   rails-assets-jquery-placeholder (= 2.1.1)!
   rails-assets-jquery-textchange (= 0.2.3)!
-  rails-assets-markdown-it (= 3.1.0)!
+  rails-assets-markdown-it (= 4.0.3)!
   rails-assets-markdown-it--markdown-it-for-inline (= 0.1.0)!
-  rails-assets-markdown-it-diaspora-mention (= 0.2.1)!
-  rails-assets-markdown-it-hashtag (= 0.2.3)!
-  rails-assets-markdown-it-sanitizer (= 0.2.2)!
-  rails-assets-markdown-it-sub (= 0.1.0)!
-  rails-assets-markdown-it-sup (= 0.1.0)!
+  rails-assets-markdown-it-diaspora-mention (= 0.3.0)!
+  rails-assets-markdown-it-hashtag (= 0.3.0)!
+  rails-assets-markdown-it-sanitizer (= 0.3.0)!
+  rails-assets-markdown-it-sub (= 1.0.0)!
+  rails-assets-markdown-it-sup (= 1.0.0)!
   rails-assets-perfect-scrollbar (= 0.5.9)!
-  rails-assets-punycode (= 1.3.2)!
   rails-i18n (= 4.0.4)
   rails-timeago (= 2.11.0)
   rails_admin (= 0.6.7)
diff --git a/app/assets/javascripts/app/helpers/text_formatter.js b/app/assets/javascripts/app/helpers/text_formatter.js
index a6fc1c391b..1de72a4c2b 100644
--- a/app/assets/javascripts/app/helpers/text_formatter.js
+++ b/app/assets/javascripts/app/helpers/text_formatter.js
@@ -4,28 +4,6 @@
   app.helpers.textFormatter = function(text, mentions) {
     mentions = mentions ? mentions : [];
 
-    var punycodeURL = function(url){
-      try {
-        while(url.indexOf("%") !== -1 && url !== decodeURI(url)) url = decodeURI(url);
-      }
-      catch(e){}
-
-      var addr = parse_url(url);
-      if( !addr.host ) addr.host = ""; // must not be 'undefined'
-
-      url = // rebuild the url
-        (!addr.scheme ? '' : addr.scheme +
-        ( (addr.scheme.toLowerCase() === "mailto" || addr.scheme.toLowerCase() === "xmpp") ? ':' : '://')) +
-        (!addr.user ? '' : addr.user +
-        (!addr.pass ? '' : ':'+addr.pass) + '@') +
-        punycode.toASCII(addr.host) +
-        (!addr.port ? '' : ':' + addr.port) +
-        (!addr.path ? '' : encodeURI(addr.path) ) +
-        (!addr.query ? '' : '?' + encodeURI(addr.query) ) +
-        (!addr.fragment ? '' : '#' + encodeURI(addr.fragment) );
-      return url;
-    };
-
     var md = window.markdownit({
       breaks:      true,
       html:        true,
@@ -41,14 +19,13 @@
                                                .replace(/<3/g,  "♥");
     });
 
-    md.use(inlinePlugin, 'link_new_window_and_punycode', 'link_open', function (tokens, idx) {
-      tokens[idx].href = tokens[idx].href.replace(/^www\./, "http://www.");
-      tokens[idx].href = punycodeURL(tokens[idx].href);
-      tokens[idx].target = "_blank";
-    });
-
-    md.use(inlinePlugin, 'image_punycode', 'image', function (tokens, idx) {
-      tokens[idx].src = punycodeURL(tokens[idx].src);
+    md.use(inlinePlugin, 'link_new_window_and_missing_http', 'link_open', function (tokens, idx) {
+      tokens[idx].attrs.forEach(function(attribute, index, array) {
+        if( attribute[0] === 'href' ) {
+          array[index][1] = attribute[1].replace(/^www\./, "http://www.");
+        }
+      });
+      tokens[idx].attrPush([ 'target', "_blank" ]);
     });
 
     var hashtagPlugin = window.markdownitHashtag;
diff --git a/app/assets/javascripts/main.js b/app/assets/javascripts/main.js
index 4e8122f05f..aaa0c50ea6 100644
--- a/app/assets/javascripts/main.js
+++ b/app/assets/javascripts/main.js
@@ -31,8 +31,6 @@
 //= require markdown-it-sanitizer
 //= require markdown-it-sub
 //= require markdown-it-sup
-//= require punycode
-//= require parse_url
 //= require clear-form
 //= require app/app
 //= require diaspora
diff --git a/spec/javascripts/app/helpers/text_formatter_spec.js b/spec/javascripts/app/helpers/text_formatter_spec.js
index cbdfbe6ac6..7b2a564528 100644
--- a/spec/javascripts/app/helpers/text_formatter_spec.js
+++ b/spec/javascripts/app/helpers/text_formatter_spec.js
@@ -119,6 +119,7 @@ describe("app.helpers.textFormatter", function(){
     it("adds a missing http://", function() {
       expect(this.formatter('[test](www.google.com)')).toContain('href="http://www.google.com"');
       expect(this.formatter('[test](http://www.google.com)')).toContain('href="http://www.google.com"');
+      expect(this.formatter('www.google.com')).toContain('href="http://www.google.com"');
     });
 
     it("respects code blocks", function() {
@@ -185,6 +186,13 @@ describe("app.helpers.textFormatter", function(){
         }, this);
       });
 
+      it("correctly encodes image src to punycode", function() {
+        _.each(this.evilUrls, function(url, num) {
+          var text = this.formatter("![](" + url + ")");
+          expect(text).toContain(this.asciiUrls[num]);
+        }, this);
+      });
+
       it("doesn't break link texts", function() {
         var linkText = "check out this awesome link!";
         var text = this.formatter( "["+linkText+"]("+this.evilUrls[0]+")" );
@@ -251,12 +259,6 @@ describe("app.helpers.textFormatter", function(){
           var parsed = this.formatter(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(uglyUrl);
-          expect(parsed).toContain(this.correctHref);
-        });
       });
 
       it("doesn't fail for misc urls", function() {
diff --git a/vendor/assets/javascripts/parse_url.js b/vendor/assets/javascripts/parse_url.js
deleted file mode 100644
index d45b3466fb..0000000000
--- a/vendor/assets/javascripts/parse_url.js
+++ /dev/null
@@ -1,88 +0,0 @@
-// source: https://github.com/kvz/phpjs/blob/master/functions/url/parse_url.js
-// commit 4966dea
-// 28 Dec 2014
-
-function parse_url(str, component) {
-  //       discuss at: http://phpjs.org/functions/parse_url/
-  //      original by: Steven Levithan (http://blog.stevenlevithan.com)
-  // reimplemented by: Brett Zamir (http://brett-zamir.me)
-  //         input by: Lorenzo Pisani
-  //         input by: Tony
-  //      improved by: Brett Zamir (http://brett-zamir.me)
-  //             note: original by http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
-  //             note: blog post at http://blog.stevenlevithan.com/archives/parseuri
-  //             note: demo at http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
-  //             note: Does not replace invalid characters with '_' as in PHP, nor does it return false with
-  //             note: a seriously malformed URL.
-  //             note: Besides function name, is essentially the same as parseUri as well as our allowing
-  //             note: an extra slash after the scheme/protocol (to allow file:/// as in PHP)
-  //        example 1: parse_url('http://username:password@hostname/path?arg=value#anchor');
-  //        returns 1: {scheme: 'http', host: 'hostname', user: 'username', pass: 'password', path: '/path', query: 'arg=value', fragment: 'anchor'}
-  //        example 2: parse_url('http://en.wikipedia.org/wiki/%22@%22_%28album%29');
-  //        returns 2: {scheme: 'http', host: 'en.wikipedia.org', path: '/wiki/%22@%22_%28album%29'}
-  //        example 3: parse_url('https://host.domain.tld/a@b.c/folder')
-  //        returns 3: {scheme: 'https', host: 'host.domain.tld', path: '/a@b.c/folder'}
-  //        example 4: parse_url('https://gooduser:secretpassword@www.example.com/a@b.c/folder?foo=bar');
-  //        returns 4: { scheme: 'https', host: 'www.example.com', path: '/a@b.c/folder', query: 'foo=bar', user: 'gooduser', pass: 'secretpassword' }
-
-  try {
-    this.php_js = this.php_js || {};
-  } catch (e) {
-    this.php_js = {};
-  }
-
-  var query;
-  var ini  = (this.php_js && this.php_js.ini) || {};
-  var mode = (ini['phpjs.parse_url.mode'] && ini['phpjs.parse_url.mode'].local_value) || 'php';
-  var key  = [
-    'source',
-    'scheme',
-    'authority',
-    'userInfo',
-    'user',
-    'pass',
-    'host',
-    'port',
-    'relative',
-    'path',
-    'directory',
-    'file',
-    'query',
-    'fragment'
-  ];
-  var parser = {
-    php   : /^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@\/]*):?([^:@\/]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
-    strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@\/]*):?([^:@\/]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
-    loose : /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@\/]*):?([^:@\/]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-scheme to catch file:/// (should restrict this)
-  };
-
-  var m   = parser[mode].exec(str);
-  var uri = {};
-  var i   = 14;
-
-  while (i--) {
-    if (m[i]) {
-      uri[key[i]] = m[i];
-    }
-  }
-
-  if (component) {
-    return uri[component.replace('PHP_URL_', '').toLowerCase()];
-  }
-
-  if (mode !== 'php') {
-    var name = (ini['phpjs.parse_url.queryKey'] &&
-      ini['phpjs.parse_url.queryKey'].local_value) || 'queryKey';
-    parser    = /(?:^|&)([^&=]*)=?([^&]*)/g;
-    uri[name] = {};
-    query     = uri[key[12]] || '';
-    query.replace(parser, function ($0, $1, $2) {
-      if ($1) {
-        uri[name][$1] = $2;
-      }
-    });
-  }
-
-  delete uri.source;
-  return uri;
-}
-- 
GitLab