Changeset 69909 in webkit


Ignore:
Timestamp:
Oct 15, 2010 8:16:18 PM (14 years ago)
Author:
rniwa@webkit.org
Message:

2010-10-15 Ryosuke Niwa <rniwa@webkit.org>

Reviewed by Tony Chang.

serializeNodesWithNamespaces should be in MarkupAccumulator
https://bugs.webkit.org/show_bug.cgi?id=47749

Moved serializeNodesWithNamespaces to MarkupAccumulator, and renamed it to serializeNodes.
MarkupAccumulator::serializeNode now returns the resultant string instead of having a separate takeResults().
Added several helper functions to MarkupAccumulator.

No new tests are added since this is a cleanup.

  • editing/MarkupAccumulator.cpp: (WebCore::MarkupAccumulator::serializeNodes): Wrapper for serializeNodes and concatenateMarkup. (WebCore::MarkupAccumulator::serializeNodesWithNamespaces): Moved from markup.cpp (WebCore::MarkupAccumulator::appendStartTag): Calls appendString instead of appending into m_succeedingMarkup directly. (WebCore::MarkupAccumulator::appendEndTag): Ditto. (WebCore::MarkupAccumulator::totalLength): Added; extracted from takeResults. (WebCore::MarkupAccumulator::concatenateMarkup): Ditto.
  • editing/MarkupAccumulator.h: (WebCore::MarkupAccumulator::length): Added; calls totalLength.
  • editing/markup.cpp: (WebCore::StyledMarkupAccumulator::appendString): Added; calls MarkupAccumulator's appendString. (WebCore::StyledMarkupAccumulator::wrapWithStyleNode): Calls appendString. (WebCore::StyledMarkupAccumulator::takeResults): Calls length, totalLength, and concatenateMarkup. (WebCore::StyledMarkupAccumulator::serializeNodes): Became a member function. (WebCore::createMarkup): Uses MarkupAccumulator.
Location:
trunk/WebCore
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r69907 r69909  
     12010-10-15  Ryosuke Niwa  <rniwa@webkit.org>
     2
     3        Reviewed by Tony Chang.
     4
     5        serializeNodesWithNamespaces should be in MarkupAccumulator
     6        https://bugs.webkit.org/show_bug.cgi?id=47749
     7
     8        Moved serializeNodesWithNamespaces to MarkupAccumulator, and renamed it to serializeNodes.
     9        MarkupAccumulator::serializeNode now returns the resultant string instead of having a separate takeResults().
     10        Added several helper functions to MarkupAccumulator.
     11
     12        No new tests are added since this is a cleanup.
     13
     14        * editing/MarkupAccumulator.cpp:
     15        (WebCore::MarkupAccumulator::serializeNodes): Wrapper for serializeNodes and concatenateMarkup.
     16        (WebCore::MarkupAccumulator::serializeNodesWithNamespaces): Moved from markup.cpp
     17        (WebCore::MarkupAccumulator::appendStartTag): Calls appendString instead of appending into m_succeedingMarkup directly.
     18        (WebCore::MarkupAccumulator::appendEndTag): Ditto.
     19        (WebCore::MarkupAccumulator::totalLength): Added; extracted from takeResults.
     20        (WebCore::MarkupAccumulator::concatenateMarkup): Ditto.
     21        * editing/MarkupAccumulator.h:
     22        (WebCore::MarkupAccumulator::length): Added; calls totalLength.
     23        * editing/markup.cpp:
     24        (WebCore::StyledMarkupAccumulator::appendString): Added; calls MarkupAccumulator's appendString.
     25        (WebCore::StyledMarkupAccumulator::wrapWithStyleNode): Calls appendString.
     26        (WebCore::StyledMarkupAccumulator::takeResults): Calls length, totalLength, and concatenateMarkup.
     27        (WebCore::StyledMarkupAccumulator::serializeNodes): Became a member function.
     28        (WebCore::createMarkup): Uses MarkupAccumulator.
     29
    1302010-10-15  Kinuko Yasuda  <kinuko@google.com>
    231
  • trunk/WebCore/editing/MarkupAccumulator.cpp

    r69880 r69909  
    11/*
    22 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
     3 * Copyright (C) 2009, 2010 Google Inc. All rights reserved.
    34 *
    45 * Redistribution and use in source and binary forms, with or without
     
    1112 *    documentation and/or other materials provided with the distribution.
    1213 *
    13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
    14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
    17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    2223 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
     24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2425 */
    2526
     
    8485}
    8586
     87String MarkupAccumulator::serializeNodes(Node* node, Node* nodeToSkip, EChildrenOnly childrenOnly)
     88{
     89    Vector<UChar> out;
     90    serializeNodesWithNamespaces(node, nodeToSkip, childrenOnly, 0);
     91    out.reserveInitialCapacity(length());
     92    concatenateMarkup(out);
     93    return String::adopt(out);
     94}
     95
     96void MarkupAccumulator::serializeNodesWithNamespaces(Node* node, Node* nodeToSkip, EChildrenOnly childrenOnly, const Namespaces* namespaces)
     97{
     98    if (node == nodeToSkip)
     99        return;
     100
     101    Namespaces namespaceHash;
     102    if (namespaces)
     103        namespaceHash = *namespaces;
     104
     105    if (!childrenOnly)
     106        appendStartTag(node, &namespaceHash);
     107
     108    if (!(node->document()->isHTMLDocument() && elementCannotHaveEndTag(node))) {
     109        for (Node* current = node->firstChild(); current; current = current->nextSibling())
     110            serializeNodesWithNamespaces(current, nodeToSkip, IncludeNode, &namespaceHash);
     111    }
     112
     113    if (!childrenOnly)
     114        appendEndTag(node);
     115}
     116
    86117void MarkupAccumulator::appendString(const String& string)
    87118{
     
    93124    Vector<UChar> markup;
    94125    appendStartMarkup(markup, node, namespaces);
    95     m_succeedingMarkup.append(String::adopt(markup));
     126    appendString(String::adopt(markup));
    96127    if (m_nodes)
    97128        m_nodes->append(node);
     
    102133    Vector<UChar> markup;
    103134    appendEndMarkup(markup, node);
    104     m_succeedingMarkup.append(String::adopt(markup));
     135    appendString(String::adopt(markup));
     136}
     137
     138size_t MarkupAccumulator::totalLength(const Vector<String>& strings)
     139{
     140    size_t length = 0;
     141    for (size_t i = 0; i < strings.size(); ++i)
     142        length += strings[i].length();
     143    return length;
    105144}
    106145
     
    108147// We're converting results of appendStartMarkup and appendEndMarkup from Vector<UChar> to String
    109148// and then back to Vector<UChar> and again to String here.
    110 String MarkupAccumulator::takeResults()
    111 {
    112     size_t length = 0;
    113 
    114     size_t postCount = m_succeedingMarkup.size();
    115     for (size_t i = 0; i < postCount; ++i)
    116         length += m_succeedingMarkup[i].length();
    117 
    118     Vector<UChar> result;
    119     result.reserveInitialCapacity(length);
    120 
    121     for (size_t i = 0; i < postCount; ++i)
    122         append(result, m_succeedingMarkup[i]);
    123 
    124     return String::adopt(result);
     149void MarkupAccumulator::concatenateMarkup(Vector<UChar>& out)
     150{
     151    for (size_t i = 0; i < m_succeedingMarkup.size(); ++i)
     152        append(out, m_succeedingMarkup[i]);
    125153}
    126154
  • trunk/WebCore/editing/MarkupAccumulator.h

    r69880 r69909  
    7070    virtual ~MarkupAccumulator();
    7171
     72    String serializeNodes(Node* node, Node* nodeToSkip, EChildrenOnly childrenOnly);
     73
     74protected:
    7275    void appendString(const String&);
    7376    void appendStartTag(Node*, Namespaces* = 0);
    7477    void appendEndTag(Node*);
    75     virtual String takeResults();
    76 
    77 protected:
     78    static size_t totalLength(const Vector<String>&);
     79    size_t length() const { return totalLength(m_succeedingMarkup); }
     80    void concatenateMarkup(Vector<UChar>& out);
    7881    void appendAttributeValue(Vector<UChar>& result, const String& attribute, bool documentIsHTML);
    7982    void appendQuotedURLAttributeValue(Vector<UChar>& result, const String& urlString);
     
    100103    Vector<Node*>* const m_nodes;
    101104    const Range* const m_range;
    102     Vector<String> m_succeedingMarkup;
    103105
    104106private:
     107    void serializeNodesWithNamespaces(Node*, Node* nodeToSkip, EChildrenOnly, const Namespaces*);
     108
     109    Vector<String> m_succeedingMarkup;
    105110    const bool m_shouldResolveURLs;
    106111};
  • trunk/WebCore/editing/markup.cpp

    r69880 r69909  
    140140    {
    141141    }
     142
     143    Node* serializeNodes(Node* startNode, Node* pastEnd);
     144    void appendString(const String& s) { return MarkupAccumulator::appendString(s); }
    142145    void wrapWithNode(Node*, bool convertBlocksToInlines = false, RangeFullySelectsNode = DoesFullySelectNode);
    143146    void wrapWithStyleNode(CSSStyleDeclaration*, Document*, bool isBlock = false);
    144147    String takeResults();
    145148
    146 protected:
     149private:
    147150    virtual void appendText(Vector<UChar>& out, Text*);
    148151    String renderedText(const Node*, const Range*);
     
    154157    bool shouldAnnotate() { return m_shouldAnnotate == AnnotateForInterchange; }
    155158
    156 private:
    157159    Vector<String> m_reversedPrecedingMarkup;
    158160    const EAnnotateForInterchange m_shouldAnnotate;
     
    187189    openTag.append('>');
    188190    m_reversedPrecedingMarkup.append(String::adopt(openTag));
    189     m_succeedingMarkup.append(isBlock ? divClose : styleSpanClose);
     191    appendString(isBlock ? divClose : styleSpanClose);
    190192}
    191193
    192194String StyledMarkupAccumulator::takeResults()
    193195{
    194     size_t length = 0;
    195 
    196     size_t preCount = m_reversedPrecedingMarkup.size();
    197     for (size_t i = 0; i < preCount; ++i)
    198         length += m_reversedPrecedingMarkup[i].length();
    199 
    200     size_t postCount = m_succeedingMarkup.size();
    201     for (size_t i = 0; i < postCount; ++i)
    202         length += m_succeedingMarkup[i].length();
    203 
    204196    Vector<UChar> result;
    205     result.reserveInitialCapacity(length);
    206 
    207     for (size_t i = preCount; i > 0; --i)
     197    result.reserveInitialCapacity(totalLength(m_reversedPrecedingMarkup) + length());
     198
     199    for (size_t i = m_reversedPrecedingMarkup.size(); i > 0; --i)
    208200        append(result, m_reversedPrecedingMarkup[i - 1]);
    209201
    210     for (size_t i = 0; i < postCount; ++i)
    211         append(result, m_succeedingMarkup[i]);
     202    concatenateMarkup(result);
    212203
    213204    return String::adopt(result);
     
    343334}
    344335
    345 static Node* serializeNodes(StyledMarkupAccumulator& accumulator, Node* startNode, Node* pastEnd)
     336Node* StyledMarkupAccumulator::serializeNodes(Node* startNode, Node* pastEnd)
    346337{
    347338    Vector<Node*> ancestorsToClose;
     
    371362        } else {
    372363            // Add the node to the markup if we're not skipping the descendants
    373             accumulator.appendStartTag(n);
     364            appendStartTag(n);
    374365
    375366            // If node has no children, close the tag now.
    376367            if (!n->childNodeCount()) {
    377                 accumulator.appendEndTag(n);
     368                appendEndTag(n);
    378369                lastClosed = n;
    379370            } else {
     
    392383                    break;
    393384                // Not at the end of the range, close ancestors up to sibling of next node.
    394                 accumulator.appendEndTag(ancestor);
     385                appendEndTag(ancestor);
    395386                lastClosed = ancestor;
    396387                ancestorsToClose.removeLast();
     
    407398                    // or b) ancestors that we never encountered during a pre-order traversal starting at startNode:
    408399                    ASSERT(startNode->isDescendantOf(parent));
    409                     accumulator.wrapWithNode(parent);
     400                    wrapWithNode(parent);
    410401                    lastClosed = parent;
    411402                }
     
    631622    Node* specialCommonAncestor = highestAncestorToWrapMarkup(updatedRange.get(), fullySelectedRoot, shouldAnnotate);
    632623
    633     Node* lastClosed = serializeNodes(accumulator, startNode, pastEnd);
     624    Node* lastClosed = accumulator.serializeNodes(startNode, pastEnd);
    634625
    635626    if (specialCommonAncestor && lastClosed) {
     
    727718}
    728719
    729 static void serializeNodesWithNamespaces(MarkupAccumulator& accumulator, Node* node, Node* nodeToSkip, EChildrenOnly childrenOnly, const Namespaces* namespaces)
    730 {
    731     if (node == nodeToSkip)
    732         return;
    733 
    734     Namespaces namespaceHash;
    735     if (namespaces)
    736         namespaceHash = *namespaces;
    737 
    738     if (!childrenOnly)
    739         accumulator.appendStartTag(node, &namespaceHash);
    740 
    741     if (!(node->document()->isHTMLDocument() && elementCannotHaveEndTag(node))) {
    742         for (Node* current = node->firstChild(); current; current = current->nextSibling())
    743             serializeNodesWithNamespaces(accumulator, current, nodeToSkip, IncludeNode, &namespaceHash);
    744     }
    745 
    746     if (!childrenOnly)
    747         accumulator.appendEndTag(node);
    748 }
    749 
    750720String createMarkup(const Node* node, EChildrenOnly childrenOnly, Vector<Node*>* nodes, EAbsoluteURLs shouldResolveURLs)
    751721{
     
    761731
    762732    MarkupAccumulator accumulator(nodes, shouldResolveURLs);
    763     serializeNodesWithNamespaces(accumulator, const_cast<Node*>(node), deleteButtonContainerElement, childrenOnly, 0);
    764     return accumulator.takeResults();
     733    return accumulator.serializeNodes(const_cast<Node*>(node), deleteButtonContainerElement, childrenOnly);
    765734}
    766735
Note: See TracChangeset for help on using the changeset viewer.