<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[181692] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/181692">181692</a></dd>
<dt>Author</dt> <dd>zalan@apple.com</dd>
<dt>Date</dt> <dd>2015-03-18 07:48:01 -0700 (Wed, 18 Mar 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Simple line layout: Split fragments on renderer boundary on the fly.
https://bugs.webkit.org/show_bug.cgi?id=142579

Reviewed by Antti Koivisto.

Fragment splitting at renderers' boundary at the end of the line is no longer needed.
This patch ensures that TextFragmentIterator::nextTextFragment() does not concatenate
fragments across renderer boundary.

Source/WebCore:

Test: fast/text/simple-line-with-multiple-renderers.html

* rendering/SimpleLineLayout.cpp:
(WebCore::SimpleLineLayout::revertRuns):
(WebCore::SimpleLineLayout::LineState::isEmpty):
(WebCore::SimpleLineLayout::LineState::appendFragmentAndCreateRunIfNeeded): Check if fragments need a new run and
whether neighboring fragments need collapsing.
(WebCore::SimpleLineLayout::LineState::revertUncommitted): Revert fragments over multiple renderers if they form
a fragment continuation.
(WebCore::SimpleLineLayout::LineState::removeTrailingWhitespace):
(WebCore::SimpleLineLayout::forceFragmentToLine): Ensure that if a fragment has continuation across multiple renderers,
they all get added to the current line.
(WebCore::SimpleLineLayout::createLineRuns):
(WebCore::SimpleLineLayout::LineState::appendFragment): Deleted.
(WebCore::SimpleLineLayout::FragmentForwardIterator::FragmentForwardIterator): Deleted.
(WebCore::SimpleLineLayout::FragmentForwardIterator::operator++): Deleted.
(WebCore::SimpleLineLayout::FragmentForwardIterator::operator!=): Deleted.
(WebCore::SimpleLineLayout::FragmentForwardIterator::operator*): Deleted.
(WebCore::SimpleLineLayout::begin): Deleted.
(WebCore::SimpleLineLayout::end): Deleted.
(WebCore::SimpleLineLayout::preWrap): Deleted.
(WebCore::SimpleLineLayout::removeTrailingWhitespace): Deleted.
(WebCore::SimpleLineLayout::updateLineConstrains): Deleted.
(WebCore::SimpleLineLayout::splitRunsAtRendererBoundary): Deleted.
(WebCore::SimpleLineLayout::createTextRuns): Deleted.
(WebCore::SimpleLineLayout::create): Deleted.
(WebCore::SimpleLineLayout::Layout::create): Deleted.
* rendering/SimpleLineLayoutFlowContents.h:
(WebCore::SimpleLineLayout::FlowContents::Iterator::operator+):
(WebCore::SimpleLineLayout::FlowContents::Iterator::operator*):
(WebCore::SimpleLineLayout::FlowContents::Iterator::operator-&gt;):
* rendering/SimpleLineLayoutFunctions.cpp:
(WebCore::SimpleLineLayout::paintFlow): Unrelated fix on non-collapsed tab position.
* rendering/SimpleLineLayoutTextFragmentIterator.cpp:
(WebCore::SimpleLineLayout::TextFragmentIterator::TextFragmentIterator):
(WebCore::SimpleLineLayout::TextFragmentIterator::nextTextFragment):
(WebCore::SimpleLineLayout::TextFragmentIterator::revertToFragment):
(WebCore::SimpleLineLayout::TextFragmentIterator::textWidth):
(WebCore::SimpleLineLayout::TextFragmentIterator::skipToNextPosition):
* rendering/SimpleLineLayoutTextFragmentIterator.h:
(WebCore::SimpleLineLayout::TextFragmentIterator::TextFragment::TextFragment):
(WebCore::SimpleLineLayout::TextFragmentIterator::TextFragment::overlapsToNextRenderer):
(WebCore::SimpleLineLayout::TextFragmentIterator::TextFragment::isCollapsible):
(WebCore::SimpleLineLayout::TextFragmentIterator::TextFragment::operator==):
(WebCore::SimpleLineLayout::TextFragmentIterator::isLineBreak):
(WebCore::SimpleLineLayout::TextFragmentIterator::TextFragment::split): Deleted.

LayoutTests:

* fast/text/simple-line-with-multiple-renderers-expected.html: Added.
* fast/text/simple-line-with-multiple-renderers.html: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorerenderingSimpleLineLayoutcpp">trunk/Source/WebCore/rendering/SimpleLineLayout.cpp</a></li>
<li><a href="#trunkSourceWebCorerenderingSimpleLineLayoutFlowContentsh">trunk/Source/WebCore/rendering/SimpleLineLayoutFlowContents.h</a></li>
<li><a href="#trunkSourceWebCorerenderingSimpleLineLayoutFunctionscpp">trunk/Source/WebCore/rendering/SimpleLineLayoutFunctions.cpp</a></li>
<li><a href="#trunkSourceWebCorerenderingSimpleLineLayoutTextFragmentIteratorcpp">trunk/Source/WebCore/rendering/SimpleLineLayoutTextFragmentIterator.cpp</a></li>
<li><a href="#trunkSourceWebCorerenderingSimpleLineLayoutTextFragmentIteratorh">trunk/Source/WebCore/rendering/SimpleLineLayoutTextFragmentIterator.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsfasttextsimplelinewithmultiplerenderersexpectedhtml">trunk/LayoutTests/fast/text/simple-line-with-multiple-renderers-expected.html</a></li>
<li><a href="#trunkLayoutTestsfasttextsimplelinewithmultiplerenderershtml">trunk/LayoutTests/fast/text/simple-line-with-multiple-renderers.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (181691 => 181692)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2015-03-18 13:47:52 UTC (rev 181691)
+++ trunk/LayoutTests/ChangeLog        2015-03-18 14:48:01 UTC (rev 181692)
</span><span class="lines">@@ -1,3 +1,17 @@
</span><ins>+2015-03-18  Zalan Bujtas  &lt;zalan@apple.com&gt;
+
+        Simple line layout: Split fragments on renderer boundary on the fly.
+        https://bugs.webkit.org/show_bug.cgi?id=142579
+
+        Reviewed by Antti Koivisto.
+
+        Fragment splitting at renderers' boundary at the end of the line is no longer needed.
+        This patch ensures that TextFragmentIterator::nextTextFragment() does not concatenate
+        fragments across renderer boundary.
+
+        * fast/text/simple-line-with-multiple-renderers-expected.html: Added.
+        * fast/text/simple-line-with-multiple-renderers.html: Added.
+
</ins><span class="cx"> 2015-03-18  Manuel Rego Casasnovas  &lt;rego@igalia.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Flex and grid items should be painted as inline-blocks
</span></span></pre></div>
<a id="trunkLayoutTestsfasttextsimplelinewithmultiplerenderersexpectedhtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/fast/text/simple-line-with-multiple-renderers-expected.html (0 => 181692)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/text/simple-line-with-multiple-renderers-expected.html                                (rev 0)
+++ trunk/LayoutTests/fast/text/simple-line-with-multiple-renderers-expected.html        2015-03-18 14:48:01 UTC (rev 181692)
</span><span class="lines">@@ -0,0 +1,36 @@
</span><ins>+&lt;!DOCTYPE html&gt;
+&lt;html&gt;
+&lt;head&gt;
+&lt;title&gt;Test that segments across multiple renderers work fine with simple line layout.&lt;/title&gt;
+&lt;/head&gt;
+&lt;body&gt;
+&lt;script&gt;
+function injectTextNodes(container, style)
+{
+        container.appendChild(document.createTextNode(&quot;         &quot;));
+        container.appendChild(document.createTextNode(&quot;        &quot;));
+        container.appendChild(document.createTextNode(&quot;        This tests     &quot;));
+        container.appendChild(document.createTextNode(&quot;            &quot;));
+        container.appendChild(document.createTextNode(&quot;whitespace                 overlapping &quot;));
+        container.appendChild(document.createTextNode(&quot;        multiple     &quot;));
+        container.appendChild(document.createTextNode(&quot;                                renderers.&quot;));
+        container.appendChild(document.createTextNode(&quot;This tests when non-&quot;));
+        container.appendChild(document.createTextNode(&quot;whitespace con&quot;));
+        container.appendChild(document.createTextNode(&quot;tent o&quot;));
+        container.appendChild(document.createTextNode(&quot;verla&quot;));
+        container.appendChild(document.createTextNode(&quot;ps multiple ren&quot;));
+        container.appendChild(document.createTextNode(&quot;deres.&quot;));
+        container.style.cssText = style;
+        document.body.appendChild(container);
+}
+
+if (window.internals)
+        internals.settings.setSimpleLineLayoutEnabled(false)
+injectTextNodes(document.createElement(&quot;div&quot;));
+injectTextNodes(document.createElement(&quot;div&quot;), &quot;width: 0px&quot;);
+injectTextNodes(document.createElement(&quot;div&quot;), &quot;width: 10px&quot;);
+injectTextNodes(document.createElement(&quot;div&quot;), &quot;width: 100px&quot;);
+injectTextNodes(document.createElement(&quot;pre&quot;));
+&lt;/script&gt;
+&lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkLayoutTestsfasttextsimplelinewithmultiplerenderershtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/fast/text/simple-line-with-multiple-renderers.html (0 => 181692)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/text/simple-line-with-multiple-renderers.html                                (rev 0)
+++ trunk/LayoutTests/fast/text/simple-line-with-multiple-renderers.html        2015-03-18 14:48:01 UTC (rev 181692)
</span><span class="lines">@@ -0,0 +1,34 @@
</span><ins>+&lt;!DOCTYPE html&gt;
+&lt;html&gt;
+&lt;head&gt;
+&lt;title&gt;Test that segments across multiple renderers work fine with simple line layout.&lt;/title&gt;
+&lt;/head&gt;
+&lt;body&gt;
+&lt;script&gt;
+function injectTextNodes(container, style)
+{
+        container.appendChild(document.createTextNode(&quot;         &quot;));
+        container.appendChild(document.createTextNode(&quot;        &quot;));
+        container.appendChild(document.createTextNode(&quot;        This tests     &quot;));
+        container.appendChild(document.createTextNode(&quot;            &quot;));
+        container.appendChild(document.createTextNode(&quot;whitespace                 overlapping &quot;));
+        container.appendChild(document.createTextNode(&quot;        multiple     &quot;));
+        container.appendChild(document.createTextNode(&quot;                                renderers.&quot;));
+        container.appendChild(document.createTextNode(&quot;This tests when non-&quot;));
+        container.appendChild(document.createTextNode(&quot;whitespace con&quot;));
+        container.appendChild(document.createTextNode(&quot;tent o&quot;));
+        container.appendChild(document.createTextNode(&quot;verla&quot;));
+        container.appendChild(document.createTextNode(&quot;ps multiple ren&quot;));
+        container.appendChild(document.createTextNode(&quot;deres.&quot;));
+        container.style.cssText = style;
+        document.body.appendChild(container);
+}
+
+injectTextNodes(document.createElement(&quot;div&quot;));
+injectTextNodes(document.createElement(&quot;div&quot;), &quot;width: 0px&quot;);
+injectTextNodes(document.createElement(&quot;div&quot;), &quot;width: 10px&quot;);
+injectTextNodes(document.createElement(&quot;div&quot;), &quot;width: 100px&quot;);
+injectTextNodes(document.createElement(&quot;pre&quot;));
+&lt;/script&gt;
+&lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (181691 => 181692)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-03-18 13:47:52 UTC (rev 181691)
+++ trunk/Source/WebCore/ChangeLog        2015-03-18 14:48:01 UTC (rev 181692)
</span><span class="lines">@@ -1,3 +1,61 @@
</span><ins>+2015-03-18  Zalan Bujtas  &lt;zalan@apple.com&gt;
+
+        Simple line layout: Split fragments on renderer boundary on the fly.
+        https://bugs.webkit.org/show_bug.cgi?id=142579
+
+        Reviewed by Antti Koivisto.
+
+        Fragment splitting at renderers' boundary at the end of the line is no longer needed.
+        This patch ensures that TextFragmentIterator::nextTextFragment() does not concatenate
+        fragments across renderer boundary.
+
+        Test: fast/text/simple-line-with-multiple-renderers.html
+
+        * rendering/SimpleLineLayout.cpp:
+        (WebCore::SimpleLineLayout::revertRuns):
+        (WebCore::SimpleLineLayout::LineState::isEmpty):
+        (WebCore::SimpleLineLayout::LineState::appendFragmentAndCreateRunIfNeeded): Check if fragments need a new run and
+        whether neighboring fragments need collapsing.
+        (WebCore::SimpleLineLayout::LineState::revertUncommitted): Revert fragments over multiple renderers if they form
+        a fragment continuation.
+        (WebCore::SimpleLineLayout::LineState::removeTrailingWhitespace):
+        (WebCore::SimpleLineLayout::forceFragmentToLine): Ensure that if a fragment has continuation across multiple renderers,
+        they all get added to the current line.
+        (WebCore::SimpleLineLayout::createLineRuns):
+        (WebCore::SimpleLineLayout::LineState::appendFragment): Deleted.
+        (WebCore::SimpleLineLayout::FragmentForwardIterator::FragmentForwardIterator): Deleted.
+        (WebCore::SimpleLineLayout::FragmentForwardIterator::operator++): Deleted.
+        (WebCore::SimpleLineLayout::FragmentForwardIterator::operator!=): Deleted.
+        (WebCore::SimpleLineLayout::FragmentForwardIterator::operator*): Deleted.
+        (WebCore::SimpleLineLayout::begin): Deleted.
+        (WebCore::SimpleLineLayout::end): Deleted.
+        (WebCore::SimpleLineLayout::preWrap): Deleted.
+        (WebCore::SimpleLineLayout::removeTrailingWhitespace): Deleted.
+        (WebCore::SimpleLineLayout::updateLineConstrains): Deleted.
+        (WebCore::SimpleLineLayout::splitRunsAtRendererBoundary): Deleted.
+        (WebCore::SimpleLineLayout::createTextRuns): Deleted.
+        (WebCore::SimpleLineLayout::create): Deleted.
+        (WebCore::SimpleLineLayout::Layout::create): Deleted.
+        * rendering/SimpleLineLayoutFlowContents.h:
+        (WebCore::SimpleLineLayout::FlowContents::Iterator::operator+):
+        (WebCore::SimpleLineLayout::FlowContents::Iterator::operator*):
+        (WebCore::SimpleLineLayout::FlowContents::Iterator::operator-&gt;):
+        * rendering/SimpleLineLayoutFunctions.cpp:
+        (WebCore::SimpleLineLayout::paintFlow): Unrelated fix on non-collapsed tab position.
+        * rendering/SimpleLineLayoutTextFragmentIterator.cpp:
+        (WebCore::SimpleLineLayout::TextFragmentIterator::TextFragmentIterator):
+        (WebCore::SimpleLineLayout::TextFragmentIterator::nextTextFragment):
+        (WebCore::SimpleLineLayout::TextFragmentIterator::revertToFragment):
+        (WebCore::SimpleLineLayout::TextFragmentIterator::textWidth):
+        (WebCore::SimpleLineLayout::TextFragmentIterator::skipToNextPosition):
+        * rendering/SimpleLineLayoutTextFragmentIterator.h:
+        (WebCore::SimpleLineLayout::TextFragmentIterator::TextFragment::TextFragment):
+        (WebCore::SimpleLineLayout::TextFragmentIterator::TextFragment::overlapsToNextRenderer):
+        (WebCore::SimpleLineLayout::TextFragmentIterator::TextFragment::isCollapsible):
+        (WebCore::SimpleLineLayout::TextFragmentIterator::TextFragment::operator==):
+        (WebCore::SimpleLineLayout::TextFragmentIterator::isLineBreak):
+        (WebCore::SimpleLineLayout::TextFragmentIterator::TextFragment::split): Deleted.
+
</ins><span class="cx"> 2015-03-18  Manuel Rego Casasnovas  &lt;rego@igalia.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Flex and grid items should be painted as inline-blocks
</span></span></pre></div>
<a id="trunkSourceWebCorerenderingSimpleLineLayoutcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/rendering/SimpleLineLayout.cpp (181691 => 181692)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/rendering/SimpleLineLayout.cpp        2015-03-18 13:47:52 UTC (rev 181691)
+++ trunk/Source/WebCore/rendering/SimpleLineLayout.cpp        2015-03-18 14:48:01 UTC (rev 181692)
</span><span class="lines">@@ -231,9 +231,27 @@
</span><span class="cx">     return 0;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static void revertRuns(Layout::RunVector&amp; runs, unsigned length, float width)
+{
+    while (length) {
+        ASSERT(runs.size());
+        Run&amp; lastRun = runs.last();
+        unsigned lastRunLength = lastRun.end - lastRun.start;
+        if (lastRunLength &gt; length) {
+            lastRun.logicalRight -= width;
+            lastRun.end -= length;
+            break;
+        }
+        length -= lastRunLength;
+        width -= (lastRun.logicalRight - lastRun.logicalLeft);
+        runs.removeLast();
+    }
+}
+
</ins><span class="cx"> class LineState {
</span><span class="cx"> public:
</span><span class="cx">     void setAvailableWidth(float width) { m_availableWidth = width; }
</span><ins>+    void setCollapedWhitespaceWidth(float width) { m_collapsedWhitespaceWidth = width; }
</ins><span class="cx">     void setLogicalLeftOffset(float offset) { m_logicalLeftOffset = offset; }
</span><span class="cx">     void setOverflowedFragment(const TextFragmentIterator::TextFragment&amp; fragment) { m_overflowedFragment = fragment; }
</span><span class="cx"> 
</span><span class="lines">@@ -241,25 +259,51 @@
</span><span class="cx">     float logicalLeftOffset() const { return m_logicalLeftOffset; }
</span><span class="cx">     const TextFragmentIterator::TextFragment&amp; overflowedFragment() const { return m_overflowedFragment; }
</span><span class="cx">     bool hasTrailingWhitespace() const { return m_trailingWhitespaceLength; }
</span><ins>+    TextFragmentIterator::TextFragment lastFragment() const { return m_fragments.last(); }
</ins><span class="cx">     bool isWhitespaceOnly() const { return m_trailingWhitespaceWidth &amp;&amp; m_runsWidth == m_trailingWhitespaceWidth; }
</span><span class="cx">     bool fits(float extra) const { return m_availableWidth &gt;= m_runsWidth + extra; }
</span><span class="cx">     bool firstCharacterFits() const { return m_firstCharacterFits; }
</span><span class="cx">     float width() const { return m_runsWidth; }
</span><ins>+    bool isEmpty() const
+    {
+        if (!m_fragments.size())
+            return true;
+        if (!m_lastCompleteFragment.isEmpty())
+            return false;
+        return m_fragments.last().overlapsToNextRenderer();
+    }
</ins><span class="cx"> 
</span><del>-    void appendFragment(const TextFragmentIterator::TextFragment&amp; fragment, Layout::RunVector&amp; runs)
</del><ins>+    void appendFragmentAndCreateRunIfNeeded(const TextFragmentIterator::TextFragment&amp; fragment, Layout::RunVector&amp; runs)
</ins><span class="cx">     {
</span><span class="cx">         // Adjust end position while collapsing.
</span><span class="cx">         unsigned endPosition = fragment.isCollapsed() ? fragment.start() + 1 : fragment.end();
</span><del>-
-        if (m_createNewRun)
</del><ins>+        // New line needs new run.
+        if (!m_runsWidth)
</ins><span class="cx">             runs.append(Run(fragment.start(), endPosition, m_runsWidth, m_runsWidth + fragment.width(), false));
</span><span class="cx">         else {
</span><del>-            ASSERT(runs.size());
-            Run&amp; lastRun = runs.last();
-            lastRun.end = endPosition;
-            lastRun.logicalRight = m_runsWidth + fragment.width();
</del><ins>+            const auto&amp; lastFragment = m_fragments.last();
+            // Advance last completed fragment when the previous fragment is all set (including multiple parts across renderers)
+            if ((lastFragment.type() != fragment.type()) || !lastFragment.overlapsToNextRenderer())
+                m_lastCompleteFragment = lastFragment;
+            // Collapse neighbouring whitespace, if they are across multiple renderers and are not collapsed yet.
+            if (lastFragment.isCollapsible() &amp;&amp; fragment.isCollapsible()) {
+                ASSERT(lastFragment.isLastInRenderer());
+                if (!lastFragment.isCollapsed()) {
+                    // Line width needs to be reset so that now it takes collapsing into consideration.
+                    m_runsWidth -= (lastFragment.width() - m_collapsedWhitespaceWidth);
+                }
+                // This fragment is collapsed completely. No run is needed.
+                return;
+            }
+            if (lastFragment.isLastInRenderer() || lastFragment.isCollapsed())
+                runs.append(Run(fragment.start(), endPosition, m_runsWidth, m_runsWidth + fragment.width(), false));
+            else {
+                Run&amp; lastRun = runs.last();
+                lastRun.end = endPosition;
+                lastRun.logicalRight += fragment.width();
+            }
</ins><span class="cx">         }
</span><del>-        m_createNewRun = fragment.isCollapsed();
</del><ins>+        m_fragments.append(fragment);
</ins><span class="cx">         m_runsWidth += fragment.width();
</span><span class="cx"> 
</span><span class="cx">         if (fragment.type() == TextFragmentIterator::TextFragment::Whitespace) {
</span><span class="lines">@@ -274,18 +318,34 @@
</span><span class="cx">             m_firstCharacterFits = fragment.start() + 1 &gt; endPosition || m_runsWidth &lt;= m_availableWidth;
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    TextFragmentIterator::TextFragment revertToLastCompleteFragment(Layout::RunVector&amp; runs)
+    {
+        ASSERT(m_fragments.size());
+        unsigned revertLength = 0;
+        float revertWidth = 0;
+        while (m_fragments.size()) {
+            const auto&amp; current = m_fragments.last();
+            if (current == m_lastCompleteFragment)
+                break;
+            revertLength += current.end() - current.start();
+            revertWidth += current.width();
+            m_fragments.removeLast();
+        }
+        m_runsWidth -= revertWidth;
+        if (revertLength)
+            revertRuns(runs, revertLength, revertWidth);
+        return m_lastCompleteFragment;
+    }
+
</ins><span class="cx">     void removeTrailingWhitespace(Layout::RunVector&amp; runs)
</span><span class="cx">     {
</span><del>-        ASSERT(runs.size());
-        Run&amp; lastRun = runs.last();
-        lastRun.logicalRight -= m_trailingWhitespaceWidth;
-        lastRun.end -= m_trailingWhitespaceLength;
-        if (lastRun.start == lastRun.end)
-            runs.removeLast();
-
</del><ins>+        // FIXME: sync m_fragment, but it's ok for now as we don't use m_fragment anymore after removeTrailingWhitespace.
+        if (!m_trailingWhitespaceLength)
+            return;
+        revertRuns(runs, m_trailingWhitespaceLength, m_trailingWhitespaceWidth);
</ins><span class="cx">         m_runsWidth -= m_trailingWhitespaceWidth;
</span><ins>+        m_trailingWhitespaceLength = 0;
</ins><span class="cx">         m_trailingWhitespaceWidth = 0;
</span><del>-        m_trailingWhitespaceLength = 0;
</del><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx"> private:
</span><span class="lines">@@ -293,12 +353,14 @@
</span><span class="cx">     float m_logicalLeftOffset { 0 };
</span><span class="cx">     TextFragmentIterator::TextFragment m_overflowedFragment;
</span><span class="cx">     float m_runsWidth { 0 };
</span><del>-    bool m_createNewRun { true };
</del><ins>+    TextFragmentIterator::TextFragment m_lastCompleteFragment;
</ins><span class="cx">     float m_trailingWhitespaceWidth { 0 }; // Use this to remove trailing whitespace without re-mesuring the text.
</span><span class="cx">     unsigned m_trailingWhitespaceLength { 0 };
</span><ins>+    float m_collapsedWhitespaceWidth { 0 };
</ins><span class="cx">     // Having one character on the line does not necessarily mean it actually fits.
</span><span class="cx">     // First character of the first fragment might be forced on to the current line even if it does not fit.
</span><span class="cx">     bool m_firstCharacterFits { false };
</span><ins>+    Vector&lt;TextFragmentIterator::TextFragment&gt; m_fragments;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> class FragmentForwardIterator : public std::iterator&lt;std::forward_iterator_tag, unsigned&gt; {
</span><span class="lines">@@ -389,14 +451,36 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     // Check if we need to skip the leading whitespace.
</span><del>-    if (style.collapseWhitespace &amp;&amp; firstFragment.type() == TextFragmentIterator::TextFragment::Whitespace)
-        firstFragment = textFragmentIterator.nextTextFragment();
</del><ins>+    if (style.collapseWhitespace) {
+        while (firstFragment.type() == TextFragmentIterator::TextFragment::Whitespace)
+            firstFragment = textFragmentIterator.nextTextFragment();
+    }
</ins><span class="cx">     return firstFragment;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static void forceFragmentToLine(LineState&amp; line, TextFragmentIterator&amp; textFragmentIterator, Layout::RunVector&amp; runs, const TextFragmentIterator::TextFragment&amp; fragment)
+{
+    line.appendFragmentAndCreateRunIfNeeded(fragment, runs);
+    // Check if there are more fragments to add to the current line.
+    if (!fragment.overlapsToNextRenderer())
+        return;
+    auto nextFragment = textFragmentIterator.nextTextFragment();
+    while (true) {
+        if (nextFragment.type() != fragment.type())
+            break;
+        line.appendFragmentAndCreateRunIfNeeded(nextFragment, runs);
+        // Does it overlap to the next segment?
+        if (!nextFragment.overlapsToNextRenderer())
+            return;
+        nextFragment = textFragmentIterator.nextTextFragment();
+    }
+    line.setOverflowedFragment(nextFragment);
+}
+
</ins><span class="cx"> static bool createLineRuns(LineState&amp; line, const LineState&amp; previousLine, Layout::RunVector&amp; runs, TextFragmentIterator&amp; textFragmentIterator)
</span><span class="cx"> {
</span><span class="cx">     const auto&amp; style = textFragmentIterator.style();
</span><ins>+    line.setCollapedWhitespaceWidth(style.spaceWidth);
</ins><span class="cx">     bool lineCanBeWrapped = style.wrapLines || style.breakWordOnOverflow;
</span><span class="cx">     auto fragment = firstFragment(textFragmentIterator, previousLine);
</span><span class="cx">     while (fragment.type() != TextFragmentIterator::TextFragment::ContentEnd) {
</span><span class="lines">@@ -404,7 +488,7 @@
</span><span class="cx">         if (fragment.type() == TextFragmentIterator::TextFragment::LineBreak) {
</span><span class="cx">             // Add the new line fragment only if there's nothing on the line. (otherwise the extra new line character would show up at the end of the content.)
</span><span class="cx">             if (!line.width())
</span><del>-                line.appendFragment(fragment, runs);
</del><ins>+                line.appendFragmentAndCreateRunIfNeeded(fragment, runs);
</ins><span class="cx">             break;
</span><span class="cx">         }
</span><span class="cx">         if (lineCanBeWrapped &amp;&amp; !line.fits(fragment.width())) {
</span><span class="lines">@@ -413,13 +497,13 @@
</span><span class="cx">             // 2. Whitespace collapse off: whitespace is wrapped.
</span><span class="cx">             // 3. First, non-whitespace fragment is either wrapped or kept on the line. (depends on overflow-wrap)
</span><span class="cx">             // 4. Non-whitespace fragment when there's already another fragment on the line gets pushed to the next line.
</span><del>-            bool emptyLine = !line.width();
</del><ins>+            bool emptyLine = line.isEmpty();
</ins><span class="cx">             // Whitespace fragment.
</span><span class="cx">             if (fragment.type() == TextFragmentIterator::TextFragment::Whitespace) {
</span><span class="cx">                 if (!style.collapseWhitespace) {
</span><span class="cx">                     // Split the fragment; (modified)fragment stays on this line, overflowedFragment is pushed to next line.
</span><span class="cx">                     line.setOverflowedFragment(splitFragmentToFitLine(fragment, line.availableWidth() - line.width(), emptyLine, textFragmentIterator));
</span><del>-                    line.appendFragment(fragment, runs);
</del><ins>+                    line.appendFragmentAndCreateRunIfNeeded(fragment, runs);
</ins><span class="cx">                 }
</span><span class="cx">                 // When whitespace collapse is on, whitespace that doesn't fit is simply skipped.
</span><span class="cx">                 break;
</span><span class="lines">@@ -428,23 +512,30 @@
</span><span class="cx">             if ((emptyLine &amp;&amp; style.breakWordOnOverflow) || !style.wrapLines) {
</span><span class="cx">                 // Split the fragment; (modified)fragment stays on this line, overflowedFragment is pushed to next line.
</span><span class="cx">                 line.setOverflowedFragment(splitFragmentToFitLine(fragment, line.availableWidth() - line.width(), emptyLine, textFragmentIterator));
</span><del>-                line.appendFragment(fragment, runs);
</del><ins>+                line.appendFragmentAndCreateRunIfNeeded(fragment, runs);
</ins><span class="cx">                 break;
</span><span class="cx">             }
</span><span class="cx">             // Non-breakable non-whitespace first fragment. Add it to the current line. -it overflows though.
</span><ins>+            ASSERT(fragment.type() == TextFragmentIterator::TextFragment::NonWhitespace);
</ins><span class="cx">             if (emptyLine) {
</span><del>-                line.appendFragment(fragment, runs);
</del><ins>+                forceFragmentToLine(line, textFragmentIterator, runs, fragment);
</ins><span class="cx">                 break;
</span><span class="cx">             }
</span><span class="cx">             // Non-breakable non-whitespace fragment when there's already content on the line. Push it to the next line.
</span><ins>+            if (line.lastFragment().overlapsToNextRenderer()) {
+                // Check if this fragment is a continuation of a previous segment. In such cases, we need to remove them all.
+                const auto&amp; currentFragment = line.revertToLastCompleteFragment(runs);
+                textFragmentIterator.revertToFragment(currentFragment);
+                break;
+            }
</ins><span class="cx">             line.setOverflowedFragment(fragment);
</span><span class="cx">             break;
</span><span class="cx">         }
</span><del>-        line.appendFragment(fragment, runs);
</del><ins>+        line.appendFragmentAndCreateRunIfNeeded(fragment, runs);
</ins><span class="cx">         // Find the next text fragment.
</span><span class="cx">         fragment = textFragmentIterator.nextTextFragment(line.width());
</span><span class="cx">     }
</span><del>-    return fragment.type() == TextFragmentIterator::TextFragment::ContentEnd &amp;&amp; line.overflowedFragment().isEmpty();
</del><ins>+    return (fragment.type() == TextFragmentIterator::TextFragment::ContentEnd &amp;&amp; line.overflowedFragment().isEmpty()) || line.overflowedFragment().type() == TextFragmentIterator::TextFragment::ContentEnd;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static void closeLineEndingAndAdjustRuns(LineState&amp; line, Layout::RunVector&amp; runs, unsigned previousRunCount, unsigned&amp; lineCount, const TextFragmentIterator&amp; textFragmentIterator)
</span><span class="lines">@@ -466,28 +557,6 @@
</span><span class="cx">     ++lineCount;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static void splitRunsAtRendererBoundary(Layout::RunVector&amp; lineRuns, const TextFragmentIterator&amp; textFragmentIterator)
-{
-    // FIXME: We should probably split during run construction instead of as a separate pass.
-    if (lineRuns.isEmpty())
-        return;
-    unsigned runIndex = 0;
-    do {
-        const Run&amp; run = lineRuns.at(runIndex);
-        ASSERT(run.start != run.end);
-        auto&amp; startSegment = textFragmentIterator.segmentForPosition(run.start);
-        if (run.end &lt;= startSegment.end)
-            continue;
-        // This run overlaps multiple renderers. Split it up.
-        // Split run at the renderer's boundary and create a new run for the left side, while use the current run as the right side.
-        float logicalRightOfLeftRun = run.logicalLeft + textFragmentIterator.textWidth(run.start, startSegment.end, run.logicalLeft);
-        lineRuns.insert(runIndex, Run(run.start, startSegment.end, run.logicalLeft, logicalRightOfLeftRun, false));
-        Run&amp; rightSideRun = lineRuns.at(runIndex + 1);
-        rightSideRun.start = startSegment.end;
-        rightSideRun.logicalLeft = logicalRightOfLeftRun;
-    } while (++runIndex &lt; lineRuns.size());
-}
-
</del><span class="cx"> static void createTextRuns(Layout::RunVector&amp; runs, RenderBlockFlow&amp; flow, unsigned&amp; lineCount)
</span><span class="cx"> {
</span><span class="cx">     LayoutUnit borderAndPaddingBefore = flow.borderAndPaddingBefore();
</span><span class="lines">@@ -505,9 +574,6 @@
</span><span class="cx">         isEndOfContent = createLineRuns(line, previousLine, runs, textFragmentIterator);
</span><span class="cx">         closeLineEndingAndAdjustRuns(line, runs, previousRunCount, lineCount, textFragmentIterator);
</span><span class="cx">     } while (!isEndOfContent);
</span><del>-
-    if (flow.firstChild() != flow.lastChild())
-        splitRunsAtRendererBoundary(runs, textFragmentIterator);
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> std::unique_ptr&lt;Layout&gt; create(RenderBlockFlow&amp; flow)
</span></span></pre></div>
<a id="trunkSourceWebCorerenderingSimpleLineLayoutFlowContentsh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/rendering/SimpleLineLayoutFlowContents.h (181691 => 181692)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/rendering/SimpleLineLayoutFlowContents.h        2015-03-18 13:47:52 UTC (rev 181691)
+++ trunk/Source/WebCore/rendering/SimpleLineLayoutFlowContents.h        2015-03-18 14:48:01 UTC (rev 181692)
</span><span class="lines">@@ -55,9 +55,11 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         Iterator&amp; operator++();
</span><ins>+        Iterator&amp; operator--();
</ins><span class="cx">         bool operator==(const Iterator&amp; other) const;
</span><span class="cx">         bool operator!=(const Iterator&amp; other) const;
</span><del>-        const Segment&amp; operator*() const { return m_flowContents.m_segments[m_segmentIndex]; }
</del><ins>+        const Segment&amp; operator*() const;
+        const Segment* operator-&gt;() const;
</ins><span class="cx"> 
</span><span class="cx">     private:
</span><span class="cx">         const FlowContents&amp; m_flowContents;
</span><span class="lines">@@ -67,15 +69,11 @@
</span><span class="cx">     Iterator begin() const { return Iterator(*this, 0); }
</span><span class="cx">     Iterator end() const { return Iterator(*this, m_segments.size()); }
</span><span class="cx"> 
</span><del>-    unsigned length() const { return m_segments.last().end; };
-
</del><span class="cx">     unsigned segmentIndexForPosition(unsigned position) const;
</span><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     unsigned segmentIndexForPositionSlow(unsigned position) const;
</span><del>-
</del><span class="cx">     const Vector&lt;Segment, 8&gt; m_segments;
</span><del>-
</del><span class="cx">     mutable unsigned m_lastSegmentIndex;
</span><span class="cx"> };
</span><span class="cx"> 
</span><span class="lines">@@ -85,6 +83,12 @@
</span><span class="cx">     return *this;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+inline FlowContents::Iterator&amp; FlowContents::Iterator::operator--()
+{
+    --m_segmentIndex;
+    return *this;
+}
+
</ins><span class="cx"> inline bool FlowContents::Iterator::operator==(const FlowContents::Iterator&amp; other) const
</span><span class="cx"> {
</span><span class="cx">     return m_segmentIndex == other.m_segmentIndex;
</span><span class="lines">@@ -95,6 +99,18 @@
</span><span class="cx">     return !(*this == other);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+inline const FlowContents::Segment&amp; FlowContents::Iterator::operator*() const
+{
+    ASSERT(m_segmentIndex &lt; m_flowContents.m_segments.size());
+    return m_flowContents.m_segments[m_segmentIndex];
+}
+
+inline const FlowContents::Segment* FlowContents::Iterator::operator-&gt;() const
+{
+    ASSERT(m_segmentIndex &lt; m_flowContents.m_segments.size());
+    return &amp;(m_flowContents.m_segments[m_segmentIndex]);
+}
+
</ins><span class="cx"> inline unsigned FlowContents::segmentIndexForPosition(unsigned position) const
</span><span class="cx"> {
</span><span class="cx">     auto&amp; lastSegment = m_segments[m_lastSegmentIndex];
</span></span></pre></div>
<a id="trunkSourceWebCorerenderingSimpleLineLayoutFunctionscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/rendering/SimpleLineLayoutFunctions.cpp (181691 => 181692)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/rendering/SimpleLineLayoutFunctions.cpp        2015-03-18 13:47:52 UTC (rev 181691)
+++ trunk/Source/WebCore/rendering/SimpleLineLayoutFunctions.cpp        2015-03-18 14:48:01 UTC (rev 181692)
</span><span class="lines">@@ -84,6 +84,7 @@
</span><span class="cx">             continue;
</span><span class="cx">         TextRun textRun(run.text());
</span><span class="cx">         textRun.setTabSize(!style.collapseWhiteSpace(), style.tabSize());
</span><ins>+        textRun.setXPos(run.rect().x());
</ins><span class="cx">         FloatPoint textOrigin = run.baseline() + paintOffset;
</span><span class="cx">         textOrigin.setY(roundToDevicePixel(LayoutUnit(textOrigin.y()), flow.document().deviceScaleFactor()));
</span><span class="cx">         context.drawText(font, textRun, textOrigin);
</span></span></pre></div>
<a id="trunkSourceWebCorerenderingSimpleLineLayoutTextFragmentIteratorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/rendering/SimpleLineLayoutTextFragmentIterator.cpp (181691 => 181692)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/rendering/SimpleLineLayoutTextFragmentIterator.cpp        2015-03-18 13:47:52 UTC (rev 181691)
+++ trunk/Source/WebCore/rendering/SimpleLineLayoutTextFragmentIterator.cpp        2015-03-18 14:48:01 UTC (rev 181692)
</span><span class="lines">@@ -48,7 +48,8 @@
</span><span class="cx"> 
</span><span class="cx"> TextFragmentIterator::TextFragmentIterator(const RenderBlockFlow&amp; flow)
</span><span class="cx">     : m_flowContents(flow)
</span><del>-    , m_lineBreakIterator((*m_flowContents.begin()).text, flow.style().locale())
</del><ins>+    , m_currentSegment(m_flowContents.begin())
+    , m_lineBreakIterator(m_currentSegment-&gt;text, flow.style().locale())
</ins><span class="cx">     , m_style(flow.style())
</span><span class="cx"> {
</span><span class="cx"> }
</span><span class="lines">@@ -60,115 +61,136 @@
</span><span class="cx">     // 2. whitespace (collasped, non-collapsed multi or single) or
</span><span class="cx">     // 3. non-whitespace characters.
</span><span class="cx">     // 4. empty, indicating content end.
</span><del>-    if (isEnd(m_position))
</del><ins>+    ASSERT(m_currentSegment != m_flowContents.end());
+    if (m_position == m_currentSegment-&gt;end)
+        ++m_currentSegment;
+    // Is it content end?
+    if (m_currentSegment == m_flowContents.end())
</ins><span class="cx">         return TextFragment(m_position, m_position, 0, TextFragment::ContentEnd);
</span><ins>+    unsigned segmentEndPosition = m_currentSegment-&gt;end;
+    unsigned startPosition = m_position;
</ins><span class="cx">     if (isLineBreak(m_position)) {
</span><del>-        TextFragment fragment(m_position, m_position + 1, 0, TextFragment::LineBreak);
-        ++m_position;
-        return fragment;
</del><ins>+        unsigned endPosition = ++m_position;
+        return TextFragment(startPosition, endPosition, 0, TextFragment::LineBreak);
</ins><span class="cx">     }
</span><del>-    unsigned startPosition = m_position;
-    unsigned endPosition = skipToNextPosition(PositionType::NonWhitespace, startPosition);
</del><ins>+    float width = 0;
+    bool overlappingFragment = false;
+    unsigned endPosition = skipToNextPosition(PositionType::NonWhitespace, startPosition, width, xPosition, overlappingFragment);
</ins><span class="cx">     ASSERT(startPosition &lt;= endPosition);
</span><del>-    if (endPosition &gt; startPosition) {
</del><ins>+    if (startPosition &lt; endPosition) {
</ins><span class="cx">         bool multipleWhitespace = startPosition + 1 &lt; endPosition;
</span><span class="cx">         bool isCollapsed = multipleWhitespace &amp;&amp; m_style.collapseWhitespace;
</span><span class="cx">         bool isBreakable = !isCollapsed &amp;&amp; multipleWhitespace;
</span><del>-        float width = isCollapsed ? m_style.spaceWidth : textWidth(startPosition, endPosition, xPosition);
</del><span class="cx">         m_position = endPosition;
</span><del>-        return TextFragment(startPosition, endPosition, width, TextFragment::Whitespace, isCollapsed, isBreakable);
</del><ins>+        return TextFragment(startPosition, endPosition, width, TextFragment::Whitespace, endPosition == segmentEndPosition, false, isCollapsed, m_style.collapseWhitespace, isBreakable);
</ins><span class="cx">     }
</span><del>-    endPosition = skipToNextPosition(PositionType::Breakable, startPosition + 1);
</del><ins>+    endPosition = skipToNextPosition(PositionType::Breakable, startPosition, width, xPosition, overlappingFragment);
</ins><span class="cx">     m_position = endPosition;
</span><del>-    return TextFragment(startPosition, endPosition, textWidth(startPosition, endPosition, xPosition), TextFragment::NonWhitespace, false, m_style.breakWordOnOverflow);
</del><ins>+    return TextFragment(startPosition, endPosition, width, TextFragment::NonWhitespace, endPosition == segmentEndPosition, overlappingFragment, false, false, m_style.breakWordOnOverflow);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-float TextFragmentIterator::textWidth(unsigned from, unsigned to, float xPosition) const
</del><ins>+void TextFragmentIterator::revertToFragment(const TextFragment&amp; fragment)
</ins><span class="cx"> {
</span><del>-    const auto&amp; fromSegment = m_flowContents.segmentForPosition(from);
-    ASSERT(is&lt;RenderText&gt;(fromSegment.renderer));
-    if ((m_style.font.isFixedPitch() &amp;&amp; fromSegment.end &gt;= to) || (from == fromSegment.start &amp;&amp; to == fromSegment.end))
-        return downcast&lt;RenderText&gt;(fromSegment.renderer).width(from - fromSegment.start, to - from, m_style.font, xPosition, nullptr, nullptr);
-
-    const auto* segment = &amp;fromSegment;
-    float textWidth = 0;
-    unsigned fragmentEnd = 0;
-    while (true) {
-        fragmentEnd = std::min(to, segment-&gt;end);
-        textWidth += segment-&gt;text.is8Bit() ? runWidth&lt;LChar&gt;(segment-&gt;text, from - segment-&gt;start, fragmentEnd - segment-&gt;start, xPosition + textWidth) :
-            runWidth&lt;UChar&gt;(segment-&gt;text, from - segment-&gt;start, fragmentEnd - segment-&gt;start, xPosition + textWidth);
-        if (fragmentEnd == to)
-            break;
-        from = fragmentEnd;
-        segment = &amp;m_flowContents.segmentForPosition(fragmentEnd);
-    };
-
-    return textWidth;
</del><ins>+    ASSERT(m_position &gt;= fragment.end());
+    ASSERT(fragment.start() &gt;= 0);
+    // Revert segment first.
+    while (m_currentSegment-&gt;start &gt; fragment.start())
+        --m_currentSegment;
+    // TODO: It reverts to the last fragment on the same position, but that's ok for now as we don't need to
+    // differentiate multiple renderers on the same position.
+    m_position = fragment.start();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template &lt;typename CharacterType&gt;
</span><del>-static unsigned nextBreakablePosition(LazyLineBreakIterator&amp; lineBreakIterator, const FlowContents::Segment&amp; segment, unsigned startPosition)
</del><ins>+unsigned TextFragmentIterator::nextBreakablePosition(const FlowContents::Segment&amp; segment, unsigned startPosition)
</ins><span class="cx"> {
</span><del>-    return nextBreakablePositionNonLoosely&lt;CharacterType, NBSPBehavior::IgnoreNBSP&gt;(lineBreakIterator, segment.text.characters&lt;CharacterType&gt;(), segment.end - segment.start, startPosition);
</del><ins>+    ASSERT(startPosition &lt; segment.end);
+    if (segment.text.impl() != m_lineBreakIterator.string().impl()) {
+        const String&amp; currentText = m_lineBreakIterator.string();
+        unsigned textLength = currentText.length();
+        UChar lastCharacter = textLength &gt; 0 ? currentText[textLength - 1] : 0;
+        UChar secondToLastCharacter = textLength &gt; 1 ? currentText[textLength - 2] : 0;
+        m_lineBreakIterator.setPriorContext(lastCharacter, secondToLastCharacter);
+        m_lineBreakIterator.resetStringAndReleaseIterator(segment.text, m_style.locale, LineBreakIteratorModeUAX14);
+    }
+    const auto* characters = segment.text.characters&lt;CharacterType&gt;();
+    unsigned segmentLength = segment.end - segment.start;
+    unsigned segmentPosition = startPosition - segment.start;
+    return segment.start + nextBreakablePositionNonLoosely&lt;CharacterType, NBSPBehavior::IgnoreNBSP&gt;(m_lineBreakIterator, characters, segmentLength, segmentPosition);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template &lt;typename CharacterType&gt;
</span><del>-static unsigned nextNonWhitespacePosition(const FlowContents::Segment&amp; segment, unsigned startPosition, const TextFragmentIterator::Style&amp; style)
</del><ins>+unsigned TextFragmentIterator::nextNonWhitespacePosition(const FlowContents::Segment&amp; segment, unsigned startPosition)
</ins><span class="cx"> {
</span><ins>+    ASSERT(startPosition &lt; segment.end);
</ins><span class="cx">     const auto* text = segment.text.characters&lt;CharacterType&gt;();
</span><span class="cx">     unsigned position = startPosition;
</span><del>-    unsigned length = segment.end - segment.start;
-    for (; position &lt; length; ++position) {
-        auto character = text[position];
-        bool isWhitespace = character == ' ' || character == '\t' || (!style.preserveNewline &amp;&amp; character == '\n');
</del><ins>+    for (; position &lt; segment.end; ++position) {
+        auto character = text[position - segment.start];
+        bool isWhitespace = character == ' ' || character == '\t' || (!m_style.preserveNewline &amp;&amp; character == '\n');
</ins><span class="cx">         if (!isWhitespace)
</span><span class="cx">             return position;
</span><span class="cx">     }
</span><span class="cx">     return position;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-unsigned TextFragmentIterator::skipToNextPosition(PositionType positionType, unsigned startPosition) const
</del><ins>+float TextFragmentIterator::textWidth(unsigned from, unsigned to, float xPosition) const
</ins><span class="cx"> {
</span><del>-    if (isEnd(startPosition))
-        return startPosition;
</del><ins>+    auto&amp; segment = *m_currentSegment;
+    ASSERT(segment.start &lt;= from &amp;&amp; from &lt;= segment.end &amp;&amp; segment.start &lt;= to &amp;&amp; to &lt;= segment.end);
+    ASSERT(is&lt;RenderText&gt;(segment.renderer));
+    if (m_style.font.isFixedPitch() || (from == segment.start &amp;&amp; to == segment.end))
+        return downcast&lt;RenderText&gt;(segment.renderer).width(from - segment.start, to - from, m_style.font, xPosition, nullptr, nullptr);
+    return segment.text.is8Bit() ? runWidth&lt;LChar&gt;(segment, from, to, xPosition) : runWidth&lt;UChar&gt;(segment, from, to, xPosition);
+}
</ins><span class="cx"> 
</span><ins>+unsigned TextFragmentIterator::skipToNextPosition(PositionType positionType, unsigned startPosition, float&amp; width, float xPosition, bool&amp; overlappingFragment)
+{
+    overlappingFragment = false;
</ins><span class="cx">     unsigned currentPosition = startPosition;
</span><del>-    FlowContents::Iterator it(m_flowContents, m_flowContents.segmentIndexForPosition(currentPosition));
-    for (auto end = m_flowContents.end(); it != end; ++it) {
-        auto&amp; segment = *it;
-        unsigned currentPositonRelativeToSegment = currentPosition - segment.start;
-        unsigned nextPositionRelativeToSegment = 0;
-        if (positionType == NonWhitespace) {
-            nextPositionRelativeToSegment = segment.text.is8Bit() ? nextNonWhitespacePosition&lt;LChar&gt;(segment, currentPositonRelativeToSegment, m_style) :
-                nextNonWhitespacePosition&lt;UChar&gt;(segment, currentPositonRelativeToSegment, m_style);
-        } else if (positionType == Breakable) {
-            if (segment.text.impl() != m_lineBreakIterator.string().impl()) {
-                UChar lastCharacter = segment.start &gt; 0 ? characterAt(segment.start - 1) : 0;
-                UChar secondToLastCharacter = segment.start &gt; 1 ? characterAt(segment.start - 2) : 0;
-                m_lineBreakIterator.setPriorContext(lastCharacter, secondToLastCharacter);
-                m_lineBreakIterator.resetStringAndReleaseIterator(segment.text, m_style.locale, LineBreakIteratorModeUAX14);
-            }
-            nextPositionRelativeToSegment = segment.text.is8Bit() ? nextBreakablePosition&lt;LChar&gt;(m_lineBreakIterator, segment, currentPositonRelativeToSegment) :
-                nextBreakablePosition&lt;UChar&gt;(m_lineBreakIterator, segment, currentPositonRelativeToSegment);
-        } else
-            ASSERT_NOT_REACHED();
-        currentPosition = segment.start + nextPositionRelativeToSegment;
-        if (currentPosition &lt; segment.end)
-            break;
</del><ins>+    unsigned nextPosition = currentPosition;
+    // Collapsed whitespace has constant width. Do not measure it.
+    if (positionType == NonWhitespace)
+        nextPosition = m_currentSegment-&gt;text.is8Bit() ? nextNonWhitespacePosition&lt;LChar&gt;(*m_currentSegment, currentPosition) : nextNonWhitespacePosition&lt;UChar&gt;(*m_currentSegment, currentPosition);
+    else if (positionType == Breakable) {
+        // nextBreakablePosition returns the same position for certain characters such as hyphens. Call next again with modified position unless it's the end of the segment.
+        nextPosition = m_currentSegment-&gt;text.is8Bit() ? nextBreakablePosition&lt;LChar&gt;(*m_currentSegment, currentPosition) : nextBreakablePosition&lt;UChar&gt;(*m_currentSegment, currentPosition);
+        // We need to know whether the word actually finishes at the end of this renderer or not.
+        if (nextPosition == m_currentSegment-&gt;end) {
+            auto nextSegment = m_currentSegment;
+            ++nextSegment;
+            if (nextSegment != m_flowContents.end())
+                overlappingFragment = nextPosition &lt; (nextSegment-&gt;text.is8Bit() ? nextBreakablePosition&lt;LChar&gt;(*nextSegment, nextPosition) : nextBreakablePosition&lt;UChar&gt;(*nextSegment, nextPosition));
+        } else if (nextPosition == currentPosition) {
+            if (++nextPosition &lt; m_currentSegment-&gt;end)
+                nextPosition = m_currentSegment-&gt;text.is8Bit() ? nextBreakablePosition&lt;LChar&gt;(*m_currentSegment, currentPosition + 1) : nextBreakablePosition&lt;UChar&gt;(*m_currentSegment, currentPosition + 1);
+        }
</ins><span class="cx">     }
</span><del>-    return currentPosition;
</del><ins>+    width = 0;
+    if (nextPosition == currentPosition)
+        return currentPosition;
+    bool measureText = positionType != NonWhitespace || !m_style.collapseWhitespace;
+    if (measureText) {
+        float textWidth = this-&gt;textWidth(currentPosition, nextPosition, xPosition);
+        xPosition += textWidth;
+        width += textWidth;
+    } else if (startPosition &lt; nextPosition)
+        width = m_style.spaceWidth;
+    return nextPosition;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template &lt;typename CharacterType&gt;
</span><del>-float TextFragmentIterator::runWidth(const String&amp; text, unsigned from, unsigned to, float xPosition) const
</del><ins>+float TextFragmentIterator::runWidth(const FlowContents::Segment&amp; segment, unsigned startPosition, unsigned endPosition, float xPosition) const
</ins><span class="cx"> {
</span><del>-    ASSERT(from &lt;= to);
-    if (from == to)
</del><ins>+    ASSERT(startPosition &lt;= endPosition);
+    if (startPosition == endPosition)
</ins><span class="cx">         return 0;
</span><del>-    bool measureWithEndSpace = m_style.collapseWhitespace &amp;&amp; to &lt; text.length() &amp;&amp; text[to] == ' ';
</del><ins>+    unsigned segmentFrom = startPosition - segment.start;
+    unsigned segmentTo = endPosition - segment.start;
+    bool measureWithEndSpace = m_style.collapseWhitespace &amp;&amp; segmentTo &lt; segment.text.length() &amp;&amp; segment.text[segmentTo] == ' ';
</ins><span class="cx">     if (measureWithEndSpace)
</span><del>-        ++to;
-    TextRun run(text.characters&lt;CharacterType&gt;() + from, to - from);
</del><ins>+        ++segmentTo;
+    TextRun run(segment.text.characters&lt;CharacterType&gt;() + segmentFrom, segmentTo - segmentFrom);
</ins><span class="cx">     run.setXPos(xPosition);
</span><span class="cx">     run.setTabSize(!!m_style.tabWidth, m_style.tabWidth);
</span><span class="cx">     float width = m_style.font.width(run);
</span></span></pre></div>
<a id="trunkSourceWebCorerenderingSimpleLineLayoutTextFragmentIteratorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/rendering/SimpleLineLayoutTextFragmentIterator.h (181691 => 181692)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/rendering/SimpleLineLayoutTextFragmentIterator.h        2015-03-18 13:47:52 UTC (rev 181691)
+++ trunk/Source/WebCore/rendering/SimpleLineLayoutTextFragmentIterator.h        2015-03-18 14:48:01 UTC (rev 181692)
</span><span class="lines">@@ -43,12 +43,15 @@
</span><span class="cx">     public:
</span><span class="cx">         enum Type { ContentEnd, LineBreak, Whitespace, NonWhitespace };
</span><span class="cx">         TextFragment() = default;
</span><del>-        TextFragment(unsigned start, unsigned end, float width, Type type, bool isCollapsed = false, bool isBreakable = false)
</del><ins>+        TextFragment(unsigned start, unsigned end, float width, Type type, bool isLastInRenderer = false, bool overlapsToNextRenderer = false, bool isCollapsed = false, bool isCollapsible = false, bool isBreakable = false)
</ins><span class="cx">             : m_start(start)
</span><span class="cx">             , m_end(end)
</span><ins>+            , m_width(width)
</ins><span class="cx">             , m_type(type)
</span><del>-            , m_width(width)
</del><ins>+            , m_isLastInRenderer(isLastInRenderer)
+            , m_overlapsToNextRenderer(overlapsToNextRenderer)
</ins><span class="cx">             , m_isCollapsed(isCollapsed)
</span><ins>+            , m_isCollapsible(isCollapsible)
</ins><span class="cx">             , m_isBreakable(isBreakable)
</span><span class="cx">         {
</span><span class="cx">         }
</span><span class="lines">@@ -57,22 +60,41 @@
</span><span class="cx">         unsigned end() const { return m_end; }
</span><span class="cx">         float width() const { return m_width; }
</span><span class="cx">         Type type() const { return m_type; }
</span><ins>+        bool isLastInRenderer() const { return m_isLastInRenderer; }
+        bool overlapsToNextRenderer() const { return m_overlapsToNextRenderer; }
</ins><span class="cx">         bool isCollapsed() const { return m_isCollapsed; }
</span><ins>+        bool isCollapsible() const { return m_isCollapsible; }
</ins><span class="cx">         bool isBreakable() const { return m_isBreakable; }
</span><span class="cx"> 
</span><span class="cx">         bool isEmpty() const { return start() == end(); }
</span><span class="cx">         TextFragment split(unsigned splitPosition, const TextFragmentIterator&amp;);
</span><ins>+        bool operator==(const TextFragment&amp; other) const
+        {
+            return m_start == other.m_start
+                &amp;&amp; m_end == other.m_end
+                &amp;&amp; m_width == other.m_width
+                &amp;&amp; m_type == other.m_type
+                &amp;&amp; m_isLastInRenderer == other.m_isLastInRenderer
+                &amp;&amp; m_overlapsToNextRenderer == other.m_overlapsToNextRenderer
+                &amp;&amp; m_isCollapsed == other.m_isCollapsed
+                &amp;&amp; m_isCollapsible == other.m_isCollapsible
+                &amp;&amp; m_isBreakable == other.m_isBreakable;
+        }
</ins><span class="cx"> 
</span><span class="cx">     private:
</span><span class="cx">         unsigned m_start { 0 };
</span><span class="cx">         unsigned m_end { 0 };
</span><ins>+        float m_width { 0 };
</ins><span class="cx">         Type m_type { NonWhitespace };
</span><del>-        float m_width { 0 };
</del><ins>+        bool m_isLastInRenderer { false };
+        bool m_overlapsToNextRenderer { false };
</ins><span class="cx">         bool m_isCollapsed { false };
</span><ins>+        bool m_isCollapsible { false };
</ins><span class="cx">         bool m_isBreakable { false };
</span><span class="cx">     };
</span><span class="cx">     TextFragment nextTextFragment(float xPosition = 0);
</span><del>-    float textWidth(unsigned from, unsigned to, float xPosition) const;
</del><ins>+    void revertToFragment(const TextFragment&amp;);
+    float textWidth(unsigned startPosition, unsigned endPosition, float xPosition) const;
</ins><span class="cx"> 
</span><span class="cx">     struct Style {
</span><span class="cx">         explicit Style(const RenderStyle&amp;);
</span><span class="lines">@@ -88,19 +110,18 @@
</span><span class="cx">         AtomicString locale;
</span><span class="cx">     };
</span><span class="cx">     const Style&amp; style() const { return m_style; }
</span><del>-    // FIXME: remove splitRunsAtRendererBoundary()
-    const FlowContents::Segment&amp; segmentForPosition(unsigned position) const { return m_flowContents.segmentForPosition(position); };
</del><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     enum PositionType { Breakable, NonWhitespace };
</span><del>-    unsigned skipToNextPosition(PositionType, unsigned startPosition) const;
-    UChar characterAt(unsigned position) const;
</del><ins>+    unsigned skipToNextPosition(PositionType, unsigned startPosition, float&amp; width, float xPosition, bool&amp; overlappingFragment);
</ins><span class="cx">     bool isLineBreak(unsigned position) const;
</span><del>-    bool isEnd(unsigned position) const;
-    template &lt;typename CharacterType&gt; float runWidth(const String&amp;, unsigned from, unsigned to, float xPosition) const;
</del><ins>+    template &lt;typename CharacterType&gt; unsigned nextBreakablePosition(const FlowContents::Segment&amp;, unsigned startPosition);
+    template &lt;typename CharacterType&gt; unsigned nextNonWhitespacePosition(const FlowContents::Segment&amp;, unsigned startPosition);
+    template &lt;typename CharacterType&gt; float runWidth(const FlowContents::Segment&amp;, unsigned startPosition, unsigned endPosition, float xPosition) const;
</ins><span class="cx"> 
</span><span class="cx">     FlowContents m_flowContents;
</span><del>-    mutable LazyLineBreakIterator m_lineBreakIterator;
</del><ins>+    FlowContents::Iterator m_currentSegment;
+    LazyLineBreakIterator m_lineBreakIterator;
</ins><span class="cx">     const Style m_style;
</span><span class="cx">     unsigned m_position { 0 };
</span><span class="cx"> };
</span><span class="lines">@@ -127,25 +148,14 @@
</span><span class="cx">     return newFragment;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-inline UChar TextFragmentIterator::characterAt(unsigned position) const
-{
-    auto&amp; segment = m_flowContents.segmentForPosition(position);
-    return segment.text[position - segment.start];
-}
-
</del><span class="cx"> inline bool TextFragmentIterator::isLineBreak(unsigned position) const
</span><span class="cx"> {
</span><del>-    if (isEnd(position))
-        return false;
-    return m_style.preserveNewline &amp;&amp; characterAt(position) == '\n';
</del><ins>+    const auto&amp; segment = *m_currentSegment;
+    ASSERT(segment.start &lt;= position &amp;&amp; position &lt; segment.end);
+    return m_style.preserveNewline &amp;&amp; segment.text[position - segment.start] == '\n';
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-inline bool TextFragmentIterator::isEnd(unsigned position) const
-{
-    return position &gt;= m_flowContents.length();
</del><span class="cx"> }
</span><del>-
</del><span class="cx"> }
</span><del>-}
</del><span class="cx"> 
</span><span class="cx"> #endif
</span></span></pre>
</div>
</div>

</body>
</html>