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/. */
|
2001-04-06 03:48:01 +04:00
|
|
|
#include "TypeInState.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/Assertions.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 "nsAutoPtr.h"
|
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCaseTreatment.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsEditRules.h"
|
|
|
|
#include "nsEditor.h"
|
|
|
|
#include "nsEditorUtils.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsHTMLCSSUtils.h"
|
|
|
|
#include "nsHTMLEditUtils.h"
|
|
|
|
#include "nsHTMLEditor.h"
|
|
|
|
#include "nsIAtom.h"
|
2001-01-28 23:13:07 +03:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIContentIterator.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsIDOMCharacterData.h"
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIEditor.h"
|
|
|
|
#include "nsIEditorIMESupport.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 "nsSelectionState.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsStringFwd.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsTextEditRules.h"
|
|
|
|
#include "nsTextEditUtils.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
|
|
|
|
|
|
|
using namespace mozilla;
|
2014-04-10 20:09:40 +04:00
|
|
|
using namespace mozilla::dom;
|
2003-06-03 02:21:06 +04:00
|
|
|
|
2012-10-05 04:37:45 +04:00
|
|
|
static bool
|
|
|
|
IsEmptyTextNode(nsHTMLEditor* aThis, nsINode* aNode)
|
|
|
|
{
|
|
|
|
bool isEmptyTextNode = false;
|
|
|
|
return nsEditor::IsTextNode(aNode) &&
|
|
|
|
NS_SUCCEEDED(aThis->IsEmptyNode(aNode, &isEmptyTextNode)) &&
|
|
|
|
isEmptyTextNode;
|
|
|
|
}
|
|
|
|
|
2003-06-03 02:21:06 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::AddDefaultProperty(nsIAtom *aProperty,
|
|
|
|
const nsAString & aAttribute,
|
|
|
|
const nsAString & aValue)
|
|
|
|
{
|
|
|
|
nsString outValue;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t index;
|
2003-06-03 02:21:06 +04:00
|
|
|
nsString attr(aAttribute);
|
|
|
|
if (TypeInState::FindPropInList(aProperty, attr, &outValue, mDefaultStyles, index))
|
|
|
|
{
|
2009-04-22 12:43:15 +04:00
|
|
|
PropItem *item = mDefaultStyles[index];
|
2003-06-03 02:21:06 +04:00
|
|
|
item->value = aValue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsString value(aValue);
|
|
|
|
PropItem *propItem = new PropItem(aProperty, attr, value);
|
2009-04-22 12:43:15 +04:00
|
|
|
mDefaultStyles.AppendElement(propItem);
|
2003-06-03 02:21:06 +04:00
|
|
|
}
|
2003-09-03 17:58:00 +04:00
|
|
|
return NS_OK;
|
2003-06-03 02:21:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::RemoveDefaultProperty(nsIAtom *aProperty,
|
|
|
|
const nsAString & aAttribute,
|
|
|
|
const nsAString & aValue)
|
|
|
|
{
|
|
|
|
nsString outValue;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t index;
|
2003-06-03 02:21:06 +04:00
|
|
|
nsString attr(aAttribute);
|
|
|
|
if (TypeInState::FindPropInList(aProperty, attr, &outValue, mDefaultStyles, index))
|
|
|
|
{
|
2009-04-22 12:43:15 +04:00
|
|
|
delete mDefaultStyles[index];
|
2003-06-03 02:21:06 +04:00
|
|
|
mDefaultStyles.RemoveElementAt(index);
|
|
|
|
}
|
2003-09-03 17:58:00 +04:00
|
|
|
return NS_OK;
|
2003-06-03 02:21:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::RemoveAllDefaultProperties()
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t j, defcon = mDefaultStyles.Length();
|
2003-06-03 02:21:06 +04:00
|
|
|
for (j=0; j<defcon; j++)
|
|
|
|
{
|
2009-04-22 12:43:15 +04:00
|
|
|
delete mDefaultStyles[j];
|
2003-06-03 02:21:06 +04:00
|
|
|
}
|
|
|
|
mDefaultStyles.Clear();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-20 10:33:21 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::SetInlineProperty(nsIAtom *aProperty,
|
|
|
|
const nsAString& aAttribute,
|
|
|
|
const nsAString& aValue)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2012-04-20 10:33:21 +04:00
|
|
|
if (!aProperty) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
if (!mRules) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
ForceCompositionEnd();
|
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
nsRefPtr<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()) {
|
2012-04-20 10:33:21 +04:00
|
|
|
// manipulating text attributes on a collapsed selection only sets state
|
|
|
|
// 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
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsAutoEditBatch batchIt(this);
|
2012-08-12 22:28:26 +04:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::insertElement, nsIEditor::eNext);
|
2001-01-28 23:13:07 +03:00
|
|
|
nsAutoSelectionReset selectionResetter(selection, this);
|
|
|
|
nsAutoTxnsConserveSelection dontSpazMySelection(this);
|
2012-04-20 10:33:21 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cancel, handled;
|
2012-08-12 22:28:26 +04:00
|
|
|
nsTextRulesInfo ruleInfo(EditAction::setTextProperty);
|
2013-03-23 07:47:57 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
2012-05-22 13:37:17 +04:00
|
|
|
nsresult res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2012-04-20 10:33:21 +04:00
|
|
|
if (!cancel && !handled) {
|
2001-01-28 23:13:07 +03:00
|
|
|
// loop thru the ranges in the selection
|
2015-02-25 19:08:00 +03:00
|
|
|
uint32_t rangeCount = selection->RangeCount();
|
2013-02-07 18:17:47 +04:00
|
|
|
for (uint32_t rangeIdx = 0; rangeIdx < rangeCount; ++rangeIdx) {
|
|
|
|
nsRefPtr<nsRange> range = selection->GetRangeAt(rangeIdx);
|
2012-04-20 10:33:21 +04:00
|
|
|
|
|
|
|
// adjust range to include any ancestors whose children are entirely
|
|
|
|
// selected
|
2001-01-28 23:13:07 +03:00
|
|
|
res = PromoteInlineRange(range);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2012-04-20 10:33:21 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// check for easy case: both range endpoints in same text node
|
2015-04-19 15:28:50 +03:00
|
|
|
nsCOMPtr<nsINode> startNode = range->GetStartParent();
|
|
|
|
nsCOMPtr<nsINode> endNode = range->GetEndParent();
|
|
|
|
if (startNode && startNode == endNode && startNode->GetAsText()) {
|
|
|
|
res = SetInlinePropertyOnTextNode(*startNode->GetAsText(),
|
|
|
|
range->StartOffset(),
|
|
|
|
range->EndOffset(),
|
|
|
|
*aProperty, &aAttribute, aValue);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
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
|
|
|
|
2012-04-20 10:33:21 +04:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/subtree-content-iterator;1", &res);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
NS_ENSURE_TRUE(iter, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCOMArray<nsIDOMNode> arrayOfNodes;
|
|
|
|
|
|
|
|
// iterate range and build up array
|
|
|
|
res = iter->Init(range);
|
|
|
|
// 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.
|
|
|
|
if (NS_SUCCEEDED(res)) {
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
for (; !iter->IsDone(); iter->Next()) {
|
|
|
|
node = do_QueryInterface(iter->GetCurrentNode());
|
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_FAILURE);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-04-20 10:33:21 +04:00
|
|
|
if (IsEditable(node)) {
|
|
|
|
arrayOfNodes.AppendObject(node);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-20 10:33:21 +04: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
|
|
|
|
// subtree iterator works - it will not have reported it).
|
2015-04-19 15:28:50 +03:00
|
|
|
if (startNode && startNode->GetAsText() && IsEditable(startNode)) {
|
|
|
|
res = SetInlinePropertyOnTextNode(*startNode->GetAsText(),
|
|
|
|
range->StartOffset(),
|
|
|
|
startNode->Length(), *aProperty,
|
|
|
|
&aAttribute, aValue);
|
2012-04-20 10:33:21 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
// then loop through the list, set the property on each node
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t listCount = arrayOfNodes.Count();
|
|
|
|
int32_t j;
|
2012-04-20 10:33:21 +04:00
|
|
|
for (j = 0; j < listCount; j++) {
|
|
|
|
res = SetInlinePropertyOnNode(arrayOfNodes[j], aProperty,
|
|
|
|
&aAttribute, &aValue);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// subtree iterator works - it will not have reported it).
|
2015-04-19 15:28:50 +03:00
|
|
|
if (endNode && endNode->GetAsText() && IsEditable(endNode)) {
|
|
|
|
res = SetInlinePropertyOnTextNode(*endNode->GetAsText(), 0,
|
|
|
|
range->EndOffset(), *aProperty,
|
|
|
|
&aAttribute, aValue);
|
2012-04-20 10:33:21 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
2012-04-20 10:33:21 +04:00
|
|
|
if (!cancel) {
|
2001-01-28 23:13:07 +03:00
|
|
|
// post-process
|
2012-04-20 10:33:21 +04:00
|
|
|
return mRules->DidDoAction(selection, &ruleInfo, res);
|
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
|
|
|
|
nsHTMLEditor::IsSimpleModifiableNode(nsIContent* aContent,
|
|
|
|
nsIAtom* aProperty,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue)
|
|
|
|
{
|
|
|
|
// 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()) {
|
|
|
|
nsCOMPtr<nsIAtom> atom = do_GetAtom(*aAttribute);
|
|
|
|
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
|
2013-02-06 10:00:59 +04:00
|
|
|
if (!mHTMLCSSUtils->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);
|
2012-06-06 11:35:47 +04:00
|
|
|
mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(newSpan, aProperty,
|
2012-05-29 11:12:51 +04:00
|
|
|
aAttribute, aValue,
|
|
|
|
/*suppress transaction*/ true);
|
|
|
|
|
2012-06-06 11:35:47 +04:00
|
|
|
return mHTMLCSSUtils->ElementsSameStyle(newSpan, element);
|
2012-05-29 11:12:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult
|
2015-04-19 15:28:50 +03:00
|
|
|
nsHTMLEditor::SetInlinePropertyOnTextNode(Text& aText,
|
|
|
|
int32_t aStartOffset,
|
|
|
|
int32_t aEndOffset,
|
|
|
|
nsIAtom& aProperty,
|
|
|
|
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
|
|
|
|
if (mHTMLCSSUtils->IsCSSEditableProperty(&aText, &aProperty, aAttribute)) {
|
|
|
|
// The HTML styles defined by aProperty/aAttribute have a CSS equivalence
|
|
|
|
// for node; let's check if it carries those CSS styles
|
|
|
|
if (mHTMLCSSUtils->IsCSSEquivalentToHTMLInlineStyleSet(&aText, &aProperty,
|
|
|
|
aAttribute, aValue, nsHTMLCSSUtils::eComputed)) {
|
|
|
|
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;
|
|
|
|
nsRefPtr<Text> text = &aText;
|
|
|
|
if (uint32_t(aEndOffset) != aText.Length()) {
|
|
|
|
// We need to split off back of text node
|
|
|
|
text = SplitNode(aText, aEndOffset, rv)->GetAsText();
|
|
|
|
NS_ENSURE_SUCCESS(rv.ErrorCode(), rv.ErrorCode());
|
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);
|
|
|
|
NS_ENSURE_SUCCESS(rv.ErrorCode(), rv.ErrorCode());
|
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}
|
|
|
|
return SetInlinePropertyOnNode(text, &aProperty, aAttribute, &aValue);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2012-05-05 13:00:06 +04:00
|
|
|
nsHTMLEditor::SetInlinePropertyOnNodeImpl(nsIContent* aNode,
|
|
|
|
nsIAtom* aProperty,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2012-04-20 10:33:21 +04:00
|
|
|
MOZ_ASSERT(aNode && aProperty);
|
2012-05-05 13:00:06 +04:00
|
|
|
MOZ_ASSERT(aValue);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2014-08-20 16:25:16 +04:00
|
|
|
nsCOMPtr<nsIAtom> attrAtom = aAttribute ? do_GetAtom(*aAttribute) : nullptr;
|
|
|
|
|
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.
|
2014-11-02 15:04:13 +03:00
|
|
|
if (!TagCanContain(*nsGkAtoms::span, *aNode)) {
|
2012-05-05 13:00:06 +04:00
|
|
|
if (aNode->HasChildren()) {
|
|
|
|
nsCOMArray<nsIContent> arrayOfNodes;
|
|
|
|
|
|
|
|
// Populate the list.
|
|
|
|
for (nsIContent* child = aNode->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
2012-10-05 04:37:45 +04:00
|
|
|
if (IsEditable(child) && !IsEmptyTextNode(this, child)) {
|
2012-05-05 13:00:06 +04:00
|
|
|
arrayOfNodes.AppendObject(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.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t listCount = arrayOfNodes.Count();
|
|
|
|
for (int32_t j = 0; j < listCount; ++j) {
|
2012-05-05 13:00:06 +04:00
|
|
|
nsresult rv = SetInlinePropertyOnNode(arrayOfNodes[j], aProperty,
|
|
|
|
aAttribute, aValue);
|
|
|
|
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.
|
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsIContent> previousSibling = GetPriorHTMLSibling(aNode);
|
|
|
|
nsCOMPtr<nsIContent> nextSibling = GetNextHTMLSibling(aNode);
|
|
|
|
if (IsSimpleModifiableNode(previousSibling, aProperty, aAttribute, aValue)) {
|
|
|
|
res = MoveNode(aNode, previousSibling, -1);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
if (IsSimpleModifiableNode(nextSibling, aProperty, aAttribute, aValue)) {
|
2014-11-02 15:04:12 +03:00
|
|
|
res = JoinNodes(*previousSibling, *nextSibling);
|
2012-05-29 11:12:51 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (IsSimpleModifiableNode(nextSibling, aProperty, aAttribute, aValue)) {
|
|
|
|
res = MoveNode(aNode, nextSibling, 0);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-23 11:49:29 +04:00
|
|
|
// don't need to do anything if property already set on node
|
2013-02-06 10:00:59 +04:00
|
|
|
if (mHTMLCSSUtils->IsCSSEditableProperty(aNode, aProperty, aAttribute)) {
|
2012-05-23 11:49:29 +04:00
|
|
|
if (mHTMLCSSUtils->IsCSSEquivalentToHTMLInlineStyleSet(
|
2012-07-06 15:50:07 +04:00
|
|
|
aNode, aProperty, aAttribute, *aValue, nsHTMLCSSUtils::eComputed)) {
|
2012-05-23 11:49:29 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
} else if (IsTextPropertySetByContent(aNode, aProperty,
|
|
|
|
aAttribute, aValue)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-17 14:18:08 +04:00
|
|
|
bool useCSS = (IsCSSEnabled() &&
|
2013-02-06 10:00:59 +04:00
|
|
|
mHTMLCSSUtils->IsCSSEditableProperty(aNode, aProperty, aAttribute)) ||
|
2012-04-24 14:57:49 +04:00
|
|
|
// bgcolor is always done using CSS
|
|
|
|
aAttribute->EqualsLiteral("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-03-03 14:08:59 +03:00
|
|
|
if (aNode->IsHTMLElement(nsGkAtoms::span) &&
|
2012-06-06 11:36:17 +04:00
|
|
|
!aNode->AsElement()->GetAttrCount()) {
|
|
|
|
tmp = aNode->AsElement();
|
|
|
|
} else {
|
2014-08-20 16:25:16 +04:00
|
|
|
tmp = InsertContainerAbove(aNode, nsGkAtoms::span);
|
|
|
|
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
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t count;
|
2012-06-06 11:36:17 +04:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(tmp->AsDOMNode(),
|
|
|
|
aProperty, aAttribute,
|
|
|
|
aValue, &count, false);
|
2012-04-17 14:18:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
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-03-03 14:08:59 +03:00
|
|
|
if (aNode->IsHTMLElement(aProperty)) {
|
2012-04-20 10:33:21 +04:00
|
|
|
// Just set the attribute on it.
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMElement> elem = do_QueryInterface(aNode);
|
|
|
|
return SetAttribute(elem, *aAttribute, *aValue);
|
|
|
|
}
|
2012-04-20 10:33:21 +04:00
|
|
|
|
2012-04-20 10:33:21 +04:00
|
|
|
// ok, chuck it in its very own container
|
2014-08-20 16:25:16 +04:00
|
|
|
nsCOMPtr<Element> tmp = InsertContainerAbove(aNode, aProperty, attrAtom,
|
|
|
|
aValue);
|
|
|
|
NS_ENSURE_STATE(tmp);
|
|
|
|
|
|
|
|
return NS_OK;
|
2012-04-20 10:33:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::SetInlinePropertyOnNode(nsIDOMNode *aNode,
|
|
|
|
nsIAtom *aProperty,
|
|
|
|
const nsAString *aAttribute,
|
|
|
|
const nsAString *aValue)
|
|
|
|
{
|
|
|
|
// Before setting the property, we remove it if it's already set.
|
|
|
|
// RemoveStyleInside might remove the node we're looking at or some of its
|
|
|
|
// descendants, however, in which case we want to set the property on
|
|
|
|
// whatever wound up in its place. We have to save the original siblings and
|
|
|
|
// parent to figure this out.
|
|
|
|
NS_ENSURE_TRUE(aNode && aProperty, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_STATE(node);
|
2012-05-05 13:00:06 +04:00
|
|
|
|
|
|
|
return SetInlinePropertyOnNode(node, aProperty, aAttribute, aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::SetInlinePropertyOnNode(nsIContent* aNode,
|
|
|
|
nsIAtom* aProperty,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
MOZ_ASSERT(aProperty);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> previousSibling = aNode->GetPreviousSibling(),
|
|
|
|
nextSibling = aNode->GetNextSibling();
|
2012-10-09 16:31:24 +04:00
|
|
|
nsCOMPtr<nsINode> parent = aNode->GetParentNode();
|
2012-04-20 10:33:21 +04:00
|
|
|
NS_ENSURE_STATE(parent);
|
|
|
|
|
2012-05-05 13:00:06 +04:00
|
|
|
nsresult res = RemoveStyleInside(aNode->AsDOMNode(), aProperty, aAttribute);
|
2012-04-17 21:50:19 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2012-04-20 10:33:21 +04:00
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
if (aNode->GetParentNode()) {
|
2012-04-20 10:33:21 +04:00
|
|
|
// The node is still where it was
|
|
|
|
return SetInlinePropertyOnNodeImpl(aNode, aProperty,
|
|
|
|
aAttribute, aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
nsCOMArray<nsIContent> nodesToSet;
|
|
|
|
nsCOMPtr<nsIContent> cur = previousSibling
|
|
|
|
? previousSibling->GetNextSibling() : parent->GetFirstChild();
|
|
|
|
while (cur && cur != nextSibling) {
|
|
|
|
if (IsEditable(cur)) {
|
|
|
|
nodesToSet.AppendObject(cur);
|
|
|
|
}
|
|
|
|
cur = cur->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t nodesToSetCount = nodesToSet.Count();
|
|
|
|
for (int32_t k = 0; k < nodesToSetCount; k++) {
|
2012-05-05 13:00:06 +04:00
|
|
|
res = SetInlinePropertyOnNodeImpl(nodesToSet[k], aProperty,
|
2012-04-20 10:33:21 +04:00
|
|
|
aAttribute, aValue);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-02 15:04:13 +03:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::SplitStyleAboveRange(nsRange* inRange, nsIAtom* aProperty,
|
|
|
|
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);
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsIDOMNode> startNode, endNode, origStartNode;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startOffset, endOffset;
|
2011-12-22 22:16:56 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
res = inRange->GetStartContainer(getter_AddRefs(startNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
res = inRange->GetStartOffset(&startOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
res = inRange->GetEndContainer(getter_AddRefs(endNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
res = inRange->GetEndOffset(&endOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2011-12-22 22:16:56 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
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
|
|
|
|
{
|
2002-06-12 18:04:47 +04:00
|
|
|
nsAutoTrackDOMPoint tracker(mRangeUpdater, address_of(endNode), &endOffset);
|
|
|
|
res = SplitStyleAbovePoint(address_of(startNode), &startOffset, aProperty, aAttribute);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
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...
|
|
|
|
res = SplitStyleAbovePoint(address_of(endNode), &endOffset, aProperty, aAttribute);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2011-12-22 22:16:56 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// reset the range
|
|
|
|
res = inRange->SetStart(startNode, startOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
res = inRange->SetEnd(endNode, endOffset);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsHTMLEditor::SplitStyleAbovePoint(nsCOMPtr<nsIDOMNode> *aNode,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t *aOffset,
|
2001-01-28 23:13:07 +03:00
|
|
|
nsIAtom *aProperty, // null here means we split all properties
|
2002-03-24 01:08:20 +03:00
|
|
|
const nsAString *aAttribute,
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> *outLeftNode,
|
|
|
|
nsCOMPtr<nsIDOMNode> *outRightNode)
|
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aNode && *aNode && aOffset, NS_ERROR_NULL_POINTER);
|
2012-07-30 18:20:58 +04:00
|
|
|
if (outLeftNode) *outLeftNode = nullptr;
|
|
|
|
if (outRightNode) *outRightNode = nullptr;
|
2001-01-28 23:13:07 +03:00
|
|
|
// split any matching style nodes above the node/offset
|
2014-10-22 16:45:30 +04:00
|
|
|
nsCOMPtr<nsIContent> node = do_QueryInterface(*aNode);
|
|
|
|
NS_ENSURE_STATE(node);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t offset;
|
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;
|
2014-10-22 16:45:30 +04:00
|
|
|
while (node && !IsBlockNode(node) && node->GetParentNode() &&
|
|
|
|
IsEditable(node->GetParentNode())) {
|
2011-10-17 18:59:28 +04:00
|
|
|
isSet = false;
|
2014-10-22 16:45:30 +04:00
|
|
|
if (useCSS && mHTMLCSSUtils->IsCSSEditableProperty(node, aProperty, aAttribute)) {
|
2002-01-09 16:51:37 +03:00
|
|
|
// the HTML style defined by aProperty/aAttribute has a CSS equivalence
|
2014-10-22 16:45:30 +04:00
|
|
|
// 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;
|
2014-10-22 16:45:30 +04:00
|
|
|
mHTMLCSSUtils->IsCSSEquivalentToHTMLInlineStyleSet(GetAsDOMNode(node),
|
|
|
|
aProperty, aAttribute, isSet, firstValue, nsHTMLCSSUtils::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=...
|
|
|
|
(aProperty == nsGkAtoms::href && nsHTMLEditUtils::IsLink(node)) ||
|
|
|
|
// or node is any prop, and we asked to split them all
|
|
|
|
(!aProperty && NodeIsProperty(GetAsDOMNode(node))) ||
|
|
|
|
// or the style is specified in the style attribute
|
|
|
|
isSet) {
|
2001-01-28 23:13:07 +03:00
|
|
|
// found a style node we need to split
|
2014-10-22 16:45:30 +04:00
|
|
|
nsresult rv = SplitNodeDeep(GetAsDOMNode(node), *aNode, *aOffset,
|
|
|
|
&offset, false, outLeftNode, outRightNode);
|
2010-03-15 21:30:01 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 23:13:07 +03:00
|
|
|
// reset startNode/startOffset
|
2014-10-22 16:45:30 +04:00
|
|
|
*aNode = GetAsDOMNode(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
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-10 16:00:14 +04:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsHTMLEditor::ClearStyle(nsCOMPtr<nsIDOMNode>* aNode, int32_t* aOffset,
|
2012-05-10 16:00:14 +04:00
|
|
|
nsIAtom* aProperty, const nsAString* aAttribute)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> leftNode, rightNode, tmp;
|
|
|
|
nsresult res = SplitStyleAbovePoint(aNode, aOffset, aProperty, aAttribute,
|
|
|
|
address_of(leftNode),
|
|
|
|
address_of(rightNode));
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
if (leftNode) {
|
|
|
|
bool bIsEmptyNode;
|
|
|
|
IsEmptyNode(leftNode, &bIsEmptyNode, false, true);
|
|
|
|
if (bIsEmptyNode) {
|
|
|
|
// delete leftNode if it became empty
|
|
|
|
res = DeleteNode(leftNode);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rightNode) {
|
2014-11-02 15:04:13 +03:00
|
|
|
nsCOMPtr<nsINode> rightNode_ = do_QueryInterface(rightNode);
|
|
|
|
NS_ENSURE_STATE(rightNode_);
|
|
|
|
nsCOMPtr<nsIDOMNode> secondSplitParent =
|
|
|
|
GetAsDOMNode(GetLeftmostChild(rightNode_));
|
2012-05-10 16:00:14 +04:00
|
|
|
// don't try to split non-containers (br's, images, hr's, etc)
|
|
|
|
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)) {
|
|
|
|
if (nsTextEditUtils::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
|
|
|
}
|
|
|
|
|
|
|
|
secondSplitParent->GetParentNode(getter_AddRefs(tmp));
|
|
|
|
secondSplitParent = tmp;
|
|
|
|
}
|
|
|
|
*aOffset = 0;
|
|
|
|
res = SplitStyleAbovePoint(address_of(secondSplitParent),
|
|
|
|
aOffset, aProperty, aAttribute,
|
|
|
|
address_of(leftNode), address_of(rightNode));
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
// should be impossible to not get a new leftnode here
|
2014-11-02 15:04:13 +03:00
|
|
|
nsCOMPtr<nsINode> leftNode_ = do_QueryInterface(leftNode);
|
|
|
|
NS_ENSURE_TRUE(leftNode_, NS_ERROR_FAILURE);
|
|
|
|
nsCOMPtr<nsINode> newSelParent = GetLeftmostChild(leftNode_);
|
2012-05-10 16:00:14 +04:00
|
|
|
if (!newSelParent) {
|
2014-08-20 16:25:16 +04:00
|
|
|
newSelParent = do_QueryInterface(leftNode);
|
|
|
|
NS_ENSURE_STATE(newSelParent);
|
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) {
|
|
|
|
res = MoveNode(savedBR, newSelParent, 0);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
}
|
|
|
|
bool bIsEmptyNode;
|
|
|
|
IsEmptyNode(rightNode, &bIsEmptyNode, false, true);
|
|
|
|
if (bIsEmptyNode) {
|
|
|
|
// delete rightNode if it became empty
|
|
|
|
res = DeleteNode(rightNode);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
}
|
|
|
|
// 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.
|
|
|
|
nsAutoTrackDOMPoint tracker(mRangeUpdater,
|
|
|
|
address_of(newSelParent), &newSelOffset);
|
|
|
|
res = RemoveStyleInside(leftNode, aProperty, aAttribute);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
}
|
|
|
|
// reset our node offset values to the resulting new sel point
|
2014-08-20 16:25:16 +04:00
|
|
|
*aNode = GetAsDOMNode(newSelParent);
|
2012-05-10 16:00:14 +04:00
|
|
|
*aOffset = newSelOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsHTMLEditor::NodeIsProperty(nsIDOMNode *aNode)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(aNode, false);
|
|
|
|
if (!IsContainer(aNode)) return false;
|
|
|
|
if (!IsEditable(aNode)) return false;
|
|
|
|
if (IsBlockNode(aNode)) return false;
|
2014-08-29 15:43:24 +04:00
|
|
|
if (NodeIsType(aNode, nsGkAtoms::a)) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2003-06-03 02:21:06 +04:00
|
|
|
nsresult nsHTMLEditor::ApplyDefaultProperties()
|
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t j, defcon = mDefaultStyles.Length();
|
2003-06-03 02:21:06 +04:00
|
|
|
for (j=0; j<defcon; j++)
|
|
|
|
{
|
2009-04-22 12:43:15 +04:00
|
|
|
PropItem *propItem = mDefaultStyles[j];
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(propItem, NS_ERROR_NULL_POINTER);
|
2003-06-03 02:21:06 +04:00
|
|
|
res = SetInlineProperty(propItem->tag, propItem->attr, propItem->value);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-03 02:21:06 +04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult nsHTMLEditor::RemoveStyleInside(nsIDOMNode *aNode,
|
2012-04-17 21:50:19 +04:00
|
|
|
// null here means remove all properties
|
|
|
|
nsIAtom *aProperty,
|
2012-02-01 14:54:22 +04:00
|
|
|
const nsAString *aAttribute,
|
2012-04-17 21:50:19 +04:00
|
|
|
const bool aChildrenOnly)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
|
2012-04-17 21:50:19 +04:00
|
|
|
if (IsTextNode(aNode)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsresult res;
|
2014-08-20 16:25:16 +04:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_STATE(content);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// first process the children
|
|
|
|
nsCOMPtr<nsIDOMNode> child, tmp;
|
|
|
|
aNode->GetFirstChild(getter_AddRefs(child));
|
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
|
|
|
|
child->GetNextSibling(getter_AddRefs(tmp));
|
|
|
|
res = RemoveStyleInside(child, aProperty, aAttribute);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
child = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// then process the node itself
|
2012-08-21 22:31:19 +04:00
|
|
|
if (!aChildrenOnly &&
|
|
|
|
(
|
|
|
|
// node is prop we asked for
|
|
|
|
(aProperty && NodeIsType(aNode, aProperty)) ||
|
|
|
|
// but check for link (<a href=...)
|
2014-08-29 15:43:24 +04:00
|
|
|
(aProperty == nsGkAtoms::href && nsHTMLEditUtils::IsLink(aNode)) ||
|
2012-08-21 22:31:19 +04:00
|
|
|
// and for named anchors
|
2014-08-29 15:43:24 +04:00
|
|
|
(aProperty == nsGkAtoms::name && nsHTMLEditUtils::IsNamedAnchor(aNode)) ||
|
2012-08-21 22:31:19 +04:00
|
|
|
// or node is any prop and we asked for that
|
|
|
|
(!aProperty && NodeIsProperty(aNode))
|
|
|
|
)
|
2012-04-17 21:50:19 +04:00
|
|
|
) {
|
2001-01-28 23:13:07 +03:00
|
|
|
// if we weren't passed an attribute, then we want to
|
|
|
|
// remove any matching inlinestyles entirely
|
2012-04-17 21:50:19 +04:00
|
|
|
if (!aAttribute || aAttribute->IsEmpty()) {
|
2003-06-17 12:45:54 +04:00
|
|
|
NS_NAMED_LITERAL_STRING(styleAttr, "style");
|
|
|
|
NS_NAMED_LITERAL_STRING(classAttr, "class");
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasStyleAttr = HasAttr(aNode, &styleAttr);
|
2012-04-17 21:50:19 +04:00
|
|
|
bool hasClassAttr = HasAttr(aNode, &classAttr);
|
|
|
|
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.
|
2014-08-20 16:25:16 +04:00
|
|
|
nsCOMPtr<Element> spanNode =
|
|
|
|
InsertContainerAbove(content, nsGkAtoms::span);
|
|
|
|
NS_ENSURE_STATE(spanNode);
|
|
|
|
res = CloneAttribute(styleAttr, spanNode->AsDOMNode(), aNode);
|
2010-06-18 00:44:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2014-08-20 16:25:16 +04:00
|
|
|
res = CloneAttribute(classAttr, spanNode->AsDOMNode(), aNode);
|
2010-06-18 00:44:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-17 12:45:54 +04:00
|
|
|
}
|
2014-08-20 16:25:16 +04:00
|
|
|
res = RemoveContainer(content);
|
2012-04-17 21:50:19 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
} else {
|
|
|
|
// otherwise we just want to eliminate the attribute
|
|
|
|
if (HasAttr(aNode, aAttribute)) {
|
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.
|
2012-04-17 21:50:19 +04:00
|
|
|
if (IsOnlyAttribute(aNode, aAttribute)) {
|
2014-08-20 16:25:16 +04:00
|
|
|
res = RemoveContainer(content);
|
2012-04-17 21:50:19 +04:00
|
|
|
} else {
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMElement> elem = do_QueryInterface(aNode);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(elem, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
res = RemoveAttribute(elem, *aAttribute);
|
|
|
|
}
|
2012-04-17 21:50:19 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
2012-04-17 21:50:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!aChildrenOnly &&
|
|
|
|
mHTMLCSSUtils->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
|
|
|
|
nsAutoString propertyValue;
|
|
|
|
bool isSet;
|
2012-07-06 15:50:07 +04:00
|
|
|
mHTMLCSSUtils->IsCSSEquivalentToHTMLInlineStyleSet(aNode, aProperty,
|
|
|
|
aAttribute, isSet, propertyValue, nsHTMLCSSUtils::eSpecified);
|
2012-04-17 21:50:19 +04:00
|
|
|
if (isSet) {
|
|
|
|
// yes, tmp has the corresponding css declarations in its style attribute
|
|
|
|
// let's remove them
|
|
|
|
mHTMLCSSUtils->RemoveCSSEquivalentToHTMLStyle(aNode,
|
|
|
|
aProperty,
|
|
|
|
aAttribute,
|
|
|
|
&propertyValue,
|
|
|
|
false);
|
2012-04-20 10:33:21 +04:00
|
|
|
// 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
|
2012-04-17 21:50:19 +04:00
|
|
|
RemoveElementIfNoStyleOrIdOrClass(aNode);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2001-10-22 10:22:52 +04:00
|
|
|
}
|
2012-04-17 21:50:19 +04:00
|
|
|
|
2012-08-23 21:17:43 +04:00
|
|
|
if (!aChildrenOnly &&
|
|
|
|
(
|
2014-08-29 15:43:24 +04:00
|
|
|
// Or node is big or small and we are setting font size
|
|
|
|
aProperty == nsGkAtoms::font &&
|
2012-04-17 21:50:19 +04:00
|
|
|
(nsHTMLEditUtils::IsBig(aNode) || nsHTMLEditUtils::IsSmall(aNode)) &&
|
2012-08-23 21:17:43 +04:00
|
|
|
(aAttribute && aAttribute->LowerCaseEqualsLiteral("size"))
|
|
|
|
)
|
|
|
|
) {
|
2014-08-20 16:25:16 +04:00
|
|
|
// if we are setting font size, remove any nested bigs and smalls
|
|
|
|
return RemoveContainer(content);
|
2012-04-17 21:50:19 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsHTMLEditor::IsOnlyAttribute(nsIDOMNode *aNode,
|
2002-03-24 01:08:20 +03:00
|
|
|
const nsAString *aAttribute)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(aNode && aAttribute, false); // ooops
|
2012-05-05 13:00:06 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(content, false); // ooops
|
2012-05-05 13:00:06 +04:00
|
|
|
|
|
|
|
return IsOnlyAttribute(content, *aAttribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsHTMLEditor::IsOnlyAttribute(const nsIContent* aContent,
|
|
|
|
const nsAString& aAttribute)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-02-01 14:54:22 +04:00
|
|
|
bool nsHTMLEditor::HasAttr(nsIDOMNode* aNode,
|
|
|
|
const nsAString* aAttribute)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(aNode, false);
|
2012-02-01 14:54:22 +04:00
|
|
|
if (!aAttribute || aAttribute->IsEmpty()) {
|
|
|
|
// everybody has the 'null' attribute
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// get element
|
2012-02-01 14:54:22 +04:00
|
|
|
nsCOMPtr<dom::Element> element = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(element, false);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAtom> atom = do_GetAtom(*aAttribute);
|
|
|
|
NS_ENSURE_TRUE(atom, false);
|
|
|
|
|
|
|
|
return element->HasAttr(kNameSpaceID_None, atom);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-02 15:04:13 +03:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::PromoteRangeIfStartsOrEndsInNamedAnchor(nsRange* inRange)
|
2002-04-11 17:33:30 +04:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(inRange, NS_ERROR_NULL_POINTER);
|
2002-04-11 17:33:30 +04:00
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsIDOMNode> startNode, endNode, parent, tmp;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startOffset, endOffset, tmpOffset;
|
2002-04-11 17:33:30 +04:00
|
|
|
|
|
|
|
res = inRange->GetStartContainer(getter_AddRefs(startNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-04-11 17:33:30 +04:00
|
|
|
res = inRange->GetStartOffset(&startOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-04-11 17:33:30 +04:00
|
|
|
res = inRange->GetEndContainer(getter_AddRefs(endNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-04-11 17:33:30 +04:00
|
|
|
res = inRange->GetEndOffset(&endOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-04-11 17:33:30 +04:00
|
|
|
|
|
|
|
tmp = startNode;
|
|
|
|
while ( tmp &&
|
|
|
|
!nsTextEditUtils::IsBody(tmp) &&
|
|
|
|
!nsHTMLEditUtils::IsNamedAnchor(tmp))
|
|
|
|
{
|
2012-07-09 04:45:12 +04:00
|
|
|
parent = GetNodeLocation(tmp, &tmpOffset);
|
2002-04-11 17:33:30 +04:00
|
|
|
tmp = parent;
|
|
|
|
}
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(tmp, NS_ERROR_NULL_POINTER);
|
2002-04-11 17:33:30 +04:00
|
|
|
if (nsHTMLEditUtils::IsNamedAnchor(tmp))
|
|
|
|
{
|
2012-07-09 04:45:12 +04:00
|
|
|
parent = GetNodeLocation(tmp, &tmpOffset);
|
2002-04-11 17:33:30 +04:00
|
|
|
startNode = parent;
|
|
|
|
startOffset = tmpOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = endNode;
|
|
|
|
while ( tmp &&
|
|
|
|
!nsTextEditUtils::IsBody(tmp) &&
|
|
|
|
!nsHTMLEditUtils::IsNamedAnchor(tmp))
|
|
|
|
{
|
2012-07-09 04:45:12 +04:00
|
|
|
parent = GetNodeLocation(tmp, &tmpOffset);
|
2002-04-11 17:33:30 +04:00
|
|
|
tmp = parent;
|
|
|
|
}
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(tmp, NS_ERROR_NULL_POINTER);
|
2002-04-11 17:33:30 +04:00
|
|
|
if (nsHTMLEditUtils::IsNamedAnchor(tmp))
|
|
|
|
{
|
2012-07-09 04:45:12 +04:00
|
|
|
parent = GetNodeLocation(tmp, &tmpOffset);
|
2002-04-11 17:33:30 +04:00
|
|
|
endNode = parent;
|
|
|
|
endOffset = tmpOffset + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = inRange->SetStart(startNode, startOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-04-11 17:33:30 +04:00
|
|
|
res = inRange->SetEnd(endNode, endOffset);
|
|
|
|
return res;
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2014-11-02 15:04:13 +03:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::PromoteInlineRange(nsRange* inRange)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(inRange, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsIDOMNode> startNode, endNode, parent;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startOffset, endOffset;
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
res = inRange->GetStartContainer(getter_AddRefs(startNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
res = inRange->GetStartOffset(&startOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
res = inRange->GetEndContainer(getter_AddRefs(endNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
res = inRange->GetEndOffset(&endOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
while ( startNode &&
|
2001-04-06 03:48:01 +04:00
|
|
|
!nsTextEditUtils::IsBody(startNode) &&
|
2009-07-26 09:21:04 +04:00
|
|
|
IsEditable(startNode) &&
|
2001-01-28 23:13:07 +03:00
|
|
|
IsAtFrontOfNode(startNode, startOffset) )
|
|
|
|
{
|
2012-07-09 04:45:12 +04:00
|
|
|
parent = GetNodeLocation(startNode, &startOffset);
|
2001-01-28 23:13:07 +03:00
|
|
|
startNode = parent;
|
|
|
|
}
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(startNode, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
while ( endNode &&
|
2001-04-06 03:48:01 +04:00
|
|
|
!nsTextEditUtils::IsBody(endNode) &&
|
2009-07-26 09:21:04 +04:00
|
|
|
IsEditable(endNode) &&
|
2001-01-28 23:13:07 +03:00
|
|
|
IsAtEndOfNode(endNode, endOffset) )
|
|
|
|
{
|
2012-07-09 04:45:12 +04:00
|
|
|
parent = GetNodeLocation(endNode, &endOffset);
|
2001-01-28 23:13:07 +03:00
|
|
|
endNode = parent;
|
|
|
|
endOffset++; // we are AFTER this node
|
|
|
|
}
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(endNode, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
res = inRange->SetStart(startNode, startOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
res = inRange->SetEnd(endNode, endOffset);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
bool nsHTMLEditor::IsAtFrontOfNode(nsIDOMNode *aNode, int32_t aOffset)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2014-11-02 15:04:13 +03:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(node, false);
|
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
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
if (IsTextNode(aNode))
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-02 15:04:13 +03:00
|
|
|
nsCOMPtr<nsIContent> firstNode = GetFirstEditableChild(*node);
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(firstNode, true);
|
2014-11-02 15:04:13 +03:00
|
|
|
int32_t offset = node->IndexOf(firstNode);
|
2011-10-17 18:59:28 +04:00
|
|
|
if (offset < aOffset) return false;
|
|
|
|
return true;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
bool nsHTMLEditor::IsAtEndOfNode(nsIDOMNode *aNode, int32_t aOffset)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2014-11-02 15:04:13 +03:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(node, false);
|
|
|
|
uint32_t len = node->Length();
|
2012-08-22 19:56:38 +04:00
|
|
|
if (aOffset == (int32_t)len) return true;
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
if (IsTextNode(aNode))
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-02 15:04:13 +03:00
|
|
|
nsCOMPtr<nsIContent> lastNode = GetLastEditableChild(*node);
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(lastNode, true);
|
2014-11-02 15:04:13 +03:00
|
|
|
int32_t offset = node->IndexOf(lastNode);
|
2011-10-17 18:59:28 +04:00
|
|
|
if (offset < aOffset) return true;
|
|
|
|
return false;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-07 04:45:26 +04:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetInlinePropertyBase(nsIAtom *aProperty,
|
2012-04-18 18:26:02 +04:00
|
|
|
const nsAString *aAttribute,
|
|
|
|
const nsAString *aValue,
|
|
|
|
bool *aFirst,
|
|
|
|
bool *aAny,
|
|
|
|
bool *aAll,
|
|
|
|
nsAString *outValue,
|
|
|
|
bool aCheckDefaults)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(aProperty, NS_ERROR_NULL_POINTER);
|
2003-05-24 01:43:10 +04:00
|
|
|
|
2014-11-04 21:11:54 +03:00
|
|
|
nsresult result = NS_OK;
|
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
|
|
|
|
2014-11-02 15:04:13 +03:00
|
|
|
nsRefPtr<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();
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> collapsedNode;
|
2014-11-02 15:04:13 +03:00
|
|
|
nsRefPtr<nsRange> range = selection->GetRangeAt(0);
|
2001-01-28 23:13:07 +03:00
|
|
|
// XXX: should be a while loop, to get each separate range
|
|
|
|
// XXX: ERROR_HANDLING can currentItem be null?
|
2013-02-07 18:17:47 +04:00
|
|
|
if (range) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool firstNodeInRange = true; // for each range, set a flag
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-04-18 18:26:02 +04:00
|
|
|
if (isCollapsed) {
|
2001-01-28 23:13:07 +03:00
|
|
|
range->GetStartContainer(getter_AddRefs(collapsedNode));
|
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);
|
2012-04-18 18:26:02 +04:00
|
|
|
mTypeInState->GetTypingState(isSet, theSetting, aProperty, tString,
|
|
|
|
&tOutString);
|
|
|
|
if (outValue) {
|
2001-04-07 04:45:26 +04:00
|
|
|
outValue->Assign(tOutString);
|
2012-04-18 18:26:02 +04:00
|
|
|
}
|
|
|
|
} else {
|
2001-01-28 23:13:07 +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
|
|
|
|
2013-02-06 10:00:59 +04:00
|
|
|
if (mHTMLCSSUtils->IsCSSEditableProperty(collapsedNode, aProperty, aAttribute)) {
|
2012-05-03 11:09:44 +04:00
|
|
|
mHTMLCSSUtils->IsCSSEquivalentToHTMLInlineStyleSet(
|
|
|
|
collapsedNode, aProperty, aAttribute, isSet, tOutString,
|
2012-07-06 15:50:07 +04:00
|
|
|
nsHTMLCSSUtils::eComputed);
|
2012-05-03 11:09:44 +04:00
|
|
|
if (outValue) {
|
|
|
|
outValue->Assign(tOutString);
|
|
|
|
}
|
2002-01-09 16:51:37 +03:00
|
|
|
*aFirst = *aAny = *aAll = isSet;
|
2012-05-03 11:09:44 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-12-22 22:16:56 +04:00
|
|
|
|
2012-05-03 11:09:44 +04:00
|
|
|
IsTextPropertySetByContent(collapsedNode, aProperty, aAttribute, aValue,
|
|
|
|
isSet, outValue);
|
|
|
|
*aFirst = *aAny = *aAll = isSet;
|
|
|
|
|
|
|
|
if (!isSet && aCheckDefaults) {
|
|
|
|
// style not set, but if it is a default then it will appear if
|
|
|
|
// content is inserted, so we should report it as set (analogous to
|
|
|
|
// TypeInState).
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t index;
|
2012-05-03 11:09:44 +04:00
|
|
|
if (aAttribute && TypeInState::FindPropInList(aProperty, *aAttribute,
|
|
|
|
outValue, mDefaultStyles,
|
|
|
|
index)) {
|
|
|
|
*aFirst = *aAny = *aAll = true;
|
|
|
|
if (outValue) {
|
|
|
|
outValue->Assign(mDefaultStyles[index]->value);
|
2003-06-03 02:21:06 +04:00
|
|
|
}
|
|
|
|
}
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2012-05-03 11:09:44 +04:00
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2001-11-29 13:08:25 +03:00
|
|
|
// non-collapsed selection
|
2003-07-09 00:22:56 +04:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1");
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(iter, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
nsAutoString firstValue, theValue;
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2002-06-18 13:20:07 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> endNode;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t endOffset;
|
2002-06-18 13:20:07 +04:00
|
|
|
result = range->GetEndContainer(getter_AddRefs(endNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2002-06-18 13:20:07 +04:00
|
|
|
result = range->GetEndOffset(&endOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
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();
|
|
|
|
nsCOMPtr<nsIDOMNode> node = content->AsDOMNode();
|
2001-08-03 00:31:09 +04:00
|
|
|
|
2012-06-10 16:39:21 +04:00
|
|
|
if (nsTextEditUtils::IsBody(node)) {
|
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
|
|
|
|
2012-04-18 18:26:02 +04:00
|
|
|
nsCOMPtr<nsIDOMCharacterData> text;
|
2001-01-28 23:13:07 +03:00
|
|
|
text = do_QueryInterface(content);
|
2001-02-07 12:24:21 +03:00
|
|
|
|
|
|
|
// just ignore any non-editable nodes
|
2012-10-05 04:58:00 +04:00
|
|
|
if (text && (!IsEditable(text) || IsEmptyTextNode(this, content))) {
|
2012-04-18 18:26:02 +04:00
|
|
|
continue;
|
2001-02-07 12:24:21 +03:00
|
|
|
}
|
2012-04-18 18:26:02 +04:00
|
|
|
if (text) {
|
|
|
|
if (!isCollapsed && first && firstNodeInRange) {
|
2011-10-17 18:59:28 +04:00
|
|
|
firstNodeInRange = false;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startOffset;
|
2001-01-28 23:13:07 +03:00
|
|
|
range->GetStartOffset(&startOffset);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count;
|
2001-01-28 23:13:07 +03:00
|
|
|
text->GetLength(&count);
|
2012-08-22 19:56:38 +04:00
|
|
|
if (startOffset == (int32_t)count) {
|
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 (node == endNode && !endOffset) {
|
|
|
|
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) {
|
2013-02-06 10:00:59 +04:00
|
|
|
if (mHTMLCSSUtils->IsCSSEditableProperty(node, aProperty, aAttribute)){
|
2012-06-10 16:39:21 +04:00
|
|
|
// the HTML styles defined by aProperty/aAttribute has a CSS
|
|
|
|
// equivalence in this implementation for node; let's check if it
|
|
|
|
// carries those css styles
|
|
|
|
if (aValue) {
|
|
|
|
firstValue.Assign(*aValue);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-07-06 15:50:07 +04:00
|
|
|
mHTMLCSSUtils->IsCSSEquivalentToHTMLInlineStyleSet(node, aProperty,
|
|
|
|
aAttribute, isSet, firstValue, nsHTMLCSSUtils::eComputed);
|
2012-04-18 18:26:02 +04:00
|
|
|
} else {
|
2012-06-10 16:39:21 +04:00
|
|
|
IsTextPropertySetByContent(node, aProperty, aAttribute, aValue, isSet,
|
|
|
|
&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 {
|
2013-02-06 10:00:59 +04:00
|
|
|
if (mHTMLCSSUtils->IsCSSEditableProperty(node, aProperty, aAttribute)){
|
2012-06-10 16:39:21 +04:00
|
|
|
// the HTML styles defined by aProperty/aAttribute has a CSS equivalence
|
|
|
|
// in this implementation for node; let's check if it carries those css styles
|
|
|
|
if (aValue) {
|
|
|
|
theValue.Assign(*aValue);
|
|
|
|
}
|
2012-07-06 15:50:07 +04:00
|
|
|
mHTMLCSSUtils->IsCSSEquivalentToHTMLInlineStyleSet(node, aProperty,
|
|
|
|
aAttribute, isSet, theValue, nsHTMLCSSUtils::eComputed);
|
2012-04-18 18:26:02 +04:00
|
|
|
} else {
|
2012-06-10 16:39:21 +04:00
|
|
|
IsTextPropertySetByContent(node, aProperty, aAttribute, aValue, isSet,
|
|
|
|
&theValue);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-07 04:45:26 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::GetInlineProperty(nsIAtom *aProperty,
|
2002-03-24 01:08:20 +03:00
|
|
|
const nsAString &aAttribute,
|
|
|
|
const nsAString &aValue,
|
2011-09-29 10:19:26 +04:00
|
|
|
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;
|
2012-07-30 18:20:58 +04:00
|
|
|
return GetInlinePropertyBase( aProperty, att, val, aFirst, aAny, aAll, nullptr);
|
2001-04-07 04:45:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::GetInlinePropertyWithAttrValue(nsIAtom *aProperty,
|
2002-03-24 01:08:20 +03:00
|
|
|
const nsAString &aAttribute,
|
|
|
|
const nsAString &aValue,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *aFirst,
|
|
|
|
bool *aAny,
|
|
|
|
bool *aAll,
|
2002-03-24 01:08:20 +03:00
|
|
|
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;
|
|
|
|
return GetInlinePropertyBase( aProperty, att, val, aFirst, aAny, aAll, &outValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::RemoveAllInlineProperties()
|
|
|
|
{
|
2003-06-03 02:21:06 +04:00
|
|
|
nsAutoEditBatch batchIt(this);
|
2012-08-12 22:28:26 +04:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::resetTextProperties, nsIEditor::eNext);
|
2003-06-03 02:21:06 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsresult res = RemoveInlinePropertyImpl(nullptr, nullptr);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-03 02:21:06 +04:00
|
|
|
return ApplyDefaultProperties();
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2002-03-24 01:08:20 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::RemoveInlineProperty(nsIAtom *aProperty, 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
|
|
|
}
|
|
|
|
|
2002-03-24 01:08:20 +03:00
|
|
|
nsresult nsHTMLEditor::RemoveInlinePropertyImpl(nsIAtom *aProperty, 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);
|
2001-01-28 23:13:07 +03:00
|
|
|
ForceCompositionEnd();
|
|
|
|
|
|
|
|
nsresult res;
|
2012-06-08 18:20:55 +04:00
|
|
|
nsRefPtr<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-02-01 14:54:22 +04:00
|
|
|
bool useCSS = IsCSSEnabled();
|
2012-05-18 12:29:39 +04:00
|
|
|
if (selection->Collapsed()) {
|
2001-01-28 23:13:07 +03:00
|
|
|
// manipulating text attributes on a collapsed selection only sets state for the next text insertion
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsAutoEditBatch batchIt(this);
|
2012-08-12 22:28:26 +04:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::removeTextProperty, nsIEditor::eNext);
|
2001-01-28 23:13:07 +03:00
|
|
|
nsAutoSelectionReset selectionResetter(selection, this);
|
|
|
|
nsAutoTxnsConserveSelection dontSpazMySelection(this);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cancel, handled;
|
2012-08-12 22:28:26 +04:00
|
|
|
nsTextRulesInfo ruleInfo(EditAction::removeTextProperty);
|
2013-03-23 07:47:57 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
2001-01-28 23:13:07 +03:00
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
if (!cancel && !handled)
|
|
|
|
{
|
|
|
|
// loop thru the ranges in the selection
|
2015-02-25 19:08:00 +03:00
|
|
|
uint32_t rangeCount = selection->RangeCount();
|
2013-02-07 18:17:47 +04:00
|
|
|
for (uint32_t rangeIdx = 0; rangeIdx < rangeCount; ++rangeIdx) {
|
|
|
|
nsRefPtr<nsRange> range = selection->GetRangeAt(rangeIdx);
|
2014-08-29 15:43:24 +04:00
|
|
|
if (aProperty == nsGkAtoms::name) {
|
2002-04-11 17:33:30 +04:00
|
|
|
// promote range if it starts or end in a named anchor and we
|
|
|
|
// want to remove named anchors
|
|
|
|
res = PromoteRangeIfStartsOrEndsInNamedAnchor(range);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// adjust range to include any ancestors who's children are entirely selected
|
|
|
|
res = PromoteInlineRange(range);
|
|
|
|
}
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-04-11 17:33:30 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// remove this style from ancestors of our range endpoints,
|
|
|
|
// splitting them as appropriate
|
|
|
|
res = SplitStyleAboveRange(range, aProperty, aAttribute);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-06-12 18:04:47 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// check for easy case: both range endpoints in same text node
|
2015-04-19 15:28:50 +03:00
|
|
|
nsCOMPtr<nsINode> startNode = range->GetStartParent();
|
|
|
|
nsCOMPtr<nsINode> endNode = range->GetEndParent();
|
|
|
|
if (startNode && startNode == endNode && startNode->GetAsText()) {
|
2001-01-28 23:13:07 +03:00
|
|
|
// we're done with this range!
|
2015-04-19 15:28:50 +03:00
|
|
|
if (useCSS && mHTMLCSSUtils->IsCSSEditableProperty(startNode,
|
|
|
|
aProperty,
|
|
|
|
aAttribute)) {
|
2002-01-09 16:51:37 +03:00
|
|
|
// the HTML style defined by aProperty/aAttribute has a CSS equivalence
|
|
|
|
// in this implementation for startNode
|
2015-04-19 15:28:50 +03:00
|
|
|
if (mHTMLCSSUtils->IsCSSEquivalentToHTMLInlineStyleSet(startNode,
|
|
|
|
aProperty, aAttribute, EmptyString(),
|
|
|
|
nsHTMLCSSUtils::eComputed)) {
|
2002-01-09 16:51:37 +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
|
2004-06-17 04:13:25 +04:00
|
|
|
nsAutoString value; value.AssignLiteral("-moz-editor-invert-value");
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startOffset, endOffset;
|
2002-01-09 16:51:37 +03:00
|
|
|
range->GetStartOffset(&startOffset);
|
|
|
|
range->GetEndOffset(&endOffset);
|
|
|
|
if (mHTMLCSSUtils->IsCSSInvertable(aProperty, aAttribute)) {
|
2015-04-19 15:28:50 +03:00
|
|
|
SetInlinePropertyOnTextNode(*startNode->GetAsText(), startOffset,
|
|
|
|
endOffset, *aProperty, aAttribute,
|
|
|
|
value);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// not the easy case. range not contained in single text node.
|
2003-07-09 00:22:56 +04:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/subtree-content-iterator;1", &res);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(iter, NS_ERROR_FAILURE);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2002-11-12 22:40:11 +03:00
|
|
|
nsCOMArray<nsIDOMNode> arrayOfNodes;
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
|
|
|
|
// iterate range and build up array
|
|
|
|
iter->Init(range);
|
2004-01-24 03:46:17 +03:00
|
|
|
while (!iter->IsDone())
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
node = do_QueryInterface(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
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
if (IsEditable(node))
|
|
|
|
{
|
2002-11-12 22:40:11 +03:00
|
|
|
arrayOfNodes.AppendObject(node);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
|
|
|
iter->Next();
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// loop through the list, remove the property on each node
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t listCount = arrayOfNodes.Count();
|
|
|
|
int32_t j;
|
2001-01-28 23:13:07 +03:00
|
|
|
for (j = 0; j < listCount; j++)
|
|
|
|
{
|
2002-11-12 22:40:11 +03:00
|
|
|
node = arrayOfNodes[j];
|
2001-01-28 23:13:07 +03:00
|
|
|
res = RemoveStyleInside(node, aProperty, aAttribute);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
if (useCSS && mHTMLCSSUtils->IsCSSEditableProperty(node, aProperty, aAttribute)) {
|
|
|
|
// the HTML style defined by aProperty/aAttribute has a CSS equivalence
|
|
|
|
// in this implementation for node
|
|
|
|
nsAutoString cssValue;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isSet = false;
|
2012-07-06 15:50:07 +04:00
|
|
|
mHTMLCSSUtils->IsCSSEquivalentToHTMLInlineStyleSet(node, aProperty,
|
|
|
|
aAttribute, isSet , cssValue, nsHTMLCSSUtils::eComputed);
|
2002-01-09 16:51:37 +03:00
|
|
|
if (isSet) {
|
|
|
|
// 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
|
|
|
|
if (mHTMLCSSUtils->IsCSSInvertable(aProperty, aAttribute)) {
|
2004-06-17 04:13:25 +04:00
|
|
|
nsAutoString value; value.AssignLiteral("-moz-editor-invert-value");
|
2002-01-09 16:51:37 +03:00
|
|
|
SetInlinePropertyOnNode(node, aProperty, aAttribute, &value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2002-11-12 22:40:11 +03:00
|
|
|
arrayOfNodes.Clear();
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!cancel)
|
|
|
|
{
|
|
|
|
// post-process
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::IncreaseFontSize()
|
|
|
|
{
|
|
|
|
return RelativeFontChange(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::DecreaseFontSize()
|
|
|
|
{
|
|
|
|
return RelativeFontChange(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsHTMLEditor::RelativeFontChange( int32_t aSizeChange)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
|
|
|
// Can only change font size by + or - 1
|
|
|
|
if ( !( (aSizeChange==1) || (aSizeChange==-1) ) )
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
|
|
|
|
ForceCompositionEnd();
|
|
|
|
|
|
|
|
// Get the selection
|
2012-07-23 14:27:22 +04:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE);
|
2001-01-28 23:13:07 +03:00
|
|
|
// Is the selection collapsed?
|
|
|
|
// if it's collapsed set typing state
|
2012-05-18 12:29:39 +04:00
|
|
|
if (selection->Collapsed()) {
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIAtom> atom;
|
2014-08-29 15:43:24 +04:00
|
|
|
if (aSizeChange == 1) {
|
|
|
|
atom = nsGkAtoms::big;
|
|
|
|
} else {
|
|
|
|
atom = nsGkAtoms::small;
|
|
|
|
}
|
2002-01-09 16:51:37 +03:00
|
|
|
|
|
|
|
// Let's see in what kind of element the selection is
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t offset;
|
2014-11-02 15:04:13 +03:00
|
|
|
nsCOMPtr<nsINode> selectedNode;
|
2012-05-31 22:23:07 +04:00
|
|
|
GetStartNodeAndOffset(selection, getter_AddRefs(selectedNode), &offset);
|
2015-03-25 20:52:56 +03:00
|
|
|
if (selectedNode && IsTextNode(selectedNode)) {
|
2014-11-02 15:04:13 +03:00
|
|
|
selectedNode = selectedNode->GetParentNode();
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2015-03-25 20:52:56 +03:00
|
|
|
NS_ENSURE_TRUE(selectedNode, NS_OK);
|
2014-11-02 15:04:13 +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
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// manipulating text attributes on a collapsed selection only sets state for the next text insertion
|
2012-05-05 13:00:06 +04:00
|
|
|
mTypeInState->SetProp(atom, EmptyString(), EmptyString());
|
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// wrap with txn batching, rules sniffing, and selection preservation code
|
|
|
|
nsAutoEditBatch batchIt(this);
|
2012-08-12 22:28:26 +04:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::setTextProperty, nsIEditor::eNext);
|
2001-01-28 23:13:07 +03:00
|
|
|
nsAutoSelectionReset selectionResetter(selection, this);
|
|
|
|
nsAutoTxnsConserveSelection dontSpazMySelection(this);
|
|
|
|
|
|
|
|
// loop thru the ranges in the selection
|
2015-02-25 19:08:00 +03:00
|
|
|
uint32_t rangeCount = selection->RangeCount();
|
2013-02-07 18:17:47 +04:00
|
|
|
for (uint32_t rangeIdx = 0; rangeIdx < rangeCount; ++rangeIdx) {
|
|
|
|
nsRefPtr<nsRange> range = selection->GetRangeAt(rangeIdx);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// adjust range to include any ancestors who's children are entirely selected
|
2013-02-07 18:17:47 +04:00
|
|
|
nsresult res = PromoteInlineRange(range);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// check for easy case: both range endpoints in same text node
|
|
|
|
nsCOMPtr<nsIDOMNode> startNode, endNode;
|
|
|
|
res = range->GetStartContainer(getter_AddRefs(startNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
res = range->GetEndContainer(getter_AddRefs(endNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
if ((startNode == endNode) && IsTextNode(startNode))
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startOffset, endOffset;
|
2001-01-28 23:13:07 +03:00
|
|
|
range->GetStartOffset(&startOffset);
|
|
|
|
range->GetEndOffset(&endOffset);
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(startNode);
|
|
|
|
res = RelativeFontChangeOnTextNode(aSizeChange, nodeAsText, startOffset, endOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +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.
|
|
|
|
|
|
|
|
// lets 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).
|
|
|
|
|
2003-07-09 00:22:56 +04:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/subtree-content-iterator;1", &res);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(iter, NS_ERROR_FAILURE);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// iterate range and build up array
|
|
|
|
res = iter->Init(range);
|
2012-05-18 12:29:39 +04:00
|
|
|
if (NS_SUCCEEDED(res)) {
|
|
|
|
nsCOMArray<nsIContent> arrayOfNodes;
|
|
|
|
while (!iter->IsDone()) {
|
2012-06-10 16:39:21 +04:00
|
|
|
NS_ENSURE_TRUE(iter->GetCurrentNode()->IsContent(), NS_ERROR_FAILURE);
|
|
|
|
nsCOMPtr<nsIContent> node = iter->GetCurrentNode()->AsContent();
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-05-18 12:29:39 +04:00
|
|
|
if (IsEditable(node)) {
|
2002-11-12 22:40:11 +03:00
|
|
|
arrayOfNodes.AppendObject(node);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
iter->Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// now that we have the list, do the font size change on each node
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t listCount = arrayOfNodes.Count();
|
|
|
|
for (int32_t j = 0; j < listCount; ++j) {
|
2012-05-18 12:29:39 +04:00
|
|
|
nsIContent* node = arrayOfNodes[j];
|
2001-01-28 23:13:07 +03:00
|
|
|
res = RelativeFontChangeOnNode(aSizeChange, node);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2002-11-12 22:40:11 +03:00
|
|
|
arrayOfNodes.Clear();
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
// now check the start and end parents of the range to see if they need to
|
2003-03-24 07:15:34 +03:00
|
|
|
// be separately handled (they do if they are text nodes, due to how the
|
2001-01-28 23:13:07 +03:00
|
|
|
// subtree iterator works - it will not have reported them).
|
|
|
|
if (IsTextNode(startNode) && IsEditable(startNode))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(startNode);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startOffset;
|
|
|
|
uint32_t textLen;
|
2001-01-28 23:13:07 +03:00
|
|
|
range->GetStartOffset(&startOffset);
|
|
|
|
nodeAsText->GetLength(&textLen);
|
|
|
|
res = RelativeFontChangeOnTextNode(aSizeChange, nodeAsText, startOffset, textLen);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
if (IsTextNode(endNode) && IsEditable(endNode))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(endNode);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t endOffset;
|
2001-01-28 23:13:07 +03:00
|
|
|
range->GetEndOffset(&endOffset);
|
|
|
|
res = RelativeFontChangeOnTextNode(aSizeChange, nodeAsText, 0, endOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-07 18:17:47 +04:00
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsHTMLEditor::RelativeFontChangeOnTextNode( int32_t aSizeChange,
|
2001-01-28 23:13:07 +03:00
|
|
|
nsIDOMCharacterData *aTextNode,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aStartOffset,
|
|
|
|
int32_t aEndOffset)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
|
|
|
// Can only change font size by + or - 1
|
|
|
|
if ( !( (aSizeChange==1) || (aSizeChange==-1) ) )
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
2014-11-02 15:04:13 +03:00
|
|
|
nsCOMPtr<nsIContent> textNode = do_QueryInterface(aTextNode);
|
|
|
|
NS_ENSURE_TRUE(textNode, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2005-11-25 11:16:51 +03:00
|
|
|
// don't need to do anything if no characters actually selected
|
2001-01-28 23:13:07 +03:00
|
|
|
if (aStartOffset == aEndOffset) return NS_OK;
|
|
|
|
|
2014-11-02 15:04:13 +03:00
|
|
|
if (!textNode->GetParentNode() ||
|
|
|
|
!CanContainTag(*textNode->GetParentNode(), *nsGkAtoms::big)) {
|
2012-05-01 10:34:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-01-09 16:51:37 +03:00
|
|
|
|
2014-08-20 16:25:16 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
nsCOMPtr<nsIContent> node = do_QueryInterface(aTextNode);
|
|
|
|
NS_ENSURE_STATE(node);
|
2001-10-22 10:22:52 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// do we need to split the text node?
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t textLen;
|
2001-01-28 23:13:07 +03:00
|
|
|
aTextNode->GetLength(&textLen);
|
|
|
|
|
|
|
|
// -1 is a magic value meaning to the end of node
|
|
|
|
if (aEndOffset == -1) aEndOffset = textLen;
|
|
|
|
|
2014-11-02 15:04:13 +03:00
|
|
|
nsresult res = NS_OK;
|
2012-08-22 19:56:38 +04:00
|
|
|
if ( (uint32_t)aEndOffset != textLen )
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
|
|
|
// we need to split off back of text node
|
2014-08-20 16:25:16 +04:00
|
|
|
res = SplitNode(GetAsDOMNode(node), aEndOffset, getter_AddRefs(tmp));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2014-08-20 16:25:16 +04:00
|
|
|
// remember left node
|
|
|
|
node = do_QueryInterface(tmp);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
if ( aStartOffset )
|
|
|
|
{
|
|
|
|
// we need to split off front of text node
|
2014-08-20 16:25:16 +04:00
|
|
|
res = SplitNode(GetAsDOMNode(node), aStartOffset, getter_AddRefs(tmp));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2001-12-07 18:28:47 +03:00
|
|
|
|
2001-10-22 10:22:52 +04:00
|
|
|
// look for siblings that are correct type of node
|
2014-08-20 16:25:16 +04:00
|
|
|
nsIAtom* nodeType = aSizeChange == 1 ? nsGkAtoms::big : nsGkAtoms::small;
|
|
|
|
nsCOMPtr<nsIContent> sibling = GetPriorHTMLSibling(node);
|
2015-03-03 14:08:59 +03:00
|
|
|
if (sibling && sibling->IsHTMLElement(nodeType)) {
|
2001-10-22 10:22:52 +04:00
|
|
|
// previous sib is already right kind of inline node; slide this over into it
|
|
|
|
res = MoveNode(node, sibling, -1);
|
|
|
|
return res;
|
|
|
|
}
|
2014-08-20 16:25:16 +04:00
|
|
|
sibling = GetNextHTMLSibling(node);
|
2015-03-03 14:08:59 +03:00
|
|
|
if (sibling && sibling->IsHTMLElement(nodeType)) {
|
2001-10-22 10:22:52 +04:00
|
|
|
// following sib is already right kind of inline node; slide this over into it
|
|
|
|
res = MoveNode(node, sibling, 0);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2012-08-22 19:56:38 +04:00
|
|
|
nsHTMLEditor::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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// 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.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = aNode->GetChildCount(); i--; ) {
|
2012-07-06 00:33:10 +04:00
|
|
|
nsresult rv = RelativeFontChangeOnNode(aSizeChange, aNode->GetChildAt(i));
|
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.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = aNode->GetChildCount(); i--; ) {
|
2012-07-06 00:33:10 +04:00
|
|
|
nsresult rv = RelativeFontChangeHelper(aSizeChange, aNode->GetChildAt(i));
|
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
|
2014-08-20 16:25:16 +04:00
|
|
|
nsHTMLEditor::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
|
|
|
|
2012-05-01 10:34:52 +04:00
|
|
|
nsIAtom* atom;
|
|
|
|
if (aSizeChange == 1) {
|
|
|
|
atom = nsGkAtoms::big;
|
|
|
|
} else {
|
|
|
|
atom = nsGkAtoms::small;
|
|
|
|
}
|
2001-01-28 23:13:07 +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
|
|
|
|
// each getting their own.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = aNode->GetChildCount(); i--; ) {
|
2012-07-06 00:33:10 +04:00
|
|
|
nsresult rv = RelativeFontChangeOnNode(aSizeChange, aNode->GetChildAt(i));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::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
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult res;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool first, any, all;
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2002-01-14 23:30:11 +03:00
|
|
|
NS_NAMED_LITERAL_STRING(attr, "face");
|
2014-08-29 15:43:24 +04:00
|
|
|
res = GetInlinePropertyBase(nsGkAtoms::font, &attr, nullptr, &first, &any,
|
|
|
|
&all, &outFace);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
if (any && !all) return res; // mixed
|
|
|
|
if (all)
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*aMixed = false;
|
2001-01-28 23:13:07 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2003-06-12 18:51:37 +04:00
|
|
|
// if there is no font face, check for tt
|
2014-08-29 15:43:24 +04:00
|
|
|
res = GetInlinePropertyBase(nsGkAtoms::tt, nullptr, nullptr, &first, &any,
|
|
|
|
&all,nullptr);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
if (any && !all) return res; // 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
|
|
|
}
|
|
|
|
|
|
|
|
if (!any)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::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();
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
nsresult res;
|
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;
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2014-08-29 15:43:24 +04:00
|
|
|
res = GetInlinePropertyBase(nsGkAtoms::font, &colorStr, nullptr, &first,
|
|
|
|
&any, &all, &aOutColor);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
if (any && !all) return res; // mixed
|
|
|
|
if (all)
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*aMixed = false;
|
2001-01-28 23:13:07 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!any)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
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
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::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
|
|
|
|
HasNonEmptyAttribute(dom::Element* aElement, nsIAtom* 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
|
|
|
|
nsHTMLEditor::HasStyleOrIdOrClass(dom::Element* aElement)
|
|
|
|
{
|
|
|
|
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
|
2012-02-01 14:54:22 +04:00
|
|
|
nsHTMLEditor::RemoveElementIfNoStyleOrIdOrClass(nsIDOMNode* aElement)
|
2002-02-18 13:49:15 +03:00
|
|
|
{
|
2012-02-01 14:54:22 +04:00
|
|
|
nsCOMPtr<dom::Element> element = do_QueryInterface(aElement);
|
|
|
|
NS_ENSURE_TRUE(element, NS_ERROR_NULL_POINTER);
|
2002-02-18 13:49:15 +03:00
|
|
|
|
|
|
|
// early way out if node is not the right kind of element
|
2015-03-03 14:08:59 +03:00
|
|
|
if ((!element->IsHTMLElement(nsGkAtoms::span) &&
|
|
|
|
!element->IsHTMLElement(nsGkAtoms::font)) ||
|
2012-04-20 10:33:21 +04:00
|
|
|
HasStyleOrIdOrClass(element)) {
|
2002-02-18 13:49:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-02-01 14:54:22 +04:00
|
|
|
|
|
|
|
return RemoveContainer(element);
|
2002-02-18 13:49:15 +03:00
|
|
|
}
|