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