Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
/*
8
* Object that can be used to serialize selections, ranges, or nodes
9
* to strings in a gazillion different ways.
10
*/
11
12
#include "nsIDocumentEncoder.h"
13
14
#include <utility>
15
16
#include "nscore.h"
17
#include "nsISupports.h"
18
#include "mozilla/dom/Document.h"
19
#include "nsCOMPtr.h"
20
#include "nsIContentSerializer.h"
21
#include "mozilla/Encoding.h"
22
#include "nsIOutputStream.h"
23
#include "nsRange.h"
24
#include "nsGkAtoms.h"
25
#include "nsHTMLDocument.h"
26
#include "nsIContent.h"
27
#include "nsIScriptContext.h"
28
#include "nsIScriptGlobalObject.h"
29
#include "mozilla/dom/Selection.h"
30
#include "nsContentUtils.h"
31
#include "nsElementTable.h"
32
#include "nsUnicharUtils.h"
33
#include "nsReadableUtils.h"
34
#include "nsTArray.h"
35
#include "nsIFrame.h"
36
#include "nsStringBuffer.h"
37
#include "mozilla/dom/Comment.h"
38
#include "mozilla/dom/DocumentType.h"
39
#include "mozilla/dom/Element.h"
40
#include "mozilla/dom/HTMLBRElement.h"
41
#include "mozilla/dom/ProcessingInstruction.h"
42
#include "mozilla/dom/ShadowRoot.h"
43
#include "mozilla/dom/Text.h"
44
#include "nsLayoutUtils.h"
45
#include "mozilla/Maybe.h"
46
#include "mozilla/ScopeExit.h"
47
48
using namespace mozilla;
49
using namespace mozilla::dom;
50
51
enum nsRangeIterationDirection { kDirectionOut = -1, kDirectionIn = 1 };
52
53
class TextStreamer {
54
public:
55
/**
56
* @param aStream Will be kept alive by the TextStreamer.
57
* @param aUnicodeEncoder Needs to be non-nullptr.
58
*/
59
TextStreamer(nsIOutputStream& aStream, UniquePtr<Encoder> aUnicodeEncoder,
60
bool aIsPlainText, nsAString& aOutputBuffer);
61
62
/**
63
* String will be truncated if it is written to stream.
64
*/
65
nsresult FlushIfStringLongEnough();
66
67
/**
68
* String will be truncated.
69
*/
70
nsresult ForceFlush();
71
72
private:
73
const static uint32_t kMaxLengthBeforeFlush = 1024;
74
75
const static uint32_t kEncoderBufferSizeInBytes = 4096;
76
77
nsresult EncodeAndWrite();
78
79
nsresult EncodeAndWriteAndTruncate();
80
81
const nsCOMPtr<nsIOutputStream> mStream;
82
const UniquePtr<Encoder> mUnicodeEncoder;
83
const bool mIsPlainText;
84
nsAString& mOutputBuffer;
85
};
86
87
TextStreamer::TextStreamer(nsIOutputStream& aStream,
88
UniquePtr<Encoder> aUnicodeEncoder,
89
bool aIsPlainText, nsAString& aOutputBuffer)
90
: mStream{&aStream},
91
mUnicodeEncoder(std::move(aUnicodeEncoder)),
92
mIsPlainText(aIsPlainText),
93
mOutputBuffer(aOutputBuffer) {
94
MOZ_ASSERT(mUnicodeEncoder);
95
}
96
97
nsresult TextStreamer::FlushIfStringLongEnough() {
98
nsresult rv = NS_OK;
99
100
if (mOutputBuffer.Length() > kMaxLengthBeforeFlush) {
101
rv = EncodeAndWriteAndTruncate();
102
}
103
104
return rv;
105
}
106
107
nsresult TextStreamer::ForceFlush() { return EncodeAndWriteAndTruncate(); }
108
109
nsresult TextStreamer::EncodeAndWrite() {
110
if (mOutputBuffer.IsEmpty()) {
111
return NS_OK;
112
}
113
114
uint8_t buffer[kEncoderBufferSizeInBytes];
115
auto src = MakeSpan(mOutputBuffer);
116
auto bufferSpan = MakeSpan(buffer);
117
// Reserve space for terminator
118
auto dst = bufferSpan.To(bufferSpan.Length() - 1);
119
for (;;) {
120
uint32_t result;
121
size_t read;
122
size_t written;
123
bool hadErrors;
124
if (mIsPlainText) {
125
Tie(result, read, written) =
126
mUnicodeEncoder->EncodeFromUTF16WithoutReplacement(src, dst, false);
127
if (result != kInputEmpty && result != kOutputFull) {
128
// There's always room for one byte in the case of
129
// an unmappable character, because otherwise
130
// we'd have gotten `kOutputFull`.
131
dst[written++] = '?';
132
}
133
} else {
134
Tie(result, read, written, hadErrors) =
135
mUnicodeEncoder->EncodeFromUTF16(src, dst, false);
136
}
137
Unused << hadErrors;
138
src = src.From(read);
139
// Sadly, we still have test cases that implement nsIOutputStream in JS, so
140
// the buffer needs to be zero-terminated for XPConnect to do its thing.
141
// See bug 170416.
142
bufferSpan[written] = 0;
143
uint32_t streamWritten;
144
nsresult rv = mStream->Write(reinterpret_cast<char*>(dst.Elements()),
145
written, &streamWritten);
146
if (NS_FAILED(rv)) {
147
return rv;
148
}
149
if (result == kInputEmpty) {
150
return NS_OK;
151
}
152
}
153
}
154
155
nsresult TextStreamer::EncodeAndWriteAndTruncate() {
156
const nsresult rv = EncodeAndWrite();
157
mOutputBuffer.Truncate();
158
return rv;
159
}
160
161
/**
162
* The scope may be limited to either a selection, range, or node.
163
*/
164
class EncodingScope {
165
public:
166
/**
167
* @return true, iff the scope is limited to a selection, range or node.
168
*/
169
bool IsLimited() const;
170
171
RefPtr<Selection> mSelection;
172
RefPtr<nsRange> mRange;
173
nsCOMPtr<nsINode> mNode;
174
bool mNodeIsContainer = false;
175
};
176
177
bool EncodingScope::IsLimited() const { return mSelection || mRange || mNode; }
178
179
struct RangeBoundaryPathsAndOffsets {
180
using ContainerPath = AutoTArray<nsIContent*, 8>;
181
using ContainerOffsets = AutoTArray<int32_t, 8>;
182
183
// The first node is the range's boundary node, the following ones the
184
// ancestors.
185
ContainerPath mStartContainerPath;
186
// The first offset represents where at the boundary node the range starts.
187
// Each other offset is the index of the child relative to its parent.
188
ContainerOffsets mStartContainerOffsets;
189
190
// The first node is the range's boundary node, the following one the
191
// ancestors.
192
ContainerPath mEndContainerPath;
193
// The first offset represents where at the boundary node the range ends.
194
// Each other offset is the index of the child relative to its parent.
195
ContainerOffsets mEndContainerOffsets;
196
};
197
198
struct ContextInfoDepth {
199
uint32_t mStart = 0;
200
uint32_t mEnd = 0;
201
};
202
203
class nsDocumentEncoder : public nsIDocumentEncoder {
204
public:
205
nsDocumentEncoder();
206
207
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
208
NS_DECL_CYCLE_COLLECTION_CLASS(nsDocumentEncoder)
209
NS_DECL_NSIDOCUMENTENCODER
210
211
protected:
212
virtual ~nsDocumentEncoder();
213
214
void Initialize(bool aClearCachedSerializer = true);
215
216
/**
217
* @param aMaxLength As described at
218
* `nsIDocumentEncodder.encodeToStringWithMaxLength`.
219
*/
220
nsresult SerializeDependingOnScope(uint32_t aMaxLength);
221
222
nsresult SerializeSelection();
223
224
nsresult SerializeNode();
225
226
/**
227
* @param aMaxLength As described at
228
* `nsIDocumentEncodder.encodeToStringWithMaxLength`.
229
*/
230
nsresult SerializeWholeDocument(uint32_t aMaxLength);
231
232
nsresult SerializeNodeStart(nsINode& aOriginalNode, int32_t aStartOffset,
233
int32_t aEndOffset,
234
nsINode* aFixupNode = nullptr);
235
nsresult SerializeToStringRecursive(nsINode* aNode, bool aDontSerializeRoot,
236
uint32_t aMaxLength = 0);
237
nsresult SerializeNodeEnd(nsINode& aOriginalNode,
238
nsINode* aFixupNode = nullptr);
239
// This serializes the content of aNode.
240
nsresult SerializeToStringIterative(nsINode* aNode);
241
nsresult SerializeRangeToString(nsRange* aRange);
242
nsresult SerializeRangeNodes(nsRange* aRange, nsINode* aNode, int32_t aDepth);
243
nsresult SerializeRangeContextStart(const nsTArray<nsINode*>& aAncestorArray);
244
nsresult SerializeRangeContextEnd();
245
246
virtual int32_t GetImmediateContextCount(
247
const nsTArray<nsINode*>& aAncestorArray) {
248
return -1;
249
}
250
251
bool IsInvisibleNodeAndShouldBeSkipped(nsINode& aNode) const {
252
if (mFlags & SkipInvisibleContent) {
253
// Treat the visibility of the ShadowRoot as if it were
254
// the host content.
255
//
256
// FIXME(emilio): I suspect instead of this a bunch of the GetParent()
257
// calls here should be doing GetFlattenedTreeParent, then this condition
258
// should be unreachable...
259
nsINode* node{&aNode};
260
if (ShadowRoot* shadowRoot = ShadowRoot::FromNode(node)) {
261
node = shadowRoot->GetHost();
262
}
263
264
if (node->IsContent()) {
265
nsIFrame* frame = node->AsContent()->GetPrimaryFrame();
266
if (!frame) {
267
if (node->IsElement() && node->AsElement()->IsDisplayContents()) {
268
return false;
269
}
270
if (node->IsText()) {
271
// We have already checked that our parent is visible.
272
//
273
// FIXME(emilio): Text not assigned to a <slot> in Shadow DOM should
274
// probably return false...
275
return false;
276
}
277
if (node->IsHTMLElement(nsGkAtoms::rp)) {
278
// Ruby parentheses are part of ruby structure, hence
279
// shouldn't be stripped out even if it is not displayed.
280
return false;
281
}
282
return true;
283
}
284
bool isVisible = frame->StyleVisibility()->IsVisible();
285
if (!isVisible && node->IsText()) {
286
return true;
287
}
288
}
289
}
290
return false;
291
}
292
293
virtual bool IncludeInContext(nsINode* aNode);
294
295
void ReleaseDocumentReferenceAndInitialize(bool aClearCachedSerializer);
296
297
class MOZ_STACK_CLASS AutoReleaseDocumentIfNeeded final {
298
public:
299
explicit AutoReleaseDocumentIfNeeded(nsDocumentEncoder* aEncoder)
300
: mEncoder(aEncoder) {}
301
302
~AutoReleaseDocumentIfNeeded() {
303
if (mEncoder->mFlags & RequiresReinitAfterOutput) {
304
const bool clearCachedSerializer = false;
305
mEncoder->ReleaseDocumentReferenceAndInitialize(clearCachedSerializer);
306
}
307
}
308
309
private:
310
nsDocumentEncoder* mEncoder;
311
};
312
313
nsCOMPtr<Document> mDocument;
314
EncodingScope mEncodingScope;
315
nsCOMPtr<nsIContentSerializer> mSerializer;
316
Maybe<TextStreamer> mTextStreamer;
317
nsCOMPtr<nsINode> mCommonAncestorOfRange;
318
nsCOMPtr<nsIDocumentEncoderNodeFixup> mNodeFixup;
319
320
nsString mMimeType;
321
const Encoding* mEncoding;
322
uint32_t mFlags;
323
uint32_t mWrapColumn;
324
ContextInfoDepth mContextInfoDepth;
325
int32_t mStartRootIndex;
326
int32_t mEndRootIndex;
327
AutoTArray<nsINode*, 8> mCommonAncestors;
328
RangeBoundaryPathsAndOffsets mRangeBoundaryPathsAndOffsets;
329
AutoTArray<AutoTArray<nsINode*, 8>, 8> mRangeContexts;
330
// Whether the serializer cares about being notified to scan elements to
331
// keep track of whether they are preformatted. This stores the out
332
// argument of nsIContentSerializer::Init().
333
bool mNeedsPreformatScanning;
334
bool mHaltRangeHint;
335
// Used when context has already been serialized for
336
// table cell selections (where parent is <tr>)
337
bool mDisableContextSerialize;
338
bool mIsCopying; // Set to true only while copying
339
nsStringBuffer* mCachedBuffer;
340
};
341
342
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDocumentEncoder)
343
NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(
344
nsDocumentEncoder, ReleaseDocumentReferenceAndInitialize(true))
345
346
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDocumentEncoder)
347
NS_INTERFACE_MAP_ENTRY(nsIDocumentEncoder)
348
NS_INTERFACE_MAP_ENTRY(nsISupports)
349
NS_INTERFACE_MAP_END
350
351
NS_IMPL_CYCLE_COLLECTION(nsDocumentEncoder, mDocument,
352
mEncodingScope.mSelection, mEncodingScope.mRange,
353
mEncodingScope.mNode, mSerializer,
354
mCommonAncestorOfRange)
355
356
nsDocumentEncoder::nsDocumentEncoder()
357
: mEncoding(nullptr), mIsCopying(false), mCachedBuffer(nullptr) {
358
Initialize();
359
mMimeType.AssignLiteral("text/plain");
360
}
361
362
void nsDocumentEncoder::Initialize(bool aClearCachedSerializer) {
363
mFlags = 0;
364
mWrapColumn = 72;
365
mContextInfoDepth = {};
366
mStartRootIndex = 0;
367
mEndRootIndex = 0;
368
mNeedsPreformatScanning = false;
369
mHaltRangeHint = false;
370
mDisableContextSerialize = false;
371
mEncodingScope = {};
372
mCommonAncestorOfRange = nullptr;
373
mNodeFixup = nullptr;
374
mRangeBoundaryPathsAndOffsets = {};
375
if (aClearCachedSerializer) {
376
mSerializer = nullptr;
377
}
378
}
379
380
static bool ParentIsTR(nsIContent* aContent) {
381
mozilla::dom::Element* parent = aContent->GetParentElement();
382
if (!parent) {
383
return false;
384
}
385
return parent->IsHTMLElement(nsGkAtoms::tr);
386
}
387
388
nsresult nsDocumentEncoder::SerializeDependingOnScope(uint32_t aMaxLength) {
389
nsresult rv = NS_OK;
390
if (mEncodingScope.mSelection) {
391
rv = SerializeSelection();
392
} else if (nsRange* range = mEncodingScope.mRange) {
393
rv = SerializeRangeToString(range);
394
} else if (mEncodingScope.mNode) {
395
rv = SerializeNode();
396
} else {
397
rv = SerializeWholeDocument(aMaxLength);
398
}
399
400
mEncodingScope = {};
401
402
return rv;
403
}
404
405
nsresult nsDocumentEncoder::SerializeSelection() {
406
NS_ENSURE_TRUE(mEncodingScope.mSelection, NS_ERROR_FAILURE);
407
408
nsresult rv = NS_OK;
409
Selection* selection = mEncodingScope.mSelection;
410
uint32_t count = selection->RangeCount();
411
412
nsCOMPtr<nsINode> node;
413
nsCOMPtr<nsINode> prevNode;
414
uint32_t firstRangeStartDepth = 0;
415
for (uint32_t i = 0; i < count; ++i) {
416
RefPtr<nsRange> range = selection->GetRangeAt(i);
417
418
// Bug 236546: newlines not added when copying table cells into clipboard
419
// Each selected cell shows up as a range containing a row with a single
420
// cell get the row, compare it to previous row and emit </tr><tr> as
421
// needed Bug 137450: Problem copying/pasting a table from a web page to
422
// Excel. Each separate block of <tr></tr> produced above will be wrapped
423
// by the immediate context. This assumes that you can't select cells that
424
// are multiple selections from two tables simultaneously.
425
node = range->GetStartContainer();
426
NS_ENSURE_TRUE(node, NS_ERROR_FAILURE);
427
if (node != prevNode) {
428
if (prevNode) {
429
rv = SerializeNodeEnd(*prevNode);
430
NS_ENSURE_SUCCESS(rv, rv);
431
}
432
nsCOMPtr<nsIContent> content = do_QueryInterface(node);
433
if (content && content->IsHTMLElement(nsGkAtoms::tr) &&
434
!ParentIsTR(content)) {
435
if (!prevNode) {
436
// Went from a non-<tr> to a <tr>
437
mCommonAncestors.Clear();
438
nsContentUtils::GetAncestors(node->GetParentNode(), mCommonAncestors);
439
rv = SerializeRangeContextStart(mCommonAncestors);
440
NS_ENSURE_SUCCESS(rv, rv);
441
// Don't let SerializeRangeToString serialize the context again
442
mDisableContextSerialize = true;
443
}
444
445
rv = SerializeNodeStart(*node, 0, -1);
446
NS_ENSURE_SUCCESS(rv, rv);
447
prevNode = node;
448
} else if (prevNode) {
449
// Went from a <tr> to a non-<tr>
450
mDisableContextSerialize = false;
451
rv = SerializeRangeContextEnd();
452
NS_ENSURE_SUCCESS(rv, rv);
453
prevNode = nullptr;
454
}
455
}
456
457
rv = SerializeRangeToString(range);
458
NS_ENSURE_SUCCESS(rv, rv);
459
if (i == 0) {
460
firstRangeStartDepth = mContextInfoDepth.mStart;
461
}
462
}
463
mContextInfoDepth.mStart = firstRangeStartDepth;
464
465
if (prevNode) {
466
rv = SerializeNodeEnd(*prevNode);
467
NS_ENSURE_SUCCESS(rv, rv);
468
mDisableContextSerialize = false;
469
rv = SerializeRangeContextEnd();
470
NS_ENSURE_SUCCESS(rv, rv);
471
}
472
473
// Just to be safe
474
mDisableContextSerialize = false;
475
476
return rv;
477
}
478
479
nsresult nsDocumentEncoder::SerializeNode() {
480
NS_ENSURE_TRUE(mEncodingScope.mNode, NS_ERROR_FAILURE);
481
482
nsresult rv = NS_OK;
483
nsINode* node = mEncodingScope.mNode;
484
const bool nodeIsContainer = mEncodingScope.mNodeIsContainer;
485
if (!mNodeFixup && !(mFlags & SkipInvisibleContent) && !mTextStreamer &&
486
nodeIsContainer) {
487
rv = SerializeToStringIterative(node);
488
} else {
489
rv = SerializeToStringRecursive(node, nodeIsContainer);
490
}
491
492
return rv;
493
}
494
495
nsresult nsDocumentEncoder::SerializeWholeDocument(uint32_t aMaxLength) {
496
NS_ENSURE_FALSE(mEncodingScope.mSelection, NS_ERROR_FAILURE);
497
NS_ENSURE_FALSE(mEncodingScope.mRange, NS_ERROR_FAILURE);
498
NS_ENSURE_FALSE(mEncodingScope.mNode, NS_ERROR_FAILURE);
499
500
nsresult rv = mSerializer->AppendDocumentStart(mDocument);
501
NS_ENSURE_SUCCESS(rv, rv);
502
503
rv = SerializeToStringRecursive(mDocument, false, aMaxLength);
504
return rv;
505
}
506
507
nsDocumentEncoder::~nsDocumentEncoder() {
508
if (mCachedBuffer) {
509
mCachedBuffer->Release();
510
}
511
}
512
513
NS_IMETHODIMP
514
nsDocumentEncoder::Init(Document* aDocument, const nsAString& aMimeType,
515
uint32_t aFlags) {
516
return NativeInit(aDocument, aMimeType, aFlags);
517
}
518
519
NS_IMETHODIMP
520
nsDocumentEncoder::NativeInit(Document* aDocument, const nsAString& aMimeType,
521
uint32_t aFlags) {
522
if (!aDocument) return NS_ERROR_INVALID_ARG;
523
524
Initialize(!mMimeType.Equals(aMimeType));
525
526
mDocument = aDocument;
527
528
mMimeType = aMimeType;
529
530
mFlags = aFlags;
531
mIsCopying = false;
532
533
return NS_OK;
534
}
535
536
NS_IMETHODIMP
537
nsDocumentEncoder::SetWrapColumn(uint32_t aWC) {
538
mWrapColumn = aWC;
539
return NS_OK;
540
}
541
542
NS_IMETHODIMP
543
nsDocumentEncoder::SetSelection(Selection* aSelection) {
544
mEncodingScope.mSelection = aSelection;
545
return NS_OK;
546
}
547
548
NS_IMETHODIMP
549
nsDocumentEncoder::SetRange(nsRange* aRange) {
550
mEncodingScope.mRange = aRange;
551
return NS_OK;
552
}
553
554
NS_IMETHODIMP
555
nsDocumentEncoder::SetNode(nsINode* aNode) {
556
mEncodingScope.mNodeIsContainer = false;
557
mEncodingScope.mNode = aNode;
558
return NS_OK;
559
}
560
561
NS_IMETHODIMP
562
nsDocumentEncoder::SetContainerNode(nsINode* aContainer) {
563
mEncodingScope.mNodeIsContainer = true;
564
mEncodingScope.mNode = aContainer;
565
return NS_OK;
566
}
567
568
NS_IMETHODIMP
569
nsDocumentEncoder::SetCharset(const nsACString& aCharset) {
570
const Encoding* encoding = Encoding::ForLabel(aCharset);
571
if (!encoding) {
572
return NS_ERROR_UCONV_NOCONV;
573
}
574
mEncoding = encoding->OutputEncoding();
575
return NS_OK;
576
}
577
578
NS_IMETHODIMP
579
nsDocumentEncoder::GetMimeType(nsAString& aMimeType) {
580
aMimeType = mMimeType;
581
return NS_OK;
582
}
583
584
bool nsDocumentEncoder::IncludeInContext(nsINode* aNode) { return false; }
585
586
class FixupNodeDeterminer {
587
public:
588
FixupNodeDeterminer(nsIDocumentEncoderNodeFixup* aNodeFixup,
589
nsINode* aFixupNode, nsINode& aOriginalNode)
590
: mIsSerializationOfFixupChildrenNeeded{false},
591
mNodeFixup(aNodeFixup),
592
mOriginalNode(aOriginalNode) {
593
if (mNodeFixup) {
594
if (aFixupNode) {
595
mFixupNode = aFixupNode;
596
} else {
597
mNodeFixup->FixupNode(&mOriginalNode,
598
&mIsSerializationOfFixupChildrenNeeded,
599
getter_AddRefs(mFixupNode));
600
}
601
}
602
}
603
604
bool IsSerializationOfFixupChildrenNeeded() const {
605
return mIsSerializationOfFixupChildrenNeeded;
606
}
607
608
/**
609
* @return The fixup node, if available, otherwise the original node. The
610
* former is kept alive by this object.
611
*/
612
nsINode& GetFixupNodeFallBackToOriginalNode() const {
613
return mFixupNode ? *mFixupNode : mOriginalNode;
614
}
615
616
private:
617
bool mIsSerializationOfFixupChildrenNeeded;
618
nsIDocumentEncoderNodeFixup* mNodeFixup;
619
nsCOMPtr<nsINode> mFixupNode;
620
nsINode& mOriginalNode;
621
};
622
623
nsresult nsDocumentEncoder::SerializeNodeStart(nsINode& aOriginalNode,
624
int32_t aStartOffset,
625
int32_t aEndOffset,
626
nsINode* aFixupNode) {
627
if (mNeedsPreformatScanning) {
628
if (aOriginalNode.IsElement()) {
629
mSerializer->ScanElementForPreformat(aOriginalNode.AsElement());
630
} else if (aOriginalNode.IsText()) {
631
const nsCOMPtr<nsINode> parent = aOriginalNode.GetParent();
632
if (parent && parent->IsElement()) {
633
mSerializer->ScanElementForPreformat(parent->AsElement());
634
}
635
}
636
}
637
638
if (IsInvisibleNodeAndShouldBeSkipped(aOriginalNode)) {
639
return NS_OK;
640
}
641
642
FixupNodeDeterminer fixupNodeDeterminer{mNodeFixup, aFixupNode,
643
aOriginalNode};
644
nsINode* node = &fixupNodeDeterminer.GetFixupNodeFallBackToOriginalNode();
645
646
nsresult rv = NS_OK;
647
648
if (node->IsElement()) {
649
if ((mFlags & (nsIDocumentEncoder::OutputPreformatted |
650
nsIDocumentEncoder::OutputDropInvisibleBreak)) &&
651
nsLayoutUtils::IsInvisibleBreak(node)) {
652
return rv;
653
}
654
rv = mSerializer->AppendElementStart(node->AsElement(),
655
aOriginalNode.AsElement());
656
return rv;
657
}
658
659
switch (node->NodeType()) {
660
case nsINode::TEXT_NODE: {
661
rv = mSerializer->AppendText(static_cast<nsIContent*>(node), aStartOffset,
662
aEndOffset);
663
break;
664
}
665
case nsINode::CDATA_SECTION_NODE: {
666
rv = mSerializer->AppendCDATASection(static_cast<nsIContent*>(node),
667
aStartOffset, aEndOffset);
668
break;
669
}
670
case nsINode::PROCESSING_INSTRUCTION_NODE: {
671
rv = mSerializer->AppendProcessingInstruction(
672
static_cast<ProcessingInstruction*>(node), aStartOffset, aEndOffset);
673
break;
674
}
675
case nsINode::COMMENT_NODE: {
676
rv = mSerializer->AppendComment(static_cast<Comment*>(node), aStartOffset,
677
aEndOffset);
678
break;
679
}
680
case nsINode::DOCUMENT_TYPE_NODE: {
681
rv = mSerializer->AppendDoctype(static_cast<DocumentType*>(node));
682
break;
683
}
684
}
685
686
return rv;
687
}
688
689
nsresult nsDocumentEncoder::SerializeNodeEnd(nsINode& aOriginalNode,
690
nsINode* aFixupNode) {
691
if (mNeedsPreformatScanning) {
692
if (aOriginalNode.IsElement()) {
693
mSerializer->ForgetElementForPreformat(aOriginalNode.AsElement());
694
} else if (aOriginalNode.IsText()) {
695
const nsCOMPtr<nsINode> parent = aOriginalNode.GetParent();
696
if (parent && parent->IsElement()) {
697
mSerializer->ForgetElementForPreformat(parent->AsElement());
698
}
699
}
700
}
701
702
if (IsInvisibleNodeAndShouldBeSkipped(aOriginalNode)) {
703
return NS_OK;
704
}
705
706
nsresult rv = NS_OK;
707
708
FixupNodeDeterminer fixupNodeDeterminer{mNodeFixup, aFixupNode,
709
aOriginalNode};
710
nsINode* node = &fixupNodeDeterminer.GetFixupNodeFallBackToOriginalNode();
711
712
if (node->IsElement()) {
713
rv = mSerializer->AppendElementEnd(node->AsElement(),
714
aOriginalNode.AsElement());
715
}
716
717
return rv;
718
}
719
720
nsresult nsDocumentEncoder::SerializeToStringRecursive(nsINode* aNode,
721
bool aDontSerializeRoot,
722
uint32_t aMaxLength) {
723
uint32_t outputLength{0};
724
nsresult rv = mSerializer->GetOutputLength(outputLength);
725
NS_ENSURE_SUCCESS(rv, rv);
726
727
if (aMaxLength > 0 && outputLength >= aMaxLength) {
728
return NS_OK;
729
}
730
731
NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
732
733
if (IsInvisibleNodeAndShouldBeSkipped(*aNode)) {
734
return NS_OK;
735
}
736
737
FixupNodeDeterminer fixupNodeDeterminer{mNodeFixup, nullptr, *aNode};
738
nsINode* maybeFixedNode =
739
&fixupNodeDeterminer.GetFixupNodeFallBackToOriginalNode();
740
741
if (mFlags & SkipInvisibleContent) {
742
if (aNode->IsContent()) {
743
if (nsIFrame* frame = aNode->AsContent()->GetPrimaryFrame()) {
744
if (!frame->IsSelectable(nullptr)) {
745
aDontSerializeRoot = true;
746
}
747
}
748
}
749
}
750
751
if (!aDontSerializeRoot) {
752
int32_t endOffset = -1;
753
if (aMaxLength > 0) {
754
MOZ_ASSERT(aMaxLength >= outputLength);
755
endOffset = aMaxLength - outputLength;
756
}
757
rv = SerializeNodeStart(*aNode, 0, endOffset, maybeFixedNode);
758
NS_ENSURE_SUCCESS(rv, rv);
759
}
760
761
nsINode* node = fixupNodeDeterminer.IsSerializationOfFixupChildrenNeeded()
762
? maybeFixedNode
763
: aNode;
764
765
for (nsINode* child = node->GetFirstChildOfTemplateOrNode(); child;
766
child = child->GetNextSibling()) {
767
rv = SerializeToStringRecursive(child, false, aMaxLength);
768
NS_ENSURE_SUCCESS(rv, rv);
769
}
770
771
if (!aDontSerializeRoot) {
772
rv = SerializeNodeEnd(*aNode, maybeFixedNode);
773
NS_ENSURE_SUCCESS(rv, rv);
774
}
775
776
if (mTextStreamer) {
777
rv = mTextStreamer->FlushIfStringLongEnough();
778
}
779
780
return rv;
781
}
782
783
nsresult nsDocumentEncoder::SerializeToStringIterative(nsINode* aNode) {
784
nsresult rv;
785
786
nsINode* node = aNode->GetFirstChildOfTemplateOrNode();
787
while (node) {
788
nsINode* current = node;
789
rv = SerializeNodeStart(*current, 0, -1, current);
790
NS_ENSURE_SUCCESS(rv, rv);
791
node = current->GetFirstChildOfTemplateOrNode();
792
while (!node && current && current != aNode) {
793
rv = SerializeNodeEnd(*current);
794
NS_ENSURE_SUCCESS(rv, rv);
795
// Check if we have siblings.
796
node = current->GetNextSibling();
797
if (!node) {
798
// Perhaps parent node has siblings.
799
current = current->GetParentNode();
800
801
// Handle template element. If the parent is a template's content,
802
// then adjust the parent to be the template element.
803
if (current && current != aNode && current->IsDocumentFragment()) {
804
nsIContent* host = current->AsDocumentFragment()->GetHost();
805
if (host && host->IsHTMLElement(nsGkAtoms::_template)) {
806
current = host;
807
}
808
}
809
}
810
}
811
}
812
813
return NS_OK;
814
}
815
816
static bool IsTextNode(nsINode* aNode) { return aNode && aNode->IsText(); }
817
818
nsresult nsDocumentEncoder::SerializeRangeNodes(nsRange* const aRange,
819
nsINode* const aNode,
820
const int32_t aDepth) {
821
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
822
NS_ENSURE_TRUE(content, NS_ERROR_FAILURE);
823
824
if (IsInvisibleNodeAndShouldBeSkipped(*aNode)) {
825
return NS_OK;
826
}
827
828
nsresult rv = NS_OK;
829
830
// get start and end nodes for this recursion level
831
nsCOMPtr<nsIContent> startNode, endNode;
832
{
833
auto& startContainerPath =
834
mRangeBoundaryPathsAndOffsets.mStartContainerPath;
835
auto& endContainerPath = mRangeBoundaryPathsAndOffsets.mEndContainerPath;
836
int32_t start = mStartRootIndex - aDepth;
837
if (start >= 0 && (uint32_t)start <= startContainerPath.Length()) {
838
startNode = startContainerPath[start];
839
}
840
841
int32_t end = mEndRootIndex - aDepth;
842
if (end >= 0 && (uint32_t)end <= endContainerPath.Length()) {
843
endNode = endContainerPath[end];
844
}
845
}
846
847
if (startNode != content && endNode != content) {
848
// node is completely contained in range. Serialize the whole subtree
849
// rooted by this node.
850
rv = SerializeToStringRecursive(aNode, false);
851
NS_ENSURE_SUCCESS(rv, rv);
852
} else {
853
// due to implementation it is impossible for text node to be both start and
854
// end of range. We would have handled that case without getting here.
855
// XXXsmaug What does this all mean?
856
if (IsTextNode(aNode)) {
857
if (startNode == content) {
858
int32_t startOffset = aRange->StartOffset();
859
rv = SerializeNodeStart(*aNode, startOffset, -1);
860
NS_ENSURE_SUCCESS(rv, rv);
861
} else {
862
int32_t endOffset = aRange->EndOffset();
863
rv = SerializeNodeStart(*aNode, 0, endOffset);
864
NS_ENSURE_SUCCESS(rv, rv);
865
}
866
rv = SerializeNodeEnd(*aNode);
867
NS_ENSURE_SUCCESS(rv, rv);
868
} else {
869
if (aNode != mCommonAncestorOfRange) {
870
if (IncludeInContext(aNode)) {
871
// halt the incrementing of mContextInfoDepth. This is
872
// so paste client will include this node in paste.
873
mHaltRangeHint = true;
874
}
875
if ((startNode == content) && !mHaltRangeHint) {
876
++mContextInfoDepth.mStart;
877
}
878
if ((endNode == content) && !mHaltRangeHint) {
879
++mContextInfoDepth.mEnd;
880
}
881
882
// serialize the start of this node
883
rv = SerializeNodeStart(*aNode, 0, -1);
884
NS_ENSURE_SUCCESS(rv, rv);
885
}
886
887
const auto& startContainerOffsets =
888
mRangeBoundaryPathsAndOffsets.mStartContainerOffsets;
889
const auto& endContainerOffsets =
890
mRangeBoundaryPathsAndOffsets.mEndContainerOffsets;
891
// do some calculations that will tell us which children of this
892
// node are in the range.
893
int32_t startOffset = 0, endOffset = -1;
894
if (startNode == content && mStartRootIndex >= aDepth) {
895
startOffset = startContainerOffsets[mStartRootIndex - aDepth];
896
}
897
if (endNode == content && mEndRootIndex >= aDepth) {
898
endOffset = endContainerOffsets[mEndRootIndex - aDepth];
899
}
900
// generated content will cause offset values of -1 to be returned.
901
uint32_t childCount = content->GetChildCount();
902
903
if (startOffset == -1) startOffset = 0;
904
if (endOffset == -1)
905
endOffset = childCount;
906
else {
907
// if we are at the "tip" of the selection, endOffset is fine.
908
// otherwise, we need to add one. This is because of the semantics
909
// of the offset list created by GetAncestorsAndOffsets(). The
910
// intermediate points on the list use the endOffset of the
911
// location of the ancestor, rather than just past it. So we need
912
// to add one here in order to include it in the children we serialize.
913
if (aNode != aRange->GetEndContainer()) {
914
endOffset++;
915
}
916
}
917
918
if (endOffset) {
919
// serialize the children of this node that are in the range
920
nsIContent* childAsNode = content->GetFirstChild();
921
int32_t j = 0;
922
923
for (; j < startOffset && childAsNode; ++j) {
924
childAsNode = childAsNode->GetNextSibling();
925
}
926
927
MOZ_ASSERT(j == startOffset);
928
929
for (; childAsNode && j < endOffset; ++j) {
930
if ((j == startOffset) || (j == endOffset - 1)) {
931
rv = SerializeRangeNodes(aRange, childAsNode, aDepth + 1);
932
} else {
933
rv = SerializeToStringRecursive(childAsNode, false);
934
}
935
936
NS_ENSURE_SUCCESS(rv, rv);
937
childAsNode = childAsNode->GetNextSibling();
938
}
939
}
940
941
// serialize the end of this node
942
if (aNode != mCommonAncestorOfRange) {
943
rv = SerializeNodeEnd(*aNode);
944
NS_ENSURE_SUCCESS(rv, rv);
945
}
946
}
947
}
948
return NS_OK;
949
}
950
951
nsresult nsDocumentEncoder::SerializeRangeContextStart(
952
const nsTArray<nsINode*>& aAncestorArray) {
953
if (mDisableContextSerialize) {
954
return NS_OK;
955
}
956
957
AutoTArray<nsINode*, 8>* serializedContext = mRangeContexts.AppendElement();
958
959
int32_t i = aAncestorArray.Length(), j;
960
nsresult rv = NS_OK;
961
962
// currently only for table-related elements; see Bug 137450
963
j = GetImmediateContextCount(aAncestorArray);
964
965
while (i > 0) {
966
nsINode* node = aAncestorArray.ElementAt(--i);
967
968
if (!node) break;
969
970
// Either a general inclusion or as immediate context
971
if (IncludeInContext(node) || i < j) {
972
rv = SerializeNodeStart(*node, 0, -1);
973
serializedContext->AppendElement(node);
974
if (NS_FAILED(rv)) break;
975
}
976
}
977
978
return rv;
979
}
980
981
nsresult nsDocumentEncoder::SerializeRangeContextEnd() {
982
if (mDisableContextSerialize) {
983
return NS_OK;
984
}
985
986
MOZ_RELEASE_ASSERT(!mRangeContexts.IsEmpty(),
987
"Tried to end context without starting one.");
988
AutoTArray<nsINode*, 8>& serializedContext = mRangeContexts.LastElement();
989
990
nsresult rv = NS_OK;
991
for (nsINode* node : Reversed(serializedContext)) {
992
rv = SerializeNodeEnd(*node);
993
994
if (NS_FAILED(rv)) break;
995
}
996
997
mRangeContexts.RemoveLastElement();
998
return rv;
999
}
1000
1001
nsresult nsDocumentEncoder::SerializeRangeToString(nsRange* aRange) {
1002
if (!aRange || aRange->Collapsed()) return NS_OK;
1003
1004
mCommonAncestorOfRange = aRange->GetCommonAncestor();
1005
1006
if (!mCommonAncestorOfRange) {
1007
return NS_OK;
1008
}
1009
1010
nsINode* startContainer = aRange->GetStartContainer();
1011
NS_ENSURE_TRUE(startContainer, NS_ERROR_FAILURE);
1012
int32_t startOffset = aRange->StartOffset();
1013
1014
nsINode* endContainer = aRange->GetEndContainer();
1015
NS_ENSURE_TRUE(endContainer, NS_ERROR_FAILURE);
1016
int32_t endOffset = aRange->EndOffset();
1017
1018
mContextInfoDepth = {};
1019
mCommonAncestors.Clear();
1020
1021
mRangeBoundaryPathsAndOffsets = {};
1022
auto& startContainerPath = mRangeBoundaryPathsAndOffsets.mStartContainerPath;
1023
auto& startContainerOffsets =
1024
mRangeBoundaryPathsAndOffsets.mStartContainerOffsets;
1025
auto& endContainerPath = mRangeBoundaryPathsAndOffsets.mEndContainerPath;
1026
auto& endContainerOffsets =
1027
mRangeBoundaryPathsAndOffsets.mEndContainerOffsets;
1028
1029
nsContentUtils::GetAncestors(mCommonAncestorOfRange, mCommonAncestors);
1030
nsContentUtils::GetAncestorsAndOffsets(
1031
startContainer, startOffset, &startContainerPath, &startContainerOffsets);
1032
nsContentUtils::GetAncestorsAndOffsets(
1033
endContainer, endOffset, &endContainerPath, &endContainerOffsets);
1034
1035
nsCOMPtr<nsIContent> commonContent =
1036
do_QueryInterface(mCommonAncestorOfRange);
1037
mStartRootIndex = startContainerPath.IndexOf(commonContent);
1038
mEndRootIndex = endContainerPath.IndexOf(commonContent);
1039
1040
nsresult rv = NS_OK;
1041
1042
rv = SerializeRangeContextStart(mCommonAncestors);
1043
NS_ENSURE_SUCCESS(rv, rv);
1044
1045
if (startContainer == endContainer && IsTextNode(startContainer)) {
1046
if (mFlags & SkipInvisibleContent) {
1047
// Check that the parent is visible if we don't a frame.
1048
// IsInvisibleNodeAndShouldBeSkipped() will do it when there's a frame.
1049
nsCOMPtr<nsIContent> content = do_QueryInterface(startContainer);
1050
if (content && !content->GetPrimaryFrame()) {
1051
nsIContent* parent = content->GetParent();
1052
if (!parent || IsInvisibleNodeAndShouldBeSkipped(*parent)) {
1053
return NS_OK;
1054
}
1055
}
1056
}
1057
rv = SerializeNodeStart(*startContainer, startOffset, endOffset);
1058
NS_ENSURE_SUCCESS(rv, rv);
1059
rv = SerializeNodeEnd(*startContainer);
1060
NS_ENSURE_SUCCESS(rv, rv);
1061
} else {
1062
rv = SerializeRangeNodes(aRange, mCommonAncestorOfRange, 0);
1063
NS_ENSURE_SUCCESS(rv, rv);
1064
}
1065
rv = SerializeRangeContextEnd();
1066
NS_ENSURE_SUCCESS(rv, rv);
1067
1068
return rv;
1069
}
1070
1071
void nsDocumentEncoder::ReleaseDocumentReferenceAndInitialize(
1072
bool aClearCachedSerializer) {
1073
mDocument = nullptr;
1074
1075
Initialize(aClearCachedSerializer);
1076
}
1077
1078
NS_IMETHODIMP
1079
nsDocumentEncoder::EncodeToString(nsAString& aOutputString) {
1080
return EncodeToStringWithMaxLength(0, aOutputString);
1081
}
1082
1083
NS_IMETHODIMP
1084
nsDocumentEncoder::EncodeToStringWithMaxLength(uint32_t aMaxLength,
1085
nsAString& aOutputString) {
1086
MOZ_ASSERT(mRangeContexts.IsEmpty(), "Re-entrant call to nsDocumentEncoder.");
1087
auto rangeContextGuard = MakeScopeExit([&] { mRangeContexts.Clear(); });
1088
1089
if (!mDocument) return NS_ERROR_NOT_INITIALIZED;
1090
1091
AutoReleaseDocumentIfNeeded autoReleaseDocument(this);
1092
1093
aOutputString.Truncate();
1094
1095
nsString output;
1096
static const size_t kStringBufferSizeInBytes = 2048;
1097
if (!mCachedBuffer) {
1098
mCachedBuffer = nsStringBuffer::Alloc(kStringBufferSizeInBytes).take();
1099
if (NS_WARN_IF(!mCachedBuffer)) {
1100
return NS_ERROR_OUT_OF_MEMORY;
1101
}
1102
}
1103
NS_ASSERTION(
1104
!mCachedBuffer->IsReadonly(),
1105
"nsIDocumentEncoder shouldn't keep reference to non-readonly buffer!");
1106
static_cast<char16_t*>(mCachedBuffer->Data())[0] = char16_t(0);
1107
mCachedBuffer->ToString(0, output, true);
1108
// output owns the buffer now!
1109
mCachedBuffer = nullptr;
1110
1111
if (!mSerializer) {
1112
nsAutoCString progId(NS_CONTENTSERIALIZER_CONTRACTID_PREFIX);
1113
AppendUTF16toUTF8(mMimeType, progId);
1114
1115
mSerializer = do_CreateInstance(progId.get());
1116
NS_ENSURE_TRUE(mSerializer, NS_ERROR_NOT_IMPLEMENTED);
1117
}
1118
1119
nsresult rv = NS_OK;
1120
1121
bool rewriteEncodingDeclaration =
1122
!mEncodingScope.IsLimited() &&
1123
!(mFlags & OutputDontRewriteEncodingDeclaration);
1124
mSerializer->Init(mFlags, mWrapColumn, mEncoding, mIsCopying,
1125
rewriteEncodingDeclaration, &mNeedsPreformatScanning,
1126
output);
1127
1128
rv = SerializeDependingOnScope(aMaxLength);
1129
NS_ENSURE_SUCCESS(rv, rv);
1130
1131
rv = mSerializer->FlushAndFinish();
1132
1133
mCachedBuffer = nsStringBuffer::FromString(output);
1134
// We have to be careful how we set aOutputString, because we don't
1135
// want it to end up sharing mCachedBuffer if we plan to reuse it.
1136
bool setOutput = false;
1137
// Try to cache the buffer.
1138
if (mCachedBuffer) {
1139
if ((mCachedBuffer->StorageSize() == kStringBufferSizeInBytes) &&
1140
!mCachedBuffer->IsReadonly()) {
1141
mCachedBuffer->AddRef();
1142
} else {
1143
if (NS_SUCCEEDED(rv)) {
1144
mCachedBuffer->ToString(output.Length(), aOutputString);
1145
setOutput = true;
1146
}
1147
mCachedBuffer = nullptr;
1148
}
1149
}
1150
1151
if (!setOutput && NS_SUCCEEDED(rv)) {
1152
aOutputString.Append(output.get(), output.Length());
1153
}
1154
1155
return rv;
1156
}
1157
1158
NS_IMETHODIMP
1159
nsDocumentEncoder::EncodeToStream(nsIOutputStream* aStream) {
1160
MOZ_ASSERT(mRangeContexts.IsEmpty(), "Re-entrant call to nsDocumentEncoder.");
1161
auto rangeContextGuard = MakeScopeExit([&] { mRangeContexts.Clear(); });
1162
NS_ENSURE_ARG_POINTER(aStream);
1163
1164
nsresult rv = NS_OK;
1165
1166
if (!mDocument) return NS_ERROR_NOT_INITIALIZED;
1167
1168
if (!mEncoding) {
1169
return NS_ERROR_UCONV_NOCONV;
1170
}
1171
1172
nsAutoString buf;
1173
const bool isPlainText = mMimeType.LowerCaseEqualsLiteral(kTextMime);
1174
mTextStreamer.emplace(*aStream, mEncoding->NewEncoder(), isPlainText, buf);
1175
1176
rv = EncodeToString(buf);
1177
1178
// Force a flush of the last chunk of data.
1179
rv = mTextStreamer->ForceFlush();
1180
NS_ENSURE_SUCCESS(rv, rv);
1181
1182
mTextStreamer.reset();
1183
1184
return rv;
1185
}
1186
1187
NS_IMETHODIMP
1188
nsDocumentEncoder::EncodeToStringWithContext(nsAString& aContextString,
1189
nsAString& aInfoString,
1190
nsAString& aEncodedString) {
1191
return NS_ERROR_NOT_IMPLEMENTED;
1192
}
1193
1194
NS_IMETHODIMP
1195
nsDocumentEncoder::SetNodeFixup(nsIDocumentEncoderNodeFixup* aFixup) {
1196
mNodeFixup = aFixup;
1197
return NS_OK;
1198
}
1199
1200
bool do_getDocumentTypeSupportedForEncoding(const char* aContentType) {
1201
if (!nsCRT::strcmp(aContentType, "text/xml") ||
1202
!nsCRT::strcmp(aContentType, "application/xml") ||
1203
!nsCRT::strcmp(aContentType, "application/xhtml+xml") ||
1204
!nsCRT::strcmp(aContentType, "image/svg+xml") ||
1205
!nsCRT::strcmp(aContentType, "text/html") ||
1206
!nsCRT::strcmp(aContentType, "text/plain")) {
1207
return true;
1208
}
1209
return false;
1210
}
1211
1212
already_AddRefed<nsIDocumentEncoder> do_createDocumentEncoder(
1213
const char* aContentType) {
1214
if (do_getDocumentTypeSupportedForEncoding(aContentType)) {
1215
return do_AddRef(new nsDocumentEncoder);
1216
}
1217
return nullptr;
1218
}
1219
1220
class nsHTMLCopyEncoder : public nsDocumentEncoder {
1221
public:
1222
nsHTMLCopyEncoder();
1223
virtual ~nsHTMLCopyEncoder();
1224
1225
NS_IMETHOD Init(Document* aDocument, const nsAString& aMimeType,
1226
uint32_t aFlags) override;
1227
1228
// overridden methods from nsDocumentEncoder
1229
MOZ_CAN_RUN_SCRIPT_BOUNDARY
1230
NS_IMETHOD SetSelection(Selection* aSelection) override;
1231
NS_IMETHOD EncodeToStringWithContext(nsAString& aContextString,
1232
nsAString& aInfoString,
1233
nsAString& aEncodedString) override;
1234
NS_IMETHOD EncodeToString(nsAString& aOutputString) override;
1235
1236
protected:
1237
enum Endpoint { kStart, kEnd };
1238
1239
nsresult PromoteRange(nsRange* inRange);
1240
nsresult PromoteAncestorChain(nsCOMPtr<nsINode>* ioNode,
1241
int32_t* ioStartOffset, int32_t* ioEndOffset);
1242
nsresult GetPromotedPoint(Endpoint aWhere, nsINode* aNode, int32_t aOffset,
1243
nsCOMPtr<nsINode>* outNode, int32_t* outOffset,
1244
nsINode* aCommon);
1245
static nsCOMPtr<nsINode> GetChildAt(nsINode* aParent, int32_t aOffset);
1246
static bool IsMozBR(Element* aNode);
1247
static nsresult GetNodeLocation(nsINode* inChild,
1248
nsCOMPtr<nsINode>* outParent,
1249
int32_t* outOffset);
1250
bool IsRoot(nsINode* aNode);
1251
static bool IsFirstNode(nsINode* aNode);
1252
static bool IsLastNode(nsINode* aNode);
1253
virtual bool IncludeInContext(nsINode* aNode) override;
1254
virtual int32_t GetImmediateContextCount(
1255
const nsTArray<nsINode*>& aAncestorArray) override;
1256
1257
bool mIsTextWidget;
1258
};
1259
1260
nsHTMLCopyEncoder::nsHTMLCopyEncoder() { mIsTextWidget = false; }
1261
1262
nsHTMLCopyEncoder::~nsHTMLCopyEncoder() {}
1263
1264
NS_IMETHODIMP
1265
nsHTMLCopyEncoder::Init(Document* aDocument, const nsAString& aMimeType,
1266
uint32_t aFlags) {
1267
if (!aDocument) return NS_ERROR_INVALID_ARG;
1268
1269
mIsTextWidget = false;
1270
Initialize();
1271
1272
mIsCopying = true;
1273
mDocument = aDocument;
1274
1275
// Hack, hack! Traditionally, the caller passes text/unicode, which is
1276
// treated as "guess text/html or text/plain" in this context. (It has a
1277
// different meaning in other contexts. Sigh.) From now on, "text/plain"
1278
// means forcing text/plain instead of guessing.
1279
if (aMimeType.EqualsLiteral("text/plain")) {
1280
mMimeType.AssignLiteral("text/plain");
1281
} else {
1282
mMimeType.AssignLiteral("text/html");
1283
}
1284
1285
// Make all links absolute when copying
1286
// (see related bugs #57296, #41924, #58646, #32768)
1287
mFlags = aFlags | OutputAbsoluteLinks;
1288
1289
if (!mDocument->IsScriptEnabled()) mFlags |= OutputNoScriptContent;
1290
1291
return NS_OK;
1292
}
1293
1294
NS_IMETHODIMP
1295
nsHTMLCopyEncoder::SetSelection(Selection* aSelection) {
1296
// check for text widgets: we need to recognize these so that
1297
// we don't tweak the selection to be outside of the magic
1298
// div that ender-lite text widgets are embedded in.
1299
1300
if (!aSelection) return NS_ERROR_NULL_POINTER;
1301
1302
uint32_t rangeCount = aSelection->RangeCount();
1303
1304
// if selection is uninitialized return
1305
if (!rangeCount) {
1306
return NS_ERROR_FAILURE;
1307
}
1308
1309
// we'll just use the common parent of the first range. Implicit assumption
1310
// here that multi-range selections are table cell selections, in which case
1311
// the common parent is somewhere in the table and we don't really care where.
1312
//
1313
// FIXME(emilio, bug 1455894): This assumption is already wrong, and will
1314
// probably be more wrong in a Shadow DOM world...
1315
//
1316
// We should be able to write this as "Find the common ancestor of the
1317
// selection, then go through the flattened tree and serialize the selected
1318
// nodes", effectively serializing the composed tree.
1319
RefPtr<nsRange> range = aSelection->GetRangeAt(0);
1320
nsINode* commonParent = range->GetCommonAncestor();
1321
1322
for (nsCOMPtr<nsIContent> selContent(do_QueryInterface(commonParent));
1323
selContent; selContent = selContent->GetParent()) {
1324
// checking for selection inside a plaintext form widget
1325
if (selContent->IsAnyOfHTMLElements(nsGkAtoms::input,
1326
nsGkAtoms::textarea)) {
1327
mIsTextWidget = true;
1328
break;
1329
}
1330
}
1331
1332
// normalize selection if we are not in a widget
1333
if (mIsTextWidget) {
1334
mEncodingScope.mSelection = aSelection;
1335
mMimeType.AssignLiteral("text/plain");
1336
return NS_OK;
1337
}
1338
1339
// XXX We should try to get rid of the Selection object here.
1340
// XXX bug 1245883
1341
1342
// also consider ourselves in a text widget if we can't find an html document
1343
if (!(mDocument && mDocument->IsHTMLDocument())) {
1344
mIsTextWidget = true;
1345
mEncodingScope.mSelection = aSelection;
1346
// mMimeType is set to text/plain when encoding starts.
1347
return NS_OK;
1348
}
1349
1350
// there's no Clone() for selection! fix...
1351
// nsresult rv = aSelection->Clone(getter_AddRefs(mSelection);
1352
// NS_ENSURE_SUCCESS(rv, rv);
1353
mEncodingScope.mSelection = new Selection();
1354
1355
// loop thru the ranges in the selection
1356
for (uint32_t rangeIdx = 0; rangeIdx < rangeCount; ++rangeIdx) {
1357
range = aSelection->GetRangeAt(rangeIdx);
1358
NS_ENSURE_TRUE(range, NS_ERROR_FAILURE);
1359
RefPtr<nsRange> myRange = range->CloneRange();
1360
MOZ_ASSERT(myRange);
1361
1362
// adjust range to include any ancestors who's children are entirely
1363
// selected
1364
nsresult rv = PromoteRange(myRange);
1365
NS_ENSURE_SUCCESS(rv, rv);
1366
1367
ErrorResult result;
1368
RefPtr<Selection> selection(mEncodingScope.mSelection);
1369
RefPtr<Document> document(mDocument);
1370
selection->AddRangeAndSelectFramesAndNotifyListeners(*myRange, document,
1371
result);
1372
rv = result.StealNSResult();
1373
NS_ENSURE_SUCCESS(rv, rv);
1374
}
1375
1376
return NS_OK;
1377
}
1378
1379
NS_IMETHODIMP
1380
nsHTMLCopyEncoder::EncodeToString(nsAString& aOutputString) {
1381
if (mIsTextWidget) {
1382
mMimeType.AssignLiteral("text/plain");
1383
}
1384
return nsDocumentEncoder::EncodeToString(aOutputString);
1385
}
1386
1387
NS_IMETHODIMP
1388
nsHTMLCopyEncoder::EncodeToStringWithContext(nsAString& aContextString,
1389
nsAString& aInfoString,
1390
nsAString& aEncodedString) {
1391
nsresult rv = EncodeToString(aEncodedString);
1392
NS_ENSURE_SUCCESS(rv, rv);
1393
1394
// do not encode any context info or range hints if we are in a text widget.
1395
if (mIsTextWidget) return NS_OK;
1396
1397
// now encode common ancestors into aContextString. Note that the common
1398
// ancestors will be for the last range in the selection in the case of
1399
// multirange selections. encoding ancestors every range in a multirange
1400
// selection in a way that could be understood by the paste code would be a
1401
// lot more work to do. As a practical matter, selections are single range,
1402
// and the ones that aren't are table cell selections where all the cells are
1403
// in the same table.
1404
1405
mSerializer->Init(mFlags, mWrapColumn, mEncoding, mIsCopying, false,
1406
&mNeedsPreformatScanning, aContextString);
1407
1408
// leaf of ancestors might be text node. If so discard it.
1409
int32_t count = mCommonAncestors.Length();
1410
int32_t i;
1411
nsCOMPtr<nsINode> node;
1412
if (count > 0) node = mCommonAncestors.ElementAt(0);
1413
1414
if (node && IsTextNode(node)) {
1415
mCommonAncestors.RemoveElementAt(0);
1416
if (mContextInfoDepth.mStart) {
1417
--mContextInfoDepth.mStart;
1418
}
1419
if (mContextInfoDepth.mEnd) {
1420
--mContextInfoDepth.mEnd;
1421
}
1422
count--;
1423
}
1424
1425
i = count;
1426
while (i > 0) {
1427
node = mCommonAncestors.ElementAt(--i);
1428
rv = SerializeNodeStart(*node, 0, -1);
1429
NS_ENSURE_SUCCESS(rv, rv);
1430
}
1431
// i = 0; guaranteed by above
1432
while (i < count) {
1433
node = mCommonAncestors.ElementAt(i++);
1434
rv = SerializeNodeEnd(*node);
1435
NS_ENSURE_SUCCESS(rv, rv);
1436
}
1437
1438
mSerializer->Finish();
1439
1440
// encode range info : the start and end depth of the selection, where the
1441
// depth is distance down in the parent hierarchy. Later we will need to add
1442
// leading/trailing whitespace info to this.
1443
nsAutoString infoString;
1444
infoString.AppendInt(mContextInfoDepth.mStart);
1445
infoString.Append(char16_t(','));
1446
infoString.AppendInt(mContextInfoDepth.mEnd);
1447
aInfoString = infoString;
1448
1449
return rv;
1450
}
1451
1452
bool nsHTMLCopyEncoder::IncludeInContext(nsINode* aNode) {
1453
nsCOMPtr<nsIContent> content(do_QueryInterface(aNode));
1454
1455
if (!content) return false;
1456
1457
return content->IsAnyOfHTMLElements(
1458
nsGkAtoms::b, nsGkAtoms::i, nsGkAtoms::u, nsGkAtoms::a, nsGkAtoms::tt,
1459
nsGkAtoms::s, nsGkAtoms::big, nsGkAtoms::small, nsGkAtoms::strike,
1460
nsGkAtoms::em, nsGkAtoms::strong, nsGkAtoms::dfn, nsGkAtoms::code,
1461
nsGkAtoms::cite, nsGkAtoms::var, nsGkAtoms::abbr, nsGkAtoms::font,
1462
nsGkAtoms::script, nsGkAtoms::span, nsGkAtoms::pre, nsGkAtoms::h1,
1463
nsGkAtoms::h2, nsGkAtoms::h3, nsGkAtoms::h4, nsGkAtoms::h5,
1464
nsGkAtoms::h6);
1465
}
1466
1467
nsresult nsHTMLCopyEncoder::PromoteRange(nsRange* inRange) {
1468
if (!inRange->IsPositioned()) {
1469
return NS_ERROR_UNEXPECTED;
1470
}
1471
nsCOMPtr<nsINode> startNode = inRange->GetStartContainer();
1472
uint32_t startOffset = inRange->StartOffset();
1473
nsCOMPtr<nsINode> endNode = inRange->GetEndContainer();
1474
uint32_t endOffset = inRange->EndOffset();
1475
nsCOMPtr<nsINode> common = inRange->GetCommonAncestor();
1476
1477
nsCOMPtr<nsINode> opStartNode;
1478
nsCOMPtr<nsINode> opEndNode;
1479
int32_t opStartOffset, opEndOffset;
1480
1481
// examine range endpoints.
1482
nsresult rv =
1483
GetPromotedPoint(kStart, startNode, static_cast<int32_t>(startOffset),
1484
address_of(opStartNode), &opStartOffset, common);
1485
NS_ENSURE_SUCCESS(rv, rv);
1486
rv = GetPromotedPoint(kEnd, endNode, static_cast<int32_t>(endOffset),
1487
address_of(opEndNode), &opEndOffset, common);
1488
NS_ENSURE_SUCCESS(rv, rv);
1489
1490
// if both range endpoints are at the common ancestor, check for possible
1491
// inclusion of ancestors
1492
if (opStartNode == common && opEndNode == common) {
1493
rv = PromoteAncestorChain(address_of(opStartNode), &opStartOffset,
1494
&opEndOffset);
1495
NS_ENSURE_SUCCESS(rv, rv);
1496
opEndNode = opStartNode;
1497
}
1498
1499
// set the range to the new values
1500
ErrorResult err;
1501
inRange->SetStart(*opStartNode, static_cast<uint32_t>(opStartOffset), err);
1502
if (NS_WARN_IF(err.Failed())) {
1503
return err.StealNSResult();
1504
}
1505
inRange->SetEnd(*opEndNode, static_cast<uint32_t>(opEndOffset), err);
1506
if (NS_WARN_IF(err.Failed())) {
1507
return err.StealNSResult();
1508
}
1509
return NS_OK;
1510
}
1511
1512
// PromoteAncestorChain will promote a range represented by
1513
// [{*ioNode,*ioStartOffset} , {*ioNode,*ioEndOffset}] The promotion is
1514
// different from that found in getPromotedPoint: it will only promote one
1515
// endpoint if it can promote the other. Thus, instead of having a
1516
// startnode/endNode, there is just the one ioNode.
1517
nsresult nsHTMLCopyEncoder::PromoteAncestorChain(nsCOMPtr<nsINode>* ioNode,
1518
int32_t* ioStartOffset,
1519
int32_t* ioEndOffset) {
1520
if (!ioNode || !ioStartOffset || !ioEndOffset) return NS_ERROR_NULL_POINTER;
1521
1522
nsresult rv = NS_OK;
1523
bool done = false;
1524
1525
nsCOMPtr<nsINode> frontNode, endNode, parent;
1526
int32_t frontOffset, endOffset;
1527
1528
// save the editable state of the ioNode, so we don't promote an ancestor if
1529
// it has different editable state
1530
nsCOMPtr<nsINode> node = *ioNode;
1531
bool isEditable = node->IsEditable();
1532
1533
// loop for as long as we can promote both endpoints
1534
while (!done) {
1535
node = *ioNode;
1536
parent = node->GetParentNode();
1537
if (!parent) {
1538
done = true;
1539
} else {
1540
// passing parent as last param to GetPromotedPoint() allows it to promote
1541
// only one level up the hierarchy.
1542
rv = GetPromotedPoint(kStart, *ioNode, *ioStartOffset,
1543
address_of(frontNode), &frontOffset, parent);
1544
NS_ENSURE_SUCCESS(rv, rv);
1545
// then we make the same attempt with the endpoint
1546
rv = GetPromotedPoint(kEnd, *ioNode, *ioEndOffset, address_of(endNode),
1547
&endOffset, parent);
1548
NS_ENSURE_SUCCESS(rv, rv);
1549
1550
// if both endpoints were promoted one level and isEditable is the same as
1551
// the original node, keep looping - otherwise we are done.
1552
if ((frontNode != parent) || (endNode != parent) ||
1553
(frontNode->IsEditable() != isEditable))
1554
done = true;
1555
else {
1556
*ioNode = frontNode;
1557
*ioStartOffset = frontOffset;
1558
*ioEndOffset = endOffset;
1559
}
1560
}
1561
}
1562
return rv;
1563
}
1564
1565
nsresult nsHTMLCopyEncoder::GetPromotedPoint(Endpoint aWhere, nsINode* aNode,
1566
int32_t aOffset,
1567
nsCOMPtr<nsINode>* outNode,
1568
int32_t* outOffset,
1569
nsINode* common) {
1570
nsresult rv = NS_OK;
1571
nsCOMPtr<nsINode> node = aNode;
1572
nsCOMPtr<nsINode> parent = aNode;
1573
int32_t offset = aOffset;
1574
bool bResetPromotion = false;
1575
1576
// default values
1577
*outNode = node;
1578
*outOffset = offset;
1579
1580
if (common == node) return NS_OK;
1581
1582
if (aWhere == kStart) {
1583
// some special casing for text nodes
1584
if (auto nodeAsText = aNode->GetAsText()) {
1585
// if not at beginning of text node, we are done
1586
if (offset > 0) {
1587
// unless everything before us in just whitespace. NOTE: we need a more
1588
// general solution that truly detects all cases of non-significant
1589
// whitesace with no false alarms.
1590
nsAutoString text;
1591
nodeAsText->SubstringData(0, offset, text, IgnoreErrors());
1592
text.CompressWhitespace();
1593
if (!text.IsEmpty()) return NS_OK;
1594
bResetPromotion = true;
1595
}
1596
// else
1597
rv = GetNodeLocation(aNode, address_of(parent), &offset);
1598
NS_ENSURE_SUCCESS(rv, rv);
1599
} else {
1600
node = GetChildAt(parent, offset);
1601
}
1602
if (!node) node = parent;
1603
1604
// finding the real start for this point. look up the tree for as long as
1605
// we are the first node in the container, and as long as we haven't hit the
1606
// body node.
1607
if (!IsRoot(node) && (parent != common)) {
1608
rv = GetNodeLocation(node, address_of(parent), &offset);
1609
NS_ENSURE_SUCCESS(rv, rv);
1610
if (offset == -1) return NS_OK; // we hit generated content; STOP
1611
while ((IsFirstNode(node)) && (!IsRoot(parent)) && (parent != common)) {
1612
if (bResetPromotion) {
1613
nsCOMPtr<nsIContent> content = do_QueryInterface(parent);
1614
if (content && content->IsHTMLElement()) {
1615
if (nsHTMLElement::IsBlock(
1616
nsHTMLTags::AtomTagToId(content->NodeInfo()->NameAtom()))) {
1617
bResetPromotion = false;
1618
}
1619
}
1620
}
1621
1622
node = parent;
1623
rv = GetNodeLocation(node, address_of(parent), &offset);
1624
NS_ENSURE_SUCCESS(rv, rv);
1625
if (offset == -1) // we hit generated content; STOP
1626
{
1627
// back up a bit
1628
parent = node;
1629
offset = 0;
1630
break;
1631
}
1632
}
1633
if (bResetPromotion) {
1634
*outNode = aNode;
1635
*outOffset = aOffset;
1636
} else {
1637
*outNode = parent;
1638
*outOffset = offset;
1639
}
1640
return rv;
1641
}
1642
}
1643
1644
if (aWhere == kEnd) {
1645
// some special casing for text nodes
1646
if (auto nodeAsText = aNode->GetAsText()) {
1647
// if not at end of text node, we are done
1648
uint32_t len = aNode->Length();
1649
if (offset < (int32_t)len) {
1650
// unless everything after us in just whitespace. NOTE: we need a more
1651
// general solution that truly detects all cases of non-significant
1652
// whitespace with no false alarms.
1653
nsAutoString text;
1654
nodeAsText->SubstringData(offset, len - offset, text, IgnoreErrors());
1655
text.CompressWhitespace();
1656
if (!text.IsEmpty()) return NS_OK;
1657
bResetPromotion = true;
1658
}
1659
rv = GetNodeLocation(aNode, address_of(parent), &offset);
1660
NS_ENSURE_SUCCESS(rv, rv);
1661
} else {
1662
if (offset) offset--; // we want node _before_ offset
1663
node = GetChildAt(parent, offset);
1664
}
1665
if (!node) node = parent;
1666
1667
// finding the real end for this point. look up the tree for as long as we
1668
// are the last node in the container, and as long as we haven't hit the
1669
// body node.
1670
if (!IsRoot(node) && (parent != common)) {
1671
rv = GetNodeLocation(node, address_of(parent), &offset);
1672
NS_ENSURE_SUCCESS(rv, rv);
1673
if (offset == -1) return NS_OK; // we hit generated content; STOP
1674
while ((IsLastNode(node)) && (!IsRoot(parent)) && (parent != common)) {
1675
if (bResetPromotion) {
1676
nsCOMPtr<nsIContent> content = do_QueryInterface(parent);
1677
if (content && content->IsHTMLElement()) {
1678
if (nsHTMLElement::IsBlock(
1679
nsHTMLTags::AtomTagToId(content->NodeInfo()->NameAtom()))) {
1680
bResetPromotion = false;
1681
}
1682
}
1683
}
1684
1685
node = parent;
1686
rv = GetNodeLocation(node, address_of(parent), &offset);
1687
NS_ENSURE_SUCCESS(rv, rv);
1688
if (offset == -1) // we hit generated content; STOP
1689
{
1690
// back up a bit
1691
parent = node;
1692
offset = 0;
1693
break;
1694
}
1695
}
1696
if (bResetPromotion) {
1697
*outNode = aNode;
1698
*outOffset = aOffset;
1699
} else {
1700
*outNode = parent;
1701
offset++; // add one since this in an endpoint - want to be AFTER node.
1702
*outOffset = offset;
1703
}
1704
return rv;
1705
}
1706
}
1707
1708
return rv;
1709
}
1710
1711
nsCOMPtr<nsINode> nsHTMLCopyEncoder::GetChildAt(nsINode* aParent,
1712
int32_t aOffset) {
1713
nsCOMPtr<nsINode> resultNode;
1714
1715
if (!aParent) return resultNode;
1716
1717
nsCOMPtr<nsIContent> content = do_QueryInterface(aParent);
1718
MOZ_ASSERT(content, "null content in nsHTMLCopyEncoder::GetChildAt");
1719
1720
resultNode = content->GetChildAt_Deprecated(aOffset);
1721
1722
return resultNode;
1723
}
1724
1725
bool nsHTMLCopyEncoder::IsMozBR(Element* aElement) {
1726
HTMLBRElement* brElement = HTMLBRElement::FromNodeOrNull(aElement);
1727
return brElement && brElement->IsPaddingForEmptyLastLine();
1728
}
1729
1730
nsresult nsHTMLCopyEncoder::GetNodeLocation(nsINode* inChild,
1731
nsCOMPtr<nsINode>* outParent,
1732
int32_t* outOffset) {
1733
NS_ASSERTION((inChild && outParent && outOffset), "bad args");
1734
if (inChild && outParent && outOffset) {
1735
nsCOMPtr<nsIContent> child = do_QueryInterface(inChild);
1736
if (!child) {
1737
return NS_ERROR_NULL_POINTER;
1738
}
1739
1740
nsIContent* parent = child->GetParent();
1741
if (!parent) {
1742
return NS_ERROR_NULL_POINTER;
1743
}
1744
1745
*outParent = parent;
1746
*outOffset = parent->ComputeIndexOf(child);
1747
return NS_OK;
1748
}
1749
return NS_ERROR_NULL_POINTER;
1750
}
1751
1752
bool nsHTMLCopyEncoder::IsRoot(nsINode* aNode) {
1753
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
1754
if (!content) {
1755
return false;
1756
}
1757
1758
if (mIsTextWidget) {
1759
return content->IsHTMLElement(nsGkAtoms::div);
1760
}
1761
1762
return content->IsAnyOfHTMLElements(nsGkAtoms::body, nsGkAtoms::td,
1763
nsGkAtoms::th);
1764
}
1765
1766
bool nsHTMLCopyEncoder::IsFirstNode(nsINode* aNode) {
1767
// need to check if any nodes before us are really visible.
1768
// Mike wrote something for me along these lines in nsSelectionController,
1769
// but I don't think it's ready for use yet - revisit.
1770
// HACK: for now, simply consider all whitespace text nodes to be
1771
// invisible formatting nodes.
1772
for (nsIContent* sibling = aNode->GetPreviousSibling(); sibling;
1773
sibling = sibling->GetPreviousSibling()) {
1774
if (!sibling->TextIsOnlyWhitespace()) {
1775
return false;
1776
}
1777
}
1778
1779
return true;
1780
}
1781
1782
bool nsHTMLCopyEncoder::IsLastNode(nsINode* aNode) {
1783
// need to check if any nodes after us are really visible.
1784
// Mike wrote something for me along these lines in nsSelectionController,
1785
// but I don't think it's ready for use yet - revisit.
1786
// HACK: for now, simply consider all whitespace text nodes to be
1787
// invisible formatting nodes.
1788