diff --git a/boot/boot.js b/boot/boot.js index 98adce691..bb006e661 100644 --- a/boot/boot.js +++ b/boot/boot.js @@ -1885,13 +1885,13 @@ $tw.loadTiddlersFromSpecification = function(filepath,excludeRegExp) { value = path.basename(filename); break; case "filename-uri-decoded": - value = decodeURIComponent(path.basename(filename)); + value = $tw.utils.decodeURIComponentSafe(path.basename(filename)); break; case "basename": value = path.basename(filename,path.extname(filename)); break; case "basename-uri-decoded": - value = decodeURIComponent(path.basename(filename,path.extname(filename))); + value = $tw.utils.decodeURIComponentSafe(path.basename(filename,path.extname(filename))); break; case "extname": value = path.extname(filename); diff --git a/core/modules/filters/encodings.js b/core/modules/filters/encodings.js index a41086604..c99687fe2 100644 --- a/core/modules/filters/encodings.js +++ b/core/modules/filters/encodings.js @@ -19,12 +19,7 @@ Export our filter functions exports.decodeuricomponent = function(source,operator,options) { var results = []; source(function(tiddler,title) { - var value = title; - try { - value = decodeURIComponent(title); - } catch(e) { - } - results.push(value); + results.push($tw.utils.decodeURIComponentSafe(title)); }); return results; }; @@ -40,12 +35,7 @@ exports.encodeuricomponent = function(source,operator,options) { exports.decodeuri = function(source,operator,options) { var results = []; source(function(tiddler,title) { - var value = title; - try { - value = decodeURI(title); - } catch(e) { - } - results.push(value); + results.push($tw.utils.decodeURISafe(title)); }); return results; }; diff --git a/core/modules/savers/andtidwiki.js b/core/modules/savers/andtidwiki.js index df2b0ceaa..9ab405e37 100644 --- a/core/modules/savers/andtidwiki.js +++ b/core/modules/savers/andtidwiki.js @@ -42,7 +42,7 @@ AndTidWiki.prototype.save = function(text,method,callback,options) { window.twi.saveWiki(text); } else { // Get the pathname of this document - var pathname = decodeURIComponent(document.location.toString().split("#")[0]); + var pathname = $tw.utils.decodeURIComponentSafe(document.location.toString().split("#")[0]); // Strip the file:// if(pathname.indexOf("file://") === 0) { pathname = pathname.substr(7); diff --git a/core/modules/savers/download.js b/core/modules/savers/download.js index 3f5740dcd..0a1d565ce 100644 --- a/core/modules/savers/download.js +++ b/core/modules/savers/download.js @@ -26,7 +26,7 @@ DownloadSaver.prototype.save = function(text,method,callback,options) { var p = document.location.pathname.lastIndexOf("/"); if(p !== -1) { // We decode the pathname because document.location is URL encoded by the browser - filename = decodeURIComponent(document.location.pathname.substr(p+1)); + filename = $tw.utils.decodeURIComponentSafe(document.location.pathname.substr(p+1)); } } if(!filename) { diff --git a/core/modules/savers/tiddlyfox.js b/core/modules/savers/tiddlyfox.js index 3abe31afe..81b373d42 100644 --- a/core/modules/savers/tiddlyfox.js +++ b/core/modules/savers/tiddlyfox.js @@ -43,7 +43,7 @@ TiddlyFoxSaver.prototype.save = function(text,method,callback) { } // Create the message element and put it in the message box var message = document.createElement("div"); - message.setAttribute("data-tiddlyfox-path",decodeURIComponent(pathname)); + message.setAttribute("data-tiddlyfox-path",$tw.utils.decodeURIComponentSafe(pathname)); message.setAttribute("data-tiddlyfox-content",text); messageBox.appendChild(message); // Add an event handler for when the file has been saved diff --git a/core/modules/savers/twedit.js b/core/modules/savers/twedit.js index 6907a542c..ec125bf94 100644 --- a/core/modules/savers/twedit.js +++ b/core/modules/savers/twedit.js @@ -21,7 +21,7 @@ TWEditSaver.prototype.save = function(text,method,callback) { return false; } // Get the pathname of this document - var pathname = decodeURIComponent(document.location.pathname); + var pathname = $tw.utils.decodeURIComponentSafe(document.location.pathname); // Strip any query or location part var p = pathname.indexOf("?"); if(p !== -1) { diff --git a/core/modules/server/routes/delete-tiddler.js b/core/modules/server/routes/delete-tiddler.js index d04020c75..85b552599 100644 --- a/core/modules/server/routes/delete-tiddler.js +++ b/core/modules/server/routes/delete-tiddler.js @@ -17,7 +17,7 @@ exports.method = "DELETE"; exports.path = /^\/bags\/default\/tiddlers\/(.+)$/; exports.handler = function(request,response,state) { - var title = decodeURIComponent(state.params[0]); + var title = $tw.utils.decodeURIComponentSafe(state.params[0]); state.wiki.deleteTiddler(title); response.writeHead(204, "OK", { "Content-Type": "text/plain" diff --git a/core/modules/server/routes/get-file.js b/core/modules/server/routes/get-file.js index 44c79329e..45aa51d34 100644 --- a/core/modules/server/routes/get-file.js +++ b/core/modules/server/routes/get-file.js @@ -20,7 +20,7 @@ exports.handler = function(request,response,state) { var path = require("path"), fs = require("fs"), util = require("util"), - suppliedFilename = decodeURIComponent(state.params[0]), + suppliedFilename = $tw.utils.decodeURIComponentSafe(state.params[0]), baseFilename = path.resolve(state.boot.wikiPath,"files"), filename = path.resolve(baseFilename,suppliedFilename), extension = path.extname(filename); diff --git a/core/modules/server/routes/get-tiddler-html.js b/core/modules/server/routes/get-tiddler-html.js index 1a1b0c5e5..ab1570e30 100644 --- a/core/modules/server/routes/get-tiddler-html.js +++ b/core/modules/server/routes/get-tiddler-html.js @@ -17,7 +17,7 @@ exports.method = "GET"; exports.path = /^\/([^\/]+)$/; exports.handler = function(request,response,state) { - var title = decodeURIComponent(state.params[0]), + var title = $tw.utils.decodeURIComponentSafe(state.params[0]), tiddler = state.wiki.getTiddler(title); if(tiddler) { var renderType = tiddler.getFieldString("_render_type"), diff --git a/core/modules/server/routes/get-tiddler.js b/core/modules/server/routes/get-tiddler.js index 4db7be012..ae6cfeadc 100644 --- a/core/modules/server/routes/get-tiddler.js +++ b/core/modules/server/routes/get-tiddler.js @@ -17,7 +17,7 @@ exports.method = "GET"; exports.path = /^\/recipes\/default\/tiddlers\/(.+)$/; exports.handler = function(request,response,state) { - var title = decodeURIComponent(state.params[0]), + var title = $tw.utils.decodeURIComponentSafe(state.params[0]), tiddler = state.wiki.getTiddler(title), tiddlerFields = {}, knownFields = [ diff --git a/core/modules/server/routes/put-tiddler.js b/core/modules/server/routes/put-tiddler.js index f6e49e5b2..f1148e247 100644 --- a/core/modules/server/routes/put-tiddler.js +++ b/core/modules/server/routes/put-tiddler.js @@ -17,7 +17,7 @@ exports.method = "PUT"; exports.path = /^\/recipes\/default\/tiddlers\/(.+)$/; exports.handler = function(request,response,state) { - var title = decodeURIComponent(state.params[0]), + var title = $tw.utils.decodeURIComponentSafe(state.params[0]), fields = JSON.parse(state.data); // Pull up any subfields in the `fields` object if(fields.fields) { diff --git a/core/modules/startup/story.js b/core/modules/startup/story.js index 18f8e8f1d..f8ce38ff2 100644 --- a/core/modules/startup/story.js +++ b/core/modules/startup/story.js @@ -120,10 +120,10 @@ function openStartupTiddlers(options) { var hash = $tw.locationHash.substr(1), split = hash.indexOf(":"); if(split === -1) { - target = decodeURIComponent(hash.trim()); + target = $tw.utils.decodeURIComponentSafe(hash.trim()); } else { - target = decodeURIComponent(hash.substr(0,split).trim()); - storyFilter = decodeURIComponent(hash.substr(split + 1).trim()); + target = $tw.utils.decodeURIComponentSafe(hash.substr(0,split).trim()); + storyFilter = $tw.utils.decodeURIComponentSafe(hash.substr(split + 1).trim()); } } // If the story wasn't specified use the current tiddlers or a blank story diff --git a/core/modules/utils/dom/dragndrop.js b/core/modules/utils/dom/dragndrop.js index de30d49c7..3e10d456d 100644 --- a/core/modules/utils/dom/dragndrop.js +++ b/core/modules/utils/dom/dragndrop.js @@ -164,7 +164,7 @@ var importDataTypes = [ }}, {type: "URL", IECompatible: true, toTiddlerFieldsArray: function(data,fallbackTitle) { // Check for tiddler data URI - var match = decodeURIComponent(data).match(/^data\:text\/vnd\.tiddler,(.*)/i); + var match = $tw.utils.decodeURIComponentSafe(data).match(/^data\:text\/vnd\.tiddler,(.*)/i); if(match) { return parseJSONTiddlers(match[1],fallbackTitle); } else { @@ -173,7 +173,7 @@ var importDataTypes = [ }}, {type: "text/x-moz-url", IECompatible: false, toTiddlerFieldsArray: function(data,fallbackTitle) { // Check for tiddler data URI - var match = decodeURIComponent(data).match(/^data\:text\/vnd\.tiddler,(.*)/i); + var match = $tw.utils.decodeURIComponentSafe(data).match(/^data\:text\/vnd\.tiddler,(.*)/i); if(match) { return parseJSONTiddlers(match[1],fallbackTitle); } else { diff --git a/core/modules/utils/utils.js b/core/modules/utils/utils.js index 7ba9373ec..d2f60537f 100644 --- a/core/modules/utils/utils.js +++ b/core/modules/utils/utils.js @@ -969,4 +969,22 @@ exports.makeCompareFunction = function(type,options) { return (types[type] || types[options.defaultType] || types.number); }; +exports.decodeURIComponentSafe = function(str) { + var value = str; + try { + value = decodeURIComponent(str); + } catch(e) { + } + return value; +}; + +exports.decodeURISafe = function(str) { + var value = str; + try { + value = decodeURI(str); + } catch(e) { + } + return value; +}; + })(); diff --git a/plugins/tiddlywiki/markdown/wrapper.js b/plugins/tiddlywiki/markdown/wrapper.js index ab24a10c4..cf673dce8 100755 --- a/plugins/tiddlywiki/markdown/wrapper.js +++ b/plugins/tiddlywiki/markdown/wrapper.js @@ -149,7 +149,7 @@ function convertNodes(remarkableTree, isStartOfInline) { out.push({ type: "link", attributes: { - to: { type: "string", value: decodeURI(currentNode.href.substr(1)) } + to: { type: "string", value: $tw.utils.decodeURISafe(currentNode.href.substr(1)) } }, children: children }); @@ -180,7 +180,7 @@ function convertNodes(remarkableTree, isStartOfInline) { type: "image", attributes: { tooltip: { type: "string", value: currentNode.alt }, - source: { type: "string", value: decodeURIComponent(currentNode.src) } + source: { type: "string", value: $tw.utils.decodeURIComponentSafe(currentNode.src) } } }); break; diff --git a/plugins/tiddlywiki/share/rawmarkup.js b/plugins/tiddlywiki/share/rawmarkup.js index c0ab77659..4286126a7 100644 --- a/plugins/tiddlywiki/share/rawmarkup.js +++ b/plugins/tiddlywiki/share/rawmarkup.js @@ -17,7 +17,7 @@ var rawHash = document.location.hash.substring(1); if(rawHash.charAt(0) === "#") { var hash; try{ - hash = decodeURIComponent(rawHash.substring(1)); + hash = $tw.utils.decodeURIComponentSafe(rawHash.substring(1)); } catch(ex) { console.log("Share plugin: Error decoding location hash",ex); } diff --git a/plugins/tiddlywiki/tiddlyweb/tiddlywebadaptor.js b/plugins/tiddlywiki/tiddlyweb/tiddlywebadaptor.js index b31610deb..d6fcfb901 100644 --- a/plugins/tiddlywiki/tiddlyweb/tiddlywebadaptor.js +++ b/plugins/tiddlywiki/tiddlyweb/tiddlywebadaptor.js @@ -346,8 +346,8 @@ TiddlyWebAdaptor.prototype.parseEtag = function(etag) { return null; } else { return { - bag: decodeURIComponent(etag.substring(1,firstSlash)), - title: decodeURIComponent(etag.substring(firstSlash + 1,lastSlash)), + bag: $tw.utils.decodeURIComponentSafe(etag.substring(1,firstSlash)), + title: $tw.utils.decodeURIComponentSafe(etag.substring(firstSlash + 1,lastSlash)), revision: etag.substring(lastSlash + 1,colon) }; }