diff --git a/editor/libeditor/HTMLEditor.h b/editor/libeditor/HTMLEditor.h index a45dc6ddfa11..142fca3ae033 100644 --- a/editor/libeditor/HTMLEditor.h +++ b/editor/libeditor/HTMLEditor.h @@ -376,6 +376,17 @@ public: /** * RemoveInlinePropertyAsAction() removes a property which changes inline * style of text. E.g., bold, italic, super and sub. + * + * @param aProperty Tag name whcih represents the inline style you want to + * remove. E.g., nsGkAtoms::strong, nsGkAtoms::b, etc. + * If nsGkAtoms::href, element which has href + * attribute will be removed. + * If nsGkAtoms::name, element which has non-empty + * name attribute will be removed. + * @param aAttribute If aProperty is nsGkAtoms::font, aAttribute should be + * nsGkAtoms::fase, nsGkAtoms::size, nsGkAtoms::color or + * nsGkAtoms::bgcolor. Otherwise, set nullptr. + * Must not use nsGkAtoms::_empty here. */ nsresult RemoveInlinePropertyAsAction(nsAtom& aProperty, nsAtom* aAttribute); diff --git a/editor/libeditor/HTMLStyleEditor.cpp b/editor/libeditor/HTMLStyleEditor.cpp index 870fe33444ab..74f9250d394f 100644 --- a/editor/libeditor/HTMLStyleEditor.cpp +++ b/editor/libeditor/HTMLStyleEditor.cpp @@ -45,6 +45,15 @@ namespace mozilla { using namespace dom; +static already_AddRefed +AtomizeAttribute(const nsAString& aAttribute) +{ + if (aAttribute.IsEmpty()) { + return nullptr; // Don't use nsGkAtoms::_empty for attribute. + } + return NS_Atomize(aAttribute); +} + bool HTMLEditor::IsEmptyTextNode(nsINode& aNode) { @@ -97,7 +106,7 @@ HTMLEditor::SetInlineProperty(const nsAString& aProperty, if (NS_WARN_IF(!property)) { return NS_ERROR_INVALID_ARG; } - RefPtr attribute = NS_Atomize(aAttribute); + RefPtr attribute = AtomizeAttribute(aAttribute); AutoEditActionDataSetter editActionData( *this, HTMLEditUtils::GetEditActionForFormatText(*property, attribute, true)); @@ -1210,7 +1219,7 @@ HTMLEditor::GetInlineProperty(const nsAString& aProperty, bool* aAll) { RefPtr property = NS_Atomize(aProperty); - RefPtr attribute = NS_Atomize(aAttribute); + RefPtr attribute = AtomizeAttribute(aAttribute); return GetInlineProperty(property, attribute, aValue, aFirst, aAny, aAll); } @@ -1251,7 +1260,7 @@ HTMLEditor::GetInlinePropertyWithAttrValue(const nsAString& aProperty, nsAString& outValue) { RefPtr property = NS_Atomize(aProperty); - RefPtr attribute = NS_Atomize(aAttribute); + RefPtr attribute = AtomizeAttribute(aAttribute); return GetInlinePropertyWithAttrValue(property, attribute, aValue, aFirst, aAny, aAll, outValue); } @@ -1326,7 +1335,7 @@ HTMLEditor::RemoveInlineProperty(const nsAString& aProperty, const nsAString& aAttribute) { RefPtr property = NS_Atomize(aProperty); - RefPtr attribute = NS_Atomize(aAttribute); + RefPtr attribute = AtomizeAttribute(aAttribute); AutoEditActionDataSetter editActionData( *this, @@ -1343,6 +1352,7 @@ HTMLEditor::RemoveInlinePropertyInternal(nsAtom* aProperty, nsAtom* aAttribute) { MOZ_ASSERT(IsEditActionDataAvailable()); + MOZ_ASSERT(aAttribute != nsGkAtoms::_empty); if (NS_WARN_IF(!mRules)) { return NS_ERROR_NOT_INITIALIZED; diff --git a/editor/libeditor/TypeInState.cpp b/editor/libeditor/TypeInState.cpp index 5593f120d2c2..3d2864b868d2 100644 --- a/editor/libeditor/TypeInState.cpp +++ b/editor/libeditor/TypeInState.cpp @@ -299,6 +299,9 @@ TypeInState::IsPropSet(nsAtom* aProp, nsAString* outValue, int32_t& outIndex) { + if (aAttr == nsGkAtoms::_empty) { + aAttr = nullptr; + } // linear search. list should be short. size_t count = mSetArray.Length(); for (size_t i = 0; i < count; i++) { @@ -347,6 +350,9 @@ TypeInState::FindPropInList(nsAtom* aProp, nsTArray& aList, int32_t& outIndex) { + if (aAttr == nsGkAtoms::_empty) { + aAttr = nullptr; + } // linear search. list should be short. size_t count = aList.Length(); for (size_t i = 0; i < count; i++) { @@ -377,7 +383,7 @@ PropItem::PropItem(nsAtom* aTag, nsAtom* aAttr, const nsAString &aValue) : tag(aTag) - , attr(aAttr) + , attr(aAttr != nsGkAtoms::_empty ? aAttr : nullptr) , value(aValue) { MOZ_COUNT_CTOR(PropItem); diff --git a/editor/libeditor/tests/mochitest.ini b/editor/libeditor/tests/mochitest.ini index e3e67377e7df..201c3eee30be 100644 --- a/editor/libeditor/tests/mochitest.ini +++ b/editor/libeditor/tests/mochitest.ini @@ -285,6 +285,7 @@ subsuite = clipboard skip-if = android_version == '24' [test_nsIEditorMailSupport_insertAsCitedQuotation.html] [test_nsIHTMLEditor_getSelectedElement.html] +[test_nsIHTMLEditor_removeInlineProperty.html] [test_nsIHTMLEditor_selectElement.html] [test_nsIHTMLEditor_setCaretAfterElement.html] [test_nsIHTMLObjectResizer_hideResizers.html] diff --git a/editor/libeditor/tests/test_nsIHTMLEditor_removeInlineProperty.html b/editor/libeditor/tests/test_nsIHTMLEditor_removeInlineProperty.html new file mode 100644 index 000000000000..a7f4f748fb6f --- /dev/null +++ b/editor/libeditor/tests/test_nsIHTMLEditor_removeInlineProperty.html @@ -0,0 +1,334 @@ + + + + Test for nsIHTMLEditor.removeInlineProperty() + + + + +
+
+
+
+
+ + + + + diff --git a/editor/nsIHTMLEditor.idl b/editor/nsIHTMLEditor.idl index 3dab967d097e..b413fe5bfc69 100644 --- a/editor/nsIHTMLEditor.idl +++ b/editor/nsIHTMLEditor.idl @@ -88,21 +88,17 @@ interface nsIHTMLEditor : nsISupports /** - * removeInlineProperty() deletes the properties from all text in the current - * selection. If aProperty is not set on the selection, nothing is done. + * removeInlineProperty() removes a property which changes inline style of + * text. E.g., bold, italic, super and sub. * - * @param aProperty the property to remove from the selection - * All atoms are for normal HTML tags (e.g.: - * nsIEditorProperty::font) except when you want to - * remove just links and not named anchors. - * For that, use nsIEditorProperty::href - * @param aAttribute the attribute of the property, if applicable. - * May be null. - * Example: aProperty=nsIEditorProptery::font, - * aAttribute="color" - * nsIEditProperty::allAttributes is special. - * It indicates that all content-based text properties - * are to be removed from the selection. + * @param aProperty Tag name whcih represents the inline style you want to + * remove. E.g., "strong", "b", etc. + * If "href",
element which has href attribute will be + * removed. + * If "name", element which has non-empty name + * attribute will be removed. + * @param aAttribute If aProperty is "font", aAttribute should be "face", + * "size", "color" or "bgcolor". */ void removeInlineProperty(in AString aProperty, in AString aAttribute);