Bug 1660378 - part 9: Avoid QI for getting nsStyledElement pointer r=smaug

The editor modules does QI too many times when it sets or removes some style
with `execCommand` or XPCOM API.  Therefore, there should be an API to
retrieve `nsStyledElement` pointer from `nsINode*`.

Differential Revision: https://phabricator.services.mozilla.com/D87990
This commit is contained in:
Masayuki Nakano 2020-08-26 05:29:06 +00:00
parent b500d7d549
commit 5bca4199fb
13 changed files with 194 additions and 124 deletions

View File

@ -702,7 +702,8 @@ void Animation::CommitStyles(ErrorResult& aRv) {
}
// Check it is an element with a style attribute
nsCOMPtr<nsStyledElement> styledElement = do_QueryInterface(target.mElement);
RefPtr<nsStyledElement> styledElement =
nsStyledElement::FromNodeOrNull(target.mElement);
if (!styledElement) {
return aRv.ThrowNoModificationAllowedError(
"Target is not capable of having a style attribute");

View File

@ -522,6 +522,11 @@ class nsINode : public mozilla::dom::EventTarget {
inline mozilla::dom::Element* AsElement();
inline const mozilla::dom::Element* AsElement() const;
/**
* Return whether the node is an nsStyledElement instance or not.
*/
virtual bool IsStyledElement() const { return false; }
/**
* Return this node as nsIContent. Should only be used for nodes for which
* IsContent() is true.

View File

@ -53,6 +53,9 @@ class nsStyledElement : public nsStyledElementBase {
nsICSSDeclaration* Style();
NS_DECLARE_STATIC_IID_ACCESSOR(NS_STYLED_ELEMENT_IID)
NS_IMPL_FROMNODE_HELPER(nsStyledElement, IsStyledElement());
bool IsStyledElement() const final { return true; }
protected:
nsICSSDeclaration* GetExistingStyle();

View File

@ -179,9 +179,8 @@ class EditorDOMPointBase final {
return mParent->AsElement();
}
already_AddRefed<nsStyledElement> GetContainerAsStyledElement() const {
nsCOMPtr<nsStyledElement> styledElement = do_QueryInterface(mParent);
return styledElement.forget();
nsStyledElement* GetContainerAsStyledElement() const {
return nsStyledElement::FromNodeOrNull(mParent);
}
dom::Text* GetContainerAsText() const {

View File

@ -315,8 +315,8 @@ nsresult HTMLEditor::RefreshGrabberInternal() {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsStyledElement> grabberStyledElement =
do_QueryInterface(mGrabber.get());
RefPtr<nsStyledElement> grabberStyledElement =
nsStyledElement::FromNodeOrNull(mGrabber.get());
if (!grabberStyledElement) {
return NS_OK;
}
@ -446,8 +446,8 @@ nsresult HTMLEditor::StartMoving() {
"Element::UnsetAttr(nsGkAtoms::_class) failed, but ignored");
// position it
if (nsCOMPtr<nsStyledElement> positioningShadowStyledElement =
do_QueryInterface(mPositioningShadow.get())) {
if (RefPtr<nsStyledElement> positioningShadowStyledElement =
nsStyledElement::FromNode(mPositioningShadow.get())) {
nsresult rv;
rv = mCSSEditUtils->SetCSSPropertyPixelsWithTransaction(
*positioningShadowStyledElement, *nsGkAtoms::width,
@ -561,8 +561,8 @@ nsresult HTMLEditor::SetFinalPosition(int32_t aX, int32_t aY) {
if (NS_WARN_IF(!mAbsolutelyPositionedObject)) {
return NS_ERROR_FAILURE;
}
if (nsCOMPtr<nsStyledElement> styledAbsolutelyPositionedElement =
do_QueryInterface(mAbsolutelyPositionedObject)) {
if (RefPtr<nsStyledElement> styledAbsolutelyPositionedElement =
nsStyledElement::FromNode(mAbsolutelyPositionedObject)) {
nsresult rv;
rv = mCSSEditUtils->SetCSSPropertyPixelsWithTransaction(
*styledAbsolutelyPositionedElement, *nsGkAtoms::top, newY);
@ -645,10 +645,12 @@ nsresult HTMLEditor::SetPositionToAbsolute(Element& aElement) {
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
"HTMLEditor::GetElementOrigin() failed, but ignored");
nsCOMPtr<nsStyledElement> styledElement = do_QueryInterface(&aElement);
nsStyledElement* styledElement = nsStyledElement::FromNode(&aElement);
if (styledElement) {
// MOZ_KnownLive(*styledElement): aElement's lifetime must be guarantted
// by the caller because of MOZ_CAN_RUN_SCRIPT method.
nsresult rv = mCSSEditUtils->SetCSSPropertyWithTransaction(
*styledElement, *nsGkAtoms::position, u"absolute"_ns);
MOZ_KnownLive(*styledElement), *nsGkAtoms::position, u"absolute"_ns);
if (rv == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
"CSSEditUtils::SetCSSProperyWithTransaction(nsGkAtoms::Position) "
@ -664,7 +666,10 @@ nsresult HTMLEditor::SetPositionToAbsolute(Element& aElement) {
AddPositioningOffset(x, y);
SnapToGrid(x, y);
if (styledElement) {
nsresult rv = SetTopAndLeftWithTransaction(*styledElement, x, y);
// MOZ_KnownLive(*styledElement): aElement's lifetime must be guarantted
// by the caller because of MOZ_CAN_RUN_SCRIPT method.
nsresult rv =
SetTopAndLeftWithTransaction(MOZ_KnownLive(*styledElement), x, y);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::SetTopAndLeftWithTransaction() failed");
return rv;
@ -685,7 +690,7 @@ nsresult HTMLEditor::SetPositionToAbsolute(Element& aElement) {
}
nsresult HTMLEditor::SetPositionToStatic(Element& aElement) {
nsCOMPtr<nsStyledElement> styledElement = do_QueryInterface(&aElement);
nsStyledElement* styledElement = nsStyledElement::FromNode(&aElement);
if (NS_WARN_IF(!styledElement)) {
return NS_ERROR_INVALID_ARG;
}
@ -694,8 +699,10 @@ nsresult HTMLEditor::SetPositionToStatic(Element& aElement) {
ScrollSelectionIntoView::Yes);
nsresult rv;
// MOZ_KnownLive(*styledElement): aElement's lifetime must be guarantted
// by the caller because of MOZ_CAN_RUN_SCRIPT method.
rv = mCSSEditUtils->RemoveCSSPropertyWithTransaction(
*styledElement, *nsGkAtoms::position, EmptyString());
MOZ_KnownLive(*styledElement), *nsGkAtoms::position, EmptyString());
if (rv == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
"CSSEditUtils::RemoveCSSPropertyWithTransaction(nsGkAtoms::position) "
@ -706,8 +713,10 @@ nsresult HTMLEditor::SetPositionToStatic(Element& aElement) {
NS_SUCCEEDED(rv),
"CSSEditUtils::RemoveCSSPropertyWithTransaction(nsGkAtoms::position) "
"failed, but ignored");
// MOZ_KnownLive(*styledElement): aElement's lifetime must be guarantted
// by the caller because of MOZ_CAN_RUN_SCRIPT method.
rv = mCSSEditUtils->RemoveCSSPropertyWithTransaction(
*styledElement, *nsGkAtoms::top, EmptyString());
MOZ_KnownLive(*styledElement), *nsGkAtoms::top, EmptyString());
if (rv == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
"CSSEditUtils::RemoveCSSPropertyWithTransaction(nsGkAtoms::top) "
@ -718,8 +727,10 @@ nsresult HTMLEditor::SetPositionToStatic(Element& aElement) {
NS_SUCCEEDED(rv),
"CSSEditUtils::RemoveCSSPropertyWithTransaction(nsGkAtoms::top) "
"failed, but ignored");
// MOZ_KnownLive(*styledElement): aElement's lifetime must be guarantted
// by the caller because of MOZ_CAN_RUN_SCRIPT method.
rv = mCSSEditUtils->RemoveCSSPropertyWithTransaction(
*styledElement, *nsGkAtoms::left, EmptyString());
MOZ_KnownLive(*styledElement), *nsGkAtoms::left, EmptyString());
if (rv == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
"CSSEditUtils::RemoveCSSPropertyWithTransaction(nsGkAtoms::left) "
@ -730,8 +741,10 @@ nsresult HTMLEditor::SetPositionToStatic(Element& aElement) {
NS_SUCCEEDED(rv),
"CSSEditUtils::RemoveCSSPropertyWithTransaction(nsGkAtoms::left) "
"failed, but ignored");
// MOZ_KnownLive(*styledElement): aElement's lifetime must be guarantted
// by the caller because of MOZ_CAN_RUN_SCRIPT method.
rv = mCSSEditUtils->RemoveCSSPropertyWithTransaction(
*styledElement, *nsGkAtoms::z_index, EmptyString());
MOZ_KnownLive(*styledElement), *nsGkAtoms::z_index, EmptyString());
if (rv == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
"CSSEditUtils::RemoveCSSPropertyWithTransaction(nsGkAtoms::z_index) "
@ -744,8 +757,10 @@ nsresult HTMLEditor::SetPositionToStatic(Element& aElement) {
"failed, but ignored");
if (!HTMLEditUtils::IsImage(styledElement)) {
// MOZ_KnownLive(*styledElement): aElement's lifetime must be guarantted
// by the caller because of MOZ_CAN_RUN_SCRIPT method.
rv = mCSSEditUtils->RemoveCSSPropertyWithTransaction(
*styledElement, *nsGkAtoms::width, EmptyString());
MOZ_KnownLive(*styledElement), *nsGkAtoms::width, EmptyString());
if (rv == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
"CSSEditUtils::RemoveCSSPropertyWithTransaction(nsGkAtoms::width) "
@ -756,8 +771,10 @@ nsresult HTMLEditor::SetPositionToStatic(Element& aElement) {
NS_SUCCEEDED(rv),
"CSSEditUtils::RemoveCSSPropertyWithTransaction(nsGkAtoms::width) "
"failed, but ignored");
// MOZ_KnownLive(*styledElement): aElement's lifetime must be guarantted
// by the caller because of MOZ_CAN_RUN_SCRIPT method.
rv = mCSSEditUtils->RemoveCSSPropertyWithTransaction(
*styledElement, *nsGkAtoms::height, EmptyString());
MOZ_KnownLive(*styledElement), *nsGkAtoms::height, EmptyString());
if (rv == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
"CSSEditUtils::RemoveCSSPropertyWithTransaction(nsGkAtoms::height) "
@ -777,17 +794,23 @@ nsresult HTMLEditor::SetPositionToStatic(Element& aElement) {
// Make sure the first fild and last child of aElement starts/ends hard
// line(s) even after removing `aElement`.
rv = EnsureHardLineBeginsWithFirstChildOf(*styledElement);
// MOZ_KnownLive(*styledElement): aElement's lifetime must be guarantted
// by the caller because of MOZ_CAN_RUN_SCRIPT method.
rv = EnsureHardLineBeginsWithFirstChildOf(MOZ_KnownLive(*styledElement));
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::EnsureHardLineBeginsWithFirstChildOf() failed");
return rv;
}
rv = EnsureHardLineEndsWithLastChildOf(*styledElement);
// MOZ_KnownLive(*styledElement): aElement's lifetime must be guarantted
// by the caller because of MOZ_CAN_RUN_SCRIPT method.
rv = EnsureHardLineEndsWithLastChildOf(MOZ_KnownLive(*styledElement));
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::EnsureHardLineEndsWithLastChildOf() failed");
return rv;
}
rv = RemoveContainerWithTransaction(*styledElement);
// MOZ_KnownLive(*styledElement): aElement's lifetime must be guarantted
// by the caller because of MOZ_CAN_RUN_SCRIPT method.
rv = RemoveContainerWithTransaction(MOZ_KnownLive(*styledElement));
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}

View File

@ -8663,6 +8663,8 @@ nsresult HTMLEditor::AlignNodesAndDescendants(
HTMLEditUtils::IsAnyListElement(content)) {
Element* listOrListItemElement = content->AsElement();
AutoEditorDOMPointOffsetInvalidator lockChild(atContent);
// MOZ_KnownLive(*listOrListItemElement): An element of aArrayOfContents
// which is array of OwningNonNull.
nsresult rv = RemoveAlignFromDescendants(
MOZ_KnownLive(*listOrListItemElement), aAlignType,
EditTarget::OnlyDescendantsExceptTable);
@ -8674,12 +8676,14 @@ nsresult HTMLEditor::AlignNodesAndDescendants(
}
if (useCSS) {
if (nsCOMPtr<nsStyledElement> styledListOrListItemElement =
do_QueryInterface(listOrListItemElement)) {
if (nsStyledElement* styledListOrListItemElement =
nsStyledElement::FromNode(listOrListItemElement)) {
// MOZ_KnownLive(*styledListOrListItemElement): An element of
// aArrayOfContents which is array of OwningNonNull.
Result<int32_t, nsresult> result =
mCSSEditUtils->SetCSSEquivalentToHTMLStyleWithTransaction(
*styledListOrListItemElement, nullptr, nsGkAtoms::align,
&aAlignType);
MOZ_KnownLive(*styledListOrListItemElement), nullptr,
nsGkAtoms::align, &aAlignType);
if (result.isErr()) {
if (result.inspectErr() == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
@ -8702,6 +8706,8 @@ nsresult HTMLEditor::AlignNodesAndDescendants(
// XXX AlignContentsInAllTableCellsAndListItems() handles only list
// item elements and table cells. Is it intentional? Why don't
// we need to align contents in other type blocks?
// MOZ_KnownLive(*listOrListItemElement): An element of aArrayOfContents
// which is array of OwningNonNull.
nsresult rv = AlignContentsInAllTableCellsAndListItems(
MOZ_KnownLive(*listOrListItemElement), aAlignType);
if (NS_FAILED(rv)) {
@ -12746,14 +12752,17 @@ nsresult HTMLEditor::RemoveAlignFromDescendants(Element& aElement,
return rv;
}
} else {
nsCOMPtr<nsStyledElement> styledBlockOrHRElement =
do_QueryInterface(blockOrHRElement);
nsStyledElement* styledBlockOrHRElement =
nsStyledElement::FromNode(blockOrHRElement);
if (NS_WARN_IF(!styledBlockOrHRElement)) {
return NS_ERROR_FAILURE;
}
// MOZ_KnownLive(*styledBlockOrHRElement): It's `blockOrHRElement
// which is OwningNonNull.
nsAutoString dummyCssValue;
nsresult rv = mCSSEditUtils->RemoveCSSInlineStyleWithTransaction(
*styledBlockOrHRElement, nsGkAtoms::textAlign, dummyCssValue);
MOZ_KnownLive(*styledBlockOrHRElement), nsGkAtoms::textAlign,
dummyCssValue);
if (NS_FAILED(rv)) {
NS_WARNING(
"CSSEditUtils::RemoveCSSInlineStyleWithTransaction(nsGkAtoms::"
@ -12924,13 +12933,16 @@ nsresult HTMLEditor::ChangeMarginStart(Element& aElement,
}
if (0 < f) {
if (nsCOMPtr<nsStyledElement> styledElement =
do_QueryInterface(&aElement)) {
if (nsStyledElement* styledElement = nsStyledElement::FromNode(&aElement)) {
nsAutoString newValue;
newValue.AppendFloat(f);
newValue.Append(nsDependentAtomString(unit));
// MOZ_KnownLive(*styledElement): It's aElement and its lifetime must
// be guaranteed by caller because of MOZ_CAN_RUN_SCRIPT method.
// MOZ_KnownLive(merginProperty): It's nsStaticAtom.
nsresult rv = mCSSEditUtils->SetCSSPropertyWithTransaction(
*styledElement, MOZ_KnownLive(marginProperty), newValue);
MOZ_KnownLive(*styledElement), MOZ_KnownLive(marginProperty),
newValue);
if (rv == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
"CSSEditUtils::SetCSSPropertyWithTransaction() destroyed the "
@ -12944,9 +12956,12 @@ nsresult HTMLEditor::ChangeMarginStart(Element& aElement,
return NS_OK;
}
if (nsCOMPtr<nsStyledElement> styledElement = do_QueryInterface(&aElement)) {
if (nsStyledElement* styledElement = nsStyledElement::FromNode(&aElement)) {
// MOZ_KnownLive(*styledElement): It's aElement and its lifetime must
// be guaranteed by caller because of MOZ_CAN_RUN_SCRIPT method.
// MOZ_KnownLive(merginProperty): It's nsStaticAtom.
nsresult rv = mCSSEditUtils->RemoveCSSPropertyWithTransaction(
*styledElement, MOZ_KnownLive(marginProperty), value);
MOZ_KnownLive(*styledElement), MOZ_KnownLive(marginProperty), value);
if (rv == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
"CSSEditUtils::RemoveCSSPropertyWithTransaction() destroyed the "
@ -13508,8 +13523,8 @@ EditActionResult HTMLEditor::AddZIndexAsSubAction(int32_t aChange) {
return EditActionHandled(NS_ERROR_FAILURE);
}
nsCOMPtr<nsStyledElement> absolutelyPositionedStyledElement =
do_QueryInterface(absolutelyPositionedElement);
nsStyledElement* absolutelyPositionedStyledElement =
nsStyledElement::FromNode(absolutelyPositionedElement);
if (NS_WARN_IF(!absolutelyPositionedStyledElement)) {
return EditActionHandled(NS_ERROR_FAILURE);
}
@ -13517,8 +13532,10 @@ EditActionResult HTMLEditor::AddZIndexAsSubAction(int32_t aChange) {
{
AutoSelectionRestorer restoreSelectionLater(*this);
Result<int32_t, nsresult> result =
AddZIndexWithTransaction(*absolutelyPositionedStyledElement, aChange);
// MOZ_KnownLive(*absolutelyPositionedStyledElement): It's
// absolutelyPositionedElement whose type is RefPtr.
Result<int32_t, nsresult> result = AddZIndexWithTransaction(
MOZ_KnownLive(*absolutelyPositionedStyledElement), aChange);
if (result.isErr()) {
NS_WARNING("HTMLEditor::AddZIndexWithTransaction() failed");
return EditActionHandled(result.unwrapErr());

View File

@ -49,13 +49,13 @@ bool HTMLEditUtils::CanContentsBeJoined(const nsIContent& aLeftContent,
if (!aLeftContent.IsElement() || !aRightContent.IsElement()) {
return false;
}
nsCOMPtr<nsStyledElement> leftStyledElement =
do_QueryInterface(const_cast<nsIContent*>(&aLeftContent));
nsStyledElement* leftStyledElement =
nsStyledElement::FromNode(const_cast<nsIContent*>(&aLeftContent));
if (!leftStyledElement) {
return false;
}
nsCOMPtr<nsStyledElement> rightStyledElement =
do_QueryInterface(const_cast<nsIContent*>(&aRightContent));
nsStyledElement* rightStyledElement =
nsStyledElement::FromNode(const_cast<nsIContent*>(&aRightContent));
if (!rightStyledElement) {
return false;
}

View File

@ -5176,16 +5176,19 @@ nsresult HTMLEditor::SetAttributeOrEquivalent(Element* aElement,
MOZ_ASSERT(aAttribute);
nsAutoScriptBlocker scriptBlocker;
nsCOMPtr<nsStyledElement> styledElement = do_QueryInterface(aElement);
nsStyledElement* styledElement = nsStyledElement::FromNodeOrNull(aElement);
if (!IsCSSEnabled() || !mCSSEditUtils) {
// we are not in an HTML+CSS editor; let's set the attribute the HTML way
if (mCSSEditUtils && styledElement) {
// MOZ_KnownLive(*styledElement): It's aElement and its lifetime must
// be guaranteed by the caller because of MOZ_CAN_RUN_SCRIPT method.
nsresult rv =
aSuppressTransaction
? mCSSEditUtils->RemoveCSSEquivalentToHTMLStyleWithoutTransaction(
*styledElement, nullptr, aAttribute, nullptr)
MOZ_KnownLive(*styledElement), nullptr, aAttribute, nullptr)
: mCSSEditUtils->RemoveCSSEquivalentToHTMLStyleWithTransaction(
*styledElement, nullptr, aAttribute, nullptr);
MOZ_KnownLive(*styledElement), nullptr, aAttribute,
nullptr);
if (rv == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
"CSSEditUtils::RemoveCSSEquivalentToHTMLStyle*Transaction() "
@ -5210,12 +5213,14 @@ nsresult HTMLEditor::SetAttributeOrEquivalent(Element* aElement,
}
if (styledElement) {
// MOZ_KnownLive(*styledElement): It's aElement and its lifetime must
// be guaranteed by the caller because of MOZ_CAN_RUN_SCRIPT method.
Result<int32_t, nsresult> count =
aSuppressTransaction
? mCSSEditUtils->SetCSSEquivalentToHTMLStyleWithoutTransaction(
*styledElement, nullptr, aAttribute, &aValue)
MOZ_KnownLive(*styledElement), nullptr, aAttribute, &aValue)
: mCSSEditUtils->SetCSSEquivalentToHTMLStyleWithTransaction(
*styledElement, nullptr, aAttribute, &aValue);
MOZ_KnownLive(*styledElement), nullptr, aAttribute, &aValue);
if (count.isErr()) {
if (count.inspectErr() == NS_ERROR_EDITOR_DESTROYED) {
return NS_ERROR_EDITOR_DESTROYED;
@ -5294,16 +5299,18 @@ nsresult HTMLEditor::RemoveAttributeOrEquivalent(Element* aElement,
CSSEditUtils::IsCSSEditableProperty(aElement, nullptr, aAttribute)) {
// XXX It might be keep handling attribute even if aElement is not
// an nsStyledElement instance.
nsCOMPtr<nsStyledElement> styledElement = do_QueryInterface(aElement);
nsStyledElement* styledElement = nsStyledElement::FromNodeOrNull(aElement);
if (NS_WARN_IF(!styledElement)) {
return NS_ERROR_INVALID_ARG;
}
// MOZ_KnownLive(*styledElement): It's aElement and its lifetime must
// be guaranteed by the caller because of MOZ_CAN_RUN_SCRIPT method.
nsresult rv =
aSuppressTransaction
? mCSSEditUtils->RemoveCSSEquivalentToHTMLStyleWithoutTransaction(
*styledElement, nullptr, aAttribute, nullptr)
MOZ_KnownLive(*styledElement), nullptr, aAttribute, nullptr)
: mCSSEditUtils->RemoveCSSEquivalentToHTMLStyleWithTransaction(
*styledElement, nullptr, aAttribute, nullptr);
MOZ_KnownLive(*styledElement), nullptr, aAttribute, nullptr);
if (NS_FAILED(rv)) {
NS_WARNING(
"CSSEditUtils::RemoveCSSEquivalentToHTMLStyle*Transaction() failed");
@ -5417,9 +5424,10 @@ nsresult HTMLEditor::SetCSSBackgroundColorWithTransaction(
// If the range is in a text node, set background color of its parent
// block.
if (startOfRange.IsInTextNode()) {
if (nsCOMPtr<nsStyledElement> blockStyledElement =
do_QueryInterface(HTMLEditUtils::GetAncestorBlockElement(
*startOfRange.ContainerAsText()))) {
if (RefPtr<nsStyledElement> blockStyledElement =
nsStyledElement::FromNodeOrNull(
HTMLEditUtils::GetAncestorBlockElement(
*startOfRange.ContainerAsText()))) {
Result<int32_t, nsresult> result =
mCSSEditUtils->SetCSSEquivalentToHTMLStyleWithTransaction(
*blockStyledElement, nullptr, nsGkAtoms::bgcolor, &aColor);
@ -5444,7 +5452,7 @@ nsresult HTMLEditor::SetCSSBackgroundColorWithTransaction(
// than the `nsRange` is collapsed?
if (startOfRange.GetContainer()->IsHTMLElement(nsGkAtoms::body) &&
selectionIsCollapsed) {
if (nsCOMPtr<nsStyledElement> styledElement =
if (RefPtr<nsStyledElement> styledElement =
startOfRange.GetContainerAsStyledElement()) {
Result<int32_t, nsresult> result =
mCSSEditUtils->SetCSSEquivalentToHTMLStyleWithTransaction(
@ -5471,9 +5479,10 @@ nsresult HTMLEditor::SetCSSBackgroundColorWithTransaction(
if (NS_WARN_IF(startOfRange.IsInDataNode())) {
continue;
}
if (nsCOMPtr<nsStyledElement> blockStyledElement = do_QueryInterface(
HTMLEditUtils::GetInclusiveAncestorBlockElement(
*startOfRange.GetChild()))) {
if (RefPtr<nsStyledElement> blockStyledElement =
nsStyledElement::FromNodeOrNull(
HTMLEditUtils::GetInclusiveAncestorBlockElement(
*startOfRange.GetChild()))) {
Result<int32_t, nsresult> result =
mCSSEditUtils->SetCSSEquivalentToHTMLStyleWithTransaction(
*blockStyledElement, nullptr, nsGkAtoms::bgcolor, &aColor);
@ -5528,11 +5537,14 @@ nsresult HTMLEditor::SetCSSBackgroundColorWithTransaction(
*startOfRange.ContainerAsText());
if (blockElement && handledBlockParent != blockElement) {
handledBlockParent = blockElement;
if (nsCOMPtr<nsStyledElement> blockStyledElement =
do_QueryInterface(blockElement)) {
if (nsStyledElement* blockStyledElement =
nsStyledElement::FromNode(blockElement)) {
// MOZ_KnownLive(*blockStyledElement): It's blockElement whose
// type is RefPtr.
Result<int32_t, nsresult> result =
mCSSEditUtils->SetCSSEquivalentToHTMLStyleWithTransaction(
*blockStyledElement, nullptr, nsGkAtoms::bgcolor, &aColor);
MOZ_KnownLive(*blockStyledElement), nullptr,
nsGkAtoms::bgcolor, &aColor);
if (result.isErr()) {
if (result.inspectErr() == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
@ -5555,11 +5567,14 @@ nsresult HTMLEditor::SetCSSBackgroundColorWithTransaction(
HTMLEditUtils::GetInclusiveAncestorBlockElement(content);
if (blockElement && handledBlockParent != blockElement) {
handledBlockParent = blockElement;
if (nsCOMPtr<nsStyledElement> blockStyledElement =
do_QueryInterface(blockElement)) {
if (nsStyledElement* blockStyledElement =
nsStyledElement::FromNode(blockElement)) {
// MOZ_KnownLive(*blockStyledElement): It's blockElement whose
// type is RefPtr.
Result<int32_t, nsresult> result =
mCSSEditUtils->SetCSSEquivalentToHTMLStyleWithTransaction(
*blockStyledElement, nullptr, nsGkAtoms::bgcolor, &aColor);
MOZ_KnownLive(*blockStyledElement), nullptr,
nsGkAtoms::bgcolor, &aColor);
if (result.isErr()) {
if (result.inspectErr() == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
@ -5583,11 +5598,14 @@ nsresult HTMLEditor::SetCSSBackgroundColorWithTransaction(
RefPtr<Element> blockElement = HTMLEditUtils::GetAncestorBlockElement(
*endOfRange.ContainerAsText());
if (blockElement && handledBlockParent != blockElement) {
if (nsCOMPtr<nsStyledElement> blockStyledElement =
do_QueryInterface(blockElement)) {
if (nsStyledElement* blockStyledElement =
nsStyledElement::FromNode(blockElement)) {
// MOZ_KnownLive(*blockStyledElement): It's blockElement whose
// type is RefPtr.
Result<int32_t, nsresult> result =
mCSSEditUtils->SetCSSEquivalentToHTMLStyleWithTransaction(
*blockStyledElement, nullptr, nsGkAtoms::bgcolor, &aColor);
MOZ_KnownLive(*blockStyledElement), nullptr,
nsGkAtoms::bgcolor, &aColor);
if (result.isErr()) {
if (result.inspectErr() == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(

View File

@ -184,8 +184,8 @@ nsresult HTMLEditor::SetAllResizersPosition() {
auto setHandlePosition =
[this](ManualNACPtr& aHandleElement, int32_t aNewX, int32_t aNewY)
MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION -> nsresult {
nsCOMPtr<nsStyledElement> handleStyledElement =
do_QueryInterface(aHandleElement.get());
RefPtr<nsStyledElement> handleStyledElement =
nsStyledElement::FromNodeOrNull(aHandleElement.get());
if (!handleStyledElement) {
return NS_OK;
}
@ -701,8 +701,8 @@ nsresult HTMLEditor::StartResizing(Element& aHandleElement) {
"Element::UnsetAttr(nsGkAtoms::_class) failed");
// position it
if (nsCOMPtr<nsStyledElement> resizingShadowStyledElement =
do_QueryInterface(mResizingShadow.get())) {
if (RefPtr<nsStyledElement> resizingShadowStyledElement =
nsStyledElement::FromNodeOrNull(mResizingShadow.get())) {
nsresult rv;
rv = mCSSEditUtils->SetCSSPropertyPixelsWithTransaction(
*resizingShadowStyledElement, *nsGkAtoms::width, mResizedObjectWidth);
@ -892,8 +892,8 @@ nsresult HTMLEditor::SetResizingInfoPosition(int32_t aX, int32_t aY, int32_t aW,
// Offset info box by 20 so it's not directly under the mouse cursor.
const int mouseCursorOffset = 20;
if (nsCOMPtr<nsStyledElement> resizingInfoStyledElement =
do_QueryInterface(mResizingInfo.get())) {
if (RefPtr<nsStyledElement> resizingInfoStyledElement =
nsStyledElement::FromNodeOrNull(mResizingInfo.get())) {
nsresult rv;
rv = mCSSEditUtils->SetCSSPropertyPixelsWithTransaction(
*resizingInfoStyledElement, *nsGkAtoms::left,
@ -979,10 +979,12 @@ nsresult HTMLEditor::SetShadowPosition(Element& aShadowElement,
? mResizingShadow.get()
: mPositioningShadow.get();
if (nsCOMPtr<nsStyledElement> styledShadowElement =
do_QueryInterface(&aShadowElement)) {
if (nsStyledElement* styledShadowElement =
nsStyledElement::FromNode(&aShadowElement)) {
// MOZ_KnownLive(*styledShadowElement): It's aShadowElement whose lifetime
// must be guaranteed by caller because of MOZ_CAN_RUN_SCRIPT method.
nsresult rv = SetAnonymousElementPositionWithTransaction(
*styledShadowElement, aElementX, aElementY);
MOZ_KnownLive(*styledShadowElement), aElementX, aElementY);
if (NS_FAILED(rv)) {
NS_WARNING(
"HTMLEditor::SetAnonymousElementPositionWithTransaction() failed");
@ -1098,8 +1100,8 @@ nsresult HTMLEditor::OnMouseMove(MouseEvent* aMouseEvent) {
int32_t newWidth = GetNewResizingWidth(clientX, clientY);
int32_t newHeight = GetNewResizingHeight(clientX, clientY);
if (nsCOMPtr<nsStyledElement> resizingShadowStyledElement =
do_QueryInterface(mResizingShadow.get())) {
if (RefPtr<nsStyledElement> resizingShadowStyledElement =
nsStyledElement::FromNodeOrNull(mResizingShadow.get())) {
nsresult rv;
rv = mCSSEditUtils->SetCSSPropertyPixelsWithTransaction(
*resizingShadowStyledElement, *nsGkAtoms::left, newX);
@ -1188,8 +1190,8 @@ nsresult HTMLEditor::OnMouseMove(MouseEvent* aMouseEvent) {
SnapToGrid(newX, newY);
if (nsCOMPtr<nsStyledElement> positioningShadowStyledElement =
do_QueryInterface(mPositioningShadow.get())) {
if (RefPtr<nsStyledElement> positioningShadowStyledElement =
nsStyledElement::FromNodeOrNull(mPositioningShadow.get())) {
nsresult rv;
rv = mCSSEditUtils->SetCSSPropertyPixelsWithTransaction(
*positioningShadowStyledElement, *nsGkAtoms::left, newX);
@ -1259,8 +1261,8 @@ nsresult HTMLEditor::SetFinalSizeWithTransaction(int32_t aX, int32_t aY) {
AutoPlaceholderBatch treatAsOneTransaction(*this,
ScrollSelectionIntoView::Yes);
RefPtr<Element> resizedElement(mResizedObject);
nsCOMPtr<nsStyledElement> resizedStyleElement =
do_QueryInterface(mResizedObject);
RefPtr<nsStyledElement> resizedStyleElement =
nsStyledElement::FromNodeOrNull(mResizedObject);
if (mResizedObjectIsAbsolutelyPositioned && resizedStyleElement) {
if (setHeight) {

View File

@ -422,8 +422,8 @@ nsresult HTMLEditor::RefreshInlineTableEditingUIInternal() {
auto setInlineTableEditButtonPosition =
[this](ManualNACPtr& aButtonElement, int32_t aNewX, int32_t aNewY)
MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION -> nsresult {
nsCOMPtr<nsStyledElement> buttonStyledElement =
do_QueryInterface(aButtonElement.get());
RefPtr<nsStyledElement> buttonStyledElement =
nsStyledElement::FromNodeOrNull(aButtonElement.get());
if (!buttonStyledElement) {
return NS_OK;
}

View File

@ -400,14 +400,16 @@ Result<bool, nsresult> HTMLEditor::ElementIsGoodContainerForTheStyle(
NS_WARNING("EditorBase::CreateHTMLContent(nsGkAtoms::span) failed");
return false;
}
nsCOMPtr<nsStyledElement> styledNewSpanElement =
do_QueryInterface(newSpanElement);
nsStyledElement* styledNewSpanElement =
nsStyledElement::FromNode(newSpanElement);
if (!styledNewSpanElement) {
return false;
}
// MOZ_KnownLive(*styledNewSpanElement): It's newSpanElement whose type is
// RefPtr.
Result<int32_t, nsresult> result =
mCSSEditUtils->SetCSSEquivalentToHTMLStyleWithoutTransaction(
*styledNewSpanElement, aProperty, aAttribute, aValue);
MOZ_KnownLive(*styledNewSpanElement), aProperty, aAttribute, aValue);
if (result.isErr()) {
// The call shouldn't return destroyed error because it must be
// impossible to run script with modifying the new orphan node.
@ -417,7 +419,7 @@ Result<bool, nsresult> HTMLEditor::ElementIsGoodContainerForTheStyle(
}
return false;
}
nsCOMPtr<nsStyledElement> styledElement = do_QueryInterface(&aElement);
nsStyledElement* styledElement = nsStyledElement::FromNode(&aElement);
if (!styledElement) {
return false;
}
@ -675,11 +677,14 @@ nsresult HTMLEditor::SetInlinePropertyOnNodeImpl(nsIContent& aContent,
}
// Add the CSS styles corresponding to the HTML style request
if (nsCOMPtr<nsStyledElement> spanStyledElement =
do_QueryInterface(spanElement)) {
if (nsStyledElement* spanStyledElement =
nsStyledElement::FromNode(spanElement)) {
// MOZ_KnownLive(*spanStyledElement): It's spanElement whose type is
// RefPtr.
Result<int32_t, nsresult> result =
mCSSEditUtils->SetCSSEquivalentToHTMLStyleWithTransaction(
*spanStyledElement, &aProperty, aAttribute, &aValue);
MOZ_KnownLive(*spanStyledElement), &aProperty, aAttribute,
&aValue);
if (result.isErr()) {
if (result.inspectErr() == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
@ -1215,12 +1220,13 @@ nsresult HTMLEditor::RemoveStyleInside(Element& aElement, nsAtom* aProperty,
if (CSSEditUtils::IsCSSEditableProperty(&aElement, aProperty, aAttribute) &&
CSSEditUtils::HaveSpecifiedCSSEquivalentStyles(aElement, aProperty,
aAttribute)) {
if (nsCOMPtr<nsStyledElement> styledElement =
do_QueryInterface(&aElement)) {
if (nsStyledElement* styledElement = nsStyledElement::FromNode(&aElement)) {
// If aElement has CSS declaration of the given style, remove it.
// MOZ_KnownLive(*styledElement): It's aElement and its lifetime must be
// guaranteed by the caller because of MOZ_CAN_RUN_SCRIPT method.
nsresult rv =
mCSSEditUtils->RemoveCSSEquivalentToHTMLStyleWithTransaction(
*styledElement, aProperty, aAttribute, nullptr);
MOZ_KnownLive(*styledElement), aProperty, aAttribute, nullptr);
if (rv == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
"CSSEditUtils::RemoveCSSEquivalentToHTMLStyleWithTransaction() "

View File

@ -292,8 +292,8 @@ void ScrollbarActivity::SetIsActive(bool aNewActive) {
}
static void SetOpacityOnElement(nsIContent* aContent, double aOpacity) {
nsCOMPtr<nsStyledElement> inlineStyleContent = do_QueryInterface(aContent);
if (inlineStyleContent) {
if (RefPtr<nsStyledElement> inlineStyleContent =
nsStyledElement::FromNodeOrNull(aContent)) {
nsICSSDeclaration* decl = inlineStyleContent->Style();
nsAutoCString str;
str.AppendFloat(aOpacity);
@ -323,8 +323,8 @@ bool ScrollbarActivity::UpdateOpacity(TimeStamp aTime) {
}
static void UnsetOpacityOnElement(nsIContent* aContent) {
nsCOMPtr<nsStyledElement> inlineStyleContent = do_QueryInterface(aContent);
if (inlineStyleContent) {
if (RefPtr<nsStyledElement> inlineStyleContent =
nsStyledElement::FromNodeOrNull(aContent)) {
nsICSSDeclaration* decl = inlineStyleContent->Style();
nsAutoString dummy;
decl->RemoveProperty("opacity"_ns, dummy, IgnoreErrors());

View File

@ -423,34 +423,30 @@ void nsResizerFrame::ResizeContent(nsIContent* aContent,
aContent->AsElement()->SetAttr(kNameSpaceID_None, nsGkAtoms::height,
aSizeInfo.height, true);
}
} else {
nsCOMPtr<nsStyledElement> inlineStyleContent = do_QueryInterface(aContent);
if (inlineStyleContent) {
nsICSSDeclaration* decl = inlineStyleContent->Style();
} else if (RefPtr<nsStyledElement> inlineStyleContent =
nsStyledElement::FromNode(aContent)) {
nsICSSDeclaration* decl = inlineStyleContent->Style();
if (aOriginalSizeInfo) {
decl->GetPropertyValue("width"_ns, aOriginalSizeInfo->width);
decl->GetPropertyValue("height"_ns, aOriginalSizeInfo->height);
}
if (aOriginalSizeInfo) {
decl->GetPropertyValue("width"_ns, aOriginalSizeInfo->width);
decl->GetPropertyValue("height"_ns, aOriginalSizeInfo->height);
}
// only set the property if the element could have changed in that
// direction
if (aDirection.mHorizontal) {
NS_ConvertUTF16toUTF8 widthstr(aSizeInfo.width);
if (!widthstr.IsEmpty() &&
!Substring(widthstr, widthstr.Length() - 2, 2).EqualsLiteral("px"))
widthstr.AppendLiteral("px");
decl->SetProperty("width"_ns, widthstr, EmptyString(), IgnoreErrors());
}
if (aDirection.mVertical) {
NS_ConvertUTF16toUTF8 heightstr(aSizeInfo.height);
if (!heightstr.IsEmpty() &&
!Substring(heightstr, heightstr.Length() - 2, 2)
.EqualsLiteral("px"))
heightstr.AppendLiteral("px");
decl->SetProperty("height"_ns, heightstr, EmptyString(),
IgnoreErrors());
}
// only set the property if the element could have changed in that
// direction
if (aDirection.mHorizontal) {
NS_ConvertUTF16toUTF8 widthstr(aSizeInfo.width);
if (!widthstr.IsEmpty() &&
!Substring(widthstr, widthstr.Length() - 2, 2).EqualsLiteral("px"))
widthstr.AppendLiteral("px");
decl->SetProperty("width"_ns, widthstr, EmptyString(), IgnoreErrors());
}
if (aDirection.mVertical) {
NS_ConvertUTF16toUTF8 heightstr(aSizeInfo.height);
if (!heightstr.IsEmpty() &&
!Substring(heightstr, heightstr.Length() - 2, 2).EqualsLiteral("px"))
heightstr.AppendLiteral("px");
decl->SetProperty("height"_ns, heightstr, EmptyString(), IgnoreErrors());
}
}
}