NS_IMETHODIMP nsIOService::EscapeString(const nsACString& aString, uint32_t aEscapeType, nsACString& aResult) { NS_ENSURE_ARG_MAX(aEscapeType, 4); nsAutoCString stringCopy(aString); nsCString result; if (!NS_Escape(stringCopy, result, (nsEscapeMask) aEscapeType)) return NS_ERROR_OUT_OF_MEMORY; aResult.Assign(result); return NS_OK; }
nsresult nsXHTMLContentSerializer::EscapeURI(nsIContent* aContent, const nsAString& aURI, nsAString& aEscapedURI) { // URL escape %xx cannot be used in JS. // No escaping if the scheme is 'javascript'. if (IsJavaScript(aContent, nsGkAtoms::href, kNameSpaceID_None, aURI)) { aEscapedURI = aURI; return NS_OK; } // nsITextToSubURI does charset convert plus uri escape // This is needed to convert to a document charset which is needed to support existing browsers. // But we eventually want to use UTF-8 instead of a document charset, then the code would be much simpler. // See HTML 4.01 spec, "Appendix B.2.1 Non-ASCII characters in URI attribute values" nsCOMPtr<nsITextToSubURI> textToSubURI; nsAutoString uri(aURI); // in order to use FindCharInSet() nsresult rv = NS_OK; if (!mCharset.IsEmpty() && !IsASCII(uri)) { textToSubURI = do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); } int32_t start = 0; int32_t end; nsAutoString part; nsXPIDLCString escapedURI; aEscapedURI.Truncate(0); // Loop and escape parts by avoiding escaping reserved characters // (and '%', '#', as well as '[' and ']' for IPv6 address literals). while ((end = uri.FindCharInSet("%#;/?:@&=+$,[]", start)) != -1) { part = Substring(aURI, start, (end-start)); if (textToSubURI && !IsASCII(part)) { rv = textToSubURI->ConvertAndEscape(mCharset.get(), part.get(), getter_Copies(escapedURI)); NS_ENSURE_SUCCESS(rv, rv); } else if (NS_WARN_IF(!NS_Escape(NS_ConvertUTF16toUTF8(part), escapedURI, url_Path))) { return NS_ERROR_OUT_OF_MEMORY; } AppendASCIItoUTF16(escapedURI, aEscapedURI); // Append a reserved character without escaping. part = Substring(aURI, end, 1); aEscapedURI.Append(part); start = end + 1; } if (start < (int32_t) aURI.Length()) { // Escape the remaining part. part = Substring(aURI, start, aURI.Length()-start); if (textToSubURI) { rv = textToSubURI->ConvertAndEscape(mCharset.get(), part.get(), getter_Copies(escapedURI)); NS_ENSURE_SUCCESS(rv, rv); } else if (NS_WARN_IF(!NS_Escape(NS_ConvertUTF16toUTF8(part), escapedURI, url_Path))) { return NS_ERROR_OUT_OF_MEMORY; } AppendASCIItoUTF16(escapedURI, aEscapedURI); } return rv; }
nsresult FileSystemDataSource::GetFolderList(nsIRDFResource *source, bool allowHidden, bool onlyFirst, nsISimpleEnumerator** aResult) { if (!isDirURI(source)) return(NS_RDF_NO_VALUE); nsresult rv; const char *parentURI = nullptr; rv = source->GetValueConst(&parentURI); if (NS_FAILED(rv)) return(rv); if (!parentURI) return(NS_ERROR_UNEXPECTED); nsCOMPtr<nsIURI> aIURI; if (NS_FAILED(rv = NS_NewURI(getter_AddRefs(aIURI), nsDependentCString(parentURI)))) return(rv); nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aIURI); if (!fileURL) return NS_OK; nsCOMPtr<nsIFile> aDir; if (NS_FAILED(rv = fileURL->GetFile(getter_AddRefs(aDir)))) return(rv); // ensure that we DO NOT resolve aliases aDir->SetFollowLinks(false); nsCOMPtr<nsISimpleEnumerator> dirContents; if (NS_FAILED(rv = aDir->GetDirectoryEntries(getter_AddRefs(dirContents)))) return(rv); if (!dirContents) return(NS_ERROR_UNEXPECTED); nsCOMArray<nsIRDFResource> resources; bool hasMore; while(NS_SUCCEEDED(rv = dirContents->HasMoreElements(&hasMore)) && hasMore) { nsCOMPtr<nsISupports> isupports; if (NS_FAILED(rv = dirContents->GetNext(getter_AddRefs(isupports)))) break; nsCOMPtr<nsIFile> aFile = do_QueryInterface(isupports); if (!aFile) break; if (!allowHidden) { bool hiddenFlag = false; if (NS_FAILED(rv = aFile->IsHidden(&hiddenFlag))) break; if (hiddenFlag) continue; } nsAutoString leafStr; if (NS_FAILED(rv = aFile->GetLeafName(leafStr))) break; if (leafStr.IsEmpty()) continue; nsAutoCString fullURI; fullURI.Assign(parentURI); if (fullURI.Last() != '/') { fullURI.Append('/'); } nsAutoCString leaf; bool escaped = NS_Escape(NS_ConvertUTF16toUTF8(leafStr), leaf, url_Path); leafStr.Truncate(); if (!escaped) { continue; } // using nsEscape() [above] doesn't escape slashes, so do that by hand int32_t aOffset; while ((aOffset = leaf.FindChar('/')) >= 0) { leaf.Cut((uint32_t)aOffset, 1); leaf.Insert("%2F", (uint32_t)aOffset); } // append the encoded name fullURI.Append(leaf); bool dirFlag = false; rv = aFile->IsDirectory(&dirFlag); if (NS_SUCCEEDED(rv) && dirFlag) { fullURI.Append('/'); } nsCOMPtr<nsIRDFResource> fileRes; mRDFService->GetResource(fullURI, getter_AddRefs(fileRes)); resources.AppendObject(fileRes); if (onlyFirst) break; } return NS_NewArrayEnumerator(aResult, resources); }