edit/page: enforce relative links to local domain
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 16 Dec 2020 13:05:11 +0000 (14:05 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 21 Dec 2020 16:46:33 +0000 (17:46 +0100)
Replace <a href> and <img src> urls on save to fix common user mistake
breaking site deployment on a different [dev] domain.

edit/page.js

index f927c1fd248c2d746996e791ed51e1676432b8d5..45d9e644cb9c28c4ede56d46f62b575bdb6813a4 100644 (file)
@@ -28,6 +28,10 @@ CKEDITOR.plugins.add('inlinesave', {
                                });
                                // treat standalone placeholders as block elements
                                body = body.replace(/<p>(\[\[.*\]\])<\/p>/g, '$1');
+                               // strip redundant domain in relative urls
+                               var hostlink = 'https?://' + window.location.hostname.replace(/\W/g, '\\$&');
+                               hostlink = new RegExp('(<a [^>]*href="|<img [^>]*src=")' + hostlink + '(?=/)', 'gi');
+                               body = body.replace(hostlink, '$1');
 
                                var data = 'body='+encodeURIComponent(body);
                                var ajaxpost = new XMLHttpRequest();