1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#ifndef EditorLineBreak_h
#define EditorLineBreak_h
#include "EditorDOMPoint.h"
#include "EditorForwards.h"
#include "EditorUtils.h"
#include "mozilla/Maybe.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/HTMLBRElement.h"
#include "mozilla/dom/Text.h"
#include "nsCOMPtr.h"
#include "nsDebug.h"
#include "nsGkAtoms.h"
#include "nsIContent.h"
namespace mozilla {
class AutoTrackLineBreak;
/******************************************************************************
* EditorLineBreakBase stores <br> or a preformatted line break position.
* This cannot represent no line break. Therefore, if a method may not return
* a line break, they need to use Maybe.
******************************************************************************/
template <typename ContentType>
class EditorLineBreakBase {
using SelfType = EditorLineBreakBase<ContentType>;
public:
using HTMLBRElement = dom::HTMLBRElement;
using Text = dom::Text;
explicit EditorLineBreakBase(const HTMLBRElement& aBRElement)
: mContent(const_cast<HTMLBRElement*>(&aBRElement)) {}
explicit EditorLineBreakBase(RefPtr<HTMLBRElement>&& aBRElement);
explicit EditorLineBreakBase(RefPtr<dom::Element>&& aBRElement);
explicit EditorLineBreakBase(nsCOMPtr<nsIContent>&& aBRElement);
EditorLineBreakBase(const Text& aText, uint32_t aOffset)
: mContent(const_cast<Text*>(&aText)), mOffsetInText(Some(aOffset)) {}
EditorLineBreakBase(RefPtr<Text>&& aText, uint32_t aOffset);
EditorLineBreakBase(nsCOMPtr<nsIContent>&& aText, uint32_t aOffset);
[[nodiscard]] static SelfType AtLastChar(const Text& aText) {
MOZ_RELEASE_ASSERT(aText.TextDataLength());
return SelfType(aText, aText.TextDataLength() - 1u);
}
[[nodiscard]] static SelfType AtLastChar(RefPtr<Text>&& aText) {
MOZ_RELEASE_ASSERT(aText);
MOZ_RELEASE_ASSERT(aText->TextDataLength());
const uint32_t lastCharIndex = aText->TextDataLength() - 1u;
return SelfType(std::forward<RefPtr<Text>>(aText), lastCharIndex);
}
[[nodiscard]] static SelfType AtLastChar(nsCOMPtr<nsIContent>&& aText) {
MOZ_RELEASE_ASSERT(aText);
MOZ_RELEASE_ASSERT(aText->IsText());
MOZ_RELEASE_ASSERT(aText->AsText()->TextDataLength());
const uint32_t lastCharIndex = aText->AsText()->TextDataLength() - 1u;
return SelfType(std::forward<nsCOMPtr<nsIContent>>(aText), lastCharIndex);
}
[[nodiscard]] bool IsInComposedDoc() const {
return mContent->IsInComposedDoc();
}
template <typename EditorDOMPointType>
[[nodiscard]] EditorDOMPointType To() const {
static_assert(std::is_same<EditorDOMPointType, EditorRawDOMPoint>::value ||
std::is_same<EditorDOMPointType, EditorDOMPoint>::value);
return mOffsetInText.isSome()
? EditorDOMPointType(mContent, mOffsetInText.value())
: EditorDOMPointType(mContent);
}
template <typename EditorDOMPointType>
[[nodiscard]] EditorDOMPointType After() const {
if (IsHTMLBRElement()) {
return EditorDOMPointType::After(BRElementRef());
}
if (mOffsetInText.value() + 1 < TextRef().TextDataLength()) {
return EditorDOMPointType(&TextRef(), mOffsetInText.value() + 1);
}
// If the line break is end of a Text node and it's followed by another
// Text, we should return start of the following Text.
if (Text* const followingText =
Text::FromNodeOrNull(TextRef().GetNextSibling())) {
return EditorDOMPointType(followingText, 0);
}
return EditorDOMPointType::After(TextRef());
}
template <typename EditorDOMPointType>
[[nodiscard]] EditorDOMPointType Before() const {
if (IsHTMLBRElement()) {
return EditorDOMPointType(&BRElementRef(),
dom::Selection::InterlinePosition::EndOfLine);
}
return To<EditorDOMPointType>();
}
[[nodiscard]] bool IsHTMLBRElement() const {
MOZ_ASSERT_IF(!mOffsetInText, mContent->IsHTMLElement(nsGkAtoms::br));
return mOffsetInText.isNothing();
}
[[nodiscard]] bool IsPreformattedLineBreak() const {
MOZ_ASSERT_IF(mOffsetInText, mContent->IsText());
return mOffsetInText.isSome();
}
[[nodiscard]] bool TextIsOnlyPreformattedLineBreak() const {
return IsPreformattedLineBreak() && !Offset() &&
TextRef().TextDataLength() == 1u;
}
[[nodiscard]] nsIContent& ContentRef() const { return *mContent; }
[[nodiscard]] HTMLBRElement& BRElementRef() const {
MOZ_DIAGNOSTIC_ASSERT(IsHTMLBRElement());
MOZ_DIAGNOSTIC_ASSERT(GetBRElement());
return *GetBRElement();
}
[[nodiscard]] HTMLBRElement* GetBRElement() const {
return HTMLBRElement::FromNode(mContent);
}
[[nodiscard]] Text& TextRef() const {
MOZ_DIAGNOSTIC_ASSERT(IsPreformattedLineBreak());
MOZ_DIAGNOSTIC_ASSERT(GetText());
return *GetText();
}
[[nodiscard]] Text* GetText() const { return Text::FromNode(mContent); }
[[nodiscard]] uint32_t Offset() const {
MOZ_ASSERT(IsPreformattedLineBreak());
return mOffsetInText.value();
}
[[nodiscard]] bool CharAtOffsetIsLineBreak() const {
MOZ_DIAGNOSTIC_ASSERT(IsPreformattedLineBreak());
return *mOffsetInText < TextRef().TextDataLength() &&
TextRef().DataBuffer().CharAt(*mOffsetInText) == '\n';
}
[[nodiscard]] bool IsDeletableFromComposedDoc() const {
if (IsPreformattedLineBreak()) {
return TextRef().IsEditable();
}
const nsIContent* const parent = BRElementRef().GetParent();
return parent && parent->IsEditable();
}
private:
ContentType mContent;
Maybe<uint32_t> mOffsetInText;
friend class AutoTrackLineBreak;
};
using EditorLineBreak = EditorLineBreakBase<nsCOMPtr<nsIContent>>;
using EditorRawLineBreak = EditorLineBreakBase<nsIContent*>;
template <>
inline EditorLineBreakBase<nsCOMPtr<nsIContent>>::EditorLineBreakBase(
RefPtr<HTMLBRElement>&& aBRElement)
: mContent(aBRElement.forget()) {
MOZ_RELEASE_ASSERT(mContent);
}
template <>
inline EditorLineBreakBase<nsCOMPtr<nsIContent>>::EditorLineBreakBase(
RefPtr<dom::Element>&& aBRElement)
: mContent(aBRElement.forget()) {
MOZ_RELEASE_ASSERT(mContent);
MOZ_RELEASE_ASSERT(mContent->IsHTMLElement(nsGkAtoms::br));
}
template <>
inline EditorLineBreakBase<nsCOMPtr<nsIContent>>::EditorLineBreakBase(
nsCOMPtr<nsIContent>&& aBRElement)
: mContent(aBRElement.forget()) {
MOZ_RELEASE_ASSERT(mContent);
MOZ_RELEASE_ASSERT(mContent->IsHTMLElement(nsGkAtoms::br));
}
template <>
inline EditorLineBreakBase<nsCOMPtr<nsIContent>>::EditorLineBreakBase(
RefPtr<Text>&& aText, uint32_t aOffset)
: mContent(std::move(aText)), mOffsetInText(Some(aOffset)) {
MOZ_RELEASE_ASSERT(mContent);
MOZ_ASSERT(EditorUtils::IsNewLinePreformatted(*mContent));
MOZ_RELEASE_ASSERT(GetText()->TextDataLength() > aOffset);
MOZ_RELEASE_ASSERT(CharAtOffsetIsLineBreak());
}
template <>
inline EditorLineBreakBase<nsCOMPtr<nsIContent>>::EditorLineBreakBase(
nsCOMPtr<nsIContent>&& aText, uint32_t aOffset)
: mContent(aText.forget()), mOffsetInText(Some(aOffset)) {
MOZ_RELEASE_ASSERT(mContent);
MOZ_RELEASE_ASSERT(mContent->IsText());
MOZ_ASSERT(EditorUtils::IsNewLinePreformatted(*mContent));
MOZ_RELEASE_ASSERT(TextRef().TextDataLength() > aOffset);
MOZ_ASSERT(CharAtOffsetIsLineBreak());
}
template <>
inline EditorLineBreakBase<nsIContent*>::EditorLineBreakBase(
const HTMLBRElement& aBRElement)
: mContent(const_cast<HTMLBRElement*>(&aBRElement)) {}
template <>
inline EditorLineBreakBase<nsIContent*>::EditorLineBreakBase(
RefPtr<HTMLBRElement>&& aBRElement)
: mContent(aBRElement) {
MOZ_RELEASE_ASSERT(mContent);
aBRElement = nullptr;
}
template <>
inline EditorLineBreakBase<nsIContent*>::EditorLineBreakBase(
RefPtr<dom::Element>&& aBRElement)
: mContent(aBRElement) {
MOZ_RELEASE_ASSERT(mContent);
MOZ_RELEASE_ASSERT(mContent->IsHTMLElement(nsGkAtoms::br));
aBRElement = nullptr;
}
template <>
inline EditorLineBreakBase<nsIContent*>::EditorLineBreakBase(
nsCOMPtr<nsIContent>&& aBRElement)
: mContent(aBRElement) {
MOZ_RELEASE_ASSERT(mContent);
MOZ_RELEASE_ASSERT(mContent->IsHTMLElement(nsGkAtoms::br));
aBRElement = nullptr;
}
template <>
inline EditorLineBreakBase<nsIContent*>::EditorLineBreakBase(
RefPtr<Text>&& aText, uint32_t aOffset)
: mContent(aText), mOffsetInText(Some(aOffset)) {
MOZ_RELEASE_ASSERT(mContent);
MOZ_ASSERT(EditorUtils::IsNewLinePreformatted(*mContent));
MOZ_RELEASE_ASSERT(GetText()->TextDataLength() > aOffset);
MOZ_RELEASE_ASSERT(CharAtOffsetIsLineBreak());
aText = nullptr;
}
template <>
inline EditorLineBreakBase<nsIContent*>::EditorLineBreakBase(
nsCOMPtr<nsIContent>&& aText, uint32_t aOffset)
: mContent(aText), mOffsetInText(Some(aOffset)) {
MOZ_RELEASE_ASSERT(mContent);
MOZ_RELEASE_ASSERT(mContent->IsText());
MOZ_ASSERT(EditorUtils::IsNewLinePreformatted(*mContent));
MOZ_RELEASE_ASSERT(GetText()->TextDataLength() > aOffset);
MOZ_ASSERT(CharAtOffsetIsLineBreak());
aText = nullptr;
}
class CreateLineBreakResult final : public CaretPoint {
public:
CreateLineBreakResult(const EditorLineBreak& aLineBreak,
const EditorDOMPoint& aCaretPoint)
: CaretPoint(aCaretPoint), mLineBreak(Some(aLineBreak)) {}
CreateLineBreakResult(EditorLineBreak&& aLineBreak,
const EditorDOMPoint& aCaretPoint)
: CaretPoint(aCaretPoint), mLineBreak(Some(std::move(aLineBreak))) {}
CreateLineBreakResult(const EditorLineBreak& aLineBreak,
EditorDOMPoint&& aCaretPoint)
: CaretPoint(aCaretPoint), mLineBreak(Some(aLineBreak)) {}
CreateLineBreakResult(EditorLineBreak&& aLineBreak,
EditorDOMPoint&& aCaretPoint)
: CaretPoint(std::move(aCaretPoint)),
mLineBreak(Some(std::move(aLineBreak))) {}
explicit CreateLineBreakResult(CreateElementResult&& aCreateElementResult)
: CaretPoint(aCreateElementResult.UnwrapCaretPoint()),
mLineBreak(Some(aCreateElementResult.UnwrapNewNode())) {}
[[nodiscard]] static CreateLineBreakResult NotHandled() {
return CreateLineBreakResult();
}
[[nodiscard]] constexpr bool Handled() const { return mLineBreak.isSome(); }
[[nodiscard]] constexpr const EditorLineBreak& LineBreakRef() const {
MOZ_ASSERT(Handled());
return mLineBreak.ref();
}
[[nodiscard]] constexpr const EditorLineBreak* operator->() const {
return &LineBreakRef();
}
// Shortcut for unclear methods of EditorLineBreak if `->` operator is used.
template <typename EditorDOMPointType>
[[nodiscard]] EditorDOMPointType AtLineBreak() const {
return LineBreakRef().To<EditorDOMPointType>();
}
template <typename EditorDOMPointType>
[[nodiscard]] EditorDOMPointType BeforeLineBreak() const {
return LineBreakRef().Before<EditorDOMPointType>();
}
template <typename EditorDOMPointType>
[[nodiscard]] EditorDOMPointType AfterLineBreak() const {
return LineBreakRef().After<EditorDOMPointType>();
}
[[nodiscard]] nsIContent& LineBreakContentRef() const {
return LineBreakRef().ContentRef();
}
[[nodiscard]] bool LineBreakIsInComposedDoc() const {
return LineBreakRef().IsInComposedDoc();
}
private:
CreateLineBreakResult() : CaretPoint(EditorDOMPoint()) {}
Maybe<EditorLineBreak> mLineBreak;
};
} // namespace mozilla
#endif // #ifndef EditorLineBreak_h
|