Skip to content

Commit

Permalink
Bug 1416328 - Part 2. Expose decoding attribute for img elements. r=b…
Browse files Browse the repository at this point in the history
…z,tnikkel

This adds support for HTMLImageElement's decoding attribute, as
described by:

whatwg/html#3221
https://whatpr.org/html/3221/images.html#decoding-images

It also exposes the same attribute on SVGImageElement, just as Blink has
chosen to do so.

UltraBlame original commit: 630049a9ac3b24ec0a141669937df372373a5d6f
  • Loading branch information
marco-c committed Oct 3, 2019
1 parent 6f9c7ae commit b199b42
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 14 deletions.
9 changes: 9 additions & 0 deletions dom/base/nsImageLoadingContent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ static void PrintReqURL(imgIRequest* req) {
}
#endif

const nsAttrValue::EnumTable nsImageLoadingContent::kDecodingTable[] = {
{ "auto", nsImageLoadingContent::ImageDecodingType::Auto },
{ "async", nsImageLoadingContent::ImageDecodingType::Async },
{ "sync", nsImageLoadingContent::ImageDecodingType::Sync },
{ nullptr, 0 }
};

const nsAttrValue::EnumTable* nsImageLoadingContent::kDecodingTableDefault =
&nsImageLoadingContent::kDecodingTable[0];

nsImageLoadingContent::nsImageLoadingContent()
: mCurrentRequestFlags(0),
Expand Down
12 changes: 11 additions & 1 deletion dom/base/nsImageLoadingContent.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "nsIContentPolicy.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/net/ReferrerPolicy.h"
#include "nsAttrValue.h"

class nsIURI;
class nsIDocument;
Expand Down Expand Up @@ -233,6 +234,15 @@ class nsImageLoadingContent : public nsIImageLoadingContent

virtual nsIContent* AsContent() = 0;

enum class ImageDecodingType : uint8_t {
Auto,
Async,
Sync,
};

static const nsAttrValue::EnumTable kDecodingTable[];
static const nsAttrValue::EnumTable* kDecodingTableDefault;

private:


Expand Down Expand Up @@ -545,4 +555,4 @@ class nsImageLoadingContent : public nsIImageLoadingContent
bool mSyncDecodingHint;
};

#endif
#endif
31 changes: 24 additions & 7 deletions dom/html/HTMLImageElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ HTMLImageElement::Y()
return GetXY().y;
}

void
HTMLImageElement::GetDecoding(nsAString& aValue)
{
GetEnumAttr(nsGkAtoms::decoding, kDecodingTableDefault->tag, aValue);
}

bool
HTMLImageElement::ParseAttribute(int32_t aNamespaceID,
nsAtom* aAttribute,
Expand All @@ -237,6 +243,10 @@ HTMLImageElement::ParseAttribute(int32_t aNamespaceID,
ParseCORSValue(aValue, aResult);
return true;
}
if (aAttribute == nsGkAtoms::decoding) {
return aResult.ParseEnumValue(aValue, kDecodingTable, false,
kDecodingTableDefault);
}
if (ParseImageAttribute(aAttribute, aValue, aResult)) {
return true;
}
Expand Down Expand Up @@ -378,6 +388,12 @@ HTMLImageElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
mUseUrgentStartForChannel = EventStateManager::IsHandlingUserInput();

PictureSourceSizesChanged(this, attrVal.String(), aNotify);
} else if (aName == nsGkAtoms::decoding &&
aNameSpaceID == kNameSpaceID_None) {

SetSyncDecodingHint(aValue &&
static_cast<ImageDecodingType>(aValue->GetEnumValue()) ==
ImageDecodingType::Sync);
}

return nsGenericHTMLElement::AfterSetAttr(aNameSpaceID, aName,
Expand Down Expand Up @@ -797,14 +813,15 @@ HTMLImageElement::NaturalWidth()
}

nsresult
HTMLImageElement::CopyInnerTo(HTMLImageElement* aDest)
HTMLImageElement::CopyInnerTo(Element* aDest, bool aPreallocateChildren)
{
bool destIsStatic = aDest->OwnerDoc()->IsStaticDocument();
auto dest = static_cast<HTMLImageElement*>(aDest);
if (destIsStatic) {
CreateStaticImageClone(aDest);
CreateStaticImageClone(dest);
}

nsresult rv = nsGenericHTMLElement::CopyInnerTo(aDest);
nsresult rv = nsGenericHTMLElement::CopyInnerTo(aDest, aPreallocateChildren);
if (NS_FAILED(rv)) {
return rv;
}
Expand All @@ -814,16 +831,16 @@ HTMLImageElement::CopyInnerTo(HTMLImageElement* aDest)



if (!aDest->InResponsiveMode() &&
aDest->HasAttr(kNameSpaceID_None, nsGkAtoms::src) &&
aDest->OwnerDoc()->ShouldLoadImages()) {
if (!dest->InResponsiveMode() &&
dest->HasAttr(kNameSpaceID_None, nsGkAtoms::src) &&
dest->OwnerDoc()->ShouldLoadImages()) {


mUseUrgentStartForChannel = EventStateManager::IsHandlingUserInput();

nsContentUtils::AddScriptRunner(
NewRunnableMethod<bool>("dom::HTMLImageElement::MaybeLoadImage",
aDest,
dest,
&HTMLImageElement::MaybeLoadImage,
false));
}
Expand Down
10 changes: 8 additions & 2 deletions dom/html/HTMLImageElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ class HTMLImageElement final : public nsGenericHTMLElement,
virtual void UnbindFromTree(bool aDeep, bool aNullParent) override;

virtual EventStates IntrinsicState() const override;
virtual nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
virtual nsresult Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult,
bool aPreallocateChildren) const override;

virtual void NodeInfoChanged(nsIDocument* aOldDoc) override;

nsresult CopyInnerTo(HTMLImageElement* aDest);
nsresult CopyInnerTo(Element* aDest, bool aPreallocateChildren);

void MaybeLoadImage(bool aAlwaysForceLoad);

Expand Down Expand Up @@ -232,6 +233,11 @@ class HTMLImageElement final : public nsGenericHTMLElement,
{
GetEnumAttr(nsGkAtoms::referrerpolicy, EmptyCString().get(), aReferrer);
}
void SetDecoding(const nsAString& aDecoding, ErrorResult& aError)
{
SetHTMLAttr(nsGkAtoms::decoding, aDecoding, aError);
}
void GetDecoding(nsAString& aValue);

net::ReferrerPolicy
GetImageReferrerPolicy() override
Expand Down
34 changes: 32 additions & 2 deletions dom/svg/SVGImageElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ SVGImageElement::Href()
: mStringAttributes[XLINK_HREF].ToDOMAnimatedString(this);
}

void
SVGImageElement::GetDecoding(nsAString& aValue)
{
GetEnumAttr(nsGkAtoms::decoding, kDecodingTableDefault->tag, aValue);
}



nsresult
Expand Down Expand Up @@ -152,6 +158,24 @@ SVGImageElement::AsyncEventRunning(AsyncEventDispatcher* aEvent)



bool
SVGImageElement::ParseAttribute(int32_t aNamespaceID,
nsAtom* aAttribute,
const nsAString& aValue,
nsIPrincipal* aMaybeScriptedPrincipal,
nsAttrValue& aResult)
{
if (aNamespaceID == kNameSpaceID_None) {
if (aAttribute == nsGkAtoms::decoding) {
return aResult.ParseEnumValue(aValue, kDecodingTable, false,
kDecodingTableDefault);
}
}

return SVGImageElementBase::ParseAttribute(aNamespaceID, aAttribute, aValue,
aMaybeScriptedPrincipal, aResult);
}

nsresult
SVGImageElement::AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
const nsAttrValue* aValue,
Expand All @@ -168,6 +192,12 @@ SVGImageElement::AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
} else {
CancelImageRequests(aNotify);
}
} else if (aName == nsGkAtoms::decoding &&
aNamespaceID == kNameSpaceID_None) {

SetSyncDecodingHint(aValue &&
static_cast<ImageDecodingType>(aValue->GetEnumValue())
== ImageDecodingType::Sync);
}
return SVGImageElementBase::AfterSetAttr(aNamespaceID, aName,
aValue, aOldValue,
Expand Down Expand Up @@ -315,12 +345,12 @@ SVGImageElement::GetStringInfo()
}

nsresult
SVGImageElement::CopyInnerTo(Element* aDest)
SVGImageElement::CopyInnerTo(Element* aDest, bool aPreallocateChildren)
{
if (aDest->OwnerDoc()->IsStaticDocument()) {
CreateStaticImageClone(static_cast<SVGImageElement*>(aDest));
}
return SVGImageElementBase::CopyInnerTo(aDest);
return SVGImageElementBase::CopyInnerTo(aDest, aPreallocateChildren);
}

}
Expand Down
16 changes: 14 additions & 2 deletions dom/svg/SVGImageElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class SVGImageElement : public SVGImageElementBase,
virtual void AsyncEventRunning(AsyncEventDispatcher* aEvent) override;


bool ParseAttribute(int32_t aNamespaceID,
nsAtom* aAttribute,
const nsAString& aValue,
nsIPrincipal* aMaybeScriptedPrincipal,
nsAttrValue& aResult) override;
virtual nsresult AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
const nsAttrValue* aValue,
const nsAttrValue* aOldValue,
Expand All @@ -67,9 +72,10 @@ class SVGImageElement : public SVGImageElementBase,

virtual bool HasValidDimensions() const override;

virtual nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
virtual nsresult Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult,
bool aPreallocateChildren) const override;

nsresult CopyInnerTo(mozilla::dom::Element* aDest);
nsresult CopyInnerTo(mozilla::dom::Element* aDest, bool aPreallocateChildren);

void MaybeLoadSVGImage();

Expand All @@ -81,6 +87,12 @@ class SVGImageElement : public SVGImageElementBase,
already_AddRefed<DOMSVGAnimatedPreserveAspectRatio> PreserveAspectRatio();
already_AddRefed<SVGAnimatedString> Href();

void SetDecoding(const nsAString& aDecoding, ErrorResult& aError)
{
SetAttr(nsGkAtoms::decoding, aDecoding, aError);
}
void GetDecoding(nsAString& aValue);

protected:
nsresult LoadSVGImage(bool aForce, bool aNotify);

Expand Down
2 changes: 2 additions & 0 deletions dom/webidl/HTMLImageElement.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ interface HTMLImageElement : HTMLElement {
attribute unsigned long width;
[CEReactions, SetterThrows]
attribute unsigned long height;
[CEReactions, SetterThrows]
attribute DOMString decoding;
readonly attribute unsigned long naturalWidth;
readonly attribute unsigned long naturalHeight;
readonly attribute boolean complete;
Expand Down
2 changes: 2 additions & 0 deletions dom/webidl/SVGImageElement.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ interface SVGImageElement : SVGGraphicsElement {
readonly attribute SVGAnimatedLength height;
[Constant]
readonly attribute SVGAnimatedPreserveAspectRatio preserveAspectRatio;
[CEReactions, SetterThrows]
attribute DOMString decoding;
};

SVGImageElement implements MozImageLoadingContent;
Expand Down
1 change: 1 addition & 0 deletions xpcom/ds/nsGkAtomList.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ GK_ATOM(decimalFormat, "decimal-format")
GK_ATOM(decimalSeparator, "decimal-separator")
GK_ATOM(declare, "declare")
GK_ATOM(decoderDoctor, "decoder-doctor")
GK_ATOM(decoding, "decoding")
GK_ATOM(decrement, "decrement")
GK_ATOM(_default, "default")
GK_ATOM(headerDefaultStyle, "default-style")
Expand Down

0 comments on commit b199b42

Please sign in to comment.