OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All r
ights reserved. | 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All r
ights reserved. |
3 * Copyright (C) 2005 Alexey Proskuryakov. | 3 * Copyright (C) 2005 Alexey Proskuryakov. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 #include "platform/text/TextBoundaries.h" | 48 #include "platform/text/TextBoundaries.h" |
49 #include "platform/text/TextBreakIteratorInternalICU.h" | 49 #include "platform/text/TextBreakIteratorInternalICU.h" |
50 #include "platform/text/UnicodeUtilities.h" | 50 #include "platform/text/UnicodeUtilities.h" |
51 #include "wtf/text/CString.h" | 51 #include "wtf/text/CString.h" |
52 #include "wtf/text/StringBuilder.h" | 52 #include "wtf/text/StringBuilder.h" |
53 #include "wtf/unicode/CharacterNames.h" | 53 #include "wtf/unicode/CharacterNames.h" |
54 #include <unicode/usearch.h> | 54 #include <unicode/usearch.h> |
55 #include <unicode/utf16.h> | 55 #include <unicode/utf16.h> |
56 | 56 |
57 using namespace WTF::Unicode; | 57 using namespace WTF::Unicode; |
| 58 using namespace std; |
58 | 59 |
59 namespace WebCore { | 60 namespace WebCore { |
60 | 61 |
61 using namespace HTMLNames; | 62 using namespace HTMLNames; |
62 | 63 |
63 // Buffer that knows how to compare with a search target. | 64 // Buffer that knows how to compare with a search target. |
64 // Keeps enough of the previous text to be able to search in the future, but no
more. | 65 // Keeps enough of the previous text to be able to search in the future, but no
more. |
65 // Non-breaking spaces are always equal to normal spaces. | 66 // Non-breaking spaces are always equal to normal spaces. |
66 // Case folding is also done if the CaseInsensitive option is specified. | 67 // Case folding is also done if the CaseInsensitive option is specified. |
67 // Matches are further filtered if the AtWordStarts option is specified, althoug
h some | 68 // Matches are further filtered if the AtWordStarts option is specified, althoug
h some |
(...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
610 emitText(m_node, m_firstLetterText, m_offset, m_offset + firstLe
tter.length()); | 611 emitText(m_node, m_firstLetterText, m_offset, m_offset + firstLe
tter.length()); |
611 m_firstLetterText = 0; | 612 m_firstLetterText = 0; |
612 m_textBox = 0; | 613 m_textBox = 0; |
613 return false; | 614 return false; |
614 } | 615 } |
615 } | 616 } |
616 if (renderer->style()->visibility() != VISIBLE && !m_ignoresStyleVisibil
ity) | 617 if (renderer->style()->visibility() != VISIBLE && !m_ignoresStyleVisibil
ity) |
617 return false; | 618 return false; |
618 int strLength = str.length(); | 619 int strLength = str.length(); |
619 int end = (m_node == m_endContainer) ? m_endOffset : INT_MAX; | 620 int end = (m_node == m_endContainer) ? m_endOffset : INT_MAX; |
620 int runEnd = std::min(strLength, end); | 621 int runEnd = min(strLength, end); |
621 | 622 |
622 if (runStart >= runEnd) | 623 if (runStart >= runEnd) |
623 return true; | 624 return true; |
624 | 625 |
625 emitText(m_node, runStart, runEnd); | 626 emitText(m_node, runStart, runEnd); |
626 return true; | 627 return true; |
627 } | 628 } |
628 | 629 |
629 if (renderer->firstTextBox()) | 630 if (renderer->firstTextBox()) |
630 m_textBox = renderer->firstTextBox(); | 631 m_textBox = renderer->firstTextBox(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 RenderText* renderer = m_firstLetterText ? m_firstLetterText : toRenderText(
m_node->renderer()); | 664 RenderText* renderer = m_firstLetterText ? m_firstLetterText : toRenderText(
m_node->renderer()); |
664 if (renderer->style()->visibility() != VISIBLE && !m_ignoresStyleVisibility)
{ | 665 if (renderer->style()->visibility() != VISIBLE && !m_ignoresStyleVisibility)
{ |
665 m_textBox = 0; | 666 m_textBox = 0; |
666 return; | 667 return; |
667 } | 668 } |
668 String str = renderer->text(); | 669 String str = renderer->text(); |
669 unsigned start = m_offset; | 670 unsigned start = m_offset; |
670 unsigned end = (m_node == m_endContainer) ? static_cast<unsigned>(m_endOffse
t) : INT_MAX; | 671 unsigned end = (m_node == m_endContainer) ? static_cast<unsigned>(m_endOffse
t) : INT_MAX; |
671 while (m_textBox) { | 672 while (m_textBox) { |
672 unsigned textBoxStart = m_textBox->start(); | 673 unsigned textBoxStart = m_textBox->start(); |
673 unsigned runStart = std::max(textBoxStart, start); | 674 unsigned runStart = max(textBoxStart, start); |
674 | 675 |
675 // Check for collapsed space at the start of this run. | 676 // Check for collapsed space at the start of this run. |
676 InlineTextBox* firstTextBox = renderer->containsReversedText() ? (m_sort
edTextBoxes.isEmpty() ? 0 : m_sortedTextBoxes[0]) : renderer->firstTextBox(); | 677 InlineTextBox* firstTextBox = renderer->containsReversedText() ? (m_sort
edTextBoxes.isEmpty() ? 0 : m_sortedTextBoxes[0]) : renderer->firstTextBox(); |
677 bool needSpace = m_lastTextNodeEndedWithCollapsedSpace | 678 bool needSpace = m_lastTextNodeEndedWithCollapsedSpace |
678 || (m_textBox == firstTextBox && textBoxStart == runStart && runStar
t > 0); | 679 || (m_textBox == firstTextBox && textBoxStart == runStart && runStar
t > 0); |
679 if (needSpace && !renderer->style()->isCollapsibleWhiteSpace(m_lastChara
cter) && m_lastCharacter) { | 680 if (needSpace && !renderer->style()->isCollapsibleWhiteSpace(m_lastChara
cter) && m_lastCharacter) { |
680 if (m_lastTextNode == m_node && runStart > 0 && str[runStart - 1] ==
' ') { | 681 if (m_lastTextNode == m_node && runStart > 0 && str[runStart - 1] ==
' ') { |
681 unsigned spaceRunStart = runStart - 1; | 682 unsigned spaceRunStart = runStart - 1; |
682 while (spaceRunStart > 0 && str[spaceRunStart - 1] == ' ') | 683 while (spaceRunStart > 0 && str[spaceRunStart - 1] == ' ') |
683 --spaceRunStart; | 684 --spaceRunStart; |
684 emitText(m_node, renderer, spaceRunStart, spaceRunStart + 1); | 685 emitText(m_node, renderer, spaceRunStart, spaceRunStart + 1); |
685 } else { | 686 } else { |
686 emitCharacter(' ', m_node, 0, runStart, runStart); | 687 emitCharacter(' ', m_node, 0, runStart, runStart); |
687 } | 688 } |
688 return; | 689 return; |
689 } | 690 } |
690 unsigned textBoxEnd = textBoxStart + m_textBox->len(); | 691 unsigned textBoxEnd = textBoxStart + m_textBox->len(); |
691 unsigned runEnd = std::min(textBoxEnd, end); | 692 unsigned runEnd = min(textBoxEnd, end); |
692 | 693 |
693 // Determine what the next text box will be, but don't advance yet | 694 // Determine what the next text box will be, but don't advance yet |
694 InlineTextBox* nextTextBox = 0; | 695 InlineTextBox* nextTextBox = 0; |
695 if (renderer->containsReversedText()) { | 696 if (renderer->containsReversedText()) { |
696 if (m_sortedTextBoxesPosition + 1 < m_sortedTextBoxes.size()) | 697 if (m_sortedTextBoxesPosition + 1 < m_sortedTextBoxes.size()) |
697 nextTextBox = m_sortedTextBoxes[m_sortedTextBoxesPosition + 1]; | 698 nextTextBox = m_sortedTextBoxes[m_sortedTextBoxesPosition + 1]; |
698 } else { | 699 } else { |
699 nextTextBox = m_textBox->nextTextBox(); | 700 nextTextBox = m_textBox->nextTextBox(); |
700 } | 701 } |
701 ASSERT(!nextTextBox || nextTextBox->renderer() == renderer); | 702 ASSERT(!nextTextBox || nextTextBox->renderer() == renderer); |
(...skipping 1082 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1784 { | 1785 { |
1785 ASSERT(!target.isEmpty()); | 1786 ASSERT(!target.isEmpty()); |
1786 target.appendTo(m_target); | 1787 target.appendTo(m_target); |
1787 | 1788 |
1788 // FIXME: We'd like to tailor the searcher to fold quote marks for us instea
d | 1789 // FIXME: We'd like to tailor the searcher to fold quote marks for us instea
d |
1789 // of doing it in a separate replacement pass here, but ICU doesn't offer a
way | 1790 // of doing it in a separate replacement pass here, but ICU doesn't offer a
way |
1790 // to add tailoring on top of the locale-specific tailoring as of this writi
ng. | 1791 // to add tailoring on top of the locale-specific tailoring as of this writi
ng. |
1791 foldQuoteMarksAndSoftHyphens(m_target.data(), m_target.size()); | 1792 foldQuoteMarksAndSoftHyphens(m_target.data(), m_target.size()); |
1792 | 1793 |
1793 size_t targetLength = m_target.size(); | 1794 size_t targetLength = m_target.size(); |
1794 m_buffer.reserveInitialCapacity(std::max(targetLength * 8, minimumSearchBuff
erSize)); | 1795 m_buffer.reserveInitialCapacity(max(targetLength * 8, minimumSearchBufferSiz
e)); |
1795 m_overlap = m_buffer.capacity() / 4; | 1796 m_overlap = m_buffer.capacity() / 4; |
1796 | 1797 |
1797 if ((m_options & AtWordStarts) && targetLength) { | 1798 if ((m_options & AtWordStarts) && targetLength) { |
1798 UChar32 targetFirstCharacter; | 1799 UChar32 targetFirstCharacter; |
1799 U16_GET(m_target.data(), 0, 0, targetLength, targetFirstCharacter); | 1800 U16_GET(m_target.data(), 0, 0, targetLength, targetFirstCharacter); |
1800 // Characters in the separator category never really occur at the beginn
ing of a word, | 1801 // Characters in the separator category never really occur at the beginn
ing of a word, |
1801 // so if the target begins with such a character, we just ignore the AtW
ordStart option. | 1802 // so if the target begins with such a character, we just ignore the AtW
ordStart option. |
1802 if (isSeparator(targetFirstCharacter)) { | 1803 if (isSeparator(targetFirstCharacter)) { |
1803 m_options &= ~AtWordStarts; | 1804 m_options &= ~AtWordStarts; |
1804 m_needsMoreContext = false; | 1805 m_needsMoreContext = false; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1842 inline void SearchBuffer::append(const CharType* characters, size_t length) | 1843 inline void SearchBuffer::append(const CharType* characters, size_t length) |
1843 { | 1844 { |
1844 ASSERT(length); | 1845 ASSERT(length); |
1845 | 1846 |
1846 if (m_atBreak) { | 1847 if (m_atBreak) { |
1847 m_buffer.shrink(0); | 1848 m_buffer.shrink(0); |
1848 m_prefixLength = 0; | 1849 m_prefixLength = 0; |
1849 m_atBreak = false; | 1850 m_atBreak = false; |
1850 } else if (m_buffer.size() == m_buffer.capacity()) { | 1851 } else if (m_buffer.size() == m_buffer.capacity()) { |
1851 memcpy(m_buffer.data(), m_buffer.data() + m_buffer.size() - m_overlap, m
_overlap * sizeof(UChar)); | 1852 memcpy(m_buffer.data(), m_buffer.data() + m_buffer.size() - m_overlap, m
_overlap * sizeof(UChar)); |
1852 m_prefixLength -= std::min(m_prefixLength, m_buffer.size() - m_overlap); | 1853 m_prefixLength -= min(m_prefixLength, m_buffer.size() - m_overlap); |
1853 m_buffer.shrink(m_overlap); | 1854 m_buffer.shrink(m_overlap); |
1854 } | 1855 } |
1855 | 1856 |
1856 size_t oldLength = m_buffer.size(); | 1857 size_t oldLength = m_buffer.size(); |
1857 size_t usableLength = std::min(m_buffer.capacity() - oldLength, length); | 1858 size_t usableLength = min(m_buffer.capacity() - oldLength, length); |
1858 ASSERT(usableLength); | 1859 ASSERT(usableLength); |
1859 m_buffer.resize(oldLength + usableLength); | 1860 m_buffer.resize(oldLength + usableLength); |
1860 UChar* destination = m_buffer.data() + oldLength; | 1861 UChar* destination = m_buffer.data() + oldLength; |
1861 StringImpl::copyChars(destination, characters, usableLength); | 1862 StringImpl::copyChars(destination, characters, usableLength); |
1862 foldQuoteMarksAndSoftHyphens(destination, usableLength); | 1863 foldQuoteMarksAndSoftHyphens(destination, usableLength); |
1863 m_numberOfCharactersJustAppended = usableLength; | 1864 m_numberOfCharactersJustAppended = usableLength; |
1864 } | 1865 } |
1865 | 1866 |
1866 inline bool SearchBuffer::needsMoreContext() const | 1867 inline bool SearchBuffer::needsMoreContext() const |
1867 { | 1868 { |
1868 return m_needsMoreContext; | 1869 return m_needsMoreContext; |
1869 } | 1870 } |
1870 | 1871 |
1871 inline void SearchBuffer::prependContext(const UChar* characters, size_t length) | 1872 inline void SearchBuffer::prependContext(const UChar* characters, size_t length) |
1872 { | 1873 { |
1873 ASSERT(m_needsMoreContext); | 1874 ASSERT(m_needsMoreContext); |
1874 ASSERT(m_prefixLength == m_buffer.size()); | 1875 ASSERT(m_prefixLength == m_buffer.size()); |
1875 | 1876 |
1876 if (!length) | 1877 if (!length) |
1877 return; | 1878 return; |
1878 | 1879 |
1879 m_atBreak = false; | 1880 m_atBreak = false; |
1880 | 1881 |
1881 size_t wordBoundaryContextStart = length; | 1882 size_t wordBoundaryContextStart = length; |
1882 if (wordBoundaryContextStart) { | 1883 if (wordBoundaryContextStart) { |
1883 U16_BACK_1(characters, 0, wordBoundaryContextStart); | 1884 U16_BACK_1(characters, 0, wordBoundaryContextStart); |
1884 wordBoundaryContextStart = startOfLastWordBoundaryContext(characters, wo
rdBoundaryContextStart); | 1885 wordBoundaryContextStart = startOfLastWordBoundaryContext(characters, wo
rdBoundaryContextStart); |
1885 } | 1886 } |
1886 | 1887 |
1887 size_t usableLength = std::min(m_buffer.capacity() - m_prefixLength, length
- wordBoundaryContextStart); | 1888 size_t usableLength = min(m_buffer.capacity() - m_prefixLength, length - wor
dBoundaryContextStart); |
1888 m_buffer.prepend(characters + length - usableLength, usableLength); | 1889 m_buffer.prepend(characters + length - usableLength, usableLength); |
1889 m_prefixLength += usableLength; | 1890 m_prefixLength += usableLength; |
1890 | 1891 |
1891 if (wordBoundaryContextStart || m_prefixLength == m_buffer.capacity()) | 1892 if (wordBoundaryContextStart || m_prefixLength == m_buffer.capacity()) |
1892 m_needsMoreContext = false; | 1893 m_needsMoreContext = false; |
1893 } | 1894 } |
1894 | 1895 |
1895 inline bool SearchBuffer::atBreak() const | 1896 inline bool SearchBuffer::atBreak() const |
1896 { | 1897 { |
1897 return m_atBreak; | 1898 return m_atBreak; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2004 // The same match may appear later, matching more characters, | 2005 // The same match may appear later, matching more characters, |
2005 // possibly including a combining character that's not yet in the buffer. | 2006 // possibly including a combining character that's not yet in the buffer. |
2006 if (!m_atBreak && static_cast<size_t>(matchStart) >= size - m_overlap) { | 2007 if (!m_atBreak && static_cast<size_t>(matchStart) >= size - m_overlap) { |
2007 size_t overlap = m_overlap; | 2008 size_t overlap = m_overlap; |
2008 if (m_options & AtWordStarts) { | 2009 if (m_options & AtWordStarts) { |
2009 // Ensure that there is sufficient context before matchStart the nex
t time around for | 2010 // Ensure that there is sufficient context before matchStart the nex
t time around for |
2010 // determining if it is at a word boundary. | 2011 // determining if it is at a word boundary. |
2011 int wordBoundaryContextStart = matchStart; | 2012 int wordBoundaryContextStart = matchStart; |
2012 U16_BACK_1(m_buffer.data(), 0, wordBoundaryContextStart); | 2013 U16_BACK_1(m_buffer.data(), 0, wordBoundaryContextStart); |
2013 wordBoundaryContextStart = startOfLastWordBoundaryContext(m_buffer.d
ata(), wordBoundaryContextStart); | 2014 wordBoundaryContextStart = startOfLastWordBoundaryContext(m_buffer.d
ata(), wordBoundaryContextStart); |
2014 overlap = std::min(size - 1, std::max(overlap, size - wordBoundaryCo
ntextStart)); | 2015 overlap = min(size - 1, max(overlap, size - wordBoundaryContextStart
)); |
2015 } | 2016 } |
2016 memcpy(m_buffer.data(), m_buffer.data() + size - overlap, overlap * size
of(UChar)); | 2017 memcpy(m_buffer.data(), m_buffer.data() + size - overlap, overlap * size
of(UChar)); |
2017 m_prefixLength -= std::min(m_prefixLength, size - overlap); | 2018 m_prefixLength -= min(m_prefixLength, size - overlap); |
2018 m_buffer.shrink(overlap); | 2019 m_buffer.shrink(overlap); |
2019 return 0; | 2020 return 0; |
2020 } | 2021 } |
2021 | 2022 |
2022 size_t matchedLength = usearch_getMatchedLength(searcher); | 2023 size_t matchedLength = usearch_getMatchedLength(searcher); |
2023 ASSERT_WITH_SECURITY_IMPLICATION(matchStart + matchedLength <= size); | 2024 ASSERT_WITH_SECURITY_IMPLICATION(matchStart + matchedLength <= size); |
2024 | 2025 |
2025 // If this match is "bad", move on to the next match. | 2026 // If this match is "bad", move on to the next match. |
2026 if (isBadMatch(m_buffer.data() + matchStart, matchedLength) || ((m_options &
AtWordStarts) && !isWordStartMatch(matchStart, matchedLength))) { | 2027 if (isBadMatch(m_buffer.data() + matchStart, matchedLength) || ((m_options &
AtWordStarts) && !isWordStartMatch(matchStart, matchedLength))) { |
2027 matchStart = usearch_next(searcher, &status); | 2028 matchStart = usearch_next(searcher, &status); |
2028 ASSERT(status == U_ZERO_ERROR); | 2029 ASSERT(status == U_ZERO_ERROR); |
2029 goto nextMatch; | 2030 goto nextMatch; |
2030 } | 2031 } |
2031 | 2032 |
2032 size_t newSize = size - (matchStart + 1); | 2033 size_t newSize = size - (matchStart + 1); |
2033 memmove(m_buffer.data(), m_buffer.data() + matchStart + 1, newSize * sizeof(
UChar)); | 2034 memmove(m_buffer.data(), m_buffer.data() + matchStart + 1, newSize * sizeof(
UChar)); |
2034 m_prefixLength -= std::min<size_t>(m_prefixLength, matchStart + 1); | 2035 m_prefixLength -= min<size_t>(m_prefixLength, matchStart + 1); |
2035 m_buffer.shrink(newSize); | 2036 m_buffer.shrink(newSize); |
2036 | 2037 |
2037 start = size - matchStart; | 2038 start = size - matchStart; |
2038 return matchedLength; | 2039 return matchedLength; |
2039 } | 2040 } |
2040 | 2041 |
2041 // -------- | 2042 // -------- |
2042 | 2043 |
2043 int TextIterator::rangeLength(const Range* r, bool forSelectionPreservation) | 2044 int TextIterator::rangeLength(const Range* r, bool forSelectionPreservation) |
2044 { | 2045 { |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2201 resultEnd = collapseTo; | 2202 resultEnd = collapseTo; |
2202 return; | 2203 return; |
2203 } | 2204 } |
2204 } | 2205 } |
2205 | 2206 |
2206 CharacterIterator computeRangeIterator(inputStart, inputEnd, iteratorFlagsFo
rFindPlainText); | 2207 CharacterIterator computeRangeIterator(inputStart, inputEnd, iteratorFlagsFo
rFindPlainText); |
2207 calculateCharacterSubrange(computeRangeIterator, matchStart, matchLength, re
sultStart, resultEnd); | 2208 calculateCharacterSubrange(computeRangeIterator, matchStart, matchLength, re
sultStart, resultEnd); |
2208 } | 2209 } |
2209 | 2210 |
2210 } | 2211 } |
OLD | NEW |