Эквивалент Python urljoin в AS3 - PullRequest
1 голос
/ 04 октября 2011

В Python есть функция urljoin, которая берет два URL-адреса и объединяет их разумно. Есть ли библиотека, которая обеспечивает аналогичную функцию в AS3?

документация по urljoin: http://docs.python.org/library/urlparse.html

И пример с питоном:

>>> urljoin('http://www.cwi.nl/doc/Python.html', '../res/jer.png')
'http://www.cwi.nl/res/jer.png'

Я хочу знать, есть ли реализация функции urljoin, а не весь пакет urlparse

Ответы [ 2 ]

3 голосов
/ 08 октября 2011

Например, вы можете использовать URI класс из as3corelib

Использование:

import com.adobe.net.URI;

// create uri you want to be updated
var newURI:URI=new URI('../res/jer.png')

// update newURI with the full path
newURI.makeAbsoluteURI(new URI('http://www.cwi.nl/doc/Python.html')) 

trace(uri.toString()) // will output http://www.cwi.nl/res/jer.png

// or make an utility function base on it:

function urljoin(url1:string, url2:String):String {
  var uri:URI=new URI(url2)
  uri.makeAbsoluteURI(url1)
  return uri.toString()
}
2 голосов
/ 08 октября 2011

Некоторый необработанный код делает то, что вы хотите, поэтому вы можете пропустить все раздутые библиотеки:

var urlJoin:Function = function(base:String, relative:String):String
{
    // See if there is already a protocol on this
    if (relative.indexOf("://") != -1)
        return relative;

    // See if this is protocol-relative
    if (relative.indexOf("//") == 0)
    {
        var protocolIndex:int = base.indexOf("://");
        return base.substr(0, protocolIndex+1) + relative;
    }

    // We need to split the domain and the path for the remaining options
    var protocolIndexEnd:int = base.indexOf("://") + 3;
    if (base.indexOf("/", protocolIndexEnd) == -1) // append slash if passed only http://bla.com
        base += "/";
    var endDomainIndex:int = base.indexOf("/", protocolIndexEnd);
    var domain:String = base.substr(0, endDomainIndex);
    var path:String = base.substr(endDomainIndex);
    if (path.lastIndexOf("/") != path.length-1) // trim off any ending file name
        path = path.substr(0, path.lastIndexOf("/")+1);

    // See if this is site-absolute
    if (relative.indexOf("/") == 0)
    {
        return domain + relative;
    }

    // See if this is document-relative with ../
    while (relative.indexOf("../") == 0)
    {
        relative = relative.substr(3);
        if (path.length > 1)
        {
            var secondToLastSlashIndex:int = path.substr(0, path.length-1).lastIndexOf("/");
            path = path.substr(0, secondToLastSlashIndex+1);
        }
    }   
    // Finally, slap on whatever ending is left
    return domain + path + relative;
};
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...