This source file includes following definitions.
- numberOfCharactersJustAppended
- push
- pop
- top
- size
- depthCrossingShadowBoundaries
- nextInPreOrderCrossingShadowBoundaries
- fullyClipsContents
- ignoresContainerClip
- pushFullyClippedState
- setUpFullyClippedStack
- m_entersAuthorShadowRoots
- m_entersAuthorShadowRoots
- initialize
- advance
- characterAt
- substring
- appendTextToStringBuilder
- handleTextNode
- handleTextBox
- firstRenderTextInFirstLetter
- handleTextNodeFirstLetter
- handleReplacedElement
- hasVisibleTextNode
- shouldEmitTabBeforeNode
- shouldEmitNewlineForNode
- shouldEmitNewlinesBeforeAndAfterNode
- shouldEmitNewlineAfterNode
- shouldEmitNewlineBeforeNode
- shouldEmitExtraNewlineForNode
- collapsedSpaceLength
- maxOffsetIncludingCollapsedSpaces
- shouldRepresentNodeOffsetZero
- shouldEmitSpaceBeforeAndAfterNode
- representNodeOffsetZero
- handleNonTextNode
- exitNode
- emitCharacter
- emitText
- emitText
- range
- node
- m_emitsOriginalText
- advance
- handleTextNode
- handleFirstLetter
- handleReplacedElement
- handleNonTextNode
- exitNode
- emitCharacter
- advanceRespectingRange
- range
- m_textIterator
- m_textIterator
- initialize
- range
- advance
- characterSubrange
- m_textIterator
- range
- advance
- m_textIterator
- advance
- length
- substring
- characterAt
- createSearcher
- searcher
- lockSearcher
- unlockSearcher
- m_targetRequiresKanaWorkaround
- append
- needsMoreContext
- prependContext
- atBreak
- reachedBreak
- isBadMatch
- isWordStartMatch
- search
- rangeLength
- subrange
- plainText
- collapsedToBoundary
- findPlainTextInternal
- findPlainText
- findPlainText
#include "config.h"
#include "core/editing/TextIterator.h"
#include "HTMLNames.h"
#include "bindings/v8/ExceptionStatePlaceholder.h"
#include "core/dom/Document.h"
#include "core/dom/NodeTraversal.h"
#include "core/dom/shadow/ShadowRoot.h"
#include "core/editing/VisiblePosition.h"
#include "core/editing/VisibleUnits.h"
#include "core/editing/htmlediting.h"
#include "core/html/HTMLElement.h"
#include "core/html/HTMLTextFormControlElement.h"
#include "core/rendering/InlineTextBox.h"
#include "core/rendering/RenderImage.h"
#include "core/rendering/RenderTableCell.h"
#include "core/rendering/RenderTableRow.h"
#include "core/rendering/RenderTextControl.h"
#include "core/rendering/RenderTextFragment.h"
#include "platform/fonts/Character.h"
#include "platform/fonts/Font.h"
#include "platform/text/TextBoundaries.h"
#include "platform/text/TextBreakIteratorInternalICU.h"
#include "platform/text/UnicodeUtilities.h"
#include "wtf/text/CString.h"
#include "wtf/text/StringBuilder.h"
#include "wtf/unicode/CharacterNames.h"
#include <unicode/usearch.h>
using namespace WTF::Unicode;
using namespace std;
namespace WebCore {
using namespace HTMLNames;
class SearchBuffer {
WTF_MAKE_NONCOPYABLE(SearchBuffer);
public:
SearchBuffer(const String& target, FindOptions);
~SearchBuffer();
template<typename CharType>
void append(const CharType*, size_t length);
size_t numberOfCharactersJustAppended() const { return m_numberOfCharactersJustAppended; }
bool needsMoreContext() const;
void prependContext(const UChar*, size_t length);
void reachedBreak();
size_t search(size_t& startOffset);
bool atBreak() const;
private:
bool isBadMatch(const UChar*, size_t length) const;
bool isWordStartMatch(size_t start, size_t length) const;
Vector<UChar> m_target;
FindOptions m_options;
Vector<UChar> m_buffer;
size_t m_overlap;
size_t m_prefixLength;
size_t m_numberOfCharactersJustAppended;
bool m_atBreak;
bool m_needsMoreContext;
bool m_targetRequiresKanaWorkaround;
Vector<UChar> m_normalizedTarget;
mutable Vector<UChar> m_normalizedMatch;
};
static const unsigned bitsInWord = sizeof(unsigned) * 8;
static const unsigned bitInWordMask = bitsInWord - 1;
BitStack::BitStack()
: m_size(0)
{
}
BitStack::~BitStack()
{
}
void BitStack::push(bool bit)
{
unsigned index = m_size / bitsInWord;
unsigned shift = m_size & bitInWordMask;
if (!shift && index == m_words.size()) {
m_words.grow(index + 1);
m_words[index] = 0;
}
unsigned& word = m_words[index];
unsigned mask = 1U << shift;
if (bit)
word |= mask;
else
word &= ~mask;
++m_size;
}
void BitStack::pop()
{
if (m_size)
--m_size;
}
bool BitStack::top() const
{
if (!m_size)
return false;
unsigned shift = (m_size - 1) & bitInWordMask;
return m_words.last() & (1U << shift);
}
unsigned BitStack::size() const
{
return m_size;
}
#if !ASSERT_DISABLED
static unsigned depthCrossingShadowBoundaries(Node* node)
{
unsigned depth = 0;
for (Node* parent = node->parentOrShadowHostNode(); parent; parent = parent->parentOrShadowHostNode())
++depth;
return depth;
}
#endif
static Node* nextInPreOrderCrossingShadowBoundaries(Node* rangeEndContainer, int rangeEndOffset)
{
if (!rangeEndContainer)
return 0;
if (rangeEndOffset >= 0 && !rangeEndContainer->offsetInCharacters()) {
if (Node* next = rangeEndContainer->traverseToChildAt(rangeEndOffset))
return next;
}
for (Node* node = rangeEndContainer; node; node = node->parentOrShadowHostNode()) {
if (Node* next = node->nextSibling())
return next;
}
return 0;
}
static inline bool fullyClipsContents(Node* node)
{
RenderObject* renderer = node->renderer();
if (!renderer || !renderer->isBox() || !renderer->hasOverflowClip())
return false;
return toRenderBox(renderer)->size().isEmpty();
}
static inline bool ignoresContainerClip(Node* node)
{
RenderObject* renderer = node->renderer();
if (!renderer || renderer->isText())
return false;
return renderer->style()->hasOutOfFlowPosition();
}
static void pushFullyClippedState(BitStack& stack, Node* node)
{
ASSERT(stack.size() == depthCrossingShadowBoundaries(node));
stack.push(fullyClipsContents(node) || (stack.top() && !ignoresContainerClip(node)));
}
static void setUpFullyClippedStack(BitStack& stack, Node* node)
{
Vector<Node*, 100> ancestry;
for (Node* parent = node->parentOrShadowHostNode(); parent; parent = parent->parentOrShadowHostNode())
ancestry.append(parent);
size_t size = ancestry.size();
for (size_t i = 0; i < size; ++i)
pushFullyClippedState(stack, ancestry[size - i - 1]);
pushFullyClippedState(stack, node);
ASSERT(stack.size() == 1 + depthCrossingShadowBoundaries(node));
}
TextIterator::TextIterator(const Range* range, TextIteratorBehaviorFlags behavior)
: m_shadowDepth(0)
, m_startContainer(0)
, m_startOffset(0)
, m_endContainer(0)
, m_endOffset(0)
, m_positionNode(0)
, m_textLength(0)
, m_needsAnotherNewline(false)
, m_textBox(0)
, m_remainingTextBox(0)
, m_firstLetterText(0)
, m_lastTextNode(0)
, m_lastTextNodeEndedWithCollapsedSpace(false)
, m_lastCharacter(0)
, m_sortedTextBoxesPosition(0)
, m_hasEmitted(false)
, m_emitsCharactersBetweenAllVisiblePositions(behavior & TextIteratorEmitsCharactersBetweenAllVisiblePositions)
, m_entersTextControls(behavior & TextIteratorEntersTextControls)
, m_emitsOriginalText(behavior & TextIteratorEmitsOriginalText)
, m_handledFirstLetter(false)
, m_ignoresStyleVisibility(behavior & TextIteratorIgnoresStyleVisibility)
, m_stopsOnFormControls(behavior & TextIteratorStopsOnFormControls)
, m_shouldStop(false)
, m_emitsImageAltText(behavior & TextIteratorEmitsImageAltText)
, m_entersAuthorShadowRoots(behavior & TextIteratorEntersAuthorShadowRoots)
{
if (range)
initialize(range->startPosition(), range->endPosition());
}
TextIterator::TextIterator(const Position& start, const Position& end, TextIteratorBehaviorFlags behavior)
: m_shadowDepth(0)
, m_startContainer(0)
, m_startOffset(0)
, m_endContainer(0)
, m_endOffset(0)
, m_positionNode(0)
, m_textLength(0)
, m_needsAnotherNewline(false)
, m_textBox(0)
, m_remainingTextBox(0)
, m_firstLetterText(0)
, m_lastTextNode(0)
, m_lastTextNodeEndedWithCollapsedSpace(false)
, m_lastCharacter(0)
, m_sortedTextBoxesPosition(0)
, m_hasEmitted(false)
, m_emitsCharactersBetweenAllVisiblePositions(behavior & TextIteratorEmitsCharactersBetweenAllVisiblePositions)
, m_entersTextControls(behavior & TextIteratorEntersTextControls)
, m_emitsOriginalText(behavior & TextIteratorEmitsOriginalText)
, m_handledFirstLetter(false)
, m_ignoresStyleVisibility(behavior & TextIteratorIgnoresStyleVisibility)
, m_stopsOnFormControls(behavior & TextIteratorStopsOnFormControls)
, m_shouldStop(false)
, m_emitsImageAltText(behavior & TextIteratorEmitsImageAltText)
, m_entersAuthorShadowRoots(behavior & TextIteratorEntersAuthorShadowRoots)
{
initialize(start, end);
}
void TextIterator::initialize(const Position& start, const Position& end)
{
ASSERT(comparePositions(start, end) <= 0);
Node* startContainer = start.containerNode();
if (!startContainer)
return;
int startOffset = start.computeOffsetInContainerNode();
Node* endContainer = end.containerNode();
if (!endContainer)
return;
int endOffset = end.computeOffsetInContainerNode();
m_startContainer = startContainer;
m_startOffset = startOffset;
m_endContainer = endContainer;
m_endOffset = endOffset;
if (startContainer->offsetInCharacters())
m_node = startContainer;
else if (Node* child = startContainer->traverseToChildAt(startOffset))
m_node = child;
else if (!startOffset)
m_node = startContainer;
else
m_node = NodeTraversal::nextSkippingChildren(*startContainer);
if (!m_node)
return;
setUpFullyClippedStack(m_fullyClippedStack, m_node);
m_offset = m_node == m_startContainer ? m_startOffset : 0;
m_iterationProgress = HandledNone;
m_pastEndNode = nextInPreOrderCrossingShadowBoundaries(endContainer, endOffset);
advance();
}
TextIterator::~TextIterator()
{
}
void TextIterator::advance()
{
if (m_shouldStop)
return;
m_positionNode = 0;
m_textLength = 0;
if (m_needsAnotherNewline) {
Node* baseNode = m_node->lastChild() ? m_node->lastChild() : m_node;
emitCharacter('\n', baseNode->parentNode(), baseNode, 1, 1);
m_needsAnotherNewline = false;
return;
}
if (!m_textBox && m_remainingTextBox) {
m_textBox = m_remainingTextBox;
m_remainingTextBox = 0;
m_firstLetterText = 0;
m_offset = 0;
}
if (m_textBox) {
handleTextBox();
if (m_positionNode)
return;
}
while (m_node && (m_node != m_pastEndNode || m_shadowDepth > 0)) {
if (!m_shouldStop && m_stopsOnFormControls && HTMLFormControlElement::enclosingFormControlElement(m_node))
m_shouldStop = true;
if (m_node == m_endContainer && !m_endOffset) {
representNodeOffsetZero();
m_node = 0;
return;
}
RenderObject* renderer = m_node->renderer();
if (!renderer) {
if (m_node->isShadowRoot()) {
m_iterationProgress = m_iterationProgress < HandledNode ? HandledNode : m_iterationProgress;
} else {
m_iterationProgress = HandledChildren;
}
} else {
if (m_iterationProgress < HandledAuthorShadowRoots) {
if (m_entersAuthorShadowRoots && m_node->isElementNode() && toElement(m_node)->hasAuthorShadowRoot()) {
ShadowRoot* youngestShadowRoot = toElement(m_node)->shadowRoot();
ASSERT(youngestShadowRoot->type() == ShadowRoot::AuthorShadowRoot);
m_node = youngestShadowRoot;
m_iterationProgress = HandledNone;
++m_shadowDepth;
pushFullyClippedState(m_fullyClippedStack, m_node);
continue;
}
m_iterationProgress = HandledAuthorShadowRoots;
}
if (m_iterationProgress < HandledUserAgentShadowRoot) {
if (m_entersTextControls && renderer->isTextControl()) {
ShadowRoot* userAgentShadowRoot = toElement(m_node)->userAgentShadowRoot();
ASSERT(userAgentShadowRoot->type() == ShadowRoot::UserAgentShadowRoot);
m_node = userAgentShadowRoot;
m_iterationProgress = HandledNone;
++m_shadowDepth;
pushFullyClippedState(m_fullyClippedStack, m_node);
continue;
}
m_iterationProgress = HandledUserAgentShadowRoot;
}
if (m_iterationProgress < HandledNode) {
bool handledNode = false;
if (renderer->isText() && m_node->nodeType() == Node::TEXT_NODE) {
handledNode = handleTextNode();
} else if (renderer && (renderer->isImage() || renderer->isWidget()
|| (m_node && m_node->isElementNode()
&& (toElement(m_node)->isFormControlElement()
|| isHTMLLegendElement(toElement(*m_node))
|| isHTMLMeterElement(toElement(*m_node))
|| isHTMLProgressElement(toElement(*m_node)))))) {
handledNode = handleReplacedElement();
} else {
handledNode = handleNonTextNode();
}
if (handledNode)
m_iterationProgress = HandledNode;
if (m_positionNode)
return;
}
}
Node* next = m_iterationProgress < HandledChildren ? m_node->firstChild() : 0;
m_offset = 0;
if (!next) {
next = m_node->nextSibling();
if (!next) {
bool pastEnd = NodeTraversal::next(*m_node) == m_pastEndNode;
Node* parentNode = m_node->parentNode();
while (!next && parentNode) {
if ((pastEnd && parentNode == m_endContainer) || m_endContainer->isDescendantOf(parentNode))
return;
bool haveRenderer = m_node->renderer();
m_node = parentNode;
m_fullyClippedStack.pop();
parentNode = m_node->parentNode();
if (haveRenderer)
exitNode();
if (m_positionNode) {
m_iterationProgress = HandledChildren;
return;
}
next = m_node->nextSibling();
}
if (!next && !parentNode && m_shadowDepth > 0) {
ShadowRoot* shadowRoot = toShadowRoot(m_node);
if (shadowRoot->type() == ShadowRoot::AuthorShadowRoot) {
ShadowRoot* nextShadowRoot = shadowRoot->olderShadowRoot();
if (nextShadowRoot && nextShadowRoot->type() == ShadowRoot::AuthorShadowRoot) {
m_fullyClippedStack.pop();
m_node = nextShadowRoot;
m_iterationProgress = HandledNone;
pushFullyClippedState(m_fullyClippedStack, m_node);
} else {
m_node = shadowRoot->host();
m_iterationProgress = HandledAuthorShadowRoots;
--m_shadowDepth;
m_fullyClippedStack.pop();
}
} else {
ASSERT(shadowRoot->type() == ShadowRoot::UserAgentShadowRoot);
m_node = shadowRoot->host();
m_iterationProgress = HandledUserAgentShadowRoot;
--m_shadowDepth;
m_fullyClippedStack.pop();
}
m_handledFirstLetter = false;
m_firstLetterText = 0;
continue;
}
}
m_fullyClippedStack.pop();
}
m_node = next;
if (m_node)
pushFullyClippedState(m_fullyClippedStack, m_node);
m_iterationProgress = HandledNone;
m_handledFirstLetter = false;
m_firstLetterText = 0;
if (m_positionNode)
return;
}
}
UChar TextIterator::characterAt(unsigned index) const
{
ASSERT_WITH_SECURITY_IMPLICATION(index < static_cast<unsigned>(length()));
if (!(index < static_cast<unsigned>(length())))
return 0;
if (m_singleCharacterBuffer) {
ASSERT(!index);
ASSERT(length() == 1);
return m_singleCharacterBuffer;
}
return string()[startOffset() + index];
}
String TextIterator::substring(unsigned position, unsigned length) const
{
ASSERT_WITH_SECURITY_IMPLICATION(position <= static_cast<unsigned>(this->length()));
ASSERT_WITH_SECURITY_IMPLICATION(position + length <= static_cast<unsigned>(this->length()));
if (!length)
return emptyString();
if (m_singleCharacterBuffer) {
ASSERT(!position);
ASSERT(length == 1);
return String(&m_singleCharacterBuffer, 1);
}
return string().substring(startOffset() + position, length);
}
void TextIterator::appendTextToStringBuilder(StringBuilder& builder, unsigned position, unsigned maxLength) const
{
unsigned lengthToAppend = std::min(static_cast<unsigned>(length()) - position, maxLength);
if (!lengthToAppend)
return;
if (m_singleCharacterBuffer) {
ASSERT(!position);
builder.append(m_singleCharacterBuffer);
} else {
builder.append(string(), startOffset() + position, lengthToAppend);
}
}
bool TextIterator::handleTextNode()
{
if (m_fullyClippedStack.top() && !m_ignoresStyleVisibility)
return false;
RenderText* renderer = toRenderText(m_node->renderer());
m_lastTextNode = m_node;
String str = renderer->text();
if (!renderer->style()->collapseWhiteSpace()) {
int runStart = m_offset;
if (m_lastTextNodeEndedWithCollapsedSpace && hasVisibleTextNode(renderer)) {
emitCharacter(' ', m_node, 0, runStart, runStart);
return false;
}
if (!m_handledFirstLetter && renderer->isTextFragment() && !m_offset) {
handleTextNodeFirstLetter(toRenderTextFragment(renderer));
if (m_firstLetterText) {
String firstLetter = m_firstLetterText->text();
emitText(m_node, m_firstLetterText, m_offset, m_offset + firstLetter.length());
m_firstLetterText = 0;
m_textBox = 0;
return false;
}
}
if (renderer->style()->visibility() != VISIBLE && !m_ignoresStyleVisibility)
return false;
int strLength = str.length();
int end = (m_node == m_endContainer) ? m_endOffset : INT_MAX;
int runEnd = min(strLength, end);
if (runStart >= runEnd)
return true;
emitText(m_node, runStart, runEnd);
return true;
}
if (renderer->firstTextBox())
m_textBox = renderer->firstTextBox();
bool shouldHandleFirstLetter = !m_handledFirstLetter && renderer->isTextFragment() && !m_offset;
if (shouldHandleFirstLetter)
handleTextNodeFirstLetter(toRenderTextFragment(renderer));
if (!renderer->firstTextBox() && str.length() > 0 && !shouldHandleFirstLetter) {
if (renderer->style()->visibility() != VISIBLE && !m_ignoresStyleVisibility)
return false;
m_lastTextNodeEndedWithCollapsedSpace = true;
return true;
}
if (m_firstLetterText)
renderer = m_firstLetterText;
if (renderer->containsReversedText()) {
m_sortedTextBoxes.clear();
for (InlineTextBox* textBox = renderer->firstTextBox(); textBox; textBox = textBox->nextTextBox()) {
m_sortedTextBoxes.append(textBox);
}
std::sort(m_sortedTextBoxes.begin(), m_sortedTextBoxes.end(), InlineTextBox::compareByStart);
m_sortedTextBoxesPosition = 0;
m_textBox = m_sortedTextBoxes.isEmpty() ? 0 : m_sortedTextBoxes[0];
}
handleTextBox();
return true;
}
void TextIterator::handleTextBox()
{
RenderText* renderer = m_firstLetterText ? m_firstLetterText : toRenderText(m_node->renderer());
if (renderer->style()->visibility() != VISIBLE && !m_ignoresStyleVisibility) {
m_textBox = 0;
return;
}
String str = renderer->text();
unsigned start = m_offset;
unsigned end = (m_node == m_endContainer) ? static_cast<unsigned>(m_endOffset) : INT_MAX;
while (m_textBox) {
unsigned textBoxStart = m_textBox->start();
unsigned runStart = max(textBoxStart, start);
InlineTextBox* firstTextBox = renderer->containsReversedText() ? (m_sortedTextBoxes.isEmpty() ? 0 : m_sortedTextBoxes[0]) : renderer->firstTextBox();
bool needSpace = m_lastTextNodeEndedWithCollapsedSpace
|| (m_textBox == firstTextBox && textBoxStart == runStart && runStart > 0);
if (needSpace && !renderer->style()->isCollapsibleWhiteSpace(m_lastCharacter) && m_lastCharacter) {
if (m_lastTextNode == m_node && runStart > 0 && str[runStart - 1] == ' ') {
unsigned spaceRunStart = runStart - 1;
while (spaceRunStart > 0 && str[spaceRunStart - 1] == ' ')
--spaceRunStart;
emitText(m_node, renderer, spaceRunStart, spaceRunStart + 1);
} else {
emitCharacter(' ', m_node, 0, runStart, runStart);
}
return;
}
unsigned textBoxEnd = textBoxStart + m_textBox->len();
unsigned runEnd = min(textBoxEnd, end);
InlineTextBox* nextTextBox = 0;
if (renderer->containsReversedText()) {
if (m_sortedTextBoxesPosition + 1 < m_sortedTextBoxes.size())
nextTextBox = m_sortedTextBoxes[m_sortedTextBoxesPosition + 1];
} else {
nextTextBox = m_textBox->nextTextBox();
}
ASSERT(!nextTextBox || nextTextBox->renderer() == renderer);
if (runStart < runEnd) {
if (str[runStart] == '\n') {
emitCharacter(' ', m_node, 0, runStart, runStart + 1);
m_offset = runStart + 1;
} else {
size_t subrunEnd = str.find('\n', runStart);
if (subrunEnd == kNotFound || subrunEnd > runEnd)
subrunEnd = runEnd;
m_offset = subrunEnd;
emitText(m_node, renderer, runStart, subrunEnd);
}
if (static_cast<unsigned>(m_positionEndOffset) < textBoxEnd)
return;
unsigned nextRunStart = nextTextBox ? nextTextBox->start() : str.length();
if (nextRunStart > runEnd)
m_lastTextNodeEndedWithCollapsedSpace = true;
m_textBox = nextTextBox;
if (renderer->containsReversedText())
++m_sortedTextBoxesPosition;
return;
}
m_textBox = nextTextBox;
if (renderer->containsReversedText())
++m_sortedTextBoxesPosition;
}
if (!m_textBox && m_remainingTextBox) {
m_textBox = m_remainingTextBox;
m_remainingTextBox = 0;
m_firstLetterText = 0;
m_offset = 0;
handleTextBox();
}
}
static inline RenderText* firstRenderTextInFirstLetter(RenderObject* firstLetter)
{
if (!firstLetter)
return 0;
for (RenderObject* current = firstLetter->firstChild(); current; current = current->nextSibling()) {
if (current->isText())
return toRenderText(current);
}
return 0;
}
void TextIterator::handleTextNodeFirstLetter(RenderTextFragment* renderer)
{
if (renderer->firstLetter()) {
RenderObject* r = renderer->firstLetter();
if (r->style()->visibility() != VISIBLE && !m_ignoresStyleVisibility)
return;
if (RenderText* firstLetter = firstRenderTextInFirstLetter(r)) {
m_handledFirstLetter = true;
m_remainingTextBox = m_textBox;
m_textBox = firstLetter->firstTextBox();
m_sortedTextBoxes.clear();
m_firstLetterText = firstLetter;
}
}
m_handledFirstLetter = true;
}
bool TextIterator::handleReplacedElement()
{
if (m_fullyClippedStack.top())
return false;
RenderObject* renderer = m_node->renderer();
if (renderer->style()->visibility() != VISIBLE && !m_ignoresStyleVisibility)
return false;
if (m_lastTextNodeEndedWithCollapsedSpace) {
emitCharacter(' ', m_lastTextNode->parentNode(), m_lastTextNode, 1, 1);
return false;
}
if (m_entersTextControls && renderer->isTextControl()) {
return true;
}
m_hasEmitted = true;
if (m_emitsCharactersBetweenAllVisiblePositions) {
emitCharacter(',', m_node->parentNode(), m_node, 0, 1);
return true;
}
m_positionNode = m_node->parentNode();
m_positionOffsetBaseNode = m_node;
m_positionStartOffset = 0;
m_positionEndOffset = 1;
m_singleCharacterBuffer = 0;
if (m_emitsImageAltText && renderer->isImage() && renderer->isRenderImage()) {
m_text = toRenderImage(renderer)->altText();
if (!m_text.isEmpty()) {
m_textLength = m_text.length();
m_lastCharacter = m_text[m_textLength - 1];
return true;
}
}
m_textLength = 0;
m_lastCharacter = 0;
return true;
}
bool TextIterator::hasVisibleTextNode(RenderText* renderer)
{
if (renderer->style()->visibility() == VISIBLE)
return true;
if (renderer->isTextFragment()) {
RenderTextFragment* fragment = toRenderTextFragment(renderer);
if (fragment->firstLetter() && fragment->firstLetter()->style()->visibility() == VISIBLE)
return true;
}
return false;
}
static bool shouldEmitTabBeforeNode(Node* node)
{
RenderObject* r = node->renderer();
if (!r || !isTableCell(node))
return false;
RenderTableCell* rc = toRenderTableCell(r);
RenderTable* t = rc->table();
return t && (t->cellBefore(rc) || t->cellAbove(rc));
}
static bool shouldEmitNewlineForNode(Node* node, bool emitsOriginalText)
{
RenderObject* renderer = node->renderer();
if (renderer ? !renderer->isBR() : !isHTMLBRElement(node))
return false;
return emitsOriginalText || !(node->isInShadowTree() && isHTMLInputElement(*node->shadowHost()));
}
static bool shouldEmitNewlinesBeforeAndAfterNode(Node& node)
{
RenderObject* r = node.renderer();
if (!r) {
return (node.hasTagName(blockquoteTag)
|| node.hasTagName(ddTag)
|| node.hasTagName(divTag)
|| node.hasTagName(dlTag)
|| node.hasTagName(dtTag)
|| node.hasTagName(h1Tag)
|| node.hasTagName(h2Tag)
|| node.hasTagName(h3Tag)
|| node.hasTagName(h4Tag)
|| node.hasTagName(h5Tag)
|| node.hasTagName(h6Tag)
|| node.hasTagName(hrTag)
|| node.hasTagName(liTag)
|| node.hasTagName(listingTag)
|| node.hasTagName(olTag)
|| node.hasTagName(pTag)
|| node.hasTagName(preTag)
|| node.hasTagName(trTag)
|| node.hasTagName(ulTag));
}
if (isTableCell(&node))
return false;
if (r->isTableRow()) {
RenderTable* t = toRenderTableRow(r)->table();
if (t && !t->isInline())
return true;
}
return !r->isInline() && r->isRenderBlock()
&& !r->isFloatingOrOutOfFlowPositioned() && !r->isBody() && !r->isRubyText();
}
static bool shouldEmitNewlineAfterNode(Node& node)
{
if (!shouldEmitNewlinesBeforeAndAfterNode(node))
return false;
Node* next = &node;
while ((next = NodeTraversal::nextSkippingChildren(*next))) {
if (next->renderer())
return true;
}
return false;
}
static bool shouldEmitNewlineBeforeNode(Node& node)
{
return shouldEmitNewlinesBeforeAndAfterNode(node);
}
static bool shouldEmitExtraNewlineForNode(Node* node)
{
RenderObject* r = node->renderer();
if (!r || !r->isBox())
return false;
if (node->hasTagName(h1Tag)
|| node->hasTagName(h2Tag)
|| node->hasTagName(h3Tag)
|| node->hasTagName(h4Tag)
|| node->hasTagName(h5Tag)
|| node->hasTagName(h6Tag)
|| node->hasTagName(pTag)) {
RenderStyle* style = r->style();
if (style) {
int bottomMargin = toRenderBox(r)->collapsedMarginAfter();
int fontSize = style->fontDescription().computedPixelSize();
if (bottomMargin * 2 >= fontSize)
return true;
}
}
return false;
}
static int collapsedSpaceLength(RenderText* renderer, int textEnd)
{
const String& text = renderer->text();
int length = text.length();
for (int i = textEnd; i < length; ++i) {
if (!renderer->style()->isCollapsibleWhiteSpace(text[i]))
return i - textEnd;
}
return length - textEnd;
}
static int maxOffsetIncludingCollapsedSpaces(Node* node)
{
int offset = caretMaxOffset(node);
if (node->renderer() && node->renderer()->isText())
offset += collapsedSpaceLength(toRenderText(node->renderer()), offset);
return offset;
}
bool TextIterator::shouldRepresentNodeOffsetZero()
{
if (m_emitsCharactersBetweenAllVisiblePositions && isRenderedTable(m_node))
return true;
if (m_lastCharacter == '\n')
return false;
if (m_hasEmitted)
return true;
if (m_node == m_startContainer)
return false;
if (!m_node->isDescendantOf(m_startContainer))
return true;
if (!m_startOffset)
return false;
if (!m_node->renderer() || m_node->renderer()->style()->visibility() != VISIBLE
|| (m_node->renderer()->isRenderBlockFlow() && !toRenderBlock(m_node->renderer())->height() && !isHTMLBodyElement(*m_node)))
return false;
VisiblePosition startPos = VisiblePosition(Position(m_startContainer, m_startOffset, Position::PositionIsOffsetInAnchor), DOWNSTREAM);
VisiblePosition currPos = VisiblePosition(positionBeforeNode(m_node), DOWNSTREAM);
return startPos.isNotNull() && currPos.isNotNull() && !inSameLine(startPos, currPos);
}
bool TextIterator::shouldEmitSpaceBeforeAndAfterNode(Node* node)
{
return isRenderedTable(node) && (node->renderer()->isInline() || m_emitsCharactersBetweenAllVisiblePositions);
}
void TextIterator::representNodeOffsetZero()
{
if (shouldEmitTabBeforeNode(m_node)) {
if (shouldRepresentNodeOffsetZero())
emitCharacter('\t', m_node->parentNode(), m_node, 0, 0);
} else if (shouldEmitNewlineBeforeNode(*m_node)) {
if (shouldRepresentNodeOffsetZero())
emitCharacter('\n', m_node->parentNode(), m_node, 0, 0);
} else if (shouldEmitSpaceBeforeAndAfterNode(m_node)) {
if (shouldRepresentNodeOffsetZero())
emitCharacter(' ', m_node->parentNode(), m_node, 0, 0);
}
}
bool TextIterator::handleNonTextNode()
{
if (shouldEmitNewlineForNode(m_node, m_emitsOriginalText))
emitCharacter('\n', m_node->parentNode(), m_node, 0, 1);
else if (m_emitsCharactersBetweenAllVisiblePositions && m_node->renderer() && m_node->renderer()->isHR())
emitCharacter(' ', m_node->parentNode(), m_node, 0, 1);
else
representNodeOffsetZero();
return true;
}
void TextIterator::exitNode()
{
if (!m_hasEmitted)
return;
Node* baseNode = m_node->lastChild() ? m_node->lastChild() : m_node;
if (m_lastTextNode && shouldEmitNewlineAfterNode(*m_node)) {
bool addNewline = shouldEmitExtraNewlineForNode(m_node);
if (m_lastCharacter != '\n') {
emitCharacter('\n', baseNode->parentNode(), baseNode, 1, 1);
ASSERT(!m_needsAnotherNewline);
m_needsAnotherNewline = addNewline;
} else if (addNewline) {
emitCharacter('\n', baseNode->parentNode(), baseNode, 1, 1);
}
}
if (!m_positionNode && shouldEmitSpaceBeforeAndAfterNode(m_node))
emitCharacter(' ', baseNode->parentNode(), baseNode, 1, 1);
}
void TextIterator::emitCharacter(UChar c, Node* textNode, Node* offsetBaseNode, int textStartOffset, int textEndOffset)
{
m_hasEmitted = true;
m_positionNode = textNode;
m_positionOffsetBaseNode = offsetBaseNode;
m_positionStartOffset = textStartOffset;
m_positionEndOffset = textEndOffset;
m_singleCharacterBuffer = c;
ASSERT(m_singleCharacterBuffer);
m_textLength = 1;
m_lastTextNodeEndedWithCollapsedSpace = false;
m_lastCharacter = c;
}
void TextIterator::emitText(Node* textNode, RenderObject* renderObject, int textStartOffset, int textEndOffset)
{
RenderText* renderer = toRenderText(renderObject);
m_text = m_emitsOriginalText ? renderer->originalText() : renderer->text();
ASSERT(!m_text.isEmpty());
ASSERT(0 <= textStartOffset && textStartOffset < static_cast<int>(m_text.length()));
ASSERT(0 <= textEndOffset && textEndOffset <= static_cast<int>(m_text.length()));
ASSERT(textStartOffset <= textEndOffset);
m_positionNode = textNode;
m_positionOffsetBaseNode = 0;
m_positionStartOffset = textStartOffset;
m_positionEndOffset = textEndOffset;
m_singleCharacterBuffer = 0;
m_textLength = textEndOffset - textStartOffset;
m_lastCharacter = m_text[textEndOffset - 1];
m_lastTextNodeEndedWithCollapsedSpace = false;
m_hasEmitted = true;
}
void TextIterator::emitText(Node* textNode, int textStartOffset, int textEndOffset)
{
emitText(textNode, m_node->renderer(), textStartOffset, textEndOffset);
}
PassRefPtrWillBeRawPtr<Range> TextIterator::range() const
{
if (m_positionNode) {
if (m_positionOffsetBaseNode) {
int index = m_positionOffsetBaseNode->nodeIndex();
m_positionStartOffset += index;
m_positionEndOffset += index;
m_positionOffsetBaseNode = 0;
}
return Range::create(m_positionNode->document(), m_positionNode, m_positionStartOffset, m_positionNode, m_positionEndOffset);
}
if (m_endContainer)
return Range::create(m_endContainer->document(), m_endContainer, m_endOffset, m_endContainer, m_endOffset);
return nullptr;
}
Node* TextIterator::node() const
{
RefPtrWillBeRawPtr<Range> textRange = range();
if (!textRange)
return 0;
Node* node = textRange->startContainer();
if (!node)
return 0;
if (node->offsetInCharacters())
return node;
return node->traverseToChildAt(textRange->startOffset());
}
SimplifiedBackwardsTextIterator::SimplifiedBackwardsTextIterator(const Range* r, TextIteratorBehaviorFlags behavior)
: m_node(0)
, m_offset(0)
, m_handledNode(false)
, m_handledChildren(false)
, m_startNode(0)
, m_startOffset(0)
, m_endNode(0)
, m_endOffset(0)
, m_positionNode(0)
, m_positionStartOffset(0)
, m_positionEndOffset(0)
, m_textOffset(0)
, m_textLength(0)
, m_lastTextNode(0)
, m_lastCharacter(0)
, m_singleCharacterBuffer(0)
, m_havePassedStartNode(false)
, m_shouldHandleFirstLetter(false)
, m_stopsOnFormControls(behavior & TextIteratorStopsOnFormControls)
, m_shouldStop(false)
, m_emitsOriginalText(false)
{
ASSERT(behavior == TextIteratorDefaultBehavior || behavior == TextIteratorStopsOnFormControls);
if (!r)
return;
Node* startNode = r->startContainer();
if (!startNode)
return;
Node* endNode = r->endContainer();
int startOffset = r->startOffset();
int endOffset = r->endOffset();
if (!startNode->offsetInCharacters() && startOffset >= 0) {
if (Node* childAtOffset = startNode->traverseToChildAt(startOffset)) {
startNode = childAtOffset;
startOffset = 0;
}
}
if (!endNode->offsetInCharacters() && endOffset > 0) {
if (Node* childAtOffset = endNode->traverseToChildAt(endOffset - 1)) {
endNode = childAtOffset;
endOffset = lastOffsetInNode(endNode);
}
}
m_node = endNode;
setUpFullyClippedStack(m_fullyClippedStack, m_node);
m_offset = endOffset;
m_handledNode = false;
m_handledChildren = !endOffset;
m_startNode = startNode;
m_startOffset = startOffset;
m_endNode = endNode;
m_endOffset = endOffset;
#ifndef NDEBUG
m_positionNode = endNode;
#endif
m_lastTextNode = 0;
m_lastCharacter = '\n';
m_havePassedStartNode = false;
advance();
}
void SimplifiedBackwardsTextIterator::advance()
{
ASSERT(m_positionNode);
if (m_shouldStop)
return;
if (m_stopsOnFormControls && HTMLFormControlElement::enclosingFormControlElement(m_node)) {
m_shouldStop = true;
return;
}
m_positionNode = 0;
m_textLength = 0;
while (m_node && !m_havePassedStartNode) {
if (!m_handledNode && !(m_node == m_endNode && !m_endOffset)) {
RenderObject* renderer = m_node->renderer();
if (renderer && renderer->isText() && m_node->nodeType() == Node::TEXT_NODE) {
if (renderer->style()->visibility() == VISIBLE && m_offset > 0)
m_handledNode = handleTextNode();
} else if (renderer && (renderer->isImage() || renderer->isWidget())) {
if (renderer->style()->visibility() == VISIBLE && m_offset > 0)
m_handledNode = handleReplacedElement();
} else {
m_handledNode = handleNonTextNode();
}
if (m_positionNode)
return;
}
if (!m_handledChildren && m_node->hasChildren()) {
m_node = m_node->lastChild();
pushFullyClippedState(m_fullyClippedStack, m_node);
} else {
if (!m_handledNode
&& canHaveChildrenForEditing(m_node)
&& m_node->parentNode()
&& (!m_node->lastChild() || (m_node == m_endNode && !m_endOffset))) {
exitNode();
if (m_positionNode) {
m_handledNode = true;
m_handledChildren = true;
return;
}
}
while (!m_node->previousSibling()) {
if (!advanceRespectingRange(m_node->parentOrShadowHostNode()))
break;
m_fullyClippedStack.pop();
exitNode();
if (m_positionNode) {
m_handledNode = true;
m_handledChildren = true;
return;
}
}
m_fullyClippedStack.pop();
if (advanceRespectingRange(m_node->previousSibling()))
pushFullyClippedState(m_fullyClippedStack, m_node);
else
m_node = 0;
}
m_offset = m_node ? maxOffsetIncludingCollapsedSpaces(m_node) : 0;
m_handledNode = false;
m_handledChildren = false;
if (m_positionNode)
return;
}
}
bool SimplifiedBackwardsTextIterator::handleTextNode()
{
m_lastTextNode = m_node;
int startOffset;
int offsetInNode;
RenderText* renderer = handleFirstLetter(startOffset, offsetInNode);
if (!renderer)
return true;
String text = renderer->text();
if (!renderer->firstTextBox() && text.length() > 0)
return true;
m_positionEndOffset = m_offset;
m_offset = startOffset + offsetInNode;
m_positionNode = m_node;
m_positionStartOffset = m_offset;
ASSERT(0 <= m_positionStartOffset - offsetInNode && m_positionStartOffset - offsetInNode <= static_cast<int>(text.length()));
ASSERT(1 <= m_positionEndOffset - offsetInNode && m_positionEndOffset - offsetInNode <= static_cast<int>(text.length()));
ASSERT(m_positionStartOffset <= m_positionEndOffset);
m_textLength = m_positionEndOffset - m_positionStartOffset;
m_textOffset = m_positionStartOffset - offsetInNode;
m_textContainer = text;
m_singleCharacterBuffer = 0;
RELEASE_ASSERT(static_cast<unsigned>(m_textOffset + m_textLength) <= text.length());
m_lastCharacter = text[m_positionEndOffset - 1];
return !m_shouldHandleFirstLetter;
}
RenderText* SimplifiedBackwardsTextIterator::handleFirstLetter(int& startOffset, int& offsetInNode)
{
RenderText* renderer = toRenderText(m_node->renderer());
startOffset = (m_node == m_startNode) ? m_startOffset : 0;
if (!renderer->isTextFragment()) {
offsetInNode = 0;
return renderer;
}
RenderTextFragment* fragment = toRenderTextFragment(renderer);
int offsetAfterFirstLetter = fragment->start();
if (startOffset >= offsetAfterFirstLetter) {
ASSERT(!m_shouldHandleFirstLetter);
offsetInNode = offsetAfterFirstLetter;
return renderer;
}
if (!m_shouldHandleFirstLetter && offsetAfterFirstLetter < m_offset) {
m_shouldHandleFirstLetter = true;
offsetInNode = offsetAfterFirstLetter;
return renderer;
}
m_shouldHandleFirstLetter = false;
offsetInNode = 0;
RenderText* firstLetterRenderer = firstRenderTextInFirstLetter(fragment->firstLetter());
m_offset = firstLetterRenderer->caretMaxOffset();
m_offset += collapsedSpaceLength(firstLetterRenderer, m_offset);
return firstLetterRenderer;
}
bool SimplifiedBackwardsTextIterator::handleReplacedElement()
{
unsigned index = m_node->nodeIndex();
emitCharacter(',', m_node->parentNode(), index, index + 1);
return true;
}
bool SimplifiedBackwardsTextIterator::handleNonTextNode()
{
if (shouldEmitNewlineForNode(m_node, m_emitsOriginalText) || shouldEmitNewlineAfterNode(*m_node) || shouldEmitTabBeforeNode(m_node)) {
unsigned index = m_node->nodeIndex();
emitCharacter('\n', m_node->parentNode(), index + 1, index + 1);
}
return true;
}
void SimplifiedBackwardsTextIterator::exitNode()
{
if (shouldEmitNewlineForNode(m_node, m_emitsOriginalText) || shouldEmitNewlineBeforeNode(*m_node) || shouldEmitTabBeforeNode(m_node)) {
emitCharacter('\n', m_node, 0, 0);
}
}
void SimplifiedBackwardsTextIterator::emitCharacter(UChar c, Node* node, int startOffset, int endOffset)
{
m_singleCharacterBuffer = c;
m_positionNode = node;
m_positionStartOffset = startOffset;
m_positionEndOffset = endOffset;
m_textOffset = 0;
m_textLength = 1;
m_lastCharacter = c;
}
bool SimplifiedBackwardsTextIterator::advanceRespectingRange(Node* next)
{
if (!next)
return false;
m_havePassedStartNode |= m_node == m_startNode;
if (m_havePassedStartNode)
return false;
m_node = next;
return true;
}
PassRefPtrWillBeRawPtr<Range> SimplifiedBackwardsTextIterator::range() const
{
if (m_positionNode)
return Range::create(m_positionNode->document(), m_positionNode, m_positionStartOffset, m_positionNode, m_positionEndOffset);
return Range::create(m_startNode->document(), m_startNode, m_startOffset, m_startNode, m_startOffset);
}
CharacterIterator::CharacterIterator(const Range* range, TextIteratorBehaviorFlags behavior)
: m_offset(0)
, m_runOffset(0)
, m_atBreak(true)
, m_textIterator(range, behavior)
{
initialize();
}
CharacterIterator::CharacterIterator(const Position& start, const Position& end, TextIteratorBehaviorFlags behavior)
: m_offset(0)
, m_runOffset(0)
, m_atBreak(true)
, m_textIterator(start, end, behavior)
{
initialize();
}
void CharacterIterator::initialize()
{
while (!atEnd() && !m_textIterator.length())
m_textIterator.advance();
}
PassRefPtrWillBeRawPtr<Range> CharacterIterator::range() const
{
RefPtrWillBeRawPtr<Range> r = m_textIterator.range();
if (!m_textIterator.atEnd()) {
if (m_textIterator.length() <= 1) {
ASSERT(!m_runOffset);
} else {
Node* n = r->startContainer();
ASSERT(n == r->endContainer());
int offset = r->startOffset() + m_runOffset;
r->setStart(n, offset, ASSERT_NO_EXCEPTION);
r->setEnd(n, offset + 1, ASSERT_NO_EXCEPTION);
}
}
return r.release();
}
void CharacterIterator::advance(int count)
{
if (count <= 0) {
ASSERT(!count);
return;
}
m_atBreak = false;
int remaining = m_textIterator.length() - m_runOffset;
if (count < remaining) {
m_runOffset += count;
m_offset += count;
return;
}
count -= remaining;
m_offset += remaining;
for (m_textIterator.advance(); !atEnd(); m_textIterator.advance()) {
int runLength = m_textIterator.length();
if (!runLength) {
m_atBreak = true;
} else {
if (count < runLength) {
m_runOffset = count;
m_offset += count;
return;
}
count -= runLength;
m_offset += runLength;
}
}
m_atBreak = true;
m_runOffset = 0;
}
static PassRefPtrWillBeRawPtr<Range> characterSubrange(CharacterIterator& it, int offset, int length)
{
it.advance(offset);
RefPtrWillBeRawPtr<Range> start = it.range();
if (length > 1)
it.advance(length - 1);
RefPtrWillBeRawPtr<Range> end = it.range();
return Range::create(start->startContainer()->document(),
start->startContainer(), start->startOffset(),
end->endContainer(), end->endOffset());
}
BackwardsCharacterIterator::BackwardsCharacterIterator(const Range* range, TextIteratorBehaviorFlags behavior)
: m_offset(0)
, m_runOffset(0)
, m_atBreak(true)
, m_textIterator(range, behavior)
{
while (!atEnd() && !m_textIterator.length())
m_textIterator.advance();
}
PassRefPtrWillBeRawPtr<Range> BackwardsCharacterIterator::range() const
{
RefPtrWillBeRawPtr<Range> r = m_textIterator.range();
if (!m_textIterator.atEnd()) {
if (m_textIterator.length() <= 1) {
ASSERT(!m_runOffset);
} else {
Node* n = r->startContainer();
ASSERT(n == r->endContainer());
int offset = r->endOffset() - m_runOffset;
r->setStart(n, offset - 1, ASSERT_NO_EXCEPTION);
r->setEnd(n, offset, ASSERT_NO_EXCEPTION);
}
}
return r.release();
}
void BackwardsCharacterIterator::advance(int count)
{
if (count <= 0) {
ASSERT(!count);
return;
}
m_atBreak = false;
int remaining = m_textIterator.length() - m_runOffset;
if (count < remaining) {
m_runOffset += count;
m_offset += count;
return;
}
count -= remaining;
m_offset += remaining;
for (m_textIterator.advance(); !atEnd(); m_textIterator.advance()) {
int runLength = m_textIterator.length();
if (!runLength) {
m_atBreak = true;
} else {
if (count < runLength) {
m_runOffset = count;
m_offset += count;
return;
}
count -= runLength;
m_offset += runLength;
}
}
m_atBreak = true;
m_runOffset = 0;
}
WordAwareIterator::WordAwareIterator(const Range* range)
: m_didLookAhead(true)
, m_textIterator(range)
{
advance();
}
WordAwareIterator::~WordAwareIterator()
{
}
void WordAwareIterator::advance()
{
m_buffer.clear();
if (!m_didLookAhead) {
ASSERT(!m_textIterator.atEnd());
m_textIterator.advance();
}
m_didLookAhead = false;
while (!m_textIterator.atEnd() && !m_textIterator.length())
m_textIterator.advance();
m_range = m_textIterator.range();
if (m_textIterator.atEnd())
return;
while (1) {
if (isSpaceOrNewline(m_textIterator.characterAt(m_textIterator.length() - 1)))
return;
if (m_buffer.isEmpty())
m_textIterator.appendTextTo(m_buffer);
m_textIterator.advance();
if (m_textIterator.atEnd() || !m_textIterator.length() || isSpaceOrNewline(m_textIterator.characterAt(0))) {
m_didLookAhead = true;
return;
}
m_textIterator.appendTextTo(m_buffer);
m_range->setEnd(m_textIterator.range()->endContainer(), m_textIterator.range()->endOffset(), IGNORE_EXCEPTION);
}
}
int WordAwareIterator::length() const
{
if (!m_buffer.isEmpty())
return m_buffer.size();
return m_textIterator.length();
}
String WordAwareIterator::substring(unsigned position, unsigned length) const
{
if (!m_buffer.isEmpty())
return String(m_buffer.data() + position, length);
return m_textIterator.substring(position, length);
}
UChar WordAwareIterator::characterAt(unsigned index) const
{
if (!m_buffer.isEmpty())
return m_buffer[index];
return m_textIterator.characterAt(index);
}
static const size_t minimumSearchBufferSize = 8192;
#ifndef NDEBUG
static bool searcherInUse;
#endif
static UStringSearch* createSearcher()
{
UErrorCode status = U_ZERO_ERROR;
String searchCollatorName = currentSearchLocaleID() + String("@collation=search");
UStringSearch* searcher = usearch_open(&newlineCharacter, 1, &newlineCharacter, 1, searchCollatorName.utf8().data(), 0, &status);
ASSERT(status == U_ZERO_ERROR || status == U_USING_FALLBACK_WARNING || status == U_USING_DEFAULT_WARNING);
return searcher;
}
static UStringSearch* searcher()
{
static UStringSearch* searcher = createSearcher();
return searcher;
}
static inline void lockSearcher()
{
#ifndef NDEBUG
ASSERT(!searcherInUse);
searcherInUse = true;
#endif
}
static inline void unlockSearcher()
{
#ifndef NDEBUG
ASSERT(searcherInUse);
searcherInUse = false;
#endif
}
inline SearchBuffer::SearchBuffer(const String& target, FindOptions options)
: m_options(options)
, m_prefixLength(0)
, m_numberOfCharactersJustAppended(0)
, m_atBreak(true)
, m_needsMoreContext(options & AtWordStarts)
, m_targetRequiresKanaWorkaround(containsKanaLetters(target))
{
ASSERT(!target.isEmpty());
target.appendTo(m_target);
foldQuoteMarksAndSoftHyphens(m_target.data(), m_target.size());
size_t targetLength = m_target.size();
m_buffer.reserveInitialCapacity(max(targetLength * 8, minimumSearchBufferSize));
m_overlap = m_buffer.capacity() / 4;
if ((m_options & AtWordStarts) && targetLength) {
UChar32 targetFirstCharacter;
U16_GET(m_target.data(), 0, 0, targetLength, targetFirstCharacter);
if (isSeparator(targetFirstCharacter)) {
m_options &= ~AtWordStarts;
m_needsMoreContext = false;
}
}
lockSearcher();
UStringSearch* searcher = WebCore::searcher();
UCollator* collator = usearch_getCollator(searcher);
UCollationStrength strength = m_options & CaseInsensitive ? UCOL_PRIMARY : UCOL_TERTIARY;
if (ucol_getStrength(collator) != strength) {
ucol_setStrength(collator, strength);
usearch_reset(searcher);
}
UErrorCode status = U_ZERO_ERROR;
usearch_setPattern(searcher, m_target.data(), targetLength, &status);
ASSERT(status == U_ZERO_ERROR);
if (m_targetRequiresKanaWorkaround)
normalizeCharactersIntoNFCForm(m_target.data(), m_target.size(), m_normalizedTarget);
}
inline SearchBuffer::~SearchBuffer()
{
UErrorCode status = U_ZERO_ERROR;
usearch_setPattern(WebCore::searcher(), &newlineCharacter, 1, &status);
ASSERT(status == U_ZERO_ERROR);
unlockSearcher();
}
template<typename CharType>
inline void SearchBuffer::append(const CharType* characters, size_t length)
{
ASSERT(length);
if (m_atBreak) {
m_buffer.shrink(0);
m_prefixLength = 0;
m_atBreak = false;
} else if (m_buffer.size() == m_buffer.capacity()) {
memcpy(m_buffer.data(), m_buffer.data() + m_buffer.size() - m_overlap, m_overlap * sizeof(UChar));
m_prefixLength -= min(m_prefixLength, m_buffer.size() - m_overlap);
m_buffer.shrink(m_overlap);
}
size_t oldLength = m_buffer.size();
size_t usableLength = min(m_buffer.capacity() - oldLength, length);
ASSERT(usableLength);
m_buffer.resize(oldLength + usableLength);
UChar* destination = m_buffer.data() + oldLength;
StringImpl::copyChars(destination, characters, usableLength);
foldQuoteMarksAndSoftHyphens(destination, usableLength);
m_numberOfCharactersJustAppended = usableLength;
}
inline bool SearchBuffer::needsMoreContext() const
{
return m_needsMoreContext;
}
inline void SearchBuffer::prependContext(const UChar* characters, size_t length)
{
ASSERT(m_needsMoreContext);
ASSERT(m_prefixLength == m_buffer.size());
if (!length)
return;
m_atBreak = false;
size_t wordBoundaryContextStart = length;
if (wordBoundaryContextStart) {
U16_BACK_1(characters, 0, wordBoundaryContextStart);
wordBoundaryContextStart = startOfLastWordBoundaryContext(characters, wordBoundaryContextStart);
}
size_t usableLength = min(m_buffer.capacity() - m_prefixLength, length - wordBoundaryContextStart);
m_buffer.prepend(characters + length - usableLength, usableLength);
m_prefixLength += usableLength;
if (wordBoundaryContextStart || m_prefixLength == m_buffer.capacity())
m_needsMoreContext = false;
}
inline bool SearchBuffer::atBreak() const
{
return m_atBreak;
}
inline void SearchBuffer::reachedBreak()
{
m_atBreak = true;
}
inline bool SearchBuffer::isBadMatch(const UChar* match, size_t matchLength) const
{
if (!m_targetRequiresKanaWorkaround)
return false;
normalizeCharactersIntoNFCForm(match, matchLength, m_normalizedMatch);
return !checkOnlyKanaLettersInStrings(m_normalizedTarget.begin(), m_normalizedTarget.size(), m_normalizedMatch.begin(), m_normalizedMatch.size());
}
inline bool SearchBuffer::isWordStartMatch(size_t start, size_t length) const
{
ASSERT(m_options & AtWordStarts);
if (!start)
return true;
int size = m_buffer.size();
int offset = start;
UChar32 firstCharacter;
U16_GET(m_buffer.data(), 0, offset, size, firstCharacter);
if (m_options & TreatMedialCapitalAsWordStart) {
UChar32 previousCharacter;
U16_PREV(m_buffer.data(), 0, offset, previousCharacter);
if (isSeparator(firstCharacter)) {
if (!isSeparator(previousCharacter))
return true;
} else if (isASCIIUpper(firstCharacter)) {
if (!isASCIIUpper(previousCharacter))
return true;
offset = start;
U16_FWD_1(m_buffer.data(), offset, size);
UChar32 nextCharacter = 0;
if (offset < size)
U16_GET(m_buffer.data(), 0, offset, size, nextCharacter);
if (!isASCIIUpper(nextCharacter) && !isASCIIDigit(nextCharacter) && !isSeparator(nextCharacter))
return true;
} else if (isASCIIDigit(firstCharacter)) {
if (!isASCIIDigit(previousCharacter))
return true;
} else if (isSeparator(previousCharacter) || isASCIIDigit(previousCharacter)) {
return true;
}
}
if (Character::isCJKIdeographOrSymbol(firstCharacter))
return true;
size_t wordBreakSearchStart = start + length;
while (wordBreakSearchStart > start)
wordBreakSearchStart = findNextWordFromIndex(m_buffer.data(), m_buffer.size(), wordBreakSearchStart, false );
return wordBreakSearchStart == start;
}
inline size_t SearchBuffer::search(size_t& start)
{
size_t size = m_buffer.size();
if (m_atBreak) {
if (!size)
return 0;
} else {
if (size != m_buffer.capacity())
return 0;
}
UStringSearch* searcher = WebCore::searcher();
UErrorCode status = U_ZERO_ERROR;
usearch_setText(searcher, m_buffer.data(), size, &status);
ASSERT(status == U_ZERO_ERROR);
usearch_setOffset(searcher, m_prefixLength, &status);
ASSERT(status == U_ZERO_ERROR);
int matchStart = usearch_next(searcher, &status);
ASSERT(status == U_ZERO_ERROR);
nextMatch:
if (!(matchStart >= 0 && static_cast<size_t>(matchStart) < size)) {
ASSERT(matchStart == USEARCH_DONE);
return 0;
}
if (!m_atBreak && static_cast<size_t>(matchStart) >= size - m_overlap) {
size_t overlap = m_overlap;
if (m_options & AtWordStarts) {
int wordBoundaryContextStart = matchStart;
U16_BACK_1(m_buffer.data(), 0, wordBoundaryContextStart);
wordBoundaryContextStart = startOfLastWordBoundaryContext(m_buffer.data(), wordBoundaryContextStart);
overlap = min(size - 1, max(overlap, size - wordBoundaryContextStart));
}
memcpy(m_buffer.data(), m_buffer.data() + size - overlap, overlap * sizeof(UChar));
m_prefixLength -= min(m_prefixLength, size - overlap);
m_buffer.shrink(overlap);
return 0;
}
size_t matchedLength = usearch_getMatchedLength(searcher);
ASSERT_WITH_SECURITY_IMPLICATION(matchStart + matchedLength <= size);
if (isBadMatch(m_buffer.data() + matchStart, matchedLength) || ((m_options & AtWordStarts) && !isWordStartMatch(matchStart, matchedLength))) {
matchStart = usearch_next(searcher, &status);
ASSERT(status == U_ZERO_ERROR);
goto nextMatch;
}
size_t newSize = size - (matchStart + 1);
memmove(m_buffer.data(), m_buffer.data() + matchStart + 1, newSize * sizeof(UChar));
m_prefixLength -= min<size_t>(m_prefixLength, matchStart + 1);
m_buffer.shrink(newSize);
start = size - matchStart;
return matchedLength;
}
int TextIterator::rangeLength(const Range* r, bool forSelectionPreservation)
{
int length = 0;
for (TextIterator it(r, forSelectionPreservation ? TextIteratorEmitsCharactersBetweenAllVisiblePositions : TextIteratorDefaultBehavior); !it.atEnd(); it.advance())
length += it.length();
return length;
}
PassRefPtrWillBeRawPtr<Range> TextIterator::subrange(Range* entireRange, int characterOffset, int characterCount)
{
CharacterIterator entireRangeIterator(entireRange);
return characterSubrange(entireRangeIterator, characterOffset, characterCount);
}
String plainText(const Range* r, TextIteratorBehaviorFlags behavior)
{
static const unsigned initialCapacity = 1 << 15;
unsigned bufferLength = 0;
StringBuilder builder;
builder.reserveCapacity(initialCapacity);
for (TextIterator it(r, behavior); !it.atEnd(); it.advance()) {
it.appendTextToStringBuilder(builder);
bufferLength += it.length();
}
if (!bufferLength)
return emptyString();
return builder.toString();
}
static PassRefPtrWillBeRawPtr<Range> collapsedToBoundary(const Range* range, bool forward)
{
RefPtrWillBeRawPtr<Range> result = range->cloneRange(ASSERT_NO_EXCEPTION);
result->collapse(!forward, ASSERT_NO_EXCEPTION);
return result.release();
}
static size_t findPlainTextInternal(CharacterIterator& it, const String& target, FindOptions options, size_t& matchStart)
{
matchStart = 0;
size_t matchLength = 0;
SearchBuffer buffer(target, options);
if (buffer.needsMoreContext()) {
RefPtrWillBeRawPtr<Range> startRange = it.range();
RefPtrWillBeRawPtr<Range> beforeStartRange = startRange->ownerDocument().createRange();
beforeStartRange->setEnd(startRange->startContainer(), startRange->startOffset(), IGNORE_EXCEPTION);
for (SimplifiedBackwardsTextIterator backwardsIterator(beforeStartRange.get()); !backwardsIterator.atEnd(); backwardsIterator.advance()) {
Vector<UChar, 1024> characters;
backwardsIterator.prependTextTo(characters);
buffer.prependContext(characters.data(), characters.size());
if (!buffer.needsMoreContext())
break;
}
}
while (!it.atEnd()) {
it.appendTextTo(buffer);
it.advance(buffer.numberOfCharactersJustAppended());
tryAgain:
size_t matchStartOffset;
if (size_t newMatchLength = buffer.search(matchStartOffset)) {
size_t lastCharacterInBufferOffset = it.characterOffset();
ASSERT(lastCharacterInBufferOffset >= matchStartOffset);
matchStart = lastCharacterInBufferOffset - matchStartOffset;
matchLength = newMatchLength;
if (!(options & Backwards))
break;
goto tryAgain;
}
if (it.atBreak() && !buffer.atBreak()) {
buffer.reachedBreak();
goto tryAgain;
}
}
return matchLength;
}
static const TextIteratorBehaviorFlags iteratorFlagsForFindPlainText = TextIteratorEntersTextControls | TextIteratorEntersAuthorShadowRoots;
PassRefPtrWillBeRawPtr<Range> findPlainText(const Range* range, const String& target, FindOptions options)
{
range->ownerDocument().updateLayout();
size_t matchStart;
size_t matchLength;
{
CharacterIterator findIterator(range, iteratorFlagsForFindPlainText);
matchLength = findPlainTextInternal(findIterator, target, options, matchStart);
if (!matchLength)
return collapsedToBoundary(range, !(options & Backwards));
}
CharacterIterator computeRangeIterator(range, iteratorFlagsForFindPlainText);
return characterSubrange(computeRangeIterator, matchStart, matchLength);
}
PassRefPtrWillBeRawPtr<Range> findPlainText(const Position& start, const Position& end, const String& target, FindOptions options)
{
if (!start.inDocument())
return nullptr;
ASSERT(start.document() == end.document());
start.document()->updateLayout();
size_t matchStart;
size_t matchLength;
{
CharacterIterator findIterator(start, end, iteratorFlagsForFindPlainText);
matchLength = findPlainTextInternal(findIterator, target, options, matchStart);
if (!matchLength) {
const Position& collapseTo = options & Backwards ? start : end;
return Range::create(*start.document(), collapseTo, collapseTo);
}
}
CharacterIterator computeRangeIterator(start, end, iteratorFlagsForFindPlainText);
return characterSubrange(computeRangeIterator, matchStart, matchLength);
}
}