Files
palemoon27/dom/base/nsTextFragment.cpp
T
roytam1 5e324904fd import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1252212. Make the RIL WorkerRun implemetations not leave exceptions on the JSContext. r=khuey (a96b929707)
- Bug 1267893 part 4 - Make setting the start time set 'did seek' to true; r=hiro (581d057f37)
- Bug 1263063 - Part 6: Use TimingParams::EndTime() instead of re-calculation ComputedTiming each time. r=dholbert (1c70d3dd80)
- Bug 1244637 - implement AnimationEffectTiming fill. r=hiro (83f10328cf)
- Bug 1244642 - Implement AnimationEffectTiming.direction r=hiro (5fb010e6f4)
- Bug 1267937 - Part 1: Clear mProgressOnLastCompose once we are not in effect. r=birtles (ccc5ee4594)
- Bug 1267937 - Part 2: A reftest which checks mProgressOnLastCompose is surely cleared in before phase. r=birtles (850fb3a6b1)
- Bug 1259733 - use forward declarations for nsIDocument in a few places; r=dholbert (1cc3af25b0)
- Bug 1232906 - Use channel.asyncOpen2 within dom/apps/AppsUtils.jsm (r=sicking) (f8fe88724f)
- Bug 1250464 - Remove workaround to get path from AppsUtils.jsm. r=myk (e1c76d7a13)
- Bug 1263158 - Check if key is present in manifest object before using it. r=fabrice (44fe2c0468)
- Bug 1230091 - ReferenceError on using not defined aApp variable in OfflineCacheInstaller.jsm, r=fabrice (8c6ab3d45c)
- Bug 1228974 - correct the group ID in Offline Cache Installer, r=fabrice (f30de7eb6c)
- var-let (364b37d086)
- Bug 1267718 - Add a nsPIDOMWindow::GetScriptableParentOrNull method. r=bz (2cf8533883)
- Bug 1268953. The load events we fire on iframe/frame/object (in the document case) should not be cancelable. r=smaug (ec36a6e8c8)
- Bug 1266194 - Implement boolean or EventListenerOptions as 3rd param to addEventListener, r=smaug (069c30d74f)
- Bug 944616 - "Blob URLs don't allow query or fragment parts". r=bz (85923ee174)
- minor (bd5daf4059)
- Bug 1167395 - Mark CharacterDataChangeInfo::Details as MOZ_STACK_CLASS, and mark mNextSibling as MOZ_NON_OWNING_REF. r=smaug (83eb176677)
- Bug 1192855 - Check validity in advance for nsRange::InsertNode; r=hsivonen (485bd59ff4)
- Bug 1214495 - Bonus fix. r=bz (bf6a4b33ec)
- Bug 1266889 - Plugin block list blocks SWF network requests, but does not prevent plugin instantiation. r=francois (be7237639a)
- Bug 1183891 - Remove warning if invalid node type is passed to nsRange::SetStart. r=smaug (8ce58952af)
- Bug 1183893 - Remove warning if invalid node type is passed to nsRange::SetEnd. r=smaug (8675f2e21f)
- Bug 1163105 - Make nsReferencedElement work with referencing elements that are not in their document's DOM tree. r=roc (83bc0fc078)
- Bug 1172144 - Improve the size check of nsTextFragment::Append, r=ehsan (e6d47af1b1)
- Bug 1151366 - remove nsGkAtoms::mozdonotsend from treesanitizer. r=ehsan (9237c22bdb)
- Bug 1158500 - make writing-mode a mapped CSS property. r=cam (8d50bfb287)
- bit of 1131348 (f2b234976b)
- Bug 1245533 - nsXHTMLContentSerializer::CheckElementEnd - small compilation issue, r=smaug (85d8a50ddb)
- align tests (cfb773549f)
- Bug 1248836 - HID Features Implementation, r=jocelyn (0cb4482faf)
- Bug 1239979: Close sockets when deinitializing Bluetooth profile managers, r=btian (c8c449767b)
- Bug 1238991: Don't connect Bluetooth OPP manager before service channel is known, r=btian (9ad0d9ff7e)
- Bug 1239979: Get pointers to Bluetooth managers during each shutdown, r=shuang (71959acf40)
- Bug 1229697 - Cancel bond when user inputs empty pincode for pairing, r=shuang (e70ee96e6b)
- Bug 1252787 - Patch : Add HID profile when device is remote, r=shawnjohnjr (bbadf5f42b)
- Bug 1236724: Check the maximum length of each array in IPC; f=jhector, r=btian (e961ee7756)
- align tests (17b6369dfa)
- Bug 1268688 - Start browser API for frames swapping to HTML. r=bz (4a17ea38c7)
- Bug 1265427 - nsDOMCameraControl needs an mOwnedStream to be consistent towards its VideoStreamTrack r=me (e008b0e4c1)
- Bug 1154665 - Part 1. Provide gps processing data to avoid setParameters fail. r=aosmond (0564b157d3)
- Bug 1154665 - Part 2. Testcase against gps parameter. r=aosmond (c52efbcd07)
- Bug 1239752 - Create ImageBitmap from ImageData should preserve alpha. r=roc (1ec8ccd266)
- Bug 1266432: Use CopySurface in ImageBitmap::PrepareForDrawing even when using D2D 1.1. r=kaku (c27dcf42f2)
- Bug 1265598: Deal with the possibility of a write map failing. r=kaku r=milan (1916e69db1)
- Bug 1266390: Preserver mIsPremultipliedAlpha when creating an ImageBitmap from an existing ImageBitmap. r=kaku (385ad1f750)
- Bug 1267100 - add makeCurrent() for WebGLContext::GetFramebufferAttachmentParameter(). r=jgilbert (d7f957610b)
- Bug 1266262 - Remove nearly-unused GLContext::mGLFormats. - r=jrmuizel (039e2a851f)
- Bug 1186688 - Remove cached state check for DrawBuffer maximums, since it's invalid with min-cap mode. - r=jrmuizel (dc92031951)
- Bug 1193526 - Add generated files. r=jgilbert (c3f54b6cef)
- Bug 1264214 - WebGL check the conflict name when LinkProgram. r=jgilbert (1f268acc58)
- Bug 842818 - Inline CloneData() and clean up ImportKeyTask::SetKeyData() r=rbarnes (299a32176c)
- Bug 1137987 - Remove nonstandard let block from dom/downloads/tests. r=aus (94e98d64ff)
- Bug 1211454 - Avoid requesting a zero-terminated string in TextEncoder when zero-termination is not needed. r=emk. (2d52f98e86)
- Bug 1259669 Rename WidgetCommandEvent::command to WidgetCommandEvent::mCommand r=masayuki (744c283978)
- Bug 1264380 - Get Composed Document of Shadow DOM Element Properly. r=wchen (0b4404ef19)
- Bug 1188539 - Remove the deprecated TouchList::identifiedTouch method; r=jst (926c24d74d)
- Bug 918706 - Return NS_ERROR_DOM_SYNTAX_ERR if method is invalid, r=khuey (f209944a0c)
- Bug 1265610 - test_postMessages.html and some dom/filesystem tests requires 'dom.input.dirpicker' to be true, r=smaug (7092cef989)
- Bug 1265610 - Fixing a JS error in the tests, CLOSED TREE r=me (d203807a1e)
- Bug 1137151: Marked destructors of refcounted FM-radio classes as protected, r=pzhang (5b2ad86c5e)
- Bug 1206174 - Improve code readability of FMRadioService r=alwu (bfcf897714)
- Bug 1254298 - Bypass Gamepad Service Shutdown Timer on e10s; r=ted r=cleu (c40fcae327)
- Bug 1156957 - Make gamepad mochitests work on e10s; r=ted (f0a1be1440)
- Bug 1248794 - Clean up observer on WindowsGamepadService shutdown; r=ted r=smaug sec-approval=abillings (06660cc3fc)
- Bug 1237896 - [Gamepad] Button Event cannot be correctly triggered after reconnect. r=qdot (fa21602600)
- Bug 1249833 - Typo in nsGeolocation.h. r=jdm (42dad72688)
- Bug 1255198 - [Telemetry] Add geolocation Telemetry probes to record fulfilled requests according to document.isVisible. r=jdm, data-review=bsmedberg (5e4f5db476)
- Bug 1218080 - Don't send the url to the parent process when opening new windows, because it is not actually used. r=smaug (fca00714f6)
- Bug 1256061 - Hold a strong reference to a request when we call a method on it. r=jdm (7ed037ef37)
- Bug 1263001 - Don't Notify() an unlinked nsGeolocationRequest. r=jdm (0693c4688f)
- missing bit of Bug 1242668 - Hold more references. r=sotaro (ba173e46f1)
- Bug 1267246 - Remove bogus assertion. r=nical (c8ccfb73c8)
- Bug 1265638 - Reset some properties when the backend of SharedSurface is switching from Basic to layer-accelerated backend. r=jgilbert (a6cf6598b7)
- Bug 1266484 - Fix crash in libsystem_kernel.dylib@0x16db6 when using Pinboard bookmarklet. r=gabor (1a96a797c6)
- Bug 1253959 - per comment 10, #ifdef code that causes ReadMetadata() to fail on Windows XP debug. r=cpearce. (ab2e19f621)
- Bug 1256038: Remove special NotifyDataArrived handling in the DirectShow reader. r=cpearce (441a26dd97)
- Bug 1284198 - Don't USE_CLOCK_API on macOS. r=terrence (659cecb516)
- Bug 956899 - Add comments to ConditionVariable and handle some edge cases gracefully; r=froydnj (be45b6b271)
- Bug 1268822 - rename mozilla::gmp::Runnable/SyncRunnable in order not to confuse NS_LOG_ADDREF/NS_LOG_RELEASE. r=rjesup. (18aec0cd35)
2024-09-13 09:58:56 +08:00

486 lines
12 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* A class which represents a fragment of text (eg inside a text
* node); if only codepoints below 256 are used, the text is stored as
* a char*; otherwise the text is stored as a char16_t*
*/
#include "nsTextFragment.h"
#include "nsCRT.h"
#include "nsReadableUtils.h"
#include "nsMemory.h"
#include "nsBidiUtils.h"
#include "nsUnicharUtils.h"
#include "nsUTF8Utils.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/SSE.h"
#include "nsTextFragmentImpl.h"
#include <algorithm>
#define TEXTFRAG_WHITE_AFTER_NEWLINE 50
#define TEXTFRAG_MAX_NEWLINES 7
// Static buffer used for common fragments
static char* sSpaceSharedString[TEXTFRAG_MAX_NEWLINES + 1];
static char* sTabSharedString[TEXTFRAG_MAX_NEWLINES + 1];
static char sSingleCharSharedString[256];
using mozilla::CheckedUint32;
// static
nsresult
nsTextFragment::Init()
{
// Create whitespace strings
uint32_t i;
for (i = 0; i <= TEXTFRAG_MAX_NEWLINES; ++i) {
sSpaceSharedString[i] = new char[1 + i + TEXTFRAG_WHITE_AFTER_NEWLINE];
sTabSharedString[i] = new char[1 + i + TEXTFRAG_WHITE_AFTER_NEWLINE];
sSpaceSharedString[i][0] = ' ';
sTabSharedString[i][0] = ' ';
uint32_t j;
for (j = 1; j < 1 + i; ++j) {
sSpaceSharedString[i][j] = '\n';
sTabSharedString[i][j] = '\n';
}
for (; j < (1 + i + TEXTFRAG_WHITE_AFTER_NEWLINE); ++j) {
sSpaceSharedString[i][j] = ' ';
sTabSharedString[i][j] = '\t';
}
}
// Create single-char strings
for (i = 0; i < 256; ++i) {
sSingleCharSharedString[i] = i;
}
return NS_OK;
}
// static
void
nsTextFragment::Shutdown()
{
uint32_t i;
for (i = 0; i <= TEXTFRAG_MAX_NEWLINES; ++i) {
delete [] sSpaceSharedString[i];
delete [] sTabSharedString[i];
sSpaceSharedString[i] = nullptr;
sTabSharedString[i] = nullptr;
}
}
nsTextFragment::~nsTextFragment()
{
ReleaseText();
MOZ_COUNT_DTOR(nsTextFragment);
}
void
nsTextFragment::ReleaseText()
{
if (mState.mLength && m1b && mState.mInHeap) {
moz_free(m2b); // m1b == m2b as far as moz_free is concerned
}
m1b = nullptr;
mState.mIsBidi = false;
// Set mState.mIs2b, mState.mInHeap, and mState.mLength = 0 with mAllBits;
mAllBits = 0;
}
nsTextFragment&
nsTextFragment::operator=(const nsTextFragment& aOther)
{
ReleaseText();
if (aOther.mState.mLength) {
if (!aOther.mState.mInHeap) {
m1b = aOther.m1b; // This will work even if aOther is using m2b
}
else {
CheckedUint32 m2bSize = aOther.mState.mLength;
m2bSize *= (aOther.mState.mIs2b ? sizeof(char16_t) : sizeof(char));
m2b = nullptr;
if (m2bSize.isValid()) {
m2b = static_cast<char16_t*>(moz_malloc(m2bSize.value()));
}
if (m2b) {
memcpy(m2b, aOther.m2b, m2bSize.value());
} else {
// allocate a buffer for a single REPLACEMENT CHARACTER
m2b = static_cast<char16_t*>(moz_xmalloc(sizeof(char16_t)));
m2b[0] = 0xFFFD; // REPLACEMENT CHARACTER
mState.mIs2b = true;
mState.mInHeap = true;
mState.mLength = 1;
}
}
if (m1b) {
mAllBits = aOther.mAllBits;
}
}
return *this;
}
static inline int32_t
FirstNon8BitUnvectorized(const char16_t *str, const char16_t *end)
{
typedef Non8BitParameters<sizeof(size_t)> p;
const size_t mask = p::mask();
const uint32_t alignMask = p::alignMask();
const uint32_t numUnicharsPerWord = p::numUnicharsPerWord();
const int32_t len = end - str;
int32_t i = 0;
// Align ourselves to a word boundary.
int32_t alignLen =
std::min(len, int32_t(((-NS_PTR_TO_INT32(str)) & alignMask) / sizeof(char16_t)));
for (; i < alignLen; i++) {
if (str[i] > 255)
return i;
}
// Check one word at a time.
const int32_t wordWalkEnd = ((len - i) / numUnicharsPerWord) * numUnicharsPerWord;
for (; i < wordWalkEnd; i += numUnicharsPerWord) {
const size_t word = *reinterpret_cast<const size_t*>(str + i);
if (word & mask)
return i;
}
// Take care of the remainder one character at a time.
for (; i < len; i++) {
if (str[i] > 255)
return i;
}
return -1;
}
#ifdef MOZILLA_MAY_SUPPORT_SSE2
namespace mozilla {
namespace SSE2 {
int32_t FirstNon8Bit(const char16_t *str, const char16_t *end);
} // namespace SSE2
} // namespace mozilla
#endif
/*
* This function returns -1 if all characters in str are 8 bit characters.
* Otherwise, it returns a value less than or equal to the index of the first
* non-8bit character in str. For example, if first non-8bit character is at
* position 25, it may return 25, or for example 24, or 16. But it guarantees
* there is no non-8bit character before returned value.
*/
static inline int32_t
FirstNon8Bit(const char16_t *str, const char16_t *end)
{
#ifdef MOZILLA_MAY_SUPPORT_SSE2
if (mozilla::supports_sse2()) {
return mozilla::SSE2::FirstNon8Bit(str, end);
}
#endif
return FirstNon8BitUnvectorized(str, end);
}
bool
nsTextFragment::SetTo(const char16_t* aBuffer, int32_t aLength, bool aUpdateBidi)
{
if (MOZ_UNLIKELY(aLength < 0 || static_cast<uint32_t>(aLength) >
NS_MAX_TEXT_FRAGMENT_LENGTH)) {
return false;
}
ReleaseText();
if (aLength == 0) {
return true;
}
char16_t firstChar = *aBuffer;
if (aLength == 1 && firstChar < 256) {
m1b = sSingleCharSharedString + firstChar;
mState.mInHeap = false;
mState.mIs2b = false;
mState.mLength = 1;
return true;
}
const char16_t *ucp = aBuffer;
const char16_t *uend = aBuffer + aLength;
// Check if we can use a shared string
if (aLength <= 1 + TEXTFRAG_WHITE_AFTER_NEWLINE + TEXTFRAG_MAX_NEWLINES &&
(firstChar == ' ' || firstChar == '\n' || firstChar == '\t')) {
if (firstChar == ' ') {
++ucp;
}
const char16_t* start = ucp;
while (ucp < uend && *ucp == '\n') {
++ucp;
}
const char16_t* endNewLine = ucp;
char16_t space = ucp < uend && *ucp == '\t' ? '\t' : ' ';
while (ucp < uend && *ucp == space) {
++ucp;
}
if (ucp == uend &&
endNewLine - start <= TEXTFRAG_MAX_NEWLINES &&
ucp - endNewLine <= TEXTFRAG_WHITE_AFTER_NEWLINE) {
char** strings = space == ' ' ? sSpaceSharedString : sTabSharedString;
m1b = strings[endNewLine - start];
// If we didn't find a space in the beginning, skip it now.
if (firstChar != ' ') {
++m1b;
}
mState.mInHeap = false;
mState.mIs2b = false;
mState.mLength = aLength;
return true;
}
}
// See if we need to store the data in ucs2 or not
int32_t first16bit = FirstNon8Bit(ucp, uend);
if (first16bit != -1) { // aBuffer contains no non-8bit character
// Use ucs2 storage because we have to
CheckedUint32 m2bSize = aLength;
m2bSize *= sizeof(char16_t);
if (!m2bSize.isValid()) {
return false;
}
m2b = static_cast<char16_t*>(moz_malloc(m2bSize.value()));
if (!m2b) {
return false;
}
memcpy(m2b, aBuffer, m2bSize.value());
mState.mIs2b = true;
if (aUpdateBidi) {
UpdateBidiFlag(aBuffer + first16bit, aLength - first16bit);
}
} else {
// Use 1 byte storage because we can
char* buff = static_cast<char*>(moz_malloc(aLength));
if (!buff) {
return false;
}
// Copy data
LossyConvertEncoding16to8 converter(buff);
copy_string(aBuffer, aBuffer+aLength, converter);
m1b = buff;
mState.mIs2b = false;
}
// Setup our fields
mState.mInHeap = true;
mState.mLength = aLength;
return true;
}
void
nsTextFragment::CopyTo(char16_t *aDest, int32_t aOffset, int32_t aCount)
{
NS_ASSERTION(aOffset >= 0, "Bad offset passed to nsTextFragment::CopyTo()!");
NS_ASSERTION(aCount >= 0, "Bad count passed to nsTextFragment::CopyTo()!");
if (aOffset < 0) {
aOffset = 0;
}
if (uint32_t(aOffset + aCount) > GetLength()) {
aCount = mState.mLength - aOffset;
}
if (aCount != 0) {
if (mState.mIs2b) {
memcpy(aDest, m2b + aOffset, sizeof(char16_t) * aCount);
} else {
const char *cp = m1b + aOffset;
const char *end = cp + aCount;
LossyConvertEncoding8to16 converter(aDest);
copy_string(cp, end, converter);
}
}
}
bool
nsTextFragment::Append(const char16_t* aBuffer, uint32_t aLength, bool aUpdateBidi)
{
if (!aLength) {
return true;
}
// This is a common case because some callsites create a textnode
// with a value by creating the node and then calling AppendData.
if (mState.mLength == 0) {
if (MOZ_UNLIKELY(aLength > INT32_MAX)) {
return false;
}
return SetTo(aBuffer, aLength, aUpdateBidi);
}
// Should we optimize for aData.Length() == 0?
// Note: Using CheckedInt here is wrong as nsTextFragment is 29 bits and needs an
// explicit check for that length and not INT_MAX. Also, this method can be a very
// hot path and cause performance loss since CheckedInt isn't inlined.
if (MOZ_UNLIKELY(NS_MAX_TEXT_FRAGMENT_LENGTH - mState.mLength < aLength)) {
return false; // Would be overflowing if we'd continue.
}
CheckedUint32 length = mState.mLength;
length += aLength;
if (mState.mIs2b) {
length *= sizeof(char16_t);
if (!length.isValid()) {
return false;
}
// Already a 2-byte string so the result will be too
char16_t* buff = static_cast<char16_t*>(moz_realloc(m2b, length.value()));
if (!buff) {
return false;
}
memcpy(buff + mState.mLength, aBuffer, aLength * sizeof(char16_t));
mState.mLength += aLength;
m2b = buff;
if (aUpdateBidi) {
UpdateBidiFlag(aBuffer, aLength);
}
return true;
}
// Current string is a 1-byte string, check if the new data fits in one byte too.
int32_t first16bit = FirstNon8Bit(aBuffer, aBuffer + aLength);
if (first16bit != -1) { // aBuffer contains no non-8bit character
length *= sizeof(char16_t);
if (!length.isValid()) {
return false;
}
// The old data was 1-byte, but the new is not so we have to expand it
// all to 2-byte
char16_t* buff = static_cast<char16_t*>(moz_malloc(length.value()));
if (!buff) {
return false;
}
// Copy data into buff
LossyConvertEncoding8to16 converter(buff);
copy_string(m1b, m1b+mState.mLength, converter);
memcpy(buff + mState.mLength, aBuffer, aLength * sizeof(char16_t));
mState.mLength += aLength;
mState.mIs2b = true;
if (mState.mInHeap) {
moz_free(m2b);
}
m2b = buff;
mState.mInHeap = true;
if (aUpdateBidi) {
UpdateBidiFlag(aBuffer + first16bit, aLength - first16bit);
}
return true;
}
// The new and the old data is all 1-byte
char* buff;
if (mState.mInHeap) {
buff = static_cast<char*>(moz_realloc(const_cast<char*>(m1b), length.value()));
if (!buff) {
return false;
}
}
else {
buff = static_cast<char*>(moz_malloc(length.value()));
if (!buff) {
return false;
}
memcpy(buff, m1b, mState.mLength);
mState.mInHeap = true;
}
// Copy aBuffer into buff.
LossyConvertEncoding16to8 converter(buff + mState.mLength);
copy_string(aBuffer, aBuffer + aLength, converter);
m1b = buff;
mState.mLength += aLength;
return true;
}
/* virtual */ size_t
nsTextFragment::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
{
if (Is2b()) {
return aMallocSizeOf(m2b);
}
if (mState.mInHeap) {
return aMallocSizeOf(m1b);
}
return 0;
}
// To save time we only do this when we really want to know, not during
// every allocation
void
nsTextFragment::UpdateBidiFlag(const char16_t* aBuffer, uint32_t aLength)
{
if (mState.mIs2b && !mState.mIsBidi) {
const char16_t* cp = aBuffer;
const char16_t* end = cp + aLength;
while (cp < end) {
char16_t ch1 = *cp++;
uint32_t utf32Char = ch1;
if (NS_IS_HIGH_SURROGATE(ch1) &&
cp < end &&
NS_IS_LOW_SURROGATE(*cp)) {
char16_t ch2 = *cp++;
utf32Char = SURROGATE_TO_UCS4(ch1, ch2);
}
if (UTF32_CHAR_IS_BIDI(utf32Char) || IsBidiControl(utf32Char)) {
mState.mIsBidi = true;
break;
}
}
}
}