tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

HTMLEditorInlines.h (5251B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef HTMLEditorInlines_h
      7 #define HTMLEditorInlines_h
      8 
      9 #include "HTMLEditor.h"
     10 
     11 #include "EditorDOMPoint.h"
     12 #include "HTMLEditHelpers.h"
     13 #include "SelectionState.h"  // for RangeItem
     14 
     15 #include "ErrorList.h"  // for nsresult
     16 
     17 #include "mozilla/AlreadyAddRefed.h"
     18 #include "mozilla/Debug.h"
     19 #include "mozilla/RefPtr.h"
     20 
     21 #include "mozilla/dom/Element.h"
     22 
     23 #include "nsAtom.h"
     24 #include "nsGkAtoms.h"
     25 #include "nsIContent.h"
     26 #include "nsRange.h"
     27 #include "nsString.h"
     28 
     29 #include <ostream>
     30 
     31 namespace mozilla {
     32 
     33 using namespace dom;
     34 
     35 Result<CreateElementResult, nsresult>
     36 HTMLEditor::ReplaceContainerAndCloneAttributesWithTransaction(
     37    Element& aOldContainer, const nsAtom& aTagName) {
     38  return ReplaceContainerWithTransactionInternal(
     39      aOldContainer, aTagName, *nsGkAtoms::_empty, u""_ns, true);
     40 }
     41 
     42 Result<CreateElementResult, nsresult>
     43 HTMLEditor::ReplaceContainerWithTransaction(Element& aOldContainer,
     44                                            const nsAtom& aTagName,
     45                                            const nsAtom& aAttribute,
     46                                            const nsAString& aAttributeValue) {
     47  return ReplaceContainerWithTransactionInternal(
     48      aOldContainer, aTagName, aAttribute, aAttributeValue, false);
     49 }
     50 
     51 Result<CreateElementResult, nsresult>
     52 HTMLEditor::ReplaceContainerWithTransaction(Element& aOldContainer,
     53                                            const nsAtom& aTagName) {
     54  return ReplaceContainerWithTransactionInternal(
     55      aOldContainer, aTagName, *nsGkAtoms::_empty, u""_ns, false);
     56 }
     57 
     58 Result<MoveNodeResult, nsresult> HTMLEditor::MoveNodeToEndWithTransaction(
     59    nsIContent& aContentToMove, nsINode& aNewContainer) {
     60  return MoveNodeWithTransaction(aContentToMove,
     61                                 EditorDOMPoint::AtEndOf(aNewContainer));
     62 }
     63 
     64 Result<MoveNodeResult, nsresult> HTMLEditor::MoveSiblingsToEndWithTransaction(
     65    nsIContent& aFirstContentToMove, nsIContent& aLastContentToMove,
     66    nsINode& aNewContainer) {
     67  return MoveSiblingsWithTransaction(aFirstContentToMove, aLastContentToMove,
     68                                     EditorDOMPoint::AtEndOf(aNewContainer));
     69 }
     70 
     71 Element* HTMLEditor::GetTableCellElementAt(
     72    Element& aTableElement, const CellIndexes& aCellIndexes) const {
     73  return GetTableCellElementAt(aTableElement, aCellIndexes.mRow,
     74                               aCellIndexes.mColumn);
     75 }
     76 
     77 already_AddRefed<RangeItem>
     78 HTMLEditor::GetSelectedRangeItemForTopLevelEditSubAction() const {
     79  if (!mSelectedRangeForTopLevelEditSubAction) {
     80    mSelectedRangeForTopLevelEditSubAction = new RangeItem();
     81  }
     82  return do_AddRef(mSelectedRangeForTopLevelEditSubAction);
     83 }
     84 
     85 already_AddRefed<nsRange> HTMLEditor::GetChangedRangeForTopLevelEditSubAction()
     86    const {
     87  if (!mChangedRangeForTopLevelEditSubAction) {
     88    mChangedRangeForTopLevelEditSubAction = nsRange::Create(GetDocument());
     89  }
     90  return do_AddRef(mChangedRangeForTopLevelEditSubAction);
     91 }
     92 
     93 // static
     94 template <typename EditorDOMPointType>
     95 HTMLEditor::CharPointType HTMLEditor::GetPreviousCharPointType(
     96    const EditorDOMPointType& aPoint) {
     97  MOZ_ASSERT(aPoint.IsInTextNode());
     98  if (aPoint.IsStartOfContainer()) {
     99    return CharPointType::TextEnd;
    100  }
    101  if (aPoint.IsPreviousCharPreformattedNewLine()) {
    102    return CharPointType::PreformattedLineBreak;
    103  }
    104  if (EditorUtils::IsWhiteSpacePreformatted(
    105          *aPoint.template ContainerAs<Text>())) {
    106    return CharPointType::PreformattedChar;
    107  }
    108  if (aPoint.IsPreviousCharASCIISpace()) {
    109    return CharPointType::ASCIIWhiteSpace;
    110  }
    111  return aPoint.IsPreviousCharNBSP() ? CharPointType::NoBreakingSpace
    112                                     : CharPointType::VisibleChar;
    113 }
    114 
    115 // static
    116 template <typename EditorDOMPointType>
    117 HTMLEditor::CharPointType HTMLEditor::GetCharPointType(
    118    const EditorDOMPointType& aPoint) {
    119  MOZ_ASSERT(aPoint.IsInTextNode());
    120  if (aPoint.IsEndOfContainer()) {
    121    return CharPointType::TextEnd;
    122  }
    123  if (aPoint.IsCharPreformattedNewLine()) {
    124    return CharPointType::PreformattedLineBreak;
    125  }
    126  if (EditorUtils::IsWhiteSpacePreformatted(
    127          *aPoint.template ContainerAs<Text>())) {
    128    return CharPointType::PreformattedChar;
    129  }
    130  if (aPoint.IsCharASCIISpace()) {
    131    return CharPointType::ASCIIWhiteSpace;
    132  }
    133  return aPoint.IsCharNBSP() ? CharPointType::NoBreakingSpace
    134                             : CharPointType::VisibleChar;
    135 }
    136 
    137 /******************************************************************************
    138 * Logging utils
    139 ******************************************************************************/
    140 
    141 inline std::ostream& operator<<(
    142    std::ostream& aStream,
    143    const HTMLEditor::PreserveWhiteSpaceStyle aPreserveWhiteSpaceStyle) {
    144  aStream << "PreserveWhiteSpaceStyle::"
    145          << (aPreserveWhiteSpaceStyle ==
    146                      HTMLEditor::PreserveWhiteSpaceStyle::No
    147                  ? "No"
    148                  : "Yes");
    149  return aStream;
    150 }
    151 
    152 }  // namespace mozilla
    153 
    154 #endif  // HTMLEditorInlines_h