2001-09-26 02:53:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2016-07-07 12:35:13 +03:00
|
|
|
|
2016-07-09 05:42:33 +03:00
|
|
|
#include "mozilla/HTMLEditor.h"
|
|
|
|
|
2016-07-07 08:01:12 +03:00
|
|
|
#include "HTMLEditUtils.h"
|
2016-07-07 07:44:32 +03:00
|
|
|
#include "TextEditUtils.h"
|
2001-04-06 03:48:01 +04:00
|
|
|
#include "TypeInState.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2016-07-08 08:03:31 +03:00
|
|
|
#include "mozilla/EditorUtils.h"
|
2016-07-09 05:34:41 +03:00
|
|
|
#include "mozilla/SelectionState.h"
|
2014-04-10 20:09:40 +04:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "mozilla/mozalloc.h"
|
|
|
|
#include "nsAString.h"
|
|
|
|
#include "nsAttrName.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCaseTreatment.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsGkAtoms.h"
|
2017-10-03 01:05:19 +03:00
|
|
|
#include "nsAtom.h"
|
2001-01-28 23:13:07 +03:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIContentIterator.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsIDOMElement.h"
|
2016-07-07 13:33:32 +03:00
|
|
|
#include "nsIEditRules.h"
|
2014-02-28 03:04:46 +04:00
|
|
|
#include "nsNameSpaceManager.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsLiteralString.h"
|
2014-11-02 15:04:13 +03:00
|
|
|
#include "nsRange.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsStringFwd.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nscore.h"
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-07-13 10:33:42 +04:00
|
|
|
class nsISupports;
|
2012-02-01 14:54:22 +04:00
|
|
|
|
2016-07-09 05:42:33 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace dom;
|
2003-06-03 02:21:06 +04:00
|
|
|
|
2012-10-05 04:37:45 +04:00
|
|
|
static bool
|
2016-07-09 05:42:33 +03:00
|
|
|
IsEmptyTextNode(HTMLEditor* aThis, nsINode* aNode)
|
2012-10-05 04:37:45 +04:00
|
|
|
{
|
|
|
|
bool isEmptyTextNode = false;
|
2016-07-08 07:10:13 +03:00
|
|
|
return EditorBase::IsTextNode(aNode) &&
|
2012-10-05 04:37:45 +04:00
|
|
|
NS_SUCCEEDED(aThis->IsEmptyNode(aNode, &isEmptyTextNode)) &&
|
|
|
|
isEmptyTextNode;
|
|
|
|
}
|
|
|
|
|
2012-04-20 10:33:21 +04:00
|
|
|
NS_IMETHODIMP
|
2017-08-25 08:40:45 +03:00
|
|
|
HTMLEditor::SetInlineProperty(const nsAString& aProperty,
|
|
|
|
const nsAString& aAttribute,
|
|
|
|
const nsAString& aValue)
|
|
|
|
{
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> property = NS_Atomize(aProperty);
|
2017-09-07 05:20:51 +03:00
|
|
|
return SetInlineProperty(property, aAttribute, aValue);
|
2017-08-25 08:40:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
HTMLEditor::SetInlineProperty(nsAtom* aProperty,
|
2016-07-09 05:42:33 +03:00
|
|
|
const nsAString& aAttribute,
|
|
|
|
const nsAString& aValue)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2015-04-24 14:27:35 +03:00
|
|
|
NS_ENSURE_TRUE(aProperty, NS_ERROR_NULL_POINTER);
|
|
|
|
NS_ENSURE_TRUE(mRules, NS_ERROR_NOT_INITIALIZED);
|
2016-07-18 19:36:19 +03:00
|
|
|
nsCOMPtr<nsIEditRules> rules(mRules);
|
2017-08-08 05:25:36 +03:00
|
|
|
CommitComposition();
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Selection> selection = GetSelection();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-05-18 12:29:39 +04:00
|
|
|
if (selection->Collapsed()) {
|
2015-04-24 14:27:35 +03:00
|
|
|
// Manipulating text attributes on a collapsed selection only sets state
|
2012-04-20 10:33:21 +04:00
|
|
|
// for the next text insertion
|
2012-05-05 13:00:06 +04:00
|
|
|
mTypeInState->SetProp(aProperty, aAttribute, aValue);
|
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-04-20 10:33:21 +04:00
|
|
|
|
2017-08-14 08:56:39 +03:00
|
|
|
AutoPlaceholderBatch batchIt(this);
|
2016-06-23 12:15:42 +03:00
|
|
|
AutoRules beginRulesSniffing(this, EditAction::insertElement,
|
|
|
|
nsIEditor::eNext);
|
2016-07-07 07:27:31 +03:00
|
|
|
AutoSelectionRestorer selectionRestorer(selection, this);
|
2017-07-31 18:20:08 +03:00
|
|
|
AutoTransactionsConserveSelection dontChangeMySelection(this);
|
2012-04-20 10:33:21 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cancel, handled;
|
2016-06-24 15:12:16 +03:00
|
|
|
TextRulesInfo ruleInfo(EditAction::setTextProperty);
|
2013-03-23 07:47:57 +04:00
|
|
|
// Protect the edit rules object from dying
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = rules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-04-20 10:33:21 +04:00
|
|
|
if (!cancel && !handled) {
|
2015-04-24 14:27:35 +03:00
|
|
|
// Loop through the ranges in the selection
|
2017-06-23 10:46:54 +03:00
|
|
|
AutoRangeArray arrayOfRanges(selection);
|
|
|
|
for (auto& range : arrayOfRanges.mRanges) {
|
2015-04-24 14:27:35 +03:00
|
|
|
// Adjust range to include any ancestors whose children are entirely
|
2012-04-20 10:33:21 +04:00
|
|
|
// selected
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = PromoteInlineRange(*range);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-04-20 10:33:21 +04:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
// Check for easy case: both range endpoints in same text node
|
2017-07-11 14:53:04 +03:00
|
|
|
nsCOMPtr<nsINode> startNode = range->GetStartContainer();
|
2017-07-11 15:11:37 +03:00
|
|
|
nsCOMPtr<nsINode> endNode = range->GetEndContainer();
|
2015-04-19 15:28:50 +03:00
|
|
|
if (startNode && startNode == endNode && startNode->GetAsText()) {
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = SetInlinePropertyOnTextNode(*startNode->GetAsText(),
|
|
|
|
range->StartOffset(),
|
|
|
|
range->EndOffset(),
|
|
|
|
*aProperty, &aAttribute, aValue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-04-20 10:33:21 +04:00
|
|
|
continue;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2012-04-20 10:33:21 +04:00
|
|
|
// Not the easy case. Range not contained in single text node. There
|
|
|
|
// are up to three phases here. There are all the nodes reported by the
|
|
|
|
// subtree iterator to be processed. And there are potentially a
|
|
|
|
// starting textnode and an ending textnode which are only partially
|
|
|
|
// contained by the range.
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-04-20 10:33:21 +04:00
|
|
|
// Let's handle the nodes reported by the iterator. These nodes are
|
|
|
|
// entirely contained in the selection range. We build up a list of them
|
|
|
|
// (since doing operations on the document during iteration would perturb
|
|
|
|
// the iterator).
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
OwningNonNull<nsIContentIterator> iter = NS_NewContentSubtreeIterator();
|
2012-04-20 10:33:21 +04:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
nsTArray<OwningNonNull<nsIContent>> arrayOfNodes;
|
2012-04-20 10:33:21 +04:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
// Iterate range and build up array
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = iter->Init(range);
|
2012-04-20 10:33:21 +04:00
|
|
|
// Init returns an error if there are no nodes in range. This can easily
|
|
|
|
// happen with the subtree iterator if the selection doesn't contain any
|
|
|
|
// *whole* nodes.
|
2016-10-19 12:09:33 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-04-20 10:33:21 +04:00
|
|
|
for (; !iter->IsDone(); iter->Next()) {
|
2015-04-24 14:27:35 +03:00
|
|
|
OwningNonNull<nsINode> node = *iter->GetCurrentNode();
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
if (node->IsContent() && IsEditable(node)) {
|
|
|
|
arrayOfNodes.AppendElement(*node->AsContent());
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-24 14:27:35 +03:00
|
|
|
// First check the start parent of the range to see if it needs to be
|
|
|
|
// separately handled (it does if it's a text node, due to how the
|
2012-04-20 10:33:21 +04:00
|
|
|
// subtree iterator works - it will not have reported it).
|
2015-04-19 15:28:50 +03:00
|
|
|
if (startNode && startNode->GetAsText() && IsEditable(startNode)) {
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = SetInlinePropertyOnTextNode(*startNode->GetAsText(),
|
|
|
|
range->StartOffset(),
|
|
|
|
startNode->Length(), *aProperty,
|
|
|
|
&aAttribute, aValue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-04-20 10:33:21 +04:00
|
|
|
}
|
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
// Then loop through the list, set the property on each node
|
|
|
|
for (auto& node : arrayOfNodes) {
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = SetInlinePropertyOnNode(*node, *aProperty, &aAttribute, aValue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-04-20 10:33:21 +04:00
|
|
|
}
|
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
// Last check the end parent of the range to see if it needs to be
|
|
|
|
// separately handled (it does if it's a text node, due to how the
|
2012-04-20 10:33:21 +04:00
|
|
|
// subtree iterator works - it will not have reported it).
|
2015-04-19 15:28:50 +03:00
|
|
|
if (endNode && endNode->GetAsText() && IsEditable(endNode)) {
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = SetInlinePropertyOnTextNode(*endNode->GetAsText(), 0,
|
2015-04-19 15:28:50 +03:00
|
|
|
range->EndOffset(), *aProperty,
|
|
|
|
&aAttribute, aValue);
|
2016-10-19 12:09:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-04-20 10:33:21 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
2012-04-20 10:33:21 +04:00
|
|
|
if (!cancel) {
|
2015-04-24 14:27:35 +03:00
|
|
|
// Post-process
|
2016-10-19 12:09:33 +03:00
|
|
|
return rules->DidDoAction(selection, &ruleInfo, rv);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-04-20 10:33:21 +04:00
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2012-05-29 11:12:51 +04:00
|
|
|
// Helper function for SetInlinePropertyOn*: is aNode a simple old <b>, <font>,
|
|
|
|
// <span style="">, etc. that we can reuse instead of creating a new one?
|
|
|
|
bool
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::IsSimpleModifiableNode(nsIContent* aContent,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* aProperty,
|
2016-07-09 05:42:33 +03:00
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue)
|
2012-05-29 11:12:51 +04:00
|
|
|
{
|
|
|
|
// aContent can be null, in which case we'll return false in a few lines
|
|
|
|
MOZ_ASSERT(aProperty);
|
|
|
|
MOZ_ASSERT_IF(aAttribute, aValue);
|
|
|
|
|
|
|
|
nsCOMPtr<dom::Element> element = do_QueryInterface(aContent);
|
|
|
|
if (!element) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First check for <b>, <i>, etc.
|
2015-03-03 14:08:59 +03:00
|
|
|
if (element->IsHTMLElement(aProperty) && !element->GetAttrCount() &&
|
2012-05-29 11:12:51 +04:00
|
|
|
(!aAttribute || aAttribute->IsEmpty())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special cases for various equivalencies: <strong>, <em>, <s>
|
|
|
|
if (!element->GetAttrCount() &&
|
2015-03-03 14:08:59 +03:00
|
|
|
((aProperty == nsGkAtoms::b &&
|
|
|
|
element->IsHTMLElement(nsGkAtoms::strong)) ||
|
|
|
|
(aProperty == nsGkAtoms::i &&
|
|
|
|
element->IsHTMLElement(nsGkAtoms::em)) ||
|
|
|
|
(aProperty == nsGkAtoms::strike &&
|
|
|
|
element->IsHTMLElement(nsGkAtoms::s)))) {
|
2012-05-29 11:12:51 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now look for things like <font>
|
|
|
|
if (aAttribute && !aAttribute->IsEmpty()) {
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> atom = NS_Atomize(*aAttribute);
|
2012-05-29 11:12:51 +04:00
|
|
|
MOZ_ASSERT(atom);
|
|
|
|
|
2012-12-14 02:08:44 +04:00
|
|
|
nsString attrValue;
|
2015-03-03 14:08:59 +03:00
|
|
|
if (element->IsHTMLElement(aProperty) &&
|
|
|
|
IsOnlyAttribute(element, *aAttribute) &&
|
2012-12-14 02:08:44 +04:00
|
|
|
element->GetAttr(kNameSpaceID_None, atom, attrValue) &&
|
|
|
|
attrValue.Equals(*aValue, nsCaseInsensitiveStringComparator())) {
|
2012-05-29 11:12:51 +04:00
|
|
|
// This is not quite correct, because it excludes cases like
|
|
|
|
// <font face=000> being the same as <font face=#000000>.
|
|
|
|
// Property-specific handling is needed (bug 760211).
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No luck so far. Now we check for a <span> with a single style=""
|
|
|
|
// attribute that sets only the style we're looking for, if this type of
|
|
|
|
// style supports it
|
2016-07-07 12:35:13 +03:00
|
|
|
if (!mCSSEditUtils->IsCSSEditableProperty(element, aProperty, aAttribute) ||
|
2015-03-03 14:08:59 +03:00
|
|
|
!element->IsHTMLElement(nsGkAtoms::span) ||
|
|
|
|
element->GetAttrCount() != 1 ||
|
2012-05-29 11:12:51 +04:00
|
|
|
!element->HasAttr(kNameSpaceID_None, nsGkAtoms::style)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some CSS styles are not so simple. For instance, underline is
|
|
|
|
// "text-decoration: underline", which decomposes into four different text-*
|
|
|
|
// properties. So for now, we just create a span, add the desired style, and
|
|
|
|
// see if it matches.
|
2014-08-13 15:53:33 +04:00
|
|
|
nsCOMPtr<Element> newSpan = CreateHTMLContent(nsGkAtoms::span);
|
|
|
|
NS_ASSERTION(newSpan, "CreateHTMLContent failed");
|
|
|
|
NS_ENSURE_TRUE(newSpan, false);
|
2016-07-07 12:35:13 +03:00
|
|
|
mCSSEditUtils->SetCSSEquivalentToHTMLStyle(newSpan, aProperty,
|
2012-05-29 11:12:51 +04:00
|
|
|
aAttribute, aValue,
|
|
|
|
/*suppress transaction*/ true);
|
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
return mCSSEditUtils->ElementsSameStyle(newSpan, element);
|
2012-05-29 11:12:51 +04:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::SetInlinePropertyOnTextNode(Text& aText,
|
|
|
|
int32_t aStartOffset,
|
|
|
|
int32_t aEndOffset,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom& aProperty,
|
2016-07-09 05:42:33 +03:00
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString& aValue)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2015-04-19 15:28:50 +03:00
|
|
|
if (!aText.GetParentNode() ||
|
|
|
|
!CanContainTag(*aText.GetParentNode(), aProperty)) {
|
2012-05-01 10:34:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-04-19 15:28:50 +03:00
|
|
|
|
|
|
|
// Don't need to do anything if no characters actually selected
|
|
|
|
if (aStartOffset == aEndOffset) {
|
|
|
|
return NS_OK;
|
2003-09-03 17:58:00 +04:00
|
|
|
}
|
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
// Don't need to do anything if property already set on node
|
2016-07-07 12:35:13 +03:00
|
|
|
if (mCSSEditUtils->IsCSSEditableProperty(&aText, &aProperty, aAttribute)) {
|
2015-04-19 15:28:50 +03:00
|
|
|
// The HTML styles defined by aProperty/aAttribute have a CSS equivalence
|
|
|
|
// for node; let's check if it carries those CSS styles
|
2016-07-07 12:35:13 +03:00
|
|
|
if (mCSSEditUtils->IsCSSEquivalentToHTMLInlineStyleSet(&aText, &aProperty,
|
|
|
|
aAttribute, aValue, CSSEditUtils::eComputed)) {
|
2015-04-19 15:28:50 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
} else if (IsTextPropertySetByContent(&aText, &aProperty, aAttribute,
|
|
|
|
&aValue)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-05-05 13:00:06 +04:00
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
// Do we need to split the text node?
|
|
|
|
ErrorResult rv;
|
2017-10-11 11:58:24 +03:00
|
|
|
nsCOMPtr<nsIContent> text = &aText;
|
2015-04-19 15:28:50 +03:00
|
|
|
if (uint32_t(aEndOffset) != aText.Length()) {
|
|
|
|
// We need to split off back of text node
|
2017-10-11 11:58:24 +03:00
|
|
|
text = SplitNode(*text, aEndOffset, rv);
|
2015-04-27 16:18:52 +03:00
|
|
|
NS_ENSURE_TRUE(!rv.Failed(), rv.StealNSResult());
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-05-05 13:00:06 +04:00
|
|
|
|
|
|
|
if (aStartOffset) {
|
2015-04-19 15:28:50 +03:00
|
|
|
// We need to split off front of text node
|
|
|
|
SplitNode(*text, aStartOffset, rv);
|
2015-04-27 16:18:52 +03:00
|
|
|
NS_ENSURE_TRUE(!rv.Failed(), rv.StealNSResult());
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-05-05 13:00:06 +04:00
|
|
|
|
|
|
|
if (aAttribute) {
|
2015-04-19 15:28:50 +03:00
|
|
|
// Look for siblings that are correct type of node
|
|
|
|
nsIContent* sibling = GetPriorHTMLSibling(text);
|
|
|
|
if (IsSimpleModifiableNode(sibling, &aProperty, aAttribute, &aValue)) {
|
|
|
|
// Previous sib is already right kind of inline node; slide this over
|
|
|
|
return MoveNode(text, sibling, -1);
|
2012-05-05 13:00:06 +04:00
|
|
|
}
|
2015-04-19 15:28:50 +03:00
|
|
|
sibling = GetNextHTMLSibling(text);
|
|
|
|
if (IsSimpleModifiableNode(sibling, &aProperty, aAttribute, &aValue)) {
|
|
|
|
// Following sib is already right kind of inline node; slide this over
|
|
|
|
return MoveNode(text, sibling, 0);
|
2012-05-05 13:00:06 +04:00
|
|
|
}
|
2001-10-22 10:22:52 +04:00
|
|
|
}
|
2015-04-19 15:28:50 +03:00
|
|
|
|
|
|
|
// Reparent the node inside inline node with appropriate {attribute,value}
|
2015-04-24 14:27:35 +03:00
|
|
|
return SetInlinePropertyOnNode(*text, aProperty, aAttribute, aValue);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::SetInlinePropertyOnNodeImpl(nsIContent& aNode,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom& aProperty,
|
2016-07-09 05:42:33 +03:00
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString& aValue)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> attrAtom = aAttribute ? NS_Atomize(*aAttribute) : nullptr;
|
2014-08-20 16:25:16 +04:00
|
|
|
|
2012-04-17 21:50:19 +04:00
|
|
|
// If this is an element that can't be contained in a span, we have to
|
|
|
|
// recurse to its children.
|
2015-04-24 14:27:35 +03:00
|
|
|
if (!TagCanContain(*nsGkAtoms::span, aNode)) {
|
|
|
|
if (aNode.HasChildren()) {
|
|
|
|
nsTArray<OwningNonNull<nsIContent>> arrayOfNodes;
|
2012-05-05 13:00:06 +04:00
|
|
|
|
|
|
|
// Populate the list.
|
2015-04-24 14:27:35 +03:00
|
|
|
for (nsCOMPtr<nsIContent> child = aNode.GetFirstChild();
|
2012-05-05 13:00:06 +04:00
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
2012-10-05 04:37:45 +04:00
|
|
|
if (IsEditable(child) && !IsEmptyTextNode(this, child)) {
|
2015-04-24 14:27:35 +03:00
|
|
|
arrayOfNodes.AppendElement(*child);
|
2012-04-17 21:50:19 +04:00
|
|
|
}
|
2012-05-05 13:00:06 +04:00
|
|
|
}
|
2012-04-17 21:50:19 +04:00
|
|
|
|
2012-05-05 13:00:06 +04:00
|
|
|
// Then loop through the list, set the property on each node.
|
2015-04-24 14:27:35 +03:00
|
|
|
for (auto& node : arrayOfNodes) {
|
2015-04-24 14:27:35 +03:00
|
|
|
nsresult rv = SetInlinePropertyOnNode(node, aProperty, aAttribute,
|
|
|
|
aValue);
|
2012-05-05 13:00:06 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-04-17 21:50:19 +04:00
|
|
|
}
|
|
|
|
}
|
2012-04-20 10:33:21 +04:00
|
|
|
return NS_OK;
|
2012-04-17 21:50:19 +04:00
|
|
|
}
|
|
|
|
|
2012-05-29 11:12:51 +04:00
|
|
|
// First check if there's an adjacent sibling we can put our node into.
|
2015-04-24 14:27:35 +03:00
|
|
|
nsCOMPtr<nsIContent> previousSibling = GetPriorHTMLSibling(&aNode);
|
|
|
|
nsCOMPtr<nsIContent> nextSibling = GetNextHTMLSibling(&aNode);
|
|
|
|
if (IsSimpleModifiableNode(previousSibling, &aProperty, aAttribute, &aValue)) {
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = MoveNode(&aNode, previousSibling, -1);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-04-24 14:27:35 +03:00
|
|
|
if (IsSimpleModifiableNode(nextSibling, &aProperty, aAttribute, &aValue)) {
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = JoinNodes(*previousSibling, *nextSibling);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-05-29 11:12:51 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-04-24 14:27:35 +03:00
|
|
|
if (IsSimpleModifiableNode(nextSibling, &aProperty, aAttribute, &aValue)) {
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = MoveNode(&aNode, nextSibling, 0);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-05-29 11:12:51 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
// Don't need to do anything if property already set on node
|
2016-07-07 12:35:13 +03:00
|
|
|
if (mCSSEditUtils->IsCSSEditableProperty(&aNode, &aProperty, aAttribute)) {
|
|
|
|
if (mCSSEditUtils->IsCSSEquivalentToHTMLInlineStyleSet(
|
|
|
|
&aNode, &aProperty, aAttribute, aValue, CSSEditUtils::eComputed)) {
|
2012-05-23 11:49:29 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-04-24 14:27:35 +03:00
|
|
|
} else if (IsTextPropertySetByContent(&aNode, &aProperty,
|
|
|
|
aAttribute, &aValue)) {
|
2012-05-23 11:49:29 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-17 14:18:08 +04:00
|
|
|
bool useCSS = (IsCSSEnabled() &&
|
2016-07-07 12:35:13 +03:00
|
|
|
mCSSEditUtils->IsCSSEditableProperty(&aNode, &aProperty,
|
|
|
|
aAttribute)) ||
|
2012-04-24 14:57:49 +04:00
|
|
|
// bgcolor is always done using CSS
|
2016-12-12 13:03:36 +03:00
|
|
|
attrAtom == nsGkAtoms::bgcolor;
|
2012-04-17 21:50:19 +04:00
|
|
|
|
2012-04-17 14:18:08 +04:00
|
|
|
if (useCSS) {
|
2012-06-06 11:36:17 +04:00
|
|
|
nsCOMPtr<dom::Element> tmp;
|
2012-04-20 10:33:21 +04:00
|
|
|
// We only add style="" to <span>s with no attributes (bug 746515). If we
|
|
|
|
// don't have one, we need to make one.
|
2015-04-24 14:27:35 +03:00
|
|
|
if (aNode.IsHTMLElement(nsGkAtoms::span) &&
|
|
|
|
!aNode.AsElement()->GetAttrCount()) {
|
|
|
|
tmp = aNode.AsElement();
|
2012-06-06 11:36:17 +04:00
|
|
|
} else {
|
2015-04-24 14:27:35 +03:00
|
|
|
tmp = InsertContainerAbove(&aNode, nsGkAtoms::span);
|
2014-08-20 16:25:16 +04:00
|
|
|
NS_ENSURE_STATE(tmp);
|
2012-04-17 14:18:08 +04:00
|
|
|
}
|
2012-06-06 11:36:17 +04:00
|
|
|
|
2012-04-20 10:33:21 +04:00
|
|
|
// Add the CSS styles corresponding to the HTML style request
|
2016-12-12 13:03:36 +03:00
|
|
|
mCSSEditUtils->SetCSSEquivalentToHTMLStyle(tmp,
|
|
|
|
&aProperty, attrAtom,
|
|
|
|
&aValue, false);
|
2012-04-20 10:33:21 +04:00
|
|
|
return NS_OK;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2012-04-20 10:33:21 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// is it already the right kind of node, but with wrong attribute?
|
2015-04-24 14:27:35 +03:00
|
|
|
if (aNode.IsHTMLElement(&aProperty)) {
|
2012-04-20 10:33:21 +04:00
|
|
|
// Just set the attribute on it.
|
2015-04-24 14:27:35 +03:00
|
|
|
nsCOMPtr<nsIDOMElement> elem = do_QueryInterface(&aNode);
|
|
|
|
return SetAttribute(elem, *aAttribute, aValue);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-04-20 10:33:21 +04:00
|
|
|
|
2012-04-20 10:33:21 +04:00
|
|
|
// ok, chuck it in its very own container
|
2015-04-24 14:27:35 +03:00
|
|
|
nsCOMPtr<Element> tmp = InsertContainerAbove(&aNode, &aProperty, attrAtom,
|
|
|
|
&aValue);
|
2014-08-20 16:25:16 +04:00
|
|
|
NS_ENSURE_STATE(tmp);
|
|
|
|
|
|
|
|
return NS_OK;
|
2012-04-20 10:33:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::SetInlinePropertyOnNode(nsIContent& aNode,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom& aProperty,
|
2016-07-09 05:42:33 +03:00
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString& aValue)
|
2012-05-05 13:00:06 +04:00
|
|
|
{
|
2015-04-24 14:27:35 +03:00
|
|
|
nsCOMPtr<nsIContent> previousSibling = aNode.GetPreviousSibling(),
|
|
|
|
nextSibling = aNode.GetNextSibling();
|
|
|
|
NS_ENSURE_STATE(aNode.GetParentNode());
|
|
|
|
OwningNonNull<nsINode> parent = *aNode.GetParentNode();
|
2012-04-20 10:33:21 +04:00
|
|
|
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = RemoveStyleInside(aNode, &aProperty, aAttribute);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-04-20 10:33:21 +04:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
if (aNode.GetParentNode()) {
|
2012-04-20 10:33:21 +04:00
|
|
|
// The node is still where it was
|
2015-04-24 14:27:35 +03:00
|
|
|
return SetInlinePropertyOnNodeImpl(aNode, aProperty,
|
|
|
|
aAttribute, aValue);
|
2012-04-20 10:33:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// It's vanished. Use the old siblings for reference to construct a
|
|
|
|
// list. But first, verify that the previous/next siblings are still
|
|
|
|
// where we expect them; otherwise we have to give up.
|
2012-10-09 16:31:24 +04:00
|
|
|
if ((previousSibling && previousSibling->GetParentNode() != parent) ||
|
|
|
|
(nextSibling && nextSibling->GetParentNode() != parent)) {
|
2012-04-20 10:33:21 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2015-04-24 14:27:35 +03:00
|
|
|
nsTArray<OwningNonNull<nsIContent>> nodesToSet;
|
2012-04-20 10:33:21 +04:00
|
|
|
nsCOMPtr<nsIContent> cur = previousSibling
|
|
|
|
? previousSibling->GetNextSibling() : parent->GetFirstChild();
|
2015-04-24 14:27:35 +03:00
|
|
|
for (; cur && cur != nextSibling; cur = cur->GetNextSibling()) {
|
2012-04-20 10:33:21 +04:00
|
|
|
if (IsEditable(cur)) {
|
2015-04-24 14:27:35 +03:00
|
|
|
nodesToSet.AppendElement(*cur);
|
2012-04-20 10:33:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
for (auto& node : nodesToSet) {
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = SetInlinePropertyOnNodeImpl(node, aProperty, aAttribute, aValue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-04-20 10:33:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2014-11-02 15:04:13 +03:00
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::SplitStyleAboveRange(nsRange* inRange,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* aProperty,
|
2016-07-09 05:42:33 +03:00
|
|
|
const nsAString* aAttribute)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(inRange, NS_ERROR_NULL_POINTER);
|
2011-12-22 22:16:56 +04:00
|
|
|
|
2017-07-11 14:53:04 +03:00
|
|
|
nsCOMPtr<nsINode> startNode = inRange->GetStartContainer();
|
2016-05-01 16:11:28 +03:00
|
|
|
int32_t startOffset = inRange->StartOffset();
|
2017-07-11 15:11:37 +03:00
|
|
|
nsCOMPtr<nsINode> endNode = inRange->GetEndContainer();
|
2016-05-01 16:11:28 +03:00
|
|
|
int32_t endOffset = inRange->EndOffset();
|
2011-12-22 22:16:56 +04:00
|
|
|
|
2016-05-01 16:11:28 +03:00
|
|
|
nsCOMPtr<nsINode> origStartNode = startNode;
|
2011-12-22 22:16:56 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// split any matching style nodes above the start of range
|
|
|
|
{
|
2016-06-24 09:08:16 +03:00
|
|
|
AutoTrackDOMPoint tracker(mRangeUpdater, address_of(endNode), &endOffset);
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv =
|
|
|
|
SplitStyleAbovePoint(address_of(startNode), &startOffset, aProperty,
|
|
|
|
aAttribute);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2002-06-12 18:04:47 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// second verse, same as the first...
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv =
|
|
|
|
SplitStyleAbovePoint(address_of(endNode), &endOffset, aProperty,
|
|
|
|
aAttribute);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-12-22 22:16:56 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// reset the range
|
2017-05-30 07:18:25 +03:00
|
|
|
rv = inRange->SetStartAndEnd(startNode, startOffset, endNode, endOffset);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2016-05-01 16:11:28 +03:00
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::SplitStyleAbovePoint(nsCOMPtr<nsINode>* aNode,
|
|
|
|
int32_t* aOffset,
|
|
|
|
// null here means we split all properties
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* aProperty,
|
2016-07-09 05:42:33 +03:00
|
|
|
const nsAString* aAttribute,
|
|
|
|
nsIContent** aOutLeftNode,
|
|
|
|
nsIContent** aOutRightNode)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2016-04-23 14:02:28 +03:00
|
|
|
NS_ENSURE_TRUE(aNode && *aNode && aOffset, NS_ERROR_NULL_POINTER);
|
2016-05-01 16:11:28 +03:00
|
|
|
NS_ENSURE_TRUE((*aNode)->IsContent(), NS_OK);
|
|
|
|
|
|
|
|
// Split any matching style nodes above the node/offset
|
|
|
|
OwningNonNull<nsIContent> node = *(*aNode)->AsContent();
|
2002-01-09 16:51:37 +03:00
|
|
|
|
2012-02-01 14:54:22 +04:00
|
|
|
bool useCSS = IsCSSEnabled();
|
2002-01-09 16:51:37 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isSet;
|
2016-05-01 16:11:28 +03:00
|
|
|
while (!IsBlockNode(node) && node->GetParent() &&
|
|
|
|
IsEditable(node->GetParent())) {
|
2011-10-17 18:59:28 +04:00
|
|
|
isSet = false;
|
2016-07-07 12:35:13 +03:00
|
|
|
if (useCSS && mCSSEditUtils->IsCSSEditableProperty(node, aProperty,
|
2016-05-01 16:11:28 +03:00
|
|
|
aAttribute)) {
|
|
|
|
// The HTML style defined by aProperty/aAttribute has a CSS equivalence
|
|
|
|
// in this implementation for the node; let's check if it carries those
|
|
|
|
// CSS styles
|
2002-01-09 16:51:37 +03:00
|
|
|
nsAutoString firstValue;
|
2016-12-12 13:03:36 +03:00
|
|
|
isSet = mCSSEditUtils->IsCSSEquivalentToHTMLInlineStyleSet(
|
|
|
|
node, aProperty, aAttribute, firstValue,
|
|
|
|
CSSEditUtils::eSpecified);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2014-10-22 16:45:30 +04:00
|
|
|
if (// node is the correct inline prop
|
2015-03-03 14:08:59 +03:00
|
|
|
(aProperty && node->IsHTMLElement(aProperty)) ||
|
2014-10-22 16:45:30 +04:00
|
|
|
// node is href - test if really <a href=...
|
2016-07-07 08:01:12 +03:00
|
|
|
(aProperty == nsGkAtoms::href && HTMLEditUtils::IsLink(node)) ||
|
2014-10-22 16:45:30 +04:00
|
|
|
// or node is any prop, and we asked to split them all
|
2016-05-01 16:12:15 +03:00
|
|
|
(!aProperty && NodeIsProperty(node)) ||
|
2014-10-22 16:45:30 +04:00
|
|
|
// or the style is specified in the style attribute
|
|
|
|
isSet) {
|
2016-05-01 16:11:28 +03:00
|
|
|
// Found a style node we need to split
|
|
|
|
int32_t offset = SplitNodeDeep(*node, *(*aNode)->AsContent(), *aOffset,
|
|
|
|
EmptyContainers::yes, aOutLeftNode,
|
|
|
|
aOutRightNode);
|
2015-10-08 18:27:19 +03:00
|
|
|
NS_ENSURE_TRUE(offset != -1, NS_ERROR_FAILURE);
|
2001-01-28 23:13:07 +03:00
|
|
|
// reset startNode/startOffset
|
2016-05-01 16:11:28 +03:00
|
|
|
*aNode = node->GetParent();
|
2001-01-28 23:13:07 +03:00
|
|
|
*aOffset = offset;
|
|
|
|
}
|
2014-10-22 16:45:30 +04:00
|
|
|
node = node->GetParent();
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2016-05-01 16:11:28 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-10 16:00:14 +04:00
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::ClearStyle(nsCOMPtr<nsINode>* aNode,
|
|
|
|
int32_t* aOffset,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* aProperty,
|
2016-07-09 05:42:33 +03:00
|
|
|
const nsAString* aAttribute)
|
2012-05-10 16:00:14 +04:00
|
|
|
{
|
2016-05-01 16:11:28 +03:00
|
|
|
nsCOMPtr<nsIContent> leftNode, rightNode;
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = SplitStyleAbovePoint(aNode, aOffset, aProperty,
|
|
|
|
aAttribute, getter_AddRefs(leftNode),
|
|
|
|
getter_AddRefs(rightNode));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2016-05-01 16:11:28 +03:00
|
|
|
|
2012-05-10 16:00:14 +04:00
|
|
|
if (leftNode) {
|
|
|
|
bool bIsEmptyNode;
|
|
|
|
IsEmptyNode(leftNode, &bIsEmptyNode, false, true);
|
|
|
|
if (bIsEmptyNode) {
|
|
|
|
// delete leftNode if it became empty
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = DeleteNode(leftNode);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-05-10 16:00:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rightNode) {
|
2016-05-01 16:11:28 +03:00
|
|
|
nsCOMPtr<nsINode> secondSplitParent = GetLeftmostChild(rightNode);
|
2016-07-08 08:15:21 +03:00
|
|
|
// don't try to split non-containers (br's, images, hr's, etc.)
|
2012-05-10 16:00:14 +04:00
|
|
|
if (!secondSplitParent) {
|
|
|
|
secondSplitParent = rightNode;
|
|
|
|
}
|
2014-08-20 16:25:16 +04:00
|
|
|
nsCOMPtr<Element> savedBR;
|
2012-05-10 16:00:14 +04:00
|
|
|
if (!IsContainer(secondSplitParent)) {
|
2016-07-07 07:44:32 +03:00
|
|
|
if (TextEditUtils::IsBreak(secondSplitParent)) {
|
2014-08-20 16:25:16 +04:00
|
|
|
savedBR = do_QueryInterface(secondSplitParent);
|
|
|
|
NS_ENSURE_STATE(savedBR);
|
2012-05-10 16:00:14 +04:00
|
|
|
}
|
|
|
|
|
2016-05-01 16:11:28 +03:00
|
|
|
secondSplitParent = secondSplitParent->GetParentNode();
|
2012-05-10 16:00:14 +04:00
|
|
|
}
|
|
|
|
*aOffset = 0;
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = SplitStyleAbovePoint(address_of(secondSplitParent),
|
|
|
|
aOffset, aProperty, aAttribute,
|
|
|
|
getter_AddRefs(leftNode),
|
|
|
|
getter_AddRefs(rightNode));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2017-05-25 11:15:54 +03:00
|
|
|
|
|
|
|
if (rightNode) {
|
|
|
|
bool bIsEmptyNode;
|
|
|
|
IsEmptyNode(rightNode, &bIsEmptyNode, false, true);
|
|
|
|
if (bIsEmptyNode) {
|
|
|
|
// delete rightNode if it became empty
|
|
|
|
rv = DeleteNode(rightNode);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!leftNode) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-10 16:00:14 +04:00
|
|
|
// should be impossible to not get a new leftnode here
|
2016-05-01 16:11:28 +03:00
|
|
|
nsCOMPtr<nsINode> newSelParent = GetLeftmostChild(leftNode);
|
2012-05-10 16:00:14 +04:00
|
|
|
if (!newSelParent) {
|
2016-05-01 16:11:28 +03:00
|
|
|
newSelParent = leftNode;
|
2012-05-10 16:00:14 +04:00
|
|
|
}
|
|
|
|
// If rightNode starts with a br, suck it out of right node and into
|
|
|
|
// leftNode. This is so we you don't revert back to the previous style
|
|
|
|
// if you happen to click at the end of a line.
|
|
|
|
if (savedBR) {
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = MoveNode(savedBR, newSelParent, 0);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-05-10 16:00:14 +04:00
|
|
|
}
|
|
|
|
// remove the style on this new hierarchy
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t newSelOffset = 0;
|
2012-05-10 16:00:14 +04:00
|
|
|
{
|
|
|
|
// Track the point at the new hierarchy. This is so we can know where
|
|
|
|
// to put the selection after we call RemoveStyleInside().
|
|
|
|
// RemoveStyleInside() could remove any and all of those nodes, so I
|
|
|
|
// have to use the range tracking system to find the right spot to put
|
|
|
|
// selection.
|
2016-06-24 09:08:16 +03:00
|
|
|
AutoTrackDOMPoint tracker(mRangeUpdater,
|
|
|
|
address_of(newSelParent), &newSelOffset);
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = RemoveStyleInside(*leftNode, aProperty, aAttribute);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-05-10 16:00:14 +04:00
|
|
|
}
|
|
|
|
// reset our node offset values to the resulting new sel point
|
2016-05-01 16:11:40 +03:00
|
|
|
*aNode = newSelParent;
|
2012-05-10 16:00:14 +04:00
|
|
|
*aOffset = newSelOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-01 16:12:15 +03:00
|
|
|
bool
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::NodeIsProperty(nsINode& aNode)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2016-05-01 16:12:15 +03:00
|
|
|
return IsContainer(&aNode) && IsEditable(&aNode) && !IsBlockNode(&aNode) &&
|
|
|
|
!aNode.IsHTMLElement(nsGkAtoms::a);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2015-05-13 18:58:26 +03:00
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::RemoveStyleInside(nsIContent& aNode,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* aProperty,
|
2016-07-09 05:42:33 +03:00
|
|
|
const nsAString* aAttribute,
|
|
|
|
const bool aChildrenOnly /* = false */)
|
2015-05-13 18:58:26 +03:00
|
|
|
{
|
2016-05-01 16:12:51 +03:00
|
|
|
if (aNode.GetAsText()) {
|
2015-05-13 18:58:26 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// first process the children
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIContent> child = aNode.GetFirstChild();
|
2012-04-17 21:50:19 +04:00
|
|
|
while (child) {
|
2001-01-28 23:13:07 +03:00
|
|
|
// cache next sibling since we might remove child
|
2015-05-13 18:58:26 +03:00
|
|
|
nsCOMPtr<nsIContent> next = child->GetNextSibling();
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = RemoveStyleInside(*child, aProperty, aAttribute);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-05-13 18:58:26 +03:00
|
|
|
child = next.forget();
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// then process the node itself
|
2012-08-21 22:31:19 +04:00
|
|
|
if (!aChildrenOnly &&
|
2016-10-24 05:27:45 +03:00
|
|
|
// node is prop we asked for
|
|
|
|
((aProperty && aNode.NodeInfo()->NameAtom() == aProperty) ||
|
|
|
|
// but check for link (<a href=...)
|
|
|
|
(aProperty == nsGkAtoms::href && HTMLEditUtils::IsLink(&aNode)) ||
|
|
|
|
// and for named anchors
|
|
|
|
(aProperty == nsGkAtoms::name && HTMLEditUtils::IsNamedAnchor(&aNode)) ||
|
|
|
|
// or node is any prop and we asked for that
|
|
|
|
(!aProperty && NodeIsProperty(aNode)))) {
|
2015-05-28 18:58:42 +03:00
|
|
|
// if we weren't passed an attribute, then we want to
|
2001-01-28 23:13:07 +03:00
|
|
|
// remove any matching inlinestyles entirely
|
2012-04-17 21:50:19 +04:00
|
|
|
if (!aAttribute || aAttribute->IsEmpty()) {
|
2015-05-13 18:58:26 +03:00
|
|
|
bool hasStyleAttr = aNode.HasAttr(kNameSpaceID_None, nsGkAtoms::style);
|
|
|
|
bool hasClassAttr = aNode.HasAttr(kNameSpaceID_None, nsGkAtoms::_class);
|
2012-04-17 21:50:19 +04:00
|
|
|
if (aProperty && (hasStyleAttr || hasClassAttr)) {
|
2003-06-17 12:45:54 +04:00
|
|
|
// aNode carries inline styles or a class attribute so we can't
|
|
|
|
// just remove the element... We need to create above the element
|
|
|
|
// a span that will carry those styles or class, then we can delete
|
|
|
|
// the node.
|
2016-12-20 14:53:00 +03:00
|
|
|
RefPtr<Element> spanNode =
|
2015-05-13 18:58:26 +03:00
|
|
|
InsertContainerAbove(&aNode, nsGkAtoms::span);
|
2014-08-20 16:25:16 +04:00
|
|
|
NS_ENSURE_STATE(spanNode);
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv =
|
2016-12-20 14:53:00 +03:00
|
|
|
CloneAttribute(nsGkAtoms::style, spanNode, aNode.AsElement());
|
2016-10-19 12:09:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv =
|
2016-12-20 14:53:00 +03:00
|
|
|
CloneAttribute(nsGkAtoms::_class, spanNode, aNode.AsElement());
|
2016-10-19 12:09:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-06-17 12:45:54 +04:00
|
|
|
}
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = RemoveContainer(&aNode);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-04-17 21:50:19 +04:00
|
|
|
} else {
|
|
|
|
// otherwise we just want to eliminate the attribute
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> attribute = NS_Atomize(*aAttribute);
|
2015-05-13 18:58:26 +03:00
|
|
|
if (aNode.HasAttr(kNameSpaceID_None, attribute)) {
|
2001-01-28 23:13:07 +03:00
|
|
|
// if this matching attribute is the ONLY one on the node,
|
|
|
|
// then remove the whole node. Otherwise just nix the attribute.
|
2015-05-13 18:58:26 +03:00
|
|
|
if (IsOnlyAttribute(&aNode, *aAttribute)) {
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = RemoveContainer(&aNode);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-04-17 21:50:19 +04:00
|
|
|
} else {
|
2015-05-13 18:58:26 +03:00
|
|
|
nsCOMPtr<nsIDOMElement> elem = do_QueryInterface(&aNode);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(elem, NS_ERROR_NULL_POINTER);
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = RemoveAttribute(elem, *aAttribute);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-17 21:50:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!aChildrenOnly &&
|
2016-07-07 12:35:13 +03:00
|
|
|
mCSSEditUtils->IsCSSEditableProperty(&aNode, aProperty, aAttribute)) {
|
2012-04-17 21:50:19 +04:00
|
|
|
// the HTML style defined by aProperty/aAttribute has a CSS equivalence in
|
|
|
|
// this implementation for the node aNode; let's check if it carries those
|
|
|
|
// css styles
|
2017-02-23 11:10:27 +03:00
|
|
|
if (aNode.IsElement()) {
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> attribute =
|
2017-02-23 11:10:27 +03:00
|
|
|
aAttribute ? NS_Atomize(*aAttribute) : nullptr;
|
|
|
|
bool hasAttribute =
|
|
|
|
mCSSEditUtils->HaveCSSEquivalentStyles(
|
|
|
|
aNode, aProperty, attribute, CSSEditUtils::eSpecified);
|
|
|
|
if (hasAttribute) {
|
|
|
|
// yes, tmp has the corresponding css declarations in its style
|
|
|
|
// attribute
|
|
|
|
// let's remove them
|
|
|
|
mCSSEditUtils->RemoveCSSEquivalentToHTMLStyle(aNode.AsElement(),
|
|
|
|
aProperty,
|
|
|
|
attribute,
|
|
|
|
nullptr,
|
|
|
|
false);
|
|
|
|
// remove the node if it is a span or font, if its style attribute is
|
|
|
|
// empty or absent, and if it does not have a class nor an id
|
|
|
|
RemoveElementIfNoStyleOrIdOrClass(*aNode.AsElement());
|
|
|
|
}
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2001-10-22 10:22:52 +04:00
|
|
|
}
|
2012-04-17 21:50:19 +04:00
|
|
|
|
2016-10-24 05:27:45 +03:00
|
|
|
// Or node is big or small and we are setting font size
|
|
|
|
if (aChildrenOnly) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (aProperty == nsGkAtoms::font &&
|
|
|
|
(aNode.IsHTMLElement(nsGkAtoms::big) ||
|
|
|
|
aNode.IsHTMLElement(nsGkAtoms::small)) &&
|
|
|
|
aAttribute && aAttribute->LowerCaseEqualsLiteral("size")) {
|
2014-08-20 16:25:16 +04:00
|
|
|
// if we are setting font size, remove any nested bigs and smalls
|
2015-05-13 18:58:26 +03:00
|
|
|
return RemoveContainer(&aNode);
|
2012-04-17 21:50:19 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2012-05-05 13:00:06 +04:00
|
|
|
bool
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::IsOnlyAttribute(const nsIContent* aContent,
|
|
|
|
const nsAString& aAttribute)
|
2012-05-05 13:00:06 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aContent);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t attrCount = aContent->GetAttrCount();
|
|
|
|
for (uint32_t i = 0; i < attrCount; ++i) {
|
2012-05-05 13:00:06 +04:00
|
|
|
const nsAttrName* name = aContent->GetAttrNameAt(i);
|
2005-12-29 00:52:39 +03:00
|
|
|
if (!name->NamespaceEquals(kNameSpaceID_None)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-12-29 00:52:39 +03:00
|
|
|
}
|
2012-05-05 13:00:06 +04:00
|
|
|
|
|
|
|
nsAutoString attrString;
|
2005-12-29 00:52:39 +03:00
|
|
|
name->LocalName()->ToString(attrString);
|
|
|
|
// if it's the attribute we know about, or a special _moz attribute,
|
|
|
|
// keep looking
|
2012-05-05 13:00:06 +04:00
|
|
|
if (!attrString.Equals(aAttribute, nsCaseInsensitiveStringComparator()) &&
|
2005-12-29 00:52:39 +03:00
|
|
|
!StringBeginsWith(attrString, NS_LITERAL_STRING("_moz"))) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-12-29 00:52:39 +03:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
// if we made it through all of them without finding a real attribute
|
2011-10-17 18:59:28 +04:00
|
|
|
// other than aAttribute, then return true
|
|
|
|
return true;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2014-11-02 15:04:13 +03:00
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::PromoteRangeIfStartsOrEndsInNamedAnchor(nsRange& aRange)
|
2002-04-11 17:33:30 +04:00
|
|
|
{
|
2016-05-01 16:12:51 +03:00
|
|
|
// We assume that <a> is not nested.
|
2017-07-11 14:53:04 +03:00
|
|
|
nsCOMPtr<nsINode> startNode = aRange.GetStartContainer();
|
2016-05-01 16:12:51 +03:00
|
|
|
int32_t startOffset = aRange.StartOffset();
|
2017-07-11 15:11:37 +03:00
|
|
|
nsCOMPtr<nsINode> endNode = aRange.GetEndContainer();
|
2016-05-01 16:12:51 +03:00
|
|
|
int32_t endOffset = aRange.EndOffset();
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2016-05-01 16:12:51 +03:00
|
|
|
nsCOMPtr<nsINode> parent = startNode;
|
2002-04-11 17:33:30 +04:00
|
|
|
|
2016-05-01 16:12:51 +03:00
|
|
|
while (parent && !parent->IsHTMLElement(nsGkAtoms::body) &&
|
2016-07-07 08:01:12 +03:00
|
|
|
!HTMLEditUtils::IsNamedAnchor(parent)) {
|
2016-05-01 16:12:51 +03:00
|
|
|
parent = parent->GetParentNode();
|
2002-04-11 17:33:30 +04:00
|
|
|
}
|
2016-05-01 16:12:51 +03:00
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
|
|
|
|
|
2016-07-07 08:01:12 +03:00
|
|
|
if (HTMLEditUtils::IsNamedAnchor(parent)) {
|
2016-05-01 16:12:51 +03:00
|
|
|
startNode = parent->GetParentNode();
|
|
|
|
startOffset = startNode ? startNode->IndexOf(parent) : -1;
|
2002-04-11 17:33:30 +04:00
|
|
|
}
|
|
|
|
|
2016-05-01 16:12:51 +03:00
|
|
|
parent = endNode;
|
|
|
|
while (parent && !parent->IsHTMLElement(nsGkAtoms::body) &&
|
2016-07-07 08:01:12 +03:00
|
|
|
!HTMLEditUtils::IsNamedAnchor(parent)) {
|
2016-05-01 16:12:51 +03:00
|
|
|
parent = parent->GetParentNode();
|
2002-04-11 17:33:30 +04:00
|
|
|
}
|
2016-05-01 16:12:51 +03:00
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
|
|
|
|
|
2016-07-07 08:01:12 +03:00
|
|
|
if (HTMLEditUtils::IsNamedAnchor(parent)) {
|
2016-05-01 16:12:51 +03:00
|
|
|
endNode = parent->GetParentNode();
|
|
|
|
endOffset = endNode ? endNode->IndexOf(parent) + 1 : 0;
|
2002-04-11 17:33:30 +04:00
|
|
|
}
|
|
|
|
|
2017-05-30 07:18:25 +03:00
|
|
|
nsresult rv = aRange.SetStartAndEnd(startNode, startOffset,
|
|
|
|
endNode, endOffset);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2016-05-01 16:12:51 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2002-04-11 17:33:30 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2014-11-02 15:04:13 +03:00
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::PromoteInlineRange(nsRange& aRange)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2017-07-11 14:53:04 +03:00
|
|
|
nsCOMPtr<nsINode> startNode = aRange.GetStartContainer();
|
2016-05-01 16:12:39 +03:00
|
|
|
int32_t startOffset = aRange.StartOffset();
|
2017-07-11 15:11:37 +03:00
|
|
|
nsCOMPtr<nsINode> endNode = aRange.GetEndContainer();
|
2016-05-01 16:12:39 +03:00
|
|
|
int32_t endOffset = aRange.EndOffset();
|
|
|
|
|
|
|
|
while (startNode && !startNode->IsHTMLElement(nsGkAtoms::body) &&
|
|
|
|
IsEditable(startNode) && IsAtFrontOfNode(*startNode, startOffset)) {
|
|
|
|
nsCOMPtr<nsINode> parent = startNode->GetParentNode();
|
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
|
|
|
|
startOffset = parent->IndexOf(startNode);
|
2001-01-28 23:13:07 +03:00
|
|
|
startNode = parent;
|
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2016-05-01 16:12:39 +03:00
|
|
|
while (endNode && !endNode->IsHTMLElement(nsGkAtoms::body) &&
|
|
|
|
IsEditable(endNode) && IsAtEndOfNode(*endNode, endOffset)) {
|
|
|
|
nsCOMPtr<nsINode> parent = endNode->GetParentNode();
|
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
|
|
|
|
// We are AFTER this node
|
|
|
|
endOffset = 1 + parent->IndexOf(endNode);
|
2001-01-28 23:13:07 +03:00
|
|
|
endNode = parent;
|
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2017-05-30 07:18:25 +03:00
|
|
|
nsresult rv = aRange.SetStartAndEnd(startNode, startOffset,
|
|
|
|
endNode, endOffset);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2016-05-01 16:12:39 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2016-05-01 16:12:39 +03:00
|
|
|
bool
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::IsAtFrontOfNode(nsINode& aNode,
|
|
|
|
int32_t aOffset)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2011-07-15 01:06:37 +04:00
|
|
|
if (!aOffset) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-07-15 01:06:37 +04:00
|
|
|
}
|
|
|
|
|
2016-05-01 16:12:39 +03:00
|
|
|
if (IsTextNode(&aNode)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2016-05-01 16:12:39 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> firstNode = GetFirstEditableChild(aNode);
|
|
|
|
NS_ENSURE_TRUE(firstNode, true);
|
|
|
|
if (aNode.IndexOf(firstNode) < aOffset) {
|
|
|
|
return false;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2016-05-01 16:12:39 +03:00
|
|
|
return true;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2016-05-01 16:12:39 +03:00
|
|
|
bool
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::IsAtEndOfNode(nsINode& aNode,
|
|
|
|
int32_t aOffset)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2016-05-01 16:12:39 +03:00
|
|
|
if (aOffset == (int32_t)aNode.Length()) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2016-05-01 16:12:39 +03:00
|
|
|
if (IsTextNode(&aNode)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2016-05-01 16:12:39 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> lastNode = GetLastEditableChild(aNode);
|
|
|
|
NS_ENSURE_TRUE(lastNode, true);
|
|
|
|
if (aNode.IndexOf(lastNode) < aOffset) {
|
|
|
|
return true;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2016-05-01 16:12:39 +03:00
|
|
|
return false;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-07 04:45:26 +04:00
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
HTMLEditor::GetInlinePropertyBase(nsAtom& aProperty,
|
2016-07-09 05:42:33 +03:00
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue,
|
|
|
|
bool* aFirst,
|
|
|
|
bool* aAny,
|
|
|
|
bool* aAll,
|
2017-08-25 09:59:17 +03:00
|
|
|
nsAString* outValue)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2012-04-18 18:26:02 +04:00
|
|
|
*aAny = false;
|
|
|
|
*aAll = true;
|
|
|
|
*aFirst = false;
|
|
|
|
bool first = true;
|
2002-01-09 16:51:37 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Selection> selection = GetSelection();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-05-18 12:29:39 +04:00
|
|
|
bool isCollapsed = selection->Collapsed();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsRange> range = selection->GetRangeAt(0);
|
2015-04-19 15:28:50 +03:00
|
|
|
// XXX: Should be a while loop, to get each separate range
|
2001-01-28 23:13:07 +03:00
|
|
|
// XXX: ERROR_HANDLING can currentItem be null?
|
2013-02-07 18:17:47 +04:00
|
|
|
if (range) {
|
2015-04-19 15:28:50 +03:00
|
|
|
// For each range, set a flag
|
|
|
|
bool firstNodeInRange = true;
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-04-18 18:26:02 +04:00
|
|
|
if (isCollapsed) {
|
2017-07-11 14:53:04 +03:00
|
|
|
nsCOMPtr<nsINode> collapsedNode = range->GetStartContainer();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(collapsedNode, NS_ERROR_FAILURE);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isSet, theSetting;
|
2012-05-03 11:09:44 +04:00
|
|
|
nsString tOutString;
|
2012-04-18 18:26:02 +04:00
|
|
|
if (aAttribute) {
|
2012-05-03 11:09:44 +04:00
|
|
|
nsString tString(*aAttribute);
|
2015-04-19 15:28:50 +03:00
|
|
|
mTypeInState->GetTypingState(isSet, theSetting, &aProperty, tString,
|
2012-04-18 18:26:02 +04:00
|
|
|
&tOutString);
|
|
|
|
if (outValue) {
|
2001-04-07 04:45:26 +04:00
|
|
|
outValue->Assign(tOutString);
|
2012-04-18 18:26:02 +04:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-19 15:28:50 +03:00
|
|
|
mTypeInState->GetTypingState(isSet, theSetting, &aProperty);
|
2012-04-18 18:26:02 +04:00
|
|
|
}
|
|
|
|
if (isSet) {
|
2001-04-07 04:45:26 +04:00
|
|
|
*aFirst = *aAny = *aAll = theSetting;
|
2001-01-28 23:13:07 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-05-03 11:09:44 +04:00
|
|
|
|
2016-07-07 12:35:13 +03:00
|
|
|
if (mCSSEditUtils->IsCSSEditableProperty(collapsedNode, &aProperty,
|
2015-04-19 15:28:50 +03:00
|
|
|
aAttribute)) {
|
2015-04-24 12:57:00 +03:00
|
|
|
if (aValue) {
|
|
|
|
tOutString.Assign(*aValue);
|
|
|
|
}
|
2015-04-19 15:28:50 +03:00
|
|
|
*aFirst = *aAny = *aAll =
|
2016-07-07 12:35:13 +03:00
|
|
|
mCSSEditUtils->IsCSSEquivalentToHTMLInlineStyleSet(collapsedNode,
|
|
|
|
&aProperty, aAttribute, tOutString, CSSEditUtils::eComputed);
|
2012-05-03 11:09:44 +04:00
|
|
|
if (outValue) {
|
|
|
|
outValue->Assign(tOutString);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-12-22 22:16:56 +04:00
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
isSet = IsTextPropertySetByContent(collapsedNode, &aProperty,
|
|
|
|
aAttribute, aValue, outValue);
|
2012-05-03 11:09:44 +04:00
|
|
|
*aFirst = *aAny = *aAll = isSet;
|
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
// Non-collapsed selection
|
|
|
|
nsCOMPtr<nsIContentIterator> iter = NS_NewContentIterator();
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
nsAutoString firstValue, theValue;
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2017-07-11 15:11:37 +03:00
|
|
|
nsCOMPtr<nsINode> endNode = range->GetEndContainer();
|
2015-04-19 15:28:50 +03:00
|
|
|
int32_t endOffset = range->EndOffset();
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-04-18 18:26:02 +04:00
|
|
|
for (iter->Init(range); !iter->IsDone(); iter->Next()) {
|
2012-06-10 16:39:21 +04:00
|
|
|
if (!iter->GetCurrentNode()->IsContent()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIContent> content = iter->GetCurrentNode()->AsContent();
|
2001-08-03 00:31:09 +04:00
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
if (content->IsHTMLElement(nsGkAtoms::body)) {
|
2001-08-03 00:31:09 +04:00
|
|
|
break;
|
2012-04-18 18:26:02 +04:00
|
|
|
}
|
2001-08-03 00:31:09 +04:00
|
|
|
|
2001-02-07 12:24:21 +03:00
|
|
|
// just ignore any non-editable nodes
|
2015-04-19 15:28:50 +03:00
|
|
|
if (content->GetAsText() && (!IsEditable(content) ||
|
|
|
|
IsEmptyTextNode(this, content))) {
|
2012-04-18 18:26:02 +04:00
|
|
|
continue;
|
2001-02-07 12:24:21 +03:00
|
|
|
}
|
2015-04-19 15:28:50 +03:00
|
|
|
if (content->GetAsText()) {
|
2012-04-18 18:26:02 +04:00
|
|
|
if (!isCollapsed && first && firstNodeInRange) {
|
2011-10-17 18:59:28 +04:00
|
|
|
firstNodeInRange = false;
|
2017-07-19 16:49:52 +03:00
|
|
|
if (range->StartOffset() == content->Length()) {
|
2012-04-18 18:26:02 +04:00
|
|
|
continue;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2015-04-19 15:28:50 +03:00
|
|
|
} else if (content == endNode && !endOffset) {
|
2012-04-18 18:26:02 +04:00
|
|
|
continue;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-04-18 18:26:02 +04:00
|
|
|
} else if (content->IsElement()) {
|
|
|
|
// handle non-text leaf nodes here
|
|
|
|
continue;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-06-10 16:39:21 +04:00
|
|
|
|
|
|
|
bool isSet = false;
|
|
|
|
if (first) {
|
2016-07-07 12:35:13 +03:00
|
|
|
if (mCSSEditUtils->IsCSSEditableProperty(content, &aProperty,
|
2015-04-19 15:28:50 +03:00
|
|
|
aAttribute)) {
|
|
|
|
// The HTML styles defined by aProperty/aAttribute have a CSS
|
2012-06-10 16:39:21 +04:00
|
|
|
// equivalence in this implementation for node; let's check if it
|
2015-04-19 15:28:50 +03:00
|
|
|
// carries those CSS styles
|
2012-06-10 16:39:21 +04:00
|
|
|
if (aValue) {
|
|
|
|
firstValue.Assign(*aValue);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2016-07-07 12:35:13 +03:00
|
|
|
isSet = mCSSEditUtils->IsCSSEquivalentToHTMLInlineStyleSet(content,
|
|
|
|
&aProperty, aAttribute, firstValue, CSSEditUtils::eComputed);
|
2012-04-18 18:26:02 +04:00
|
|
|
} else {
|
2015-04-19 15:28:50 +03:00
|
|
|
isSet = IsTextPropertySetByContent(content, &aProperty, aAttribute,
|
|
|
|
aValue, &firstValue);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-06-10 16:39:21 +04:00
|
|
|
*aFirst = isSet;
|
|
|
|
first = false;
|
|
|
|
if (outValue) {
|
|
|
|
*outValue = firstValue;
|
|
|
|
}
|
|
|
|
} else {
|
2016-07-07 12:35:13 +03:00
|
|
|
if (mCSSEditUtils->IsCSSEditableProperty(content, &aProperty,
|
2015-04-19 15:28:50 +03:00
|
|
|
aAttribute)) {
|
|
|
|
// The HTML styles defined by aProperty/aAttribute have a CSS
|
|
|
|
// equivalence in this implementation for node; let's check if it
|
|
|
|
// carries those CSS styles
|
2012-06-10 16:39:21 +04:00
|
|
|
if (aValue) {
|
|
|
|
theValue.Assign(*aValue);
|
|
|
|
}
|
2016-07-07 12:35:13 +03:00
|
|
|
isSet = mCSSEditUtils->IsCSSEquivalentToHTMLInlineStyleSet(content,
|
|
|
|
&aProperty, aAttribute, theValue, CSSEditUtils::eComputed);
|
2012-04-18 18:26:02 +04:00
|
|
|
} else {
|
2015-04-19 15:28:50 +03:00
|
|
|
isSet = IsTextPropertySetByContent(content, &aProperty, aAttribute,
|
|
|
|
aValue, &theValue);
|
2012-06-10 16:39:21 +04:00
|
|
|
}
|
|
|
|
if (firstValue != theValue) {
|
2012-04-18 18:26:02 +04:00
|
|
|
*aAll = false;
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-06-10 16:39:21 +04:00
|
|
|
|
|
|
|
if (isSet) {
|
|
|
|
*aAny = true;
|
|
|
|
} else {
|
|
|
|
*aAll = false;
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
2012-04-18 18:26:02 +04:00
|
|
|
if (!*aAny) {
|
|
|
|
// make sure that if none of the selection is set, we don't report all is
|
|
|
|
// set
|
2011-10-17 18:59:28 +04:00
|
|
|
*aAll = false;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2015-04-19 15:28:50 +03:00
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2016-07-09 05:42:33 +03:00
|
|
|
NS_IMETHODIMP
|
2017-08-25 08:40:45 +03:00
|
|
|
HTMLEditor::GetInlineProperty(const nsAString& aProperty,
|
|
|
|
const nsAString& aAttribute,
|
|
|
|
const nsAString& aValue,
|
|
|
|
bool* aFirst,
|
|
|
|
bool* aAny,
|
|
|
|
bool* aAll)
|
|
|
|
{
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> property = NS_Atomize(aProperty);
|
2017-09-07 05:20:51 +03:00
|
|
|
return GetInlineProperty(property, aAttribute, aValue, aFirst, aAny, aAll);
|
2017-08-25 08:40:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
HTMLEditor::GetInlineProperty(nsAtom* aProperty,
|
2016-07-09 05:42:33 +03:00
|
|
|
const nsAString& aAttribute,
|
|
|
|
const nsAString& aValue,
|
|
|
|
bool* aFirst,
|
|
|
|
bool* aAny,
|
|
|
|
bool* aAll)
|
2001-04-07 04:45:26 +04:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(aProperty && aFirst && aAny && aAll, NS_ERROR_NULL_POINTER);
|
2012-07-30 18:20:58 +04:00
|
|
|
const nsAString *att = nullptr;
|
2003-05-24 01:34:47 +04:00
|
|
|
if (!aAttribute.IsEmpty())
|
2001-04-07 04:45:26 +04:00
|
|
|
att = &aAttribute;
|
2012-07-30 18:20:58 +04:00
|
|
|
const nsAString *val = nullptr;
|
2003-05-24 01:34:47 +04:00
|
|
|
if (!aValue.IsEmpty())
|
2001-04-07 04:45:26 +04:00
|
|
|
val = &aValue;
|
2015-04-19 15:28:50 +03:00
|
|
|
return GetInlinePropertyBase(*aProperty, att, val, aFirst, aAny, aAll, nullptr);
|
2001-04-07 04:45:26 +04:00
|
|
|
}
|
|
|
|
|
2016-07-09 05:42:33 +03:00
|
|
|
NS_IMETHODIMP
|
2017-08-25 08:40:45 +03:00
|
|
|
HTMLEditor::GetInlinePropertyWithAttrValue(const nsAString& aProperty,
|
|
|
|
const nsAString& aAttribute,
|
|
|
|
const nsAString& aValue,
|
|
|
|
bool* aFirst,
|
|
|
|
bool* aAny,
|
|
|
|
bool* aAll,
|
|
|
|
nsAString& outValue)
|
|
|
|
{
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> property = NS_Atomize(aProperty);
|
2017-09-07 05:20:51 +03:00
|
|
|
return GetInlinePropertyWithAttrValue(property, aAttribute, aValue, aFirst,
|
|
|
|
aAny, aAll, outValue);
|
2017-08-25 08:40:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
HTMLEditor::GetInlinePropertyWithAttrValue(nsAtom* aProperty,
|
2016-07-09 05:42:33 +03:00
|
|
|
const nsAString& aAttribute,
|
|
|
|
const nsAString& aValue,
|
|
|
|
bool* aFirst,
|
|
|
|
bool* aAny,
|
|
|
|
bool* aAll,
|
|
|
|
nsAString& outValue)
|
2001-04-07 04:45:26 +04:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(aProperty && aFirst && aAny && aAll, NS_ERROR_NULL_POINTER);
|
2012-07-30 18:20:58 +04:00
|
|
|
const nsAString *att = nullptr;
|
2003-05-24 01:34:47 +04:00
|
|
|
if (!aAttribute.IsEmpty())
|
2001-04-07 04:45:26 +04:00
|
|
|
att = &aAttribute;
|
2012-07-30 18:20:58 +04:00
|
|
|
const nsAString *val = nullptr;
|
2003-05-24 01:34:47 +04:00
|
|
|
if (!aValue.IsEmpty())
|
2001-04-07 04:45:26 +04:00
|
|
|
val = &aValue;
|
2015-04-19 15:28:50 +03:00
|
|
|
return GetInlinePropertyBase(*aProperty, att, val, aFirst, aAny, aAll, &outValue);
|
2001-04-07 04:45:26 +04:00
|
|
|
}
|
|
|
|
|
2016-07-09 05:42:33 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
HTMLEditor::RemoveAllInlineProperties()
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2017-08-14 08:56:39 +03:00
|
|
|
AutoPlaceholderBatch batchIt(this);
|
2016-06-23 12:15:42 +03:00
|
|
|
AutoRules beginRulesSniffing(this, EditAction::resetTextProperties,
|
|
|
|
nsIEditor::eNext);
|
2003-06-03 02:21:06 +04:00
|
|
|
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = RemoveInlinePropertyImpl(nullptr, nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2017-08-25 09:59:17 +03:00
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2016-07-09 05:42:33 +03:00
|
|
|
NS_IMETHODIMP
|
2017-08-25 08:40:45 +03:00
|
|
|
HTMLEditor::RemoveInlineProperty(const nsAString& aProperty,
|
|
|
|
const nsAString& aAttribute)
|
|
|
|
{
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> property = NS_Atomize(aProperty);
|
2017-09-07 05:20:51 +03:00
|
|
|
return RemoveInlineProperty(property, aAttribute);
|
2017-08-25 08:40:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
HTMLEditor::RemoveInlineProperty(nsAtom* aProperty,
|
2016-07-09 05:42:33 +03:00
|
|
|
const nsAString& aAttribute)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2001-04-07 04:45:26 +04:00
|
|
|
return RemoveInlinePropertyImpl(aProperty, &aAttribute);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
HTMLEditor::RemoveInlinePropertyImpl(nsAtom* aProperty,
|
2016-07-09 05:42:33 +03:00
|
|
|
const nsAString* aAttribute)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2012-05-05 13:00:06 +04:00
|
|
|
MOZ_ASSERT_IF(aProperty, aAttribute);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(mRules, NS_ERROR_NOT_INITIALIZED);
|
2017-08-08 05:25:36 +03:00
|
|
|
CommitComposition();
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Selection> selection = GetSelection();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-05-18 12:29:39 +04:00
|
|
|
if (selection->Collapsed()) {
|
2015-04-19 15:28:50 +03:00
|
|
|
// Manipulating text attributes on a collapsed selection only sets state
|
|
|
|
// for the next text insertion
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// For links, aProperty uses "href", use "a" instead
|
2014-08-29 15:43:24 +04:00
|
|
|
if (aProperty == nsGkAtoms::href || aProperty == nsGkAtoms::name) {
|
|
|
|
aProperty = nsGkAtoms::a;
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-05-05 13:00:06 +04:00
|
|
|
if (aProperty) {
|
|
|
|
mTypeInState->ClearProp(aProperty, *aAttribute);
|
|
|
|
} else {
|
|
|
|
mTypeInState->ClearAllProps();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-05-05 13:00:06 +04:00
|
|
|
|
2017-08-14 08:56:39 +03:00
|
|
|
AutoPlaceholderBatch batchIt(this);
|
2016-06-23 12:15:42 +03:00
|
|
|
AutoRules beginRulesSniffing(this, EditAction::removeTextProperty,
|
|
|
|
nsIEditor::eNext);
|
2016-07-07 07:27:31 +03:00
|
|
|
AutoSelectionRestorer selectionRestorer(selection, this);
|
2017-07-31 18:20:08 +03:00
|
|
|
AutoTransactionsConserveSelection dontChangeMySelection(this);
|
2015-04-19 15:28:50 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cancel, handled;
|
2016-06-24 15:12:16 +03:00
|
|
|
TextRulesInfo ruleInfo(EditAction::removeTextProperty);
|
2013-03-23 07:47:57 +04:00
|
|
|
// Protect the edit rules object from dying
|
2016-07-18 19:36:19 +03:00
|
|
|
nsCOMPtr<nsIEditRules> rules(mRules);
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = rules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-04-19 15:28:50 +03:00
|
|
|
if (!cancel && !handled) {
|
|
|
|
// Loop through the ranges in the selection
|
2016-12-15 09:01:53 +03:00
|
|
|
// Since ranges might be modified by SplitStyleAboveRange, we need hold
|
|
|
|
// current ranges
|
2017-06-23 10:46:54 +03:00
|
|
|
AutoRangeArray arrayOfRanges(selection);
|
|
|
|
for (auto& range : arrayOfRanges.mRanges) {
|
2014-08-29 15:43:24 +04:00
|
|
|
if (aProperty == nsGkAtoms::name) {
|
2015-04-19 15:28:50 +03:00
|
|
|
// Promote range if it starts or end in a named anchor and we want to
|
|
|
|
// remove named anchors
|
2017-06-23 10:46:54 +03:00
|
|
|
rv = PromoteRangeIfStartsOrEndsInNamedAnchor(*range);
|
2016-10-19 12:09:33 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-04-19 15:28:50 +03:00
|
|
|
} else {
|
|
|
|
// Adjust range to include any ancestors whose children are entirely
|
|
|
|
// selected
|
2017-06-23 10:46:54 +03:00
|
|
|
rv = PromoteInlineRange(*range);
|
2016-10-19 12:09:33 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2002-04-11 17:33:30 +04:00
|
|
|
}
|
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
// Remove this style from ancestors of our range endpoints, splitting
|
|
|
|
// them as appropriate
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = SplitStyleAboveRange(range, aProperty, aAttribute);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-06-12 18:04:47 +04:00
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
// Check for easy case: both range endpoints in same text node
|
2017-07-11 14:53:04 +03:00
|
|
|
nsCOMPtr<nsINode> startNode = range->GetStartContainer();
|
2017-07-11 15:11:37 +03:00
|
|
|
nsCOMPtr<nsINode> endNode = range->GetEndContainer();
|
2015-04-19 15:28:50 +03:00
|
|
|
if (startNode && startNode == endNode && startNode->GetAsText()) {
|
2015-04-19 15:28:50 +03:00
|
|
|
// We're done with this range!
|
|
|
|
if (IsCSSEnabled() &&
|
2016-07-07 12:35:13 +03:00
|
|
|
mCSSEditUtils->IsCSSEditableProperty(startNode, aProperty,
|
2015-04-19 15:28:50 +03:00
|
|
|
aAttribute)) {
|
|
|
|
// The HTML style defined by aProperty/aAttribute has a CSS
|
|
|
|
// equivalence in this implementation for startNode
|
2016-07-07 12:35:13 +03:00
|
|
|
if (mCSSEditUtils->IsCSSEquivalentToHTMLInlineStyleSet(startNode,
|
2015-04-19 15:28:50 +03:00
|
|
|
aProperty, aAttribute, EmptyString(),
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::eComputed)) {
|
2015-04-19 15:28:50 +03:00
|
|
|
// startNode's computed style indicates the CSS equivalence to the
|
|
|
|
// HTML style to remove is applied; but we found no element in the
|
|
|
|
// ancestors of startNode carrying specified styles; assume it
|
|
|
|
// comes from a rule and try to insert a span "inverting" the style
|
2016-07-07 12:35:13 +03:00
|
|
|
if (mCSSEditUtils->IsCSSInvertible(*aProperty, aAttribute)) {
|
2015-04-19 15:28:50 +03:00
|
|
|
NS_NAMED_LITERAL_STRING(value, "-moz-editor-invert-value");
|
|
|
|
SetInlinePropertyOnTextNode(*startNode->GetAsText(),
|
|
|
|
range->StartOffset(),
|
|
|
|
range->EndOffset(), *aProperty,
|
|
|
|
aAttribute, value);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-19 15:28:50 +03:00
|
|
|
} else {
|
|
|
|
// Not the easy case. Range not contained in single text node.
|
|
|
|
nsCOMPtr<nsIContentIterator> iter = NS_NewContentSubtreeIterator();
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2016-05-01 16:11:52 +03:00
|
|
|
nsTArray<OwningNonNull<nsIContent>> arrayOfNodes;
|
2015-04-19 15:28:50 +03:00
|
|
|
|
|
|
|
// Iterate range and build up array
|
|
|
|
for (iter->Init(range); !iter->IsDone(); iter->Next()) {
|
|
|
|
nsCOMPtr<nsINode> node = iter->GetCurrentNode();
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_FAILURE);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2016-05-01 16:11:52 +03:00
|
|
|
if (IsEditable(node) && node->IsContent()) {
|
|
|
|
arrayOfNodes.AppendElement(*node->AsContent());
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
2015-04-19 15:28:50 +03:00
|
|
|
|
|
|
|
// Loop through the list, remove the property on each node
|
|
|
|
for (auto& node : arrayOfNodes) {
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = RemoveStyleInside(node, aProperty, aAttribute);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-04-19 15:28:50 +03:00
|
|
|
if (IsCSSEnabled() &&
|
2016-07-07 12:35:13 +03:00
|
|
|
mCSSEditUtils->IsCSSEditableProperty(node, aProperty,
|
2015-04-19 15:28:50 +03:00
|
|
|
aAttribute) &&
|
2016-07-07 12:35:13 +03:00
|
|
|
mCSSEditUtils->IsCSSEquivalentToHTMLInlineStyleSet(node,
|
2015-04-19 15:28:50 +03:00
|
|
|
aProperty, aAttribute, EmptyString(),
|
2016-07-07 12:35:13 +03:00
|
|
|
CSSEditUtils::eComputed) &&
|
2015-04-19 15:28:50 +03:00
|
|
|
// startNode's computed style indicates the CSS equivalence to
|
|
|
|
// the HTML style to remove is applied; but we found no element
|
|
|
|
// in the ancestors of startNode carrying specified styles;
|
|
|
|
// assume it comes from a rule and let's try to insert a span
|
|
|
|
// "inverting" the style
|
2016-07-07 12:35:13 +03:00
|
|
|
mCSSEditUtils->IsCSSInvertible(*aProperty, aAttribute)) {
|
2015-04-19 15:28:50 +03:00
|
|
|
NS_NAMED_LITERAL_STRING(value, "-moz-editor-invert-value");
|
2016-05-01 16:11:52 +03:00
|
|
|
SetInlinePropertyOnNode(node, *aProperty, aAttribute, value);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-19 15:28:50 +03:00
|
|
|
if (!cancel) {
|
|
|
|
// Post-process
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = rules->DidDoAction(selection, &ruleInfo, rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2015-04-19 15:28:50 +03:00
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2016-07-09 05:42:33 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
HTMLEditor::IncreaseFontSize()
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2015-04-24 14:27:36 +03:00
|
|
|
return RelativeFontChange(FontSize::incr);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2016-07-09 05:42:33 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
HTMLEditor::DecreaseFontSize()
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2015-04-24 14:27:36 +03:00
|
|
|
return RelativeFontChange(FontSize::decr);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::RelativeFontChange(FontSize aDir)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2017-08-08 05:25:36 +03:00
|
|
|
CommitComposition();
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2015-04-24 14:27:36 +03:00
|
|
|
// Get the selection
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Selection> selection = GetSelection();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE);
|
2015-04-24 14:27:36 +03:00
|
|
|
// If selection is collapsed, set typing state
|
2012-05-18 12:29:39 +04:00
|
|
|
if (selection->Collapsed()) {
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom& atom = aDir == FontSize::incr ? *nsGkAtoms::big :
|
2015-04-24 14:27:36 +03:00
|
|
|
*nsGkAtoms::small;
|
2002-01-09 16:51:37 +03:00
|
|
|
|
|
|
|
// Let's see in what kind of element the selection is
|
2015-04-24 14:27:36 +03:00
|
|
|
NS_ENSURE_TRUE(selection->RangeCount() &&
|
2017-07-11 14:53:04 +03:00
|
|
|
selection->GetRangeAt(0)->GetStartContainer(), NS_OK);
|
2015-04-24 14:27:36 +03:00
|
|
|
OwningNonNull<nsINode> selectedNode =
|
2017-07-11 14:53:04 +03:00
|
|
|
*selection->GetRangeAt(0)->GetStartContainer();
|
2015-04-24 14:27:36 +03:00
|
|
|
if (IsTextNode(selectedNode)) {
|
|
|
|
NS_ENSURE_TRUE(selectedNode->GetParentNode(), NS_OK);
|
|
|
|
selectedNode = *selectedNode->GetParentNode();
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2015-04-24 14:27:36 +03:00
|
|
|
if (!CanContainTag(selectedNode, atom)) {
|
2012-05-01 10:34:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-01-09 16:51:37 +03:00
|
|
|
|
2015-04-24 14:27:36 +03:00
|
|
|
// Manipulating text attributes on a collapsed selection only sets state
|
|
|
|
// for the next text insertion
|
|
|
|
mTypeInState->SetProp(&atom, EmptyString(), EmptyString());
|
2012-05-05 13:00:06 +04:00
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2015-04-24 14:27:36 +03:00
|
|
|
|
|
|
|
// Wrap with txn batching, rules sniffing, and selection preservation code
|
2017-08-14 08:56:39 +03:00
|
|
|
AutoPlaceholderBatch batchIt(this);
|
2016-06-23 12:15:42 +03:00
|
|
|
AutoRules beginRulesSniffing(this, EditAction::setTextProperty,
|
|
|
|
nsIEditor::eNext);
|
2016-07-07 07:27:31 +03:00
|
|
|
AutoSelectionRestorer selectionRestorer(selection, this);
|
2017-07-31 18:20:08 +03:00
|
|
|
AutoTransactionsConserveSelection dontChangeMySelection(this);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2015-04-24 14:27:36 +03:00
|
|
|
// Loop through the ranges in the selection
|
2017-06-23 10:46:54 +03:00
|
|
|
AutoRangeArray arrayOfRanges(selection);
|
|
|
|
for (auto& range : arrayOfRanges.mRanges) {
|
2015-04-24 14:27:36 +03:00
|
|
|
// Adjust range to include any ancestors with entirely selected children
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = PromoteInlineRange(*range);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-04-24 14:27:36 +03:00
|
|
|
|
|
|
|
// Check for easy case: both range endpoints in same text node
|
2017-07-11 14:53:04 +03:00
|
|
|
nsCOMPtr<nsINode> startNode = range->GetStartContainer();
|
2017-07-11 15:11:37 +03:00
|
|
|
nsCOMPtr<nsINode> endNode = range->GetEndContainer();
|
2015-04-24 14:27:36 +03:00
|
|
|
if (startNode == endNode && IsTextNode(startNode)) {
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = RelativeFontChangeOnTextNode(aDir, *startNode->GetAsText(),
|
|
|
|
range->StartOffset(),
|
|
|
|
range->EndOffset());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-04-24 14:27:36 +03:00
|
|
|
} else {
|
|
|
|
// Not the easy case. Range not contained in single text node. There
|
|
|
|
// are up to three phases here. There are all the nodes reported by the
|
|
|
|
// subtree iterator to be processed. And there are potentially a
|
|
|
|
// starting textnode and an ending textnode which are only partially
|
|
|
|
// contained by the range.
|
|
|
|
|
|
|
|
// Let's handle the nodes reported by the iterator. These nodes are
|
|
|
|
// entirely contained in the selection range. We build up a list of them
|
|
|
|
// (since doing operations on the document during iteration would perturb
|
|
|
|
// the iterator).
|
|
|
|
|
|
|
|
OwningNonNull<nsIContentIterator> iter = NS_NewContentSubtreeIterator();
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2015-04-24 14:27:36 +03:00
|
|
|
// Iterate range and build up array
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = iter->Init(range);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2015-04-24 14:27:36 +03:00
|
|
|
nsTArray<OwningNonNull<nsIContent>> arrayOfNodes;
|
|
|
|
for (; !iter->IsDone(); iter->Next()) {
|
2012-06-10 16:39:21 +04:00
|
|
|
NS_ENSURE_TRUE(iter->GetCurrentNode()->IsContent(), NS_ERROR_FAILURE);
|
2015-04-24 14:27:36 +03:00
|
|
|
OwningNonNull<nsIContent> node = *iter->GetCurrentNode()->AsContent();
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-05-18 12:29:39 +04:00
|
|
|
if (IsEditable(node)) {
|
2015-04-24 14:27:36 +03:00
|
|
|
arrayOfNodes.AppendElement(node);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
2015-04-24 14:27:36 +03:00
|
|
|
|
|
|
|
// Now that we have the list, do the font size change on each node
|
|
|
|
for (auto& node : arrayOfNodes) {
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = RelativeFontChangeOnNode(aDir == FontSize::incr ? +1 : -1, node);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
2015-04-24 14:27:36 +03:00
|
|
|
// Now check the start and end parents of the range to see if they need
|
|
|
|
// to be separately handled (they do if they are text nodes, due to how
|
|
|
|
// the subtree iterator works - it will not have reported them).
|
|
|
|
if (IsTextNode(startNode) && IsEditable(startNode)) {
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = RelativeFontChangeOnTextNode(aDir, *startNode->GetAsText(),
|
|
|
|
range->StartOffset(),
|
|
|
|
startNode->Length());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2015-04-24 14:27:36 +03:00
|
|
|
if (IsTextNode(endNode) && IsEditable(endNode)) {
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = RelativeFontChangeOnTextNode(aDir, *endNode->GetAsText(), 0,
|
|
|
|
range->EndOffset());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-24 14:27:36 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::RelativeFontChangeOnTextNode(FontSize aDir,
|
|
|
|
Text& aTextNode,
|
|
|
|
int32_t aStartOffset,
|
|
|
|
int32_t aEndOffset)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2016-05-01 16:12:27 +03:00
|
|
|
// Don't need to do anything if no characters actually selected
|
|
|
|
if (aStartOffset == aEndOffset) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2016-05-01 16:12:27 +03:00
|
|
|
if (!aTextNode.GetParentNode() ||
|
|
|
|
!CanContainTag(*aTextNode.GetParentNode(), *nsGkAtoms::big)) {
|
2012-05-01 10:34:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-01-09 16:51:37 +03:00
|
|
|
|
2016-05-01 16:12:27 +03:00
|
|
|
OwningNonNull<nsIContent> node = aTextNode;
|
2001-10-22 10:22:52 +04:00
|
|
|
|
2016-05-01 16:12:27 +03:00
|
|
|
// Do we need to split the text node?
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// -1 is a magic value meaning to the end of node
|
2016-05-01 16:12:27 +03:00
|
|
|
if (aEndOffset == -1) {
|
|
|
|
aEndOffset = aTextNode.Length();
|
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2016-05-01 16:12:27 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
if ((uint32_t)aEndOffset != aTextNode.Length()) {
|
|
|
|
// We need to split off back of text node
|
|
|
|
node = SplitNode(node, aEndOffset, rv);
|
|
|
|
NS_ENSURE_TRUE(!rv.Failed(), rv.StealNSResult());
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2016-05-01 16:12:27 +03:00
|
|
|
if (aStartOffset) {
|
|
|
|
// We need to split off front of text node
|
|
|
|
SplitNode(node, aStartOffset, rv);
|
|
|
|
NS_ENSURE_TRUE(!rv.Failed(), rv.StealNSResult());
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2001-12-07 18:28:47 +03:00
|
|
|
|
2016-05-01 16:12:27 +03:00
|
|
|
// Look for siblings that are correct type of node
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* nodeType = aDir == FontSize::incr ? nsGkAtoms::big
|
2016-05-01 16:12:27 +03:00
|
|
|
: nsGkAtoms::small;
|
2014-08-20 16:25:16 +04:00
|
|
|
nsCOMPtr<nsIContent> sibling = GetPriorHTMLSibling(node);
|
2015-03-03 14:08:59 +03:00
|
|
|
if (sibling && sibling->IsHTMLElement(nodeType)) {
|
2016-05-01 16:12:27 +03:00
|
|
|
// Previous sib is already right kind of inline node; slide this over
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = MoveNode(node, sibling, -1);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2016-05-01 16:12:27 +03:00
|
|
|
return NS_OK;
|
2001-10-22 10:22:52 +04:00
|
|
|
}
|
2014-08-20 16:25:16 +04:00
|
|
|
sibling = GetNextHTMLSibling(node);
|
2015-03-03 14:08:59 +03:00
|
|
|
if (sibling && sibling->IsHTMLElement(nodeType)) {
|
2016-05-01 16:12:27 +03:00
|
|
|
// Following sib is already right kind of inline node; slide this over
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = MoveNode(node, sibling, 0);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2016-05-01 16:12:27 +03:00
|
|
|
return NS_OK;
|
2001-10-22 10:22:52 +04:00
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2016-05-01 16:12:27 +03:00
|
|
|
// Else reparent the node inside font node with appropriate relative size
|
2014-08-20 16:25:16 +04:00
|
|
|
nsCOMPtr<Element> newElement = InsertContainerAbove(node, nodeType);
|
2014-08-20 16:25:16 +04:00
|
|
|
NS_ENSURE_STATE(newElement);
|
|
|
|
|
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2012-05-18 12:29:39 +04:00
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::RelativeFontChangeHelper(int32_t aSizeChange,
|
|
|
|
nsINode* aNode)
|
2001-10-22 10:22:52 +04:00
|
|
|
{
|
2012-05-18 12:29:39 +04:00
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
|
2001-10-22 10:22:52 +04:00
|
|
|
/* This routine looks for all the font nodes in the tree rooted by aNode,
|
|
|
|
including aNode itself, looking for font nodes that have the size attr
|
|
|
|
set. Any such nodes need to have big or small put inside them, since
|
|
|
|
they override any big/small that are above them.
|
|
|
|
*/
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2001-10-22 10:22:52 +04:00
|
|
|
// Can only change font size by + or - 1
|
2012-05-18 12:29:39 +04:00
|
|
|
if (aSizeChange != 1 && aSizeChange != -1) {
|
2001-10-22 10:22:52 +04:00
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
2012-05-18 12:29:39 +04:00
|
|
|
}
|
2001-10-22 10:22:52 +04:00
|
|
|
|
2012-05-18 12:29:39 +04:00
|
|
|
// If this is a font node with size, put big/small inside it.
|
2015-03-03 14:08:59 +03:00
|
|
|
if (aNode->IsHTMLElement(nsGkAtoms::font) &&
|
2012-05-18 12:29:39 +04:00
|
|
|
aNode->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::size)) {
|
|
|
|
// Cycle through children and adjust relative font size.
|
2017-08-04 20:12:46 +03:00
|
|
|
AutoTArray<nsCOMPtr<nsIContent>, 10> childList;
|
|
|
|
for (nsIContent* child = aNode->GetFirstChild();
|
|
|
|
child; child = child->GetNextSibling()) {
|
|
|
|
childList.AppendElement(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& child: childList) {
|
|
|
|
nsresult rv = RelativeFontChangeOnNode(aSizeChange, child);
|
2012-05-18 12:29:39 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-10-22 10:22:52 +04:00
|
|
|
}
|
2013-01-01 01:49:49 +04:00
|
|
|
|
|
|
|
// RelativeFontChangeOnNode already calls us recursively,
|
|
|
|
// so we don't need to check our children again.
|
|
|
|
return NS_OK;
|
2001-10-22 10:22:52 +04:00
|
|
|
}
|
|
|
|
|
2013-01-01 01:49:49 +04:00
|
|
|
// Otherwise cycle through the children.
|
2017-08-04 20:12:46 +03:00
|
|
|
AutoTArray<nsCOMPtr<nsIContent>, 10> childList;
|
|
|
|
for (nsIContent* child = aNode->GetFirstChild();
|
|
|
|
child; child = child->GetNextSibling()) {
|
|
|
|
childList.AppendElement(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& child: childList) {
|
|
|
|
nsresult rv = RelativeFontChangeHelper(aSizeChange, child);
|
2012-05-18 12:29:39 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-10-22 10:22:52 +04:00
|
|
|
}
|
2002-01-09 16:51:37 +03:00
|
|
|
|
2012-05-18 12:29:39 +04:00
|
|
|
return NS_OK;
|
2001-10-22 10:22:52 +04:00
|
|
|
}
|
|
|
|
|
2012-05-18 12:29:39 +04:00
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::RelativeFontChangeOnNode(int32_t aSizeChange,
|
|
|
|
nsIContent* aNode)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2012-05-18 12:29:39 +04:00
|
|
|
MOZ_ASSERT(aNode);
|
2001-01-28 23:13:07 +03:00
|
|
|
// Can only change font size by + or - 1
|
2012-05-18 12:29:39 +04:00
|
|
|
if (aSizeChange != 1 && aSizeChange != -1) {
|
2001-01-28 23:13:07 +03:00
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
2012-05-18 12:29:39 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* atom;
|
2012-05-01 10:34:52 +04:00
|
|
|
if (aSizeChange == 1) {
|
|
|
|
atom = nsGkAtoms::big;
|
|
|
|
} else {
|
|
|
|
atom = nsGkAtoms::small;
|
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2012-05-18 12:29:39 +04:00
|
|
|
// Is it the opposite of what we want?
|
2015-03-03 14:08:59 +03:00
|
|
|
if ((aSizeChange == 1 && aNode->IsHTMLElement(nsGkAtoms::small)) ||
|
|
|
|
(aSizeChange == -1 && aNode->IsHTMLElement(nsGkAtoms::big))) {
|
2001-10-22 10:22:52 +04:00
|
|
|
// first populate any nested font tags that have the size attr set
|
2012-05-18 12:29:39 +04:00
|
|
|
nsresult rv = RelativeFontChangeHelper(aSizeChange, aNode);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 23:13:07 +03:00
|
|
|
// in that case, just remove this node and pull up the children
|
2012-05-18 12:29:39 +04:00
|
|
|
return RemoveContainer(aNode);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-05-18 12:29:39 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// can it be put inside a "big" or "small"?
|
2014-11-02 15:04:13 +03:00
|
|
|
if (TagCanContain(*atom, *aNode)) {
|
2001-10-22 10:22:52 +04:00
|
|
|
// first populate any nested font tags that have the size attr set
|
2012-05-18 12:29:39 +04:00
|
|
|
nsresult rv = RelativeFontChangeHelper(aSizeChange, aNode);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// ok, chuck it in.
|
2001-10-22 10:22:52 +04:00
|
|
|
// first look at siblings of aNode for matching bigs or smalls.
|
|
|
|
// if we find one, move aNode into it.
|
2012-05-18 12:29:39 +04:00
|
|
|
nsIContent* sibling = GetPriorHTMLSibling(aNode);
|
2015-03-03 14:08:59 +03:00
|
|
|
if (sibling && sibling->IsHTMLElement(atom)) {
|
2001-10-22 10:22:52 +04:00
|
|
|
// previous sib is already right kind of inline node; slide this over into it
|
2014-08-20 16:25:16 +04:00
|
|
|
return MoveNode(aNode, sibling, -1);
|
2001-10-22 10:22:52 +04:00
|
|
|
}
|
2012-05-18 12:29:39 +04:00
|
|
|
|
|
|
|
sibling = GetNextHTMLSibling(aNode);
|
2015-03-03 14:08:59 +03:00
|
|
|
if (sibling && sibling->IsHTMLElement(atom)) {
|
2001-10-22 10:22:52 +04:00
|
|
|
// following sib is already right kind of inline node; slide this over into it
|
2014-08-20 16:25:16 +04:00
|
|
|
return MoveNode(aNode, sibling, 0);
|
2001-10-22 10:22:52 +04:00
|
|
|
}
|
2012-05-18 12:29:39 +04:00
|
|
|
|
2001-10-22 10:22:52 +04:00
|
|
|
// else insert it above aNode
|
2014-08-20 16:25:16 +04:00
|
|
|
nsCOMPtr<Element> newElement = InsertContainerAbove(aNode, atom);
|
|
|
|
NS_ENSURE_STATE(newElement);
|
|
|
|
|
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-05-18 12:29:39 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// none of the above? then cycle through the children.
|
|
|
|
// MOOSE: we should group the children together if possible
|
|
|
|
// into a single "big" or "small". For the moment they are
|
2015-05-28 18:58:42 +03:00
|
|
|
// each getting their own.
|
2017-08-04 20:12:46 +03:00
|
|
|
AutoTArray<nsCOMPtr<nsIContent>, 10> childList;
|
|
|
|
for (nsIContent* child = aNode->GetFirstChild();
|
|
|
|
child; child = child->GetNextSibling()) {
|
|
|
|
childList.AppendElement(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& child: childList) {
|
|
|
|
nsresult rv = RelativeFontChangeOnNode(aSizeChange, child);
|
2012-05-18 12:29:39 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-05-18 12:29:39 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::GetFontFaceState(bool* aMixed,
|
|
|
|
nsAString& outFace)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(aMixed, NS_ERROR_FAILURE);
|
2011-10-17 18:59:28 +04:00
|
|
|
*aMixed = true;
|
2003-07-18 06:27:19 +04:00
|
|
|
outFace.Truncate();
|
2001-04-07 04:45:26 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool first, any, all;
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2002-01-14 23:30:11 +03:00
|
|
|
NS_NAMED_LITERAL_STRING(attr, "face");
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv =
|
2017-03-14 05:26:36 +03:00
|
|
|
GetInlinePropertyBase(*nsGkAtoms::font, &attr.AsString(), nullptr, &first,
|
|
|
|
&any, &all, &outFace);
|
2016-10-19 12:09:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (any && !all) {
|
|
|
|
return NS_OK; // mixed
|
|
|
|
}
|
|
|
|
if (all) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aMixed = false;
|
2016-10-19 12:09:33 +03:00
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2003-06-12 18:51:37 +04:00
|
|
|
// if there is no font face, check for tt
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = GetInlinePropertyBase(*nsGkAtoms::tt, nullptr, nullptr, &first, &any,
|
|
|
|
&all,nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (any && !all) {
|
|
|
|
return rv; // mixed
|
|
|
|
}
|
|
|
|
if (all) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aMixed = false;
|
2014-08-29 15:43:24 +04:00
|
|
|
outFace.AssignLiteral("tt");
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2016-10-19 12:09:33 +03:00
|
|
|
if (!any) {
|
2001-01-28 23:13:07 +03:00
|
|
|
// there was no font face attrs of any kind. We are in normal font.
|
2003-07-18 06:27:19 +04:00
|
|
|
outFace.Truncate();
|
2011-10-17 18:59:28 +04:00
|
|
|
*aMixed = false;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2016-10-19 12:09:33 +03:00
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
NS_IMETHODIMP
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::GetFontColorState(bool* aMixed,
|
|
|
|
nsAString& aOutColor)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(aMixed, NS_ERROR_NULL_POINTER);
|
2011-10-17 18:59:28 +04:00
|
|
|
*aMixed = true;
|
2003-07-18 06:27:19 +04:00
|
|
|
aOutColor.Truncate();
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2002-01-14 23:30:11 +03:00
|
|
|
NS_NAMED_LITERAL_STRING(colorStr, "color");
|
2011-09-29 10:19:26 +04:00
|
|
|
bool first, any, all;
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv =
|
2017-03-14 05:26:36 +03:00
|
|
|
GetInlinePropertyBase(*nsGkAtoms::font, &colorStr.AsString(), nullptr,
|
|
|
|
&first, &any, &all, &aOutColor);
|
2016-10-19 12:09:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (any && !all) {
|
|
|
|
return NS_OK; // mixed
|
|
|
|
}
|
|
|
|
if (all) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aMixed = false;
|
2016-10-19 12:09:33 +03:00
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2016-10-19 12:09:33 +03:00
|
|
|
if (!any) {
|
2001-01-28 23:13:07 +03:00
|
|
|
// there was no font color attrs of any kind..
|
2003-07-18 06:27:19 +04:00
|
|
|
aOutColor.Truncate();
|
2011-10-17 18:59:28 +04:00
|
|
|
*aMixed = false;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2016-10-19 12:09:33 +03:00
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2002-01-09 16:51:37 +03:00
|
|
|
// the return value is true only if the instance of the HTML editor we created
|
|
|
|
// can handle CSS styles (for instance, Composer can, Messenger can't) and if
|
|
|
|
// the CSS preference is checked
|
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::GetIsCSSEnabled(bool* aIsCSSEnabled)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2012-02-01 14:54:22 +04:00
|
|
|
*aIsCSSEnabled = IsCSSEnabled();
|
2002-01-09 16:51:37 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-01 14:54:22 +04:00
|
|
|
static bool
|
2016-07-09 05:42:33 +03:00
|
|
|
HasNonEmptyAttribute(Element* aElement,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* aName)
|
2002-02-18 13:49:15 +03:00
|
|
|
{
|
2012-02-01 14:54:22 +04:00
|
|
|
MOZ_ASSERT(aElement);
|
|
|
|
|
|
|
|
nsAutoString value;
|
|
|
|
return aElement->GetAttr(kNameSpaceID_None, aName, value) && !value.IsEmpty();
|
|
|
|
}
|
2002-02-18 13:49:15 +03:00
|
|
|
|
2012-02-01 14:54:22 +04:00
|
|
|
bool
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::HasStyleOrIdOrClass(Element* aElement)
|
2012-02-01 14:54:22 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aElement);
|
2002-02-18 13:49:15 +03:00
|
|
|
|
|
|
|
// remove the node if its style attribute is empty or absent,
|
|
|
|
// and if it does not have a class nor an id
|
2012-02-01 14:54:22 +04:00
|
|
|
return HasNonEmptyAttribute(aElement, nsGkAtoms::style) ||
|
|
|
|
HasNonEmptyAttribute(aElement, nsGkAtoms::_class) ||
|
|
|
|
HasNonEmptyAttribute(aElement, nsGkAtoms::id);
|
2002-02-18 13:49:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-07-09 05:42:33 +03:00
|
|
|
HTMLEditor::RemoveElementIfNoStyleOrIdOrClass(Element& aElement)
|
2002-02-18 13:49:15 +03:00
|
|
|
{
|
|
|
|
// early way out if node is not the right kind of element
|
2015-05-13 18:58:25 +03:00
|
|
|
if ((!aElement.IsHTMLElement(nsGkAtoms::span) &&
|
|
|
|
!aElement.IsHTMLElement(nsGkAtoms::font)) ||
|
|
|
|
HasStyleOrIdOrClass(&aElement)) {
|
2002-02-18 13:49:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-02-01 14:54:22 +04:00
|
|
|
|
2015-05-13 18:58:25 +03:00
|
|
|
return RemoveContainer(&aElement);
|
2002-02-18 13:49:15 +03:00
|
|
|
}
|
2016-07-09 05:42:33 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|