Bug 1238804. Make <base> actually work in a srcdoc document. r=smaug

This commit is contained in:
Boris Zbarsky 2016-01-13 21:36:24 -05:00
parent b6ccf7a9ba
commit 030f3395b7
3 changed files with 36 additions and 8 deletions

View File

@ -349,17 +349,32 @@ public:
}
/**
* Return the base URI for relative URIs in the document (the document uri
* unless it's overridden by SetBaseURI, HTML <base> tags, etc.). The
* returned URI could be null if there is no document URI. If the document
* is a srcdoc document, return the parent document's base URL.
* Return the fallback base URL for this document, as defined in the HTML
* specification. Note that this can return null if there is no document URI.
*
* XXXbz: This doesn't implement the bits for about:blank yet.
*/
nsIURI* GetDocBaseURI() const
nsIURI* GetFallbackBaseURI() const
{
if (mIsSrcdocDocument && mParentDocument) {
return mParentDocument->GetDocBaseURI();
}
return mDocumentBaseURI ? mDocumentBaseURI : mDocumentURI;
return mDocumentURI;
}
/**
* Return the base URI for relative URIs in the document (the document uri
* unless it's overridden by SetBaseURI, HTML <base> tags, etc.). The
* returned URI could be null if there is no document URI. If the document is
* a srcdoc document and has no explicit base URL, return the parent
* document's base URL.
*/
nsIURI* GetDocBaseURI() const
{
if (mDocumentBaseURI) {
return mDocumentBaseURI;
}
return GetFallbackBaseURI();
}
virtual already_AddRefed<nsIURI> GetBaseURI(bool aTryUseXHRDocBaseURI = false) const override;

View File

@ -164,14 +164,15 @@ SetBaseURIUsingFirstBaseWithHref(nsIDocument* aDocument, nsIContent* aMustMatch)
return;
}
// Resolve the <base> element's href relative to our document URI
// Resolve the <base> element's href relative to our document's
// fallback base URI.
nsAutoString href;
child->GetAttr(kNameSpaceID_None, nsGkAtoms::href, href);
nsCOMPtr<nsIURI> newBaseURI;
nsContentUtils::NewURIWithDocumentCharset(
getter_AddRefs(newBaseURI), href, aDocument,
aDocument->GetDocumentURI());
aDocument->GetFallbackBaseURI());
// Try to set our base URI. If that fails, try to set base URI to null
nsresult rv = aDocument->SetBaseURI(newBaseURI);

View File

@ -77,5 +77,17 @@
iframe.setAttribute("srcdoc", "<p>foobar</p>");
document.body.appendChild(iframe);
}, "The fallback base URL of an iframe srcdoc document is the document base URL of the document's browsing context's browsing context container's document.");
async_test(function () {
var iframe = document.createElement("iframe");
iframe.onload = this.step_func_done(function () {
var doc = iframe.contentDocument;
assert_resolve_url(doc, location.href.replace("/document-base-url.html", "/sub"));
assert_equals(doc.baseURI, document.baseURI.replace("/document-base-url.html", "/sub/"),
"The srcdoc document's base URL should be set by the <base> tag.");
});
iframe.setAttribute("srcdoc", "<base href='sub/'><p>foobar</p>");
document.body.appendChild(iframe);
}, "The base URL of an iframe srcdoc document with a <base> tag should be set by that tag.");
</script>
</body>