📄 kurlgoogle.cpp
字号:
m_string = String::fromUTF8(m_utf8.data(), m_utf8.length()); m_stringIsValid = true; } return m_string;}// KURL ------------------------------------------------------------------------// Creates with NULL-terminated string input representing an absolute URL.// WebCore generally calls this only with hardcoded strings, so the input is// ASCII. We treat is as UTF-8 just in case.KURL::KURL(const char *url){ // FIXME The Mac code checks for beginning with a slash and converting to a // file: URL. We will want to add this as well once we can compile on a // system like that. m_url.init(KURL(), url, strlen(url), 0); // The one-argument constructors should never generate a NULL string. // This is a funny quirk of KURL.cpp (probably a bug) which we preserve. if (m_url.utf8String().isNull()) m_url.setAscii(CString("", 0));}// Initializes with a string representing an absolute URL. No encoding// information is specified. This generally happens when a KURL is converted// to a string and then converted back. In this case, the URL is already// canonical and in proper escaped form so needs no encoding. We treat it was// UTF-8 just in case.KURL::KURL(const String& url){ if (!url.isNull()) m_url.init(KURL(), url, 0); else { // WebCore expects us to preserve the nullness of strings when this // constructor is used. In all other cases, it expects a non-null // empty string, which is what init() will create. m_url.m_isValid = false; m_url.m_protocolInHTTPFamily = false; }}// Constructs a new URL given a base URL and a possibly relative input URL.// This assumes UTF-8 encoding.KURL::KURL(const KURL& base, const String& relative){ m_url.init(base, relative, 0);}// Constructs a new URL given a base URL and a possibly relative input URL.// Any query portion of the relative URL will be encoded in the given encoding.KURL::KURL(const KURL& base, const String& relative, const TextEncoding& encoding){ m_url.init(base, relative, &encoding.encodingForFormSubmission());}KURL::KURL(const CString& canonicalSpec, const url_parse::Parsed& parsed, bool isValid) : m_url(parsed, isValid){ // We know the reference fragment is the only part that can be UTF-8, so // we know it's ASCII when there is no ref. if (parsed.ref.is_nonempty()) m_url.setUtf8(canonicalSpec); else m_url.setAscii(canonicalSpec);}#if PLATFORM(CF)KURL::KURL(CFURLRef){ notImplemented(); invalidate();}CFURLRef KURL::createCFURL() const{ notImplemented(); return 0;}#endifKURL KURL::copy() const{ KURL result = *this; m_url.copyTo(&result.m_url); return result;}bool KURL::isNull() const{ return m_url.utf8String().isNull();}bool KURL::isEmpty() const{ return !m_url.utf8String().length();}bool KURL::isValid() const{ return m_url.m_isValid;}bool KURL::protocolInHTTPFamily() const{ return m_url.m_protocolInHTTPFamily;}bool KURL::hasPath() const{ // Note that http://www.google.com/" has a path, the path is "/". This can // return false only for invalid or nonstandard URLs. return m_url.m_parsed.path.len >= 0;}// We handle "parameters" separated by a semicolon, while KURL.cpp does not,// which can lead to different results in some cases.String KURL::lastPathComponent() const{ // When the output ends in a slash, WebCore has different expectations than // the GoogleURL library. For "/foo/bar/" the library will return the empty // string, but WebCore wants "bar". url_parse::Component path = m_url.m_parsed.path; if (path.len > 0 && m_url.utf8String().data()[path.end() - 1] == '/') path.len--; url_parse::Component file; url_parse::ExtractFileName(m_url.utf8String().data(), path, &file); // Bug: https://bugs.webkit.org/show_bug.cgi?id=21015 this function returns // a null string when the path is empty, which we duplicate here. if (!file.is_nonempty()) return String(); return m_url.componentString(file);}String KURL::protocol() const{ return m_url.componentString(m_url.m_parsed.scheme);}String KURL::host() const{ // Note: KURL.cpp unescapes here. return m_url.componentString(m_url.m_parsed.host);}// Returns 0 when there is no port or it is invalid.//// We treat URL's with out-of-range port numbers as invalid URLs, and they will// be rejected by the canonicalizer. KURL.cpp will allow them in parsing, but// return 0 from this port() function, so we mirror that behavior here.unsigned short KURL::port() const{ if (!m_url.m_isValid || m_url.m_parsed.port.len <= 0) return 0; int port = url_parse::ParsePort(m_url.utf8String().data(), m_url.m_parsed.port); if (port == url_parse::PORT_UNSPECIFIED) return 0; return static_cast<unsigned short>(port);}// Returns the empty string if there is no password.String KURL::pass() const{ // Bug: https://bugs.webkit.org/show_bug.cgi?id=21015 this function returns // a null string when the password is empty, which we duplicate here. if (!m_url.m_parsed.password.is_nonempty()) return String(); // Note: KURL.cpp unescapes here. return m_url.componentString(m_url.m_parsed.password);}// Returns the empty string if there is no username.String KURL::user() const{ // Note: KURL.cpp unescapes here. return m_url.componentString(m_url.m_parsed.username);}String KURL::ref() const{ // Empty but present refs ("foo.com/bar#") should result in the empty // string, which m_url.componentString will produce. Nonexistant refs should be // the NULL string. if (!m_url.m_parsed.ref.is_valid()) return String(); // Note: KURL.cpp unescapes here. return m_url.componentString(m_url.m_parsed.ref);}bool KURL::hasRef() const{ // Note: KURL.cpp unescapes here. // FIXME determine if KURL.cpp agrees about an empty ref return m_url.m_parsed.ref.len >= 0;}String KURL::query() const{ if (m_url.m_parsed.query.len >= 0) { // KURL's query() includes the question mark, even though the reference // doesn't. Move the query component backwards one to account for it // (our library doesn't count the question mark). url_parse::Component queryComp = m_url.m_parsed.query; queryComp.begin--; queryComp.len++; return m_url.componentString(queryComp); } // Bug: https://bugs.webkit.org/show_bug.cgi?id=21015 this function returns // an empty string when the query is empty rather than a null (not sure // which is right). return String("", 0);}String KURL::path() const{ // Note: KURL.cpp unescapes here. return m_url.componentString(m_url.m_parsed.path);}void KURL::setProtocol(const String& protocol){ KURLGooglePrivate::Replacements replacements; replacements.SetScheme(CharactersOrEmpty(protocol), url_parse::Component(0, protocol.length())); m_url.replaceComponents(replacements);}void KURL::setHost(const String& host){ KURLGooglePrivate::Replacements replacements; replacements.SetHost(CharactersOrEmpty(host), url_parse::Component(0, host.length())); m_url.replaceComponents(replacements);}// This function is used only in the JSC build.void KURL::setHostAndPort(const String& s){ String newhost = s.left(s.find(":")); String newport = s.substring(s.find(":") + 1); KURLGooglePrivate::Replacements replacements; // Host can't be removed, so we always set. replacements.SetHost(CharactersOrEmpty(newhost), url_parse::Component(0, newhost.length())); if (newport.isEmpty()) // Port may be removed, so we support clearing. replacements.ClearPort(); else replacements.SetPort(CharactersOrEmpty(newport), url_parse::Component(0, newport.length())); m_url.replaceComponents(replacements);}void KURL::setPort(unsigned short i){ KURLGooglePrivate::Replacements replacements; String portStr; if (i) { portStr = String::number(static_cast<int>(i)); replacements.SetPort( reinterpret_cast<const url_parse::UTF16Char*>(portStr.characters()), url_parse::Component(0, portStr.length())); } else { // Clear any existing port when it is set to 0. replacements.ClearPort(); } m_url.replaceComponents(replacements);}void KURL::setUser(const String& user){ // This function is commonly called to clear the username, which we // normally don't have, so we optimize this case. if (user.isEmpty() && !m_url.m_parsed.username.is_valid()) return; // The canonicalizer will clear any usernames that are empty, so we // don't have to explicitly call ClearUsername() here. KURLGooglePrivate::Replacements replacements; replacements.SetUsername(CharactersOrEmpty(user), url_parse::Component(0, user.length())); m_url.replaceComponents(replacements);}void KURL::setPass(const String& pass){ // This function is commonly called to clear the password, which we // normally don't have, so we optimize this case. if (pass.isEmpty() && !m_url.m_parsed.password.is_valid()) return; // The canonicalizer will clear any passwords that are empty, so we // don't have to explicitly call ClearUsername() here. KURLGooglePrivate::Replacements replacements; replacements.SetPassword(CharactersOrEmpty(pass), url_parse::Component(0, pass.length())); m_url.replaceComponents(replacements);}void KURL::setRef(const String& ref){ // This function is commonly called to clear the ref, which we // normally don't have, so we optimize this case. if (ref.isNull() && !m_url.m_parsed.ref.is_valid()) return; KURLGooglePrivate::Replacements replacements;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -