WebCore/rendering/RenderRubyBase.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2009 Google Inc. All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions are
       
     6  * met:
       
     7  *
       
     8  *     * Redistributions of source code must retain the above copyright
       
     9  * notice, this list of conditions and the following disclaimer.
       
    10  *     * Redistributions in binary form must reproduce the above
       
    11  * copyright notice, this list of conditions and the following disclaimer
       
    12  * in the documentation and/or other materials provided with the
       
    13  * distribution.
       
    14  *     * Neither the name of Google Inc. nor the names of its
       
    15  * contributors may be used to endorse or promote products derived from
       
    16  * this software without specific prior written permission.
       
    17  *
       
    18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    29  */
       
    30 
       
    31 #include "config.h"
       
    32 
       
    33 #if ENABLE(RUBY)
       
    34 #include "RenderRubyBase.h"
       
    35 
       
    36 namespace WebCore {
       
    37 
       
    38 RenderRubyBase::RenderRubyBase(Node* node)
       
    39     : RenderBlock(node)
       
    40 {
       
    41     setInline(false);
       
    42 }
       
    43 
       
    44 RenderRubyBase::~RenderRubyBase()
       
    45 {
       
    46 }
       
    47 
       
    48 bool RenderRubyBase::isChildAllowed(RenderObject* child, RenderStyle*) const
       
    49 {
       
    50     return child->isInline();
       
    51 }
       
    52 
       
    53 bool RenderRubyBase::hasOnlyWrappedInlineChildren(RenderObject* beforeChild) const
       
    54 {
       
    55     // Tests whether all children in the base before beforeChild are either floated/positioned,
       
    56     // or inline objects wrapped in anonymous blocks.
       
    57     // Note that beforeChild may be 0, in which case all children are looked at.
       
    58     for (RenderObject* child = firstChild(); child != beforeChild; child = child->nextSibling()) {
       
    59         if (!child->isFloatingOrPositioned() && !(child->isAnonymousBlock() && child->childrenInline()))
       
    60             return false;
       
    61     }
       
    62     return true;
       
    63 }
       
    64 
       
    65 void RenderRubyBase::moveChildren(RenderRubyBase* toBase, RenderObject* fromBeforeChild)
       
    66 {
       
    67     // This function removes all children that are before (!) beforeChild
       
    68     // and appends them to toBase.
       
    69     ASSERT(toBase);
       
    70     
       
    71     // First make sure that beforeChild (if set) is indeed a direct child of this.
       
    72     // Inline children might be wrapped in an anonymous block if there's a continuation.
       
    73     // Theoretically, in ruby bases, this can happen with only the first such a child,
       
    74     // so it should be OK to just climb the tree.
       
    75     while (fromBeforeChild && fromBeforeChild->parent() != this)
       
    76         fromBeforeChild = fromBeforeChild->parent();
       
    77 
       
    78     if (childrenInline())
       
    79         moveInlineChildren(toBase, fromBeforeChild);
       
    80     else
       
    81         moveBlockChildren(toBase, fromBeforeChild);
       
    82 
       
    83     setNeedsLayoutAndPrefWidthsRecalc();
       
    84     toBase->setNeedsLayoutAndPrefWidthsRecalc();
       
    85 }
       
    86 
       
    87 void RenderRubyBase::moveInlineChildren(RenderRubyBase* toBase, RenderObject* fromBeforeChild)
       
    88 {
       
    89     RenderBlock* toBlock;
       
    90 
       
    91     if (toBase->childrenInline()) {
       
    92         // The standard and easy case: move the children into the target base
       
    93         toBlock = toBase;
       
    94     } else {
       
    95         // We need to wrap the inline objects into an anonymous block.
       
    96         // If toBase has a suitable block, we re-use it, otherwise create a new one.
       
    97         RenderObject* lastChild = toBase->lastChild();
       
    98         if (lastChild && lastChild->isAnonymousBlock() && lastChild->childrenInline())
       
    99             toBlock = toRenderBlock(lastChild);
       
   100         else {
       
   101             toBlock = toBase->createAnonymousBlock();
       
   102             toBase->children()->appendChildNode(toBase, toBlock);
       
   103         }
       
   104     }
       
   105     // Move our inline children into the target block we determined above.
       
   106     moveChildrenTo(toBlock, firstChild(), fromBeforeChild);
       
   107 }
       
   108 
       
   109 void RenderRubyBase::moveBlockChildren(RenderRubyBase* toBase, RenderObject* fromBeforeChild)
       
   110 {
       
   111     if (toBase->childrenInline()) {
       
   112         // First check whether we move only wrapped inline objects.
       
   113         if (hasOnlyWrappedInlineChildren(fromBeforeChild)) {
       
   114             // The reason why the base is in block flow must be after beforeChild.
       
   115             // We therefore can extract the inline objects and move them to toBase.
       
   116             for (RenderObject* child = firstChild(); child != fromBeforeChild; child = firstChild()) {
       
   117                 if (child->isAnonymousBlock()) {
       
   118                     RenderBlock* anonBlock = toRenderBlock(child);
       
   119                     ASSERT(anonBlock->childrenInline());
       
   120                     ASSERT(!anonBlock->inlineElementContinuation());
       
   121                     anonBlock->moveAllChildrenTo(toBase, toBase->children());
       
   122                     anonBlock->deleteLineBoxTree();
       
   123                     anonBlock->destroy();
       
   124                 } else {
       
   125                     ASSERT(child->isFloatingOrPositioned());
       
   126                     moveChildTo(toBase, child);
       
   127                 }
       
   128             }
       
   129         } else {
       
   130             // Moving block children -> have to set toBase as block flow
       
   131             toBase->makeChildrenNonInline();
       
   132             // Move children, potentially collapsing anonymous block wrappers.
       
   133             mergeBlockChildren(toBase, fromBeforeChild);
       
   134 
       
   135             // Now we need to check if the leftover children are all inline.
       
   136             // If so, make this base inline again.
       
   137             if (hasOnlyWrappedInlineChildren()) {
       
   138                 RenderObject* next = 0;
       
   139                 for (RenderObject* child = firstChild(); child; child = next) {
       
   140                     next = child->nextSibling();
       
   141                     if (child->isFloatingOrPositioned())
       
   142                         continue;
       
   143                     ASSERT(child->isAnonymousBlock());
       
   144 
       
   145                     RenderBlock* anonBlock = toRenderBlock(child);
       
   146                     ASSERT(anonBlock->childrenInline());
       
   147                     ASSERT(!anonBlock->inlineElementContinuation());
       
   148                     // Move inline children out of anonymous block.
       
   149                     anonBlock->moveAllChildrenTo(this, anonBlock);
       
   150                     anonBlock->deleteLineBoxTree();
       
   151                     anonBlock->destroy();
       
   152                 }
       
   153                 setChildrenInline(true);
       
   154             }
       
   155         }
       
   156     } else
       
   157         mergeBlockChildren(toBase, fromBeforeChild);
       
   158 }
       
   159 
       
   160 void RenderRubyBase::mergeBlockChildren(RenderRubyBase* toBase, RenderObject* fromBeforeChild)
       
   161 {
       
   162     // This function removes all children that are before fromBeforeChild and appends them to toBase.
       
   163     ASSERT(!childrenInline());
       
   164     ASSERT(toBase);
       
   165     ASSERT(!toBase->childrenInline());
       
   166 
       
   167     // Quick check whether we have anything to do, to simplify the following code.
       
   168     if (fromBeforeChild != firstChild())
       
   169         return;
       
   170 
       
   171     // If an anonymous block would be put next to another such block, then merge those.
       
   172     RenderObject* firstChildHere = firstChild();
       
   173     RenderObject* lastChildThere = toBase->lastChild();
       
   174     if (firstChildHere && firstChildHere->isAnonymousBlock() && firstChildHere->childrenInline() 
       
   175             && lastChildThere && lastChildThere->isAnonymousBlock() && lastChildThere->childrenInline()) {            
       
   176         RenderBlock* anonBlockHere = toRenderBlock(firstChildHere);
       
   177         RenderBlock* anonBlockThere = toRenderBlock(lastChildThere);
       
   178         anonBlockHere->moveAllChildrenTo(anonBlockThere, anonBlockThere->children());
       
   179         anonBlockHere->deleteLineBoxTree();
       
   180         anonBlockHere->destroy();
       
   181     }
       
   182     // Move all remaining children normally.
       
   183     moveChildrenTo(toBase, firstChild(), fromBeforeChild);
       
   184 }
       
   185 
       
   186 } // namespace WebCore
       
   187 
       
   188 #endif // ENABLE(RUBY)