Skip to content

Commit

Permalink
Bug 353575 - Allow IDTracker to look up elements in <svg:use> shadow …
Browse files Browse the repository at this point in the history
…trees. r=smaug

Other UAs allow this, and it seems in the general consensus of
WICG/webcomponents#179.

This matches WebKit's behavior. Blink, for some reason shows red on the
test-case, probably because they're not doing quite this, but they
manage to render masks inside the display: none symbol element or such.

Differential Revision: https://phabricator.services.mozilla.com/D72610

UltraBlame original commit: 8efff752175e177108adceddb73a60f98f8cdd27
  • Loading branch information
marco-c committed Apr 28, 2020
1 parent 60135dc commit 5ff2e58
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
37 changes: 29 additions & 8 deletions dom/base/IDTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,39 @@
#include "IDTracker.h"

#include "mozilla/Encoding.h"
#include "mozilla/dom/DocumentOrShadowRoot.h"
#include "nsAtom.h"
#include "nsContentUtils.h"
#include "nsIURI.h"
#include "nsIReferrerInfo.h"
#include "nsEscape.h"
#include "nsCycleCollectionParticipant.h"
#include "nsStringFwd.h"

namespace mozilla {
namespace dom {

static DocumentOrShadowRoot* DocOrShadowFromContent(nsIContent& aContent) {
static Element* LookupElement(DocumentOrShadowRoot& aDocOrShadow,
const nsAString& aRef, bool aReferenceImage) {
if (aReferenceImage) {
return aDocOrShadow.LookupImageElement(aRef);
}
return aDocOrShadow.GetElementById(aRef);
}

static DocumentOrShadowRoot* FindTreeToWatch(nsIContent& aContent,
const nsAString& aID,
bool aReferenceImage) {
ShadowRoot* shadow = aContent.GetContainingShadow();


while (shadow && shadow->Host()->IsSVGElement(nsGkAtoms::use)) {



if (LookupElement(*shadow, aID, aReferenceImage)) {
return shadow;
}
shadow = shadow->Host()->GetContainingShadow();
}

Expand Down Expand Up @@ -49,7 +68,6 @@ void IDTracker::ResetToURIFragmentID(nsIContent* aFromContent, nsIURI* aURI,


Document* doc = aFromContent->OwnerDoc();
DocumentOrShadowRoot* docOrShadow = DocOrShadowFromContent(*aFromContent);
auto encoding = doc->GetDocumentCharacterSet();

nsAutoString ref;
Expand All @@ -75,6 +93,7 @@ void IDTracker::ResetToURIFragmentID(nsIContent* aFromContent, nsIURI* aURI,

bool isEqualExceptRef;
rv = aURI->EqualsExceptRef(doc->GetDocumentURI(), &isEqualExceptRef);
DocumentOrShadowRoot* docOrShadow;
if (NS_FAILED(rv) || !isEqualExceptRef) {
RefPtr<Document::ExternalResourceLoad> load;
doc = doc->RequestExternalResource(aURI, aReferrerInfo, aFromContent,
Expand All @@ -92,6 +111,8 @@ void IDTracker::ResetToURIFragmentID(nsIContent* aFromContent, nsIURI* aURI,
load->AddObserver(observer);

}
} else {
docOrShadow = FindTreeToWatch(*aFromContent, ref, aReferenceImage);
}

if (aWatch) {
Expand All @@ -111,8 +132,10 @@ void IDTracker::ResetWithID(Element& aFrom, nsAtom* aID, bool aWatch) {

mReferencingImage = false;

DocumentOrShadowRoot* docOrShadow = DocOrShadowFromContent(aFrom);
HaveNewDocumentOrShadowRoot(docOrShadow, aWatch, nsDependentAtomString(aID));
nsDependentAtomString str(aID);
DocumentOrShadowRoot* docOrShadow =
FindTreeToWatch(aFrom, str, false);
HaveNewDocumentOrShadowRoot(docOrShadow, aWatch, str);
}

void IDTracker::HaveNewDocumentOrShadowRoot(DocumentOrShadowRoot* aDocOrShadow,
Expand All @@ -131,9 +154,7 @@ void IDTracker::HaveNewDocumentOrShadowRoot(DocumentOrShadowRoot* aDocOrShadow,
return;
}

Element* e = mReferencingImage ? aDocOrShadow->LookupImageElement(aRef)
: aDocOrShadow->GetElementById(aRef);
if (e) {
if (Element* e = LookupElement(*aDocOrShadow, aRef, mReferencingImage)) {
mElement = e;
}
}
Expand Down Expand Up @@ -189,7 +210,7 @@ NS_IMETHODIMP
IDTracker::DocumentLoadNotification::Observe(nsISupports* aSubject,
const char* aTopic,
const char16_t* aData) {
NS_ASSERTION(PL_strcmp(aTopic, "external-resource-document-created") == 0,
NS_ASSERTION(!strcmp(aTopic, "external-resource-document-created"),
"Unexpected topic");
if (mTarget) {
nsCOMPtr<Document> doc = do_QueryInterface(aSubject);
Expand Down
4 changes: 2 additions & 2 deletions layout/reftests/svg/fragid-shadow-7.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
<svg width="100" height="100">
<defs>
<pattern id="rect" width="100" height="100">
<rect fill="lime" width="100" height="100" />
<rect fill="red" width="100" height="100" />
</pattern>
<symbol id="useme">
<pattern id="rect" width="100" height="100">
<rect fill="red" width="100" height="100" />
<rect fill="lime" width="100" height="100" />
</pattern>
<rect fill="url(#rect)" width="100" height="100" />
</symbol>
Expand Down
4 changes: 2 additions & 2 deletions layout/reftests/svg/fragid-shadow-8.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<svg width="100" height="100">
<defs>
<pattern id="rect" width="100" height="100">
<rect fill="lime" width="100" height="100" />
<rect fill="red" width="100" height="100" />
</pattern>
<symbol id="useme">
<pattern id="rect" width="100" height="100">
<rect fill="red" width="100" height="100" />
<rect fill="lime" width="100" height="100" />
</pattern>
<rect fill="url(#rect)" width="100" height="100" />
</symbol>
Expand Down

0 comments on commit 5ff2e58

Please sign in to comment.