https://bugs.webkit.org/show_bug.cgi?id=48664
        
Reviewed by Simon Fraser.

Make repaint invalidation work with flipped blocks ("rl" and "bt" writing modes).
        
This patch disables the layout state for flipped blocks, since the paint offset isn't right.
        
Bugs in the overflow sides for lines have been fixed.  before/AfterSideVisible/LayoutOverflowForLine
now respect flipping (and don't incorrectly change sides).  This is a very confusing aspect of
the overflow stuff (and at some point we should consider renaming top/left/right/bottom since they
aren't physical).
        
offsetFromContainer and mapLocalToContainer remain physical and refer to the top left offset.
This means at every step up you convert to the correct physical coordinate.  It also means these
functions will be unreliable during layout for flipped blocks.  At first glance, this seems to be
more or less ok.
        
computeRectForRepaint now delays flipping conversions until you cross a writing-mode boundary.  This
allows for documents to be fully flipped and repaint during layout can still be reliable.  This is
critical for incremental line layout repainting.
        
Added new tests in fast/repaint and also rebaselined the tests in that directory because I'm tired
of all the stale results and failures.

WebCore: 

* editing/SelectionController.cpp:
(WebCore::SelectionController::absoluteBoundsForLocalRect):
(WebCore::SelectionController::paintCaret):
* rendering/InlineTextBox.cpp:
(WebCore::InlineTextBox::selectionRect):
* rendering/RenderBlock.cpp:
(WebCore::RenderBlock::layoutBlock):
(WebCore::RenderBlock::layoutOnlyPositionedObjects):
(WebCore::RenderBlock::paintSelection):
* rendering/RenderBlockLineLayout.cpp:
(WebCore::RenderBlock::beforeSideVisibleOverflowForLine):
(WebCore::RenderBlock::afterSideVisibleOverflowForLine):
(WebCore::RenderBlock::beforeSideLayoutOverflowForLine):
(WebCore::RenderBlock::afterSideLayoutOverflowForLine):
* rendering/RenderBox.cpp:
(WebCore::RenderBox::layout):
(WebCore::RenderBox::mapLocalToContainer):
(WebCore::RenderBox::offsetFromContainer):
(WebCore::RenderBox::clippedOverflowRectForRepaint):
(WebCore::RenderBox::computeRectForRepaint):
(WebCore::RenderBox::flipForWritingMode):
(WebCore::RenderBox::locationOffsetIncludingFlipping):
* rendering/RenderBox.h:
* rendering/RenderFlexibleBox.cpp:
(WebCore::RenderFlexibleBox::layoutBlock):
* rendering/RenderInline.cpp:
(WebCore::RenderInline::clippedOverflowRectForRepaint):
* rendering/RenderSlider.cpp:
(WebCore::RenderSlider::layout):
* rendering/RenderTable.cpp:
(WebCore::RenderTable::layout):
* rendering/RenderTableRow.cpp:
(WebCore::RenderTableRow::layout):
* rendering/RenderTableSection.cpp:
(WebCore::RenderTableSection::layout):
(WebCore::RenderTableSection::layoutRows):
* rendering/RenderView.cpp:
(WebCore::RenderView::computeRectForRepaint):
* rendering/ShadowElement.cpp:
(WebCore::ShadowBlockElement::layoutAsPart):

LayoutTests: 

* fast/repaint/japanese-rl-selection-clear.html: Added.
* fast/repaint/japanese-rl-selection-repaint.html: Added.
* fast/repaint/repaint-svg-after-style-change-expected.png:
* platform/mac/fast/repaint/4774354-expected.checksum:
* platform/mac/fast/repaint/4774354-expected.png:
* platform/mac/fast/repaint/4776765-expected.checksum:
* platform/mac/fast/repaint/4776765-expected.png:
* platform/mac/fast/repaint/background-generated-expected.checksum:
* platform/mac/fast/repaint/background-generated-expected.png:
* platform/mac/fast/repaint/background-misaligned-expected.png:
* platform/mac/fast/repaint/backgroundSizeRepaint-expected.checksum:
* platform/mac/fast/repaint/backgroundSizeRepaint-expected.png:
* platform/mac/fast/repaint/block-layout-inline-children-replaced-expected.checksum:
* platform/mac/fast/repaint/block-layout-inline-children-replaced-expected.png:
* platform/mac/fast/repaint/block-no-inflow-children-expected.checksum:
* platform/mac/fast/repaint/block-no-inflow-children-expected.png:
* platform/mac/fast/repaint/block-selection-gap-in-composited-layer-expected.checksum:
* platform/mac/fast/repaint/block-selection-gap-in-composited-layer-expected.png:
* platform/mac/fast/repaint/block-selection-gap-stale-cache-2-expected.png:
* platform/mac/fast/repaint/border-fit-lines-expected.checksum:
* platform/mac/fast/repaint/border-fit-lines-expected.png:
* platform/mac/fast/repaint/border-radius-repaint-expected.checksum:
* platform/mac/fast/repaint/border-radius-repaint-expected.png:
* platform/mac/fast/repaint/border-repaint-glitch-expected.checksum:
* platform/mac/fast/repaint/border-repaint-glitch-expected.png:
* platform/mac/fast/repaint/box-shadow-dynamic-expected.checksum:
* platform/mac/fast/repaint/box-shadow-dynamic-expected.png:
* platform/mac/fast/repaint/box-shadow-h-expected.checksum:
* platform/mac/fast/repaint/box-shadow-h-expected.png:
* platform/mac/fast/repaint/box-shadow-v-expected.checksum:
* platform/mac/fast/repaint/box-shadow-v-expected.png:
* platform/mac/fast/repaint/bugzilla-3509-expected.checksum:
* platform/mac/fast/repaint/bugzilla-3509-expected.png:
* platform/mac/fast/repaint/bugzilla-5699-expected.checksum:
* platform/mac/fast/repaint/bugzilla-5699-expected.png:
* platform/mac/fast/repaint/bugzilla-6278-expected.checksum:
* platform/mac/fast/repaint/bugzilla-6278-expected.png:
* platform/mac/fast/repaint/bugzilla-6388-expected.checksum:
* platform/mac/fast/repaint/bugzilla-6388-expected.png:
* platform/mac/fast/repaint/bugzilla-6473-expected.checksum:
* platform/mac/fast/repaint/bugzilla-6473-expected.png:
* platform/mac/fast/repaint/bugzilla-7235-expected.checksum:
* platform/mac/fast/repaint/bugzilla-7235-expected.png:
* platform/mac/fast/repaint/button-spurious-layout-hint-expected.png:
* platform/mac/fast/repaint/caret-outside-block-expected.checksum:
* platform/mac/fast/repaint/caret-outside-block-expected.png:
* platform/mac/fast/repaint/change-transform-expected.checksum:
* platform/mac/fast/repaint/change-transform-expected.png:
* platform/mac/fast/repaint/clip-with-layout-delta-expected.png:
* platform/mac/fast/repaint/clipped-relative-expected.checksum:
* platform/mac/fast/repaint/clipped-relative-expected.png:
* platform/mac/fast/repaint/containing-block-position-change-expected.checksum:
* platform/mac/fast/repaint/containing-block-position-change-expected.png:
* platform/mac/fast/repaint/content-into-overflow-expected.png:
* platform/mac/fast/repaint/continuation-after-outline-expected.checksum:
* platform/mac/fast/repaint/continuation-after-outline-expected.png:
* platform/mac/fast/repaint/control-clip-expected.checksum:
* platform/mac/fast/repaint/control-clip-expected.png:
* platform/mac/fast/repaint/create-layer-repaint-expected.checksum:
* platform/mac/fast/repaint/create-layer-repaint-expected.png:
* platform/mac/fast/repaint/delete-into-nested-block-expected.checksum:
* platform/mac/fast/repaint/delete-into-nested-block-expected.png:
* platform/mac/fast/repaint/dynamic-table-vertical-alignment-change-expected.checksum:
* platform/mac/fast/repaint/dynamic-table-vertical-alignment-change-expected.png:
* platform/mac/fast/repaint/erase-overflow-expected.png:
* platform/mac/fast/repaint/fixed-expected.checksum:
* platform/mac/fast/repaint/fixed-expected.png:
* platform/mac/fast/repaint/flexible-box-overflow-expected.checksum:
* platform/mac/fast/repaint/flexible-box-overflow-expected.png:
* platform/mac/fast/repaint/flexible-box-overflow-horizontal-expected.checksum:
* platform/mac/fast/repaint/flexible-box-overflow-horizontal-expected.png:
* platform/mac/fast/repaint/float-in-new-block-with-layout-delta-expected.png:
* platform/mac/fast/repaint/float-move-during-layout-expected.checksum:
* platform/mac/fast/repaint/float-move-during-layout-expected.png:
* platform/mac/fast/repaint/float-new-in-block-expected.png:
* platform/mac/fast/repaint/float-overflow-expected.png:
* platform/mac/fast/repaint/float-overflow-right-expected.png:
* platform/mac/fast/repaint/focus-layers-expected.checksum:
* platform/mac/fast/repaint/focus-layers-expected.png:
* platform/mac/fast/repaint/focus-ring-expected.png:
* platform/mac/fast/repaint/inline-block-overflow-expected.checksum:
* platform/mac/fast/repaint/inline-block-overflow-expected.png:
* platform/mac/fast/repaint/inline-block-resize-expected.png:
* platform/mac/fast/repaint/inline-color-change-expected.checksum:
* platform/mac/fast/repaint/inline-color-change-expected.png:
* platform/mac/fast/repaint/inline-outline-repaint-expected.checksum:
* platform/mac/fast/repaint/inline-outline-repaint-expected.png:
* platform/mac/fast/repaint/intermediate-layout-position-clip-expected.png:
* platform/mac/fast/repaint/intermediate-layout-position-expected.png:
* platform/mac/fast/repaint/invisible-objects-expected.png:
* platform/mac/fast/repaint/japanese-rl-selection-clear-expected.checksum: Added.
* platform/mac/fast/repaint/japanese-rl-selection-clear-expected.png: Added.
* platform/mac/fast/repaint/japanese-rl-selection-clear-expected.txt: Added.
* platform/mac/fast/repaint/japanese-rl-selection-repaint-expected.checksum: Added.
* platform/mac/fast/repaint/japanese-rl-selection-repaint-expected.png: Added.
* platform/mac/fast/repaint/japanese-rl-selection-repaint-expected.txt: Added.
* platform/mac/fast/repaint/layer-child-outline-expected.checksum:
* platform/mac/fast/repaint/layer-child-outline-expected.png:
* platform/mac/fast/repaint/layer-full-repaint-expected.png:
* platform/mac/fast/repaint/layer-hide-when-needs-layout-expected.png:
* platform/mac/fast/repaint/layer-outline-expected.checksum:
* platform/mac/fast/repaint/layer-outline-expected.png:
* platform/mac/fast/repaint/layer-outline-horizontal-expected.checksum:
* platform/mac/fast/repaint/layer-outline-horizontal-expected.png:
* platform/mac/fast/repaint/layer-visibility-expected.png:
* platform/mac/fast/repaint/layout-state-only-positioned-expected.png:
* platform/mac/fast/repaint/layout-state-relative-expected.checksum:
* platform/mac/fast/repaint/layout-state-relative-expected.png:
* platform/mac/fast/repaint/layout-state-scrolloffset-expected.checksum:
* platform/mac/fast/repaint/layout-state-scrolloffset-expected.png:
* platform/mac/fast/repaint/layout-state-scrolloffset2-expected.checksum:
* platform/mac/fast/repaint/layout-state-scrolloffset2-expected.png:
* platform/mac/fast/repaint/layout-state-scrolloffset3-expected.checksum:
* platform/mac/fast/repaint/layout-state-scrolloffset3-expected.png:
* platform/mac/fast/repaint/line-overflow-expected.checksum:
* platform/mac/fast/repaint/line-overflow-expected.png:
* platform/mac/fast/repaint/lines-with-layout-delta-expected.checksum:
* platform/mac/fast/repaint/lines-with-layout-delta-expected.png:
* platform/mac/fast/repaint/list-marker-2-expected.checksum:
* platform/mac/fast/repaint/list-marker-2-expected.png:
* platform/mac/fast/repaint/list-marker-expected.checksum:
* platform/mac/fast/repaint/list-marker-expected.png:
* platform/mac/fast/repaint/make-children-non-inline-expected.checksum:
* platform/mac/fast/repaint/make-children-non-inline-expected.png:
* platform/mac/fast/repaint/moving-shadow-on-container-expected.checksum:
* platform/mac/fast/repaint/moving-shadow-on-container-expected.png:
* platform/mac/fast/repaint/moving-shadow-on-path-expected.checksum:
* platform/mac/fast/repaint/moving-shadow-on-path-expected.png:
* platform/mac/fast/repaint/multicol-repaint-expected.checksum:
* platform/mac/fast/repaint/multicol-repaint-expected.png:
* platform/mac/fast/repaint/opacity-change-on-overflow-float-expected.checksum:
* platform/mac/fast/repaint/opacity-change-on-overflow-float-expected.png:
* platform/mac/fast/repaint/outline-child-repaint-expected.checksum:
* platform/mac/fast/repaint/outline-child-repaint-expected.png:
* platform/mac/fast/repaint/outline-inset-expected.png:
* platform/mac/fast/repaint/outline-repaint-glitch-expected.checksum:
* platform/mac/fast/repaint/outline-repaint-glitch-expected.png:
* platform/mac/fast/repaint/outline-shrinking-expected.checksum:
* platform/mac/fast/repaint/outline-shrinking-expected.png:
* platform/mac/fast/repaint/overflow-clip-subtree-layout-expected.png:
* platform/mac/fast/repaint/overflow-delete-line-expected.checksum:
* platform/mac/fast/repaint/overflow-delete-line-expected.png:
* platform/mac/fast/repaint/overflow-into-content-expected.png:
* platform/mac/fast/repaint/overflow-outline-repaint-expected.checksum:
* platform/mac/fast/repaint/overflow-outline-repaint-expected.png:
* platform/mac/fast/repaint/overflow-scroll-body-appear-expected.checksum:
* platform/mac/fast/repaint/overflow-scroll-body-appear-expected.png:
* platform/mac/fast/repaint/overflow-scroll-delete-expected.checksum:
* platform/mac/fast/repaint/overflow-scroll-delete-expected.png:
* platform/mac/fast/repaint/reflection-redraw-expected.checksum:
* platform/mac/fast/repaint/reflection-redraw-expected.png:
* platform/mac/fast/repaint/reflection-repaint-test-expected.png:
* platform/mac/fast/repaint/renderer-destruction-by-invalidateSelection-crash-expected.png:
* platform/mac/fast/repaint/repaint-resized-overflow-expected.checksum:
* platform/mac/fast/repaint/repaint-resized-overflow-expected.png:
* platform/mac/fast/repaint/search-field-cancel-expected.checksum:
* platform/mac/fast/repaint/search-field-cancel-expected.png:
* platform/mac/fast/repaint/selected-replaced-expected.checksum:
* platform/mac/fast/repaint/selected-replaced-expected.png:
* platform/mac/fast/repaint/selection-after-delete-expected.checksum:
* platform/mac/fast/repaint/selection-after-delete-expected.png:
* platform/mac/fast/repaint/selection-after-remove-expected.checksum:
* platform/mac/fast/repaint/selection-after-remove-expected.png:
* platform/mac/fast/repaint/selection-clear-expected.checksum:
* platform/mac/fast/repaint/selection-clear-expected.png:
* platform/mac/fast/repaint/selection-gap-overflow-scroll-expected.checksum:
* platform/mac/fast/repaint/selection-gap-overflow-scroll-expected.png:
* platform/mac/fast/repaint/selection-rl-expected.checksum:
* platform/mac/fast/repaint/selection-rl-expected.png:
* platform/mac/fast/repaint/shadow-multiple-horizontal-expected.png:
* platform/mac/fast/repaint/shadow-multiple-strict-horizontal-expected.checksum:
* platform/mac/fast/repaint/shadow-multiple-strict-horizontal-expected.png:
* platform/mac/fast/repaint/shadow-multiple-strict-vertical-expected.checksum:
* platform/mac/fast/repaint/shadow-multiple-strict-vertical-expected.png:
* platform/mac/fast/repaint/shadow-multiple-vertical-expected.png:
* platform/mac/fast/repaint/stacked-diacritics-expected.checksum:
* platform/mac/fast/repaint/stacked-diacritics-expected.png:
* platform/mac/fast/repaint/static-to-positioned-expected.checksum:
* platform/mac/fast/repaint/static-to-positioned-expected.png:
* platform/mac/fast/repaint/subtree-root-clip-2-expected.png:
* platform/mac/fast/repaint/subtree-root-clip-3-expected.png:
* platform/mac/fast/repaint/subtree-root-clip-expected.png:
* platform/mac/fast/repaint/subtree-root-skipped-expected.checksum:
* platform/mac/fast/repaint/subtree-root-skipped-expected.png:
* platform/mac/fast/repaint/table-cell-collapsed-border-expected.checksum:
* platform/mac/fast/repaint/table-cell-collapsed-border-expected.png:
* platform/mac/fast/repaint/table-cell-move-expected.checksum:
* platform/mac/fast/repaint/table-cell-move-expected.png:
* platform/mac/fast/repaint/table-cell-vertical-overflow-expected.png:
* platform/mac/fast/repaint/table-col-background-expected.png:
* platform/mac/fast/repaint/table-collapsed-border-expected.checksum:
* platform/mac/fast/repaint/table-collapsed-border-expected.png:
* platform/mac/fast/repaint/table-extra-bottom-grow-expected.checksum:
* platform/mac/fast/repaint/table-extra-bottom-grow-expected.png:
* platform/mac/fast/repaint/table-outer-border-expected.png:
* platform/mac/fast/repaint/table-row-expected.png:
* platform/mac/fast/repaint/table-section-overflow-expected.png:
* platform/mac/fast/repaint/table-section-repaint-expected.png:
* platform/mac/fast/repaint/table-two-pass-layout-overpaint-expected.png:
* platform/mac/fast/repaint/text-append-dirty-lines-expected.checksum:
* platform/mac/fast/repaint/text-append-dirty-lines-expected.png:
* platform/mac/fast/repaint/text-selection-rect-in-overflow-2-expected.checksum:
* platform/mac/fast/repaint/text-selection-rect-in-overflow-2-expected.png:
* platform/mac/fast/repaint/text-selection-rect-in-overflow-expected.checksum:
* platform/mac/fast/repaint/text-selection-rect-in-overflow-expected.png:
* platform/mac/fast/repaint/text-shadow-expected.checksum:
* platform/mac/fast/repaint/text-shadow-expected.png:
* platform/mac/fast/repaint/text-shadow-horizontal-expected.checksum:
* platform/mac/fast/repaint/text-shadow-horizontal-expected.png:
* platform/mac/fast/repaint/transform-absolute-child-expected.png:
* platform/mac/fast/repaint/transform-absolute-in-positioned-container-expected.checksum:
* platform/mac/fast/repaint/transform-absolute-in-positioned-container-expected.png:
* platform/mac/fast/repaint/transform-disable-layoutstate-expected.png:
* platform/mac/fast/repaint/transform-layout-repaint-expected.png:
* platform/mac/fast/repaint/transform-relative-position-expected.checksum:
* platform/mac/fast/repaint/transform-relative-position-expected.png:
* platform/mac/fast/repaint/transform-repaint-descendants-expected.checksum:
* platform/mac/fast/repaint/transform-repaint-descendants-expected.png:
* platform/mac/fast/repaint/transform-replaced-shadows-expected.png:
* platform/mac/fast/repaint/transform-translate-expected.png:



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@71851 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/WebCore/ChangeLog b/WebCore/ChangeLog
index a8635bf..6c4633e 100644
--- a/WebCore/ChangeLog
+++ b/WebCore/ChangeLog
@@ -1,3 +1,71 @@
+2010-11-11  David Hyatt  <hyatt@apple.com>
+
+        Reviewed by Simon Fraser.
+
+        https://bugs.webkit.org/show_bug.cgi?id=48664
+        
+        Make repaint invalidation work with flipped blocks ("rl" and "bt" writing modes).
+        
+        This patch disables the layout state for flipped blocks, since the paint offset isn't right.
+        
+        Bugs in the overflow sides for lines have been fixed.  before/AfterSideVisible/LayoutOverflowForLine
+        now respect flipping (and don't incorrectly change sides).  This is a very confusing aspect of
+        the overflow stuff (and at some point we should consider renaming top/left/right/bottom since they
+        aren't physical).
+        
+        offsetFromContainer and mapLocalToContainer remain physical and refer to the top left offset.
+        This means at every step up you convert to the correct physical coordinate.  It also means these
+        functions will be unreliable during layout for flipped blocks.  At first glance, this seems to be
+        more or less ok.
+        
+        computeRectForRepaint now delays flipping conversions until you cross a writing-mode boundary.  This
+        allows for documents to be fully flipped and repaint during layout can still be reliable.  This is
+        critical for incremental line layout repainting.
+        
+        Added new tests in fast/repaint and also rebaselined the tests in that directory because I'm tired
+        of all the stale results and failures.
+
+        * editing/SelectionController.cpp:
+        (WebCore::SelectionController::absoluteBoundsForLocalRect):
+        (WebCore::SelectionController::paintCaret):
+        * rendering/InlineTextBox.cpp:
+        (WebCore::InlineTextBox::selectionRect):
+        * rendering/RenderBlock.cpp:
+        (WebCore::RenderBlock::layoutBlock):
+        (WebCore::RenderBlock::layoutOnlyPositionedObjects):
+        (WebCore::RenderBlock::paintSelection):
+        * rendering/RenderBlockLineLayout.cpp:
+        (WebCore::RenderBlock::beforeSideVisibleOverflowForLine):
+        (WebCore::RenderBlock::afterSideVisibleOverflowForLine):
+        (WebCore::RenderBlock::beforeSideLayoutOverflowForLine):
+        (WebCore::RenderBlock::afterSideLayoutOverflowForLine):
+        * rendering/RenderBox.cpp:
+        (WebCore::RenderBox::layout):
+        (WebCore::RenderBox::mapLocalToContainer):
+        (WebCore::RenderBox::offsetFromContainer):
+        (WebCore::RenderBox::clippedOverflowRectForRepaint):
+        (WebCore::RenderBox::computeRectForRepaint):
+        (WebCore::RenderBox::flipForWritingMode):
+        (WebCore::RenderBox::locationOffsetIncludingFlipping):
+        * rendering/RenderBox.h:
+        * rendering/RenderFlexibleBox.cpp:
+        (WebCore::RenderFlexibleBox::layoutBlock):
+        * rendering/RenderInline.cpp:
+        (WebCore::RenderInline::clippedOverflowRectForRepaint):
+        * rendering/RenderSlider.cpp:
+        (WebCore::RenderSlider::layout):
+        * rendering/RenderTable.cpp:
+        (WebCore::RenderTable::layout):
+        * rendering/RenderTableRow.cpp:
+        (WebCore::RenderTableRow::layout):
+        * rendering/RenderTableSection.cpp:
+        (WebCore::RenderTableSection::layout):
+        (WebCore::RenderTableSection::layoutRows):
+        * rendering/RenderView.cpp:
+        (WebCore::RenderView::computeRectForRepaint):
+        * rendering/ShadowElement.cpp:
+        (WebCore::ShadowBlockElement::layoutAsPart):
+
 2010-11-11  Chris Marrin  <cmarrin@apple.com>
 
         Reviewed by Simon Fraser.
diff --git a/WebCore/editing/SelectionController.cpp b/WebCore/editing/SelectionController.cpp
index bb42662..1164c45 100644
--- a/WebCore/editing/SelectionController.cpp
+++ b/WebCore/editing/SelectionController.cpp
@@ -970,8 +970,11 @@
     RenderObject* caretPainter = caretRenderer();
     if (!caretPainter)
         return IntRect();
-        
-    return caretPainter->localToAbsoluteQuad(FloatRect(rect)).enclosingBoundingBox();
+    
+    IntRect localRect(rect);
+    if (caretPainter->isBox())
+        toRenderBox(caretPainter)->flipForWritingMode(localRect);
+    return caretPainter->localToAbsoluteQuad(FloatRect(localRect)).enclosingBoundingBox();
 }
 
 IntRect SelectionController::absoluteCaretBounds()
@@ -1085,6 +1088,8 @@
         return;
 
     IntRect drawingRect = localCaretRectForPainting();
+    if (caretRenderer() && caretRenderer()->isBox())
+        toRenderBox(caretRenderer())->flipForWritingMode(drawingRect);
     drawingRect.move(tx, ty);
     IntRect caret = intersection(drawingRect, clipRect);
     if (caret.isEmpty())
diff --git a/WebCore/rendering/InlineTextBox.cpp b/WebCore/rendering/InlineTextBox.cpp
index 9c96cc6..bf865ad 100644
--- a/WebCore/rendering/InlineTextBox.cpp
+++ b/WebCore/rendering/InlineTextBox.cpp
@@ -171,9 +171,7 @@
     int width = isHorizontal() ? logicalWidth : selHeight;
     int height = isHorizontal() ? selHeight : logicalWidth;
     
-    IntRect result = IntRect(topPoint, IntSize(width, height));
-    flipForWritingMode(result);
-    return result;
+    return IntRect(topPoint, IntSize(width, height));
 }
 
 void InlineTextBox::deleteLine(RenderArena* arena)
diff --git a/WebCore/rendering/RenderBlock.cpp b/WebCore/rendering/RenderBlock.cpp
index edecb69..902034c 100644
--- a/WebCore/rendering/RenderBlock.cpp
+++ b/WebCore/rendering/RenderBlock.cpp
@@ -1154,7 +1154,7 @@
             colInfo->clearForcedBreaks();
     }
 
-    LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), hasColumns() || hasTransform() || hasReflection(), pageHeight, pageHeightChanged, colInfo);
+    LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), hasColumns() || hasTransform() || hasReflection() || style()->isFlippedBlocksWritingMode(), pageHeight, pageHeightChanged, colInfo);
 
     // We use four values, maxTopPos, maxTopNeg, maxBottomPos, and maxBottomNeg, to track
     // our current maximal positive and negative margins.  These values are used when we
@@ -1987,7 +1987,7 @@
     if (!posChildNeedsLayout() || normalChildNeedsLayout() || selfNeedsLayout())
         return false;
 
-    LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), hasColumns() || hasTransform() || hasReflection());
+    LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), hasColumns() || hasTransform() || hasReflection() || style()->isFlippedBlocksWritingMode());
 
     if (needsPositionedMovementLayout()) {
         tryLayoutDoingPositionedMovementOnly();
@@ -2566,8 +2566,9 @@
             if (RenderLayer* layer = enclosingLayer()) {
                 gapRectsBounds.move(IntSize(-tx, -ty));
                 if (!hasLayer()) {
-                    FloatRect localBounds(gapRectsBounds);
-                    gapRectsBounds = localToContainerQuad(localBounds, layer->renderer()).enclosingBoundingBox();
+                    IntRect localBounds(gapRectsBounds);
+                    flipForWritingMode(localBounds);
+                    gapRectsBounds = localToContainerQuad(FloatRect(localBounds), layer->renderer()).enclosingBoundingBox();
                     gapRectsBounds.move(layer->scrolledContentOffset());
                 }
                 layer->addBlockSelectionGapsBounds(gapRectsBounds);
diff --git a/WebCore/rendering/RenderBlockLineLayout.cpp b/WebCore/rendering/RenderBlockLineLayout.cpp
index 191efe7..f6951ed 100644
--- a/WebCore/rendering/RenderBlockLineLayout.cpp
+++ b/WebCore/rendering/RenderBlockLineLayout.cpp
@@ -2027,66 +2027,38 @@
 
 int RenderBlock::beforeSideVisibleOverflowForLine(RootInlineBox* line) const
 {
-    switch (style()->writingMode()) {
-    case TopToBottomWritingMode:
+    // Overflow is in the block's coordinate space, which means it isn't purely physical.  For flipped blocks (rl and bt),
+    // we continue to use top and left overflow even though physically it's bottom and right.
+    if (style()->isHorizontalWritingMode())
         return line->topVisibleOverflow();
-    case LeftToRightWritingMode:
-        return line->leftVisibleOverflow();
-    case RightToLeftWritingMode:
-        return line->rightVisibleOverflow();
-    case BottomToTopWritingMode:
-        return line->bottomVisibleOverflow();
-    }
-    ASSERT_NOT_REACHED();
-    return line->topVisibleOverflow();
+    return line->leftVisibleOverflow();
 }
 
 int RenderBlock::afterSideVisibleOverflowForLine(RootInlineBox* line) const
 {
-    switch (style()->writingMode()) {
-    case TopToBottomWritingMode:
+    // Overflow is in the block's coordinate space, which means it isn't purely physical.  For flipped blocks (rl and bt),
+    // we continue to use bottom and right overflow even though physically it's top and left.
+    if (style()->isHorizontalWritingMode())
         return line->bottomVisibleOverflow();
-    case LeftToRightWritingMode:
-        return line->rightVisibleOverflow();
-    case RightToLeftWritingMode:
-        return line->leftVisibleOverflow();
-    case BottomToTopWritingMode:
-        return line->topVisibleOverflow();
-    }
-    ASSERT_NOT_REACHED();
-    return line->bottomVisibleOverflow();
+    return line->rightVisibleOverflow();
 }
 
 int RenderBlock::beforeSideLayoutOverflowForLine(RootInlineBox* line) const
 {
-    switch (style()->writingMode()) {
-    case TopToBottomWritingMode:
+    // Overflow is in the block's coordinate space, which means it isn't purely physical.  For flipped blocks (rl and bt),
+    // we continue to use top and left overflow even though physically it's bottom and right.
+    if (style()->isHorizontalWritingMode())
         return line->topLayoutOverflow();
-    case LeftToRightWritingMode:
-        return line->leftLayoutOverflow();
-    case RightToLeftWritingMode:
-        return line->rightLayoutOverflow();
-    case BottomToTopWritingMode:
-        return line->bottomLayoutOverflow();
-    }
-    ASSERT_NOT_REACHED();
-    return line->topLayoutOverflow();
+    return line->leftLayoutOverflow();
 }
 
 int RenderBlock::afterSideLayoutOverflowForLine(RootInlineBox* line) const
 {
-    switch (style()->writingMode()) {
-    case TopToBottomWritingMode:
+    // Overflow is in the block's coordinate space, which means it isn't purely physical.  For flipped blocks (rl and bt),
+    // we continue to use bottom and right overflow even though physically it's top and left.
+    if (style()->isHorizontalWritingMode())
         return line->bottomLayoutOverflow();
-    case LeftToRightWritingMode:
-        return line->rightLayoutOverflow();
-    case RightToLeftWritingMode:
-        return line->leftLayoutOverflow();
-    case BottomToTopWritingMode:
-        return line->topLayoutOverflow();
-    }
-    ASSERT_NOT_REACHED();
-    return line->bottomLayoutOverflow();
+    return line->rightLayoutOverflow();
 }
 
 void RenderBlock::deleteEllipsisLineBoxes()
diff --git a/WebCore/rendering/RenderBox.cpp b/WebCore/rendering/RenderBox.cpp
index f4aaf0e..2286ca7 100644
--- a/WebCore/rendering/RenderBox.cpp
+++ b/WebCore/rendering/RenderBox.cpp
@@ -378,7 +378,7 @@
         return;
     }
 
-    LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()));
+    LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), style()->isFlippedBlocksWritingMode());
     while (child) {
         child->layoutIfNeeded();
         ASSERT(!child->needsLayout());
@@ -1174,7 +1174,7 @@
         fixed |= isFixedPos;
     
     IntSize containerOffset = offsetFromContainer(o, roundedIntPoint(transformState.mappedPoint()));
-
+    
     bool preserve3D = useTransforms && (o->style()->preserves3D() || style()->preserves3D());
     if (useTransforms && shouldUseTransformFromContainer(o)) {
         TransformationMatrix t;
@@ -1234,9 +1234,11 @@
         offset += relativePositionOffset();
 
     if (!isInline() || isReplaced()) {
-        if (style()->position() != AbsolutePosition && style()->position() != FixedPosition)
+        if (style()->position() != AbsolutePosition && style()->position() != FixedPosition) {
             o->adjustForColumns(offset, IntPoint(point.x() + x(), point.y() + y()));
-        offset.expand(x(), y());
+            offset += locationOffsetIncludingFlipping();
+        } else
+            offset += locationOffset();
     }
 
     if (o->hasOverflowClip())
@@ -1329,12 +1331,21 @@
             r.inflate(v->maximalOutlineSize());
         }
     }
+    
     computeRectForRepaint(repaintContainer, r);
     return r;
 }
 
 void RenderBox::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& rect, bool fixed)
 {
+    // The rect we compute at each step is shifted by our x/y offset in the parent container's coordinate space.
+    // Only when we cross a writing mode boundary will we have to possibly flipForWritingMode (to convert into a more appropriate
+    // offset corner for the enclosing container).  This allows for a fully RL or BT document to repaint
+    // properly even during layout, since the rect remains flipped all the way until the end.
+    //
+    // RenderView::computeRectForRepaint then converts the rect to physical coordinates.  We also convert to
+    // physical when we hit a repaintContainer boundary.  Therefore the final rect returned is always in the
+    // physical coordinate space of the repaintContainer.
     if (RenderView* v = view()) {
         // LayoutState is only valid for root-relative repainting
         if (v->layoutStateEnabled() && !repaintContainer) {
@@ -1357,8 +1368,11 @@
     if (hasReflection())
         rect.unite(reflectedRect(rect));
 
-    if (repaintContainer == this)
+    if (repaintContainer == this) {
+        if (repaintContainer->style()->isFlippedBlocksWritingMode())
+            flipForWritingMode(rect);
         return;
+    }
 
     bool containerSkipped;
     RenderObject* o = container(repaintContainer, &containerSkipped);
@@ -1366,7 +1380,10 @@
         return;
 
     IntPoint topLeft = rect.location();
-    topLeft.move(x(), y());
+    if (isWritingModeRoot() && !isPositioned())
+        topLeft.move(locationOffsetIncludingFlipping());
+    else
+        topLeft.move(x(), y());
 
     EPosition position = style()->position();
 
@@ -1376,7 +1393,10 @@
         fixed = position == FixedPosition;
         rect = layer()->transform()->mapRect(rect);
         topLeft = rect.location();
-        topLeft.move(x(), y());
+        if (isWritingModeRoot() && !isPositioned())
+            topLeft.move(locationOffsetIncludingFlipping());
+        else
+            topLeft.move(x(), y());
     } else if (position == FixedPosition)
         fixed = true;
 
@@ -1424,10 +1444,7 @@
         rect.move(-containerOffset);
         return;
     }
-    
-    if (o->isBox())
-        toRenderBox(o)->flipForWritingMode(rect);
-    
+
     o->computeRectForRepaint(repaintContainer, rect, fixed);
 }
 
@@ -3199,7 +3216,7 @@
     } 
 }
 
-IntPoint RenderBox::flipForWritingMode(RenderBox* child, const IntPoint& point, FlippingAdjustment adjustment)
+IntPoint RenderBox::flipForWritingMode(const RenderBox* child, const IntPoint& point, FlippingAdjustment adjustment) const
 {
     if (!style()->isFlippedBlocksWritingMode())
         return point;
@@ -3211,7 +3228,7 @@
     return IntPoint(point.x() + width() - child->width() - child->x() - (adjustment == ParentToChildFlippingAdjustment ? child->x() : 0), point.y());
 }
 
-void RenderBox::flipForWritingMode(IntRect& rect)
+void RenderBox::flipForWritingMode(IntRect& rect) const
 {
     if (!style()->isFlippedBlocksWritingMode())
         return;
@@ -3222,35 +3239,35 @@
         rect.setX(width() - rect.right());
 }
 
-int RenderBox::flipForWritingMode(int position)
+int RenderBox::flipForWritingMode(int position) const
 {
     if (!style()->isFlippedBlocksWritingMode())
         return position;
     return logicalHeight() - position;
 }
 
-IntPoint RenderBox::flipForWritingMode(const IntPoint& position)
+IntPoint RenderBox::flipForWritingMode(const IntPoint& position) const
 {
     if (!style()->isFlippedBlocksWritingMode())
         return position;
     return style()->isHorizontalWritingMode() ? IntPoint(position.x(), height() - position.y()) : IntPoint(width() - position.x(), position.y());
 }
 
-IntSize RenderBox::flipForWritingMode(const IntSize& offset)
+IntSize RenderBox::flipForWritingMode(const IntSize& offset) const
 {
     if (!style()->isFlippedBlocksWritingMode())
         return offset;
     return style()->isHorizontalWritingMode() ? IntSize(offset.width(), height() - offset.height()) : IntSize(width() - offset.width(), offset.height());
 }
 
-IntSize RenderBox::locationOffsetIncludingFlipping()
+IntSize RenderBox::locationOffsetIncludingFlipping() const
 {
     if (!parent() || !parent()->isBox())
         return locationOffset();
     
-    RenderBox* parent = parentBox();
-    IntPoint localPoint = parent->flipForWritingMode(this, location(), ChildToParentFlippingAdjustment);
-    return IntSize(localPoint.x(), localPoint.y());
+    IntRect rect(frameRect());
+    parentBox()->flipForWritingMode(rect);
+    return IntSize(rect.x(), rect.y());
 }
 
 } // namespace WebCore
diff --git a/WebCore/rendering/RenderBox.h b/WebCore/rendering/RenderBox.h
index 44d84ba..36fbede 100644
--- a/WebCore/rendering/RenderBox.h
+++ b/WebCore/rendering/RenderBox.h
@@ -381,12 +381,12 @@
     virtual int baselinePosition(bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const;
 
     enum FlippingAdjustment { ChildToParentFlippingAdjustment, ParentToChildFlippingAdjustment };
-    IntPoint flipForWritingMode(RenderBox* child, const IntPoint&, FlippingAdjustment);
-    int flipForWritingMode(int position);
-    IntPoint flipForWritingMode(const IntPoint&);
-    IntSize flipForWritingMode(const IntSize&);
-    void flipForWritingMode(IntRect&);
-    IntSize locationOffsetIncludingFlipping();
+    IntPoint flipForWritingMode(const RenderBox* child, const IntPoint&, FlippingAdjustment) const;
+    int flipForWritingMode(int position) const; // The offset is in the block direction (y for horizontal writing modes, x for vertical writing modes).
+    IntPoint flipForWritingMode(const IntPoint&) const;
+    IntSize flipForWritingMode(const IntSize&) const;
+    void flipForWritingMode(IntRect&) const;
+    IntSize locationOffsetIncludingFlipping() const;
 
 protected:
     virtual void styleWillChange(StyleDifference, const RenderStyle* newStyle);
diff --git a/WebCore/rendering/RenderFlexibleBox.cpp b/WebCore/rendering/RenderFlexibleBox.cpp
index 63bada6..50b991c 100644
--- a/WebCore/rendering/RenderFlexibleBox.cpp
+++ b/WebCore/rendering/RenderFlexibleBox.cpp
@@ -209,7 +209,7 @@
         return;
 
     LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
-    LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), hasTransform() || hasReflection());
+    LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), hasTransform() || hasReflection() || style()->isFlippedBlocksWritingMode());
 
     int previousWidth = width();
     int previousHeight = height();
diff --git a/WebCore/rendering/RenderInline.cpp b/WebCore/rendering/RenderInline.cpp
index 1a792e7..3093f56 100644
--- a/WebCore/rendering/RenderInline.cpp
+++ b/WebCore/rendering/RenderInline.cpp
@@ -628,7 +628,6 @@
     }
 
     IntRect r(-ow + left, -ow + top, boundingBox.width() + ow * 2, boundingBox.height() + ow * 2);
-    cb->flipForWritingMode(r);
 
     if (cb->hasColumns())
         cb->adjustRectForColumns(r);
diff --git a/WebCore/rendering/RenderSlider.cpp b/WebCore/rendering/RenderSlider.cpp
index 240278d..a996ada 100644
--- a/WebCore/rendering/RenderSlider.cpp
+++ b/WebCore/rendering/RenderSlider.cpp
@@ -281,7 +281,7 @@
         if (oldSize != size())
             thumb->setChildNeedsLayout(true, false);
 
-        LayoutStateMaintainer statePusher(view(), this, size());
+        LayoutStateMaintainer statePusher(view(), this, size(), style()->isFlippedBlocksWritingMode());
 
         IntRect oldThumbRect = thumb->frameRect();
 
diff --git a/WebCore/rendering/RenderTable.cpp b/WebCore/rendering/RenderTable.cpp
index 749ced5..7115222 100644
--- a/WebCore/rendering/RenderTable.cpp
+++ b/WebCore/rendering/RenderTable.cpp
@@ -252,7 +252,7 @@
     recalcSectionsIfNeeded();
         
     LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
-    LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()));
+    LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), style()->isFlippedBlocksWritingMode());
 
     setLogicalHeight(0);
     m_overflow.clear();
diff --git a/WebCore/rendering/RenderTableRow.cpp b/WebCore/rendering/RenderTableRow.cpp
index 5ccf8eb..98544a6 100644
--- a/WebCore/rendering/RenderTableRow.cpp
+++ b/WebCore/rendering/RenderTableRow.cpp
@@ -115,7 +115,7 @@
     ASSERT(needsLayout());
 
     // Table rows do not add translation.
-    LayoutStateMaintainer statePusher(view(), this, IntSize());
+    LayoutStateMaintainer statePusher(view(), this, IntSize(), style()->isFlippedBlocksWritingMode());
 
     bool paginated = view()->layoutState()->isPaginated();
                 
diff --git a/WebCore/rendering/RenderTableSection.cpp b/WebCore/rendering/RenderTableSection.cpp
index e2726a6..f4051e7 100644
--- a/WebCore/rendering/RenderTableSection.cpp
+++ b/WebCore/rendering/RenderTableSection.cpp
@@ -393,7 +393,7 @@
 {
     ASSERT(needsLayout());
 
-    LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()));
+    LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), style()->isFlippedBlocksWritingMode());
     for (RenderObject* child = children()->firstChild(); child; child = child->nextSibling()) {
         if (child->isTableRow()) {
             child->layoutIfNeeded();
@@ -484,7 +484,7 @@
     int vspacing = table()->vBorderSpacing();
     int nEffCols = table()->numEffCols();
 
-    LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()));
+    LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), style()->isFlippedBlocksWritingMode());
 
     for (int r = 0; r < totalRows; r++) {
         // Set the row's x/y position and width/height.
diff --git a/WebCore/rendering/RenderView.cpp b/WebCore/rendering/RenderView.cpp
index 90a7089..6b4b65c 100644
--- a/WebCore/rendering/RenderView.cpp
+++ b/WebCore/rendering/RenderView.cpp
@@ -292,6 +292,15 @@
     if (printing())
         return;
 
+    if (style()->isFlippedBlocksWritingMode()) {
+        // We have to flip by hand since the view's logical height has not been determined.  We
+        // can use the viewport width and height.
+        if (style()->isHorizontalWritingMode())
+            rect.setY(viewHeight() - rect.bottom());
+        else
+            rect.setX(viewWidth() - rect.right());
+    }
+
     if (fixed && m_frameView)
         rect.move(m_frameView->scrollX(), m_frameView->scrollY());
         
diff --git a/WebCore/rendering/ShadowElement.cpp b/WebCore/rendering/ShadowElement.cpp
index c52ce17..cea3164 100644
--- a/WebCore/rendering/ShadowElement.cpp
+++ b/WebCore/rendering/ShadowElement.cpp
@@ -45,7 +45,7 @@
     RenderBox* selfRenderer = toRenderBox(renderer());
     IntRect oldRect = selfRenderer->frameRect();
 
-    LayoutStateMaintainer statePusher(parentRenderer->view(), parentRenderer, parentRenderer->size());
+    LayoutStateMaintainer statePusher(parentRenderer->view(), parentRenderer, parentRenderer->size(), parentRenderer->style()->isFlippedBlocksWritingMode());
 
     if (oldRect.size() != partRect.size())
         selfRenderer->setChildNeedsLayout(true, false);