Hello all,
after getting one more small but pesky problem with url handling I decided
that it may be worth discussion on the core level.
In short, I miss (may be just not aware of) core tools that:
* check if a string (url) is a relative link
* resolve relative url (and remove as much "/./" and "/../" parts as
possible)
* turn url into some "standart notation" (so two urls that point the same
file can be different but in that "notation" are the same)
* "concatenating" urls
If you have some quick solutions (inside the core, including jQuery),
please let me know.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
DETAILS
Now, let's consider those tasks and applications in details.
In SharedTiddlersPlugin [1], all the tasks rise. To figure if the url is
relative, a small function inherited from Udo's codes is used:
var isRelativeURL = function(url) {
// as Unix filesystem root is "/", urls starting with it are not considered
as relative
return (url.search(/^(?:((http(s)?)|(file)):)|(.\:\\)|(\\\\)|(\/)/) !=
0);
};
It's somewhat satisfactory, although it supports only few protocols. Also,
it probably should be turned into a method, but I don't like to make such a
method of String..
Next thing is actually "concatenating" urls (at least in my implementation
it is used when resolving urls). It is done via
var getPathFromURL = function(url) {
return (url.lastIndexOf("/") > -1) ?
url.substr(0, url.lastIndexOf("/") + 1) : "";
};
like this:
if(isRelativeURL(url1))
url1 = getPathFromURL(url2) + url1;
or in the next version using this:
var resolveUrlFrom(urlToResolve, sourceUrl) {
return (isRelativeURL(urlToResolve) && sourceUrl) ?
getPathFromURL(sourceUrl) + urlToResolve : urlToResolve;
};
This simple implementation doesn't check if urlToResolve contains "./" or
"../" or "../../" in the beginning and hence can make a url "nonstandart"
even the two arguments were.
Resolving urls in the next version is done via
// limitedly turns URI (URL) reference into an absolute URI (URL) and
windows paths into URL
var stp_resolveURL = function(url) {
if (url.search(/^((http(s)?)|(file)):/) != 0) {
// no protocol prefix..
if (isRelativeURL(url))
resolveUrlFrom(url, document.location.toString());
else
// "url" is an "absolute" path to a local file. Prefix it with
file://
url = "file://" + url;
// replace every \ by a /, to cover Windows style pathes
url = url.replace(/\\/mg,"/");
}
return url;
};
This has the drawback of limited number of protocal prefixes, too (and may
be some others). Yet, STP is not the only extension which needs these
tools. For instance, SaveAsPlugin [2] turned out to have an issue with
relative paths in the (unfortunately undocumented) "target:" parameter.
Current behaviour is, when "target:./some/relative/path/file.ext" is used,
in Opera saving is done to the Opera folder: if you take a look at the
calculation of the link variable in the go method, you'll see that the
"file:///" prefix is added to the url without resolving it. For tweaking
that myself, I need to a) copy my suboptimal solution to this plugin as
well or b) use some library method, which seems preferable.
Finally, standardizing urls is not implemented in STP yet. At first glance,
it should consist of resolving, URL decoding and URL encoding and removing
"./" and "../" parts, but I'm not sure if that's all.
I'm interested to hear what you guys think about stratagy of "where to
place/develop succh functions" (or if core/jQuery has something alike, may
be that's not of concern).
Best regards,
Yakov.
[1] http://yakovl.bplaced.net/TW/STP/STP.html#SharedTiddlersPluginCode
[2] http://www.TiddlyTools.com/#SaveAsPlugin
--
You received this message because you are subscribed to the Google Groups
"TiddlyWikiDev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/tiddlywikidev.
For more options, visit https://groups.google.com/groups/opt_out.