mirror of
https://github.com/ariya/phantomjs.git
synced 2024-10-26 14:29:13 +03:00
Add phantom.resolveRelativeUrl and phantom.fullyDecodeUrl.
These utility functions make it easier to work with encoded URLs. See issues #12216 and #11035.
This commit is contained in:
parent
1c203a3927
commit
305cc4467a
@ -434,6 +434,19 @@ void Phantom::debugExit(int code)
|
||||
doExit(code);
|
||||
}
|
||||
|
||||
QString Phantom::resolveRelativeUrl(QString url, QString base)
|
||||
{
|
||||
QUrl u = QUrl::fromEncoded(url.toLatin1());
|
||||
QUrl b = QUrl::fromEncoded(base.toLatin1());
|
||||
|
||||
return b.resolved(u).toEncoded();
|
||||
}
|
||||
|
||||
QString Phantom::fullyDecodeUrl(QString url)
|
||||
{
|
||||
return QUrl::fromEncoded(url.toLatin1()).toDisplayString();
|
||||
}
|
||||
|
||||
// private slots:
|
||||
void Phantom::printConsoleMessage(const QString& message)
|
||||
{
|
||||
|
@ -174,6 +174,40 @@ public slots:
|
||||
void exit(int code = 0);
|
||||
void debugExit(int code = 0);
|
||||
|
||||
// URL utilities
|
||||
|
||||
/**
|
||||
* Resolve a URL relative to a base.
|
||||
*/
|
||||
QString resolveRelativeUrl(QString url, QString base);
|
||||
|
||||
/**
|
||||
* Decode a URL to human-readable form.
|
||||
* @param url The URL to be decoded.
|
||||
*
|
||||
* This operation potentially destroys information. It should only be
|
||||
* used when displaying URLs to the user, not when recording URLs for
|
||||
* later processing. Quoting http://qt-project.org/doc/qt-5/qurl.html:
|
||||
*
|
||||
* _Full decoding_
|
||||
*
|
||||
* This [operation] should be used with care, since there are
|
||||
* two conditions that cannot be reliably represented in the
|
||||
* returned QString. They are:
|
||||
*
|
||||
* + Non-UTF-8 sequences: URLs may contain sequences of
|
||||
* percent-encoded characters that do not form valid UTF-8
|
||||
* sequences. Since URLs need to be decoded using UTF-8, any
|
||||
* decoder failure will result in the QString containing one or
|
||||
* more replacement characters where the sequence existed.
|
||||
*
|
||||
* + Encoded delimiters: URLs are also allowed to make a
|
||||
* distinction between a delimiter found in its literal form and
|
||||
* its equivalent in percent-encoded form. This is most commonly
|
||||
* found in the query, but is permitted in most parts of the URL.
|
||||
*/
|
||||
QString fullyDecodeUrl(QString url);
|
||||
|
||||
signals:
|
||||
void aboutToExit(int code);
|
||||
|
||||
|
16
test/basics/url-utils.js
Normal file
16
test/basics/url-utils.js
Normal file
@ -0,0 +1,16 @@
|
||||
// These are cursory tests; we assume the underlying Qt
|
||||
// features are properly tested elsewhere.
|
||||
|
||||
test(function () {
|
||||
assert_equals(
|
||||
phantom.resolveRelativeUrl(
|
||||
"../scripts/foo.js",
|
||||
"http://example.com/topic/page.html"),
|
||||
"http://example.com/scripts/foo.js");
|
||||
|
||||
assert_equals(
|
||||
phantom.fullyDecodeUrl(
|
||||
"https://ja.wikipedia.org/wiki/%E8%87%A8%E6%B5%B7%E5%AD%A6%E6%A0%A1"),
|
||||
"https://ja.wikipedia.org/wiki/臨海学校");
|
||||
|
||||
}, "resolveRelativeUrl and fullyDecodeUrl");
|
Loading…
Reference in New Issue
Block a user