<!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>[181013] 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/181013">181013</a></dd>
<dt>Author</dt> <dd>mmaxfield@apple.com</dd>
<dt>Date</dt> <dd>2015-03-04 12:48:04 -0800 (Wed, 04 Mar 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Implement -apple-trailing-word: -apple-partially-balanced
https://bugs.webkit.org/show_bug.cgi?id=142253

Reviewed by David Hyatt.

Source/WebCore:

This patch implements a history mechanism for line breaking. In particular, this patch partitions
the updates to the current line breaking location into two kinds: conceptually new breaking locations,
and fixups to existing locations. Then, this patch remembers all the fixed up breaking locations, up
to a maximum number of remembered locations.

The patch then uses this memory to change the line-breaking selection based on the rules of
-apple-trailing-word.

Test: fast/text/trailing-word.html

* rendering/line/BreakingContextInlineHeaders.h:
Use InlineIteratorHistory as a proxy for the current breaking location. Note that all these functions
are inlined, so the overhead should be next to nothing when -apple-trailing-word is not in use.
(WebCore::BreakingContext::BreakingContext): Use InlineIteratorHistory instead of InlineIterator
(WebCore::BreakingContext::lineBreak): Ditto.
(WebCore::BreakingContext::clearLineBreakIfFitsOnLine): Ditto
(WebCore::BreakingContext::commitLineBreakAtCurrentWidth): Ditto
(WebCore::BreakingContext::InlineIteratorHistory::InlineIteratorHistory): Keeps track of historical
breaking locations
(WebCore::BreakingContext::InlineIteratorHistory::push): Remember a new breaking location
(WebCore::BreakingContext::InlineIteratorHistory::update): Update an existing breaking location
(WebCore::BreakingContext::InlineIteratorHistory::renderer): Forwarded to the current breaking location
(WebCore::BreakingContext::InlineIteratorHistory::offset): Ditto
(WebCore::BreakingContext::InlineIteratorHistory::atTextParagraphSeparator):  Ditto
(WebCore::BreakingContext::InlineIteratorHistory::previousInSameNode): Ditto
(WebCore::BreakingContext::InlineIteratorHistory::get): Get one of the remembered breaking locations
(WebCore::BreakingContext::InlineIteratorHistory::current): Get the current breaking location
(WebCore::BreakingContext::InlineIteratorHistory::historyLength):
(WebCore::BreakingContext::InlineIteratorHistory::moveTo): Forwarded to the current breaking location.
(WebCore::BreakingContext::InlineIteratorHistory::increment): Ditto
(WebCore::BreakingContext::InlineIteratorHistory::clear): Ditto
(WebCore::BreakingContext::handleBR): Use InlineIteratorHistory instead of InlineIterator
(WebCore::BreakingContext::handleFloat): Ditto
(WebCore::BreakingContext::handleText): Use InlineIteratorHistory instead of InlineIterator
(WebCore::BreakingContext::commitAndUpdateLineBreakIfNeeded): Style
(WebCore::checkMidpoints): Use InlineIteratorHistory instead of InlineIterator
(WebCore::BreakingContext::handleEndOfLine): If -apple-trailing-word is in effect, use
optimalLineBreakLocationForTrailingWord().
(WebCore::BreakingContext::optimalLineBreakLocationForTrailingWord): Use the remembered breaking
locations and choose the optimal one.
(WebCore::BreakingContext::lineBreakRef): Deleted.

LayoutTests:

* fast/text/trailing-word-expected.html: Added.
* fast/text/trailing-word.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="#trunkSourceWebCorerenderinglineBreakingContextInlineHeadersh">trunk/Source/WebCore/rendering/line/BreakingContextInlineHeaders.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsfasttexttrailingwordexpectedhtml">trunk/LayoutTests/fast/text/trailing-word-expected.html</a></li>
<li><a href="#trunkLayoutTestsfasttexttrailingwordhtml">trunk/LayoutTests/fast/text/trailing-word.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (181012 => 181013)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2015-03-04 20:30:09 UTC (rev 181012)
+++ trunk/LayoutTests/ChangeLog        2015-03-04 20:48:04 UTC (rev 181013)
</span><span class="lines">@@ -1,3 +1,13 @@
</span><ins>+2015-03-04  Myles C. Maxfield  &lt;mmaxfield@apple.com&gt;
+
+        Implement -apple-trailing-word: -apple-partially-balanced
+        https://bugs.webkit.org/show_bug.cgi?id=142253
+
+        Reviewed by David Hyatt.
+
+        * fast/text/trailing-word-expected.html: Added.
+        * fast/text/trailing-word.html: Added.
+
</ins><span class="cx"> 2015-03-04  Marcos Chavarría Teijeiro  &lt;chavarria1991@gmail.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Unreviewed Gardening 4th March
</span></span></pre></div>
<a id="trunkLayoutTestsfasttexttrailingwordexpectedhtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/fast/text/trailing-word-expected.html (0 => 181013)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/text/trailing-word-expected.html                                (rev 0)
+++ trunk/LayoutTests/fast/text/trailing-word-expected.html        2015-03-04 20:48:04 UTC (rev 181013)
</span><span class="lines">@@ -0,0 +1,15 @@
</span><ins>+&lt;!DOCTYPE html&gt;
+&lt;html&gt;
+&lt;head&gt;
+&lt;/head&gt;
+&lt;body&gt;
+This test makes sure that -apple-trailing-word: -apple-partially-balanced breaks lines correctly.
+&lt;div style=&quot;font-family: Ahem;&quot;&gt;
+&lt;div&gt;ABC DEF GHI JKL MNO PQRS TUVW XYZ ABC DEF GHI JKL&lt;br&gt;MNO&lt;/div&gt;
+&lt;div&gt;ABC DEF GHI JKL MNO PQRS TUVW XYZ ABC DEF GHI&lt;br&gt;JKL MNO&lt;/div&gt;
+&lt;div&gt;ABC DEF GHI JKL MNO PQRS TUVW XYZ ABC DEF G H&lt;br&gt;I J K&lt;/div&gt;
+&lt;div&gt;ABC DEF GHI JKL MNO PQRS TUVW XYZ ABC DEF GHI JKL&lt;br&gt;MNO PQRS&lt;/div&gt;
+&lt;div&gt;ABC DEF GHI JKL MNO PQRS TUVW XYZ ABC DEF GHI JKL&lt;br&gt;MNO PQRS TUV WXYZ ABC DEF GHI JKL MNO PQRS TUV&lt;br&gt;WXYZ&lt;/div&gt;
+&lt;/div&gt;
+&lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkLayoutTestsfasttexttrailingwordhtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/fast/text/trailing-word.html (0 => 181013)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/text/trailing-word.html                                (rev 0)
+++ trunk/LayoutTests/fast/text/trailing-word.html        2015-03-04 20:48:04 UTC (rev 181013)
</span><span class="lines">@@ -0,0 +1,15 @@
</span><ins>+&lt;!DOCTYPE html&gt;
+&lt;html&gt;
+&lt;head&gt;
+&lt;/head&gt;
+&lt;body&gt;
+This test makes sure that -apple-trailing-word: -apple-partially-balanced breaks lines correctly.
+&lt;div style=&quot;font-family: Ahem;&quot;&gt;
+&lt;div&gt;ABC DEF GHI JKL MNO PQRS TUVW XYZ ABC DEF GHI JKL MNO&lt;/div&gt;
+&lt;div style=&quot;-apple-trailing-word: -apple-partially-balanced;&quot;&gt;ABC DEF GHI JKL MNO PQRS TUVW XYZ ABC DEF GHI JKL MNO&lt;/div&gt;
+&lt;div style=&quot;-apple-trailing-word: -apple-partially-balanced;&quot;&gt;ABC DEF GHI JKL MNO PQRS TUVW XYZ ABC DEF G H I J K&lt;/div&gt;
+&lt;div style=&quot;-apple-trailing-word: -apple-partially-balanced;&quot;&gt;ABC DEF GHI JKL MNO PQRS TUVW XYZ ABC DEF GHI JKL MNO PQRS&lt;/div&gt;
+&lt;div style=&quot;-apple-trailing-word: -apple-partially-balanced;&quot;&gt;ABC DEF GHI JKL MNO PQRS TUVW XYZ ABC DEF GHI JKL MNO PQRS TUV WXYZ ABC DEF GHI JKL MNO PQRS TUV WXYZ&lt;/div&gt;
+&lt;/div&gt;
+&lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (181012 => 181013)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-03-04 20:30:09 UTC (rev 181012)
+++ trunk/Source/WebCore/ChangeLog        2015-03-04 20:48:04 UTC (rev 181013)
</span><span class="lines">@@ -1,3 +1,52 @@
</span><ins>+2015-03-04  Myles C. Maxfield  &lt;mmaxfield@apple.com&gt;
+
+        Implement -apple-trailing-word: -apple-partially-balanced
+        https://bugs.webkit.org/show_bug.cgi?id=142253
+
+        Reviewed by David Hyatt.
+
+        This patch implements a history mechanism for line breaking. In particular, this patch partitions
+        the updates to the current line breaking location into two kinds: conceptually new breaking locations,
+        and fixups to existing locations. Then, this patch remembers all the fixed up breaking locations, up
+        to a maximum number of remembered locations.
+
+        The patch then uses this memory to change the line-breaking selection based on the rules of
+        -apple-trailing-word.
+
+        Test: fast/text/trailing-word.html
+
+        * rendering/line/BreakingContextInlineHeaders.h:
+        Use InlineIteratorHistory as a proxy for the current breaking location. Note that all these functions
+        are inlined, so the overhead should be next to nothing when -apple-trailing-word is not in use.
+        (WebCore::BreakingContext::BreakingContext): Use InlineIteratorHistory instead of InlineIterator
+        (WebCore::BreakingContext::lineBreak): Ditto.
+        (WebCore::BreakingContext::clearLineBreakIfFitsOnLine): Ditto
+        (WebCore::BreakingContext::commitLineBreakAtCurrentWidth): Ditto
+        (WebCore::BreakingContext::InlineIteratorHistory::InlineIteratorHistory): Keeps track of historical
+        breaking locations
+        (WebCore::BreakingContext::InlineIteratorHistory::push): Remember a new breaking location
+        (WebCore::BreakingContext::InlineIteratorHistory::update): Update an existing breaking location
+        (WebCore::BreakingContext::InlineIteratorHistory::renderer): Forwarded to the current breaking location
+        (WebCore::BreakingContext::InlineIteratorHistory::offset): Ditto
+        (WebCore::BreakingContext::InlineIteratorHistory::atTextParagraphSeparator):  Ditto
+        (WebCore::BreakingContext::InlineIteratorHistory::previousInSameNode): Ditto
+        (WebCore::BreakingContext::InlineIteratorHistory::get): Get one of the remembered breaking locations
+        (WebCore::BreakingContext::InlineIteratorHistory::current): Get the current breaking location
+        (WebCore::BreakingContext::InlineIteratorHistory::historyLength):
+        (WebCore::BreakingContext::InlineIteratorHistory::moveTo): Forwarded to the current breaking location.
+        (WebCore::BreakingContext::InlineIteratorHistory::increment): Ditto
+        (WebCore::BreakingContext::InlineIteratorHistory::clear): Ditto
+        (WebCore::BreakingContext::handleBR): Use InlineIteratorHistory instead of InlineIterator
+        (WebCore::BreakingContext::handleFloat): Ditto
+        (WebCore::BreakingContext::handleText): Use InlineIteratorHistory instead of InlineIterator
+        (WebCore::BreakingContext::commitAndUpdateLineBreakIfNeeded): Style
+        (WebCore::checkMidpoints): Use InlineIteratorHistory instead of InlineIterator
+        (WebCore::BreakingContext::handleEndOfLine): If -apple-trailing-word is in effect, use
+        optimalLineBreakLocationForTrailingWord().
+        (WebCore::BreakingContext::optimalLineBreakLocationForTrailingWord): Use the remembered breaking
+        locations and choose the optimal one.
+        (WebCore::BreakingContext::lineBreakRef): Deleted.
+
</ins><span class="cx"> 2015-03-04  Timothy Horton  &lt;timothy_horton@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         &lt;attachment&gt; title text disappears when dragging
</span></span></pre></div>
<a id="trunkSourceWebCorerenderinglineBreakingContextInlineHeadersh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/rendering/line/BreakingContextInlineHeaders.h (181012 => 181013)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/rendering/line/BreakingContextInlineHeaders.h        2015-03-04 20:30:09 UTC (rev 181012)
+++ trunk/Source/WebCore/rendering/line/BreakingContextInlineHeaders.h        2015-03-04 20:48:04 UTC (rev 181013)
</span><span class="lines">@@ -68,7 +68,11 @@
</span><span class="cx">         : m_lineBreaker(lineBreaker)
</span><span class="cx">         , m_resolver(resolver)
</span><span class="cx">         , m_current(resolver.position())
</span><del>-        , m_lineBreak(resolver.position())
</del><ins>+#if ENABLE(CSS_TRAILING_WORD)
+        , m_lineBreakHistory(InlineIterator(resolver.position()), block.style().trailingWord() == TrailingWord::PartiallyBalanced ? 5 : 1)
+#else
+        , m_lineBreakHistory(InlineIterator(resolver.position()), 1)
+#endif
</ins><span class="cx">         , m_block(block)
</span><span class="cx">         , m_lastObject(m_current.renderer())
</span><span class="cx">         , m_nextObject(nullptr)
</span><span class="lines">@@ -101,8 +105,7 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     RenderObject* currentObject() { return m_current.renderer(); }
</span><del>-    InlineIterator lineBreak() { return m_lineBreak; }
-    InlineIterator&amp; lineBreakRef() {return m_lineBreak; }
</del><ins>+    InlineIterator lineBreak() { return m_lineBreakHistory.current(); }
</ins><span class="cx">     LineWidth&amp; lineWidth() { return m_width; }
</span><span class="cx">     bool atEnd() { return m_atEnd; }
</span><span class="cx"> 
</span><span class="lines">@@ -119,25 +122,88 @@
</span><span class="cx">     bool canBreakAtThisPosition();
</span><span class="cx">     void commitAndUpdateLineBreakIfNeeded();
</span><span class="cx">     InlineIterator handleEndOfLine();
</span><ins>+#if ENABLE(CSS_TRAILING_WORD)
+    InlineIterator optimalLineBreakLocationForTrailingWord();
+#endif
</ins><span class="cx"> 
</span><span class="cx">     void clearLineBreakIfFitsOnLine(bool ignoringTrailingSpace = false)
</span><span class="cx">     {
</span><span class="cx">         if (m_width.fitsOnLine(ignoringTrailingSpace) || m_lastWS == NOWRAP)
</span><del>-            m_lineBreak.clear();
</del><ins>+            m_lineBreakHistory.clear();
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     void commitLineBreakAtCurrentWidth(RenderObject&amp; object, unsigned offset = 0, int nextBreak = -1)
</span><span class="cx">     {
</span><span class="cx">         m_width.commit();
</span><del>-        m_lineBreak.moveTo(&amp;object, offset, nextBreak);
</del><ins>+        m_lineBreakHistory.moveTo(&amp;object, offset, nextBreak);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx"> private:
</span><ins>+    // This class keeps a sliding window of the past n locations for an InlineIterator.
+    class InlineIteratorHistory : private Vector&lt;InlineIterator, 1&gt; {
+    public:
+        InlineIteratorHistory() = delete;
+        InlineIteratorHistory(const InlineIterator&amp; initial, size_t capacity)
+            : m_capacity(capacity)
+        {
+            ASSERT(capacity &gt; 0);
+            this-&gt;append(initial);
+        }
+
+        void push(std::function&lt;void(InlineIterator&amp; modifyMe)&gt; updater)
+        {
+            ASSERT(!this-&gt;isEmpty());
+            if (m_capacity != 1)
+                this-&gt;insert(0, InlineIterator(this-&gt;at(0)));
+            updater(this-&gt;at(0));
+            if (m_capacity != 1)
+                this-&gt;resize(m_capacity);
+        }
+
+        void update(std::function&lt;void(InlineIterator&amp; modifyMe)&gt; updater)
+        {
+            ASSERT(!this-&gt;isEmpty());
+            updater(this-&gt;at(0));
+        }
+
+        RenderObject* renderer() const { return this-&gt;at(0).renderer(); }
+        unsigned offset() const { return this-&gt;at(0).offset(); }
+        bool atTextParagraphSeparator() const { return this-&gt;at(0).atTextParagraphSeparator(); }
+        UChar previousInSameNode() const { return this-&gt;at(0).previousInSameNode(); }
+        const InlineIterator&amp; get(size_t i) const { return this-&gt;at(i); };
+        const InlineIterator&amp; current() const { return get(0); }
+        size_t historyLength() const { return this-&gt;size(); }
+
+        void moveTo(RenderObject* object, unsigned offset, int nextBreak = -1)
+        {
+            push([&amp;](InlineIterator&amp; modifyMe) {
+                modifyMe.moveTo(object, offset, nextBreak);
+            });
+        }
+
+        void increment()
+        {
+            update([](InlineIterator&amp; modifyMe) {
+                modifyMe.increment();
+            });
+        }
+
+        void clear()
+        {
+            push([](InlineIterator&amp; modifyMe) {
+                modifyMe.clear();
+            });
+        }
+
+    private:
+        const size_t m_capacity;
+    };
+
</ins><span class="cx">     LineBreaker&amp; m_lineBreaker;
</span><span class="cx">     InlineBidiResolver&amp; m_resolver;
</span><span class="cx"> 
</span><span class="cx">     InlineIterator m_current;
</span><del>-    InlineIterator m_lineBreak;
</del><ins>+    InlineIteratorHistory m_lineBreakHistory;
</ins><span class="cx">     InlineIterator m_startOfIgnoredSpaces;
</span><span class="cx"> 
</span><span class="cx">     RenderBlockFlow&amp; m_block;
</span><span class="lines">@@ -228,8 +294,10 @@
</span><span class="cx"> {
</span><span class="cx">     if (m_width.fitsOnLine()) {
</span><span class="cx">         RenderObject* br = m_current.renderer();
</span><del>-        m_lineBreak.moveToStartOf(br);
-        m_lineBreak.increment();
</del><ins>+        m_lineBreakHistory.push([&amp;](InlineIterator&amp; modifyMe) {
+            modifyMe.moveToStartOf(br);
+            modifyMe.increment();
+        });
</ins><span class="cx"> 
</span><span class="cx">         // A &lt;br&gt; always breaks a line, so don't let the line be collapsed
</span><span class="cx">         // away. Also, the space at the end of a line with a &lt;br&gt; does not
</span><span class="lines">@@ -350,9 +418,9 @@
</span><span class="cx">     // it after moving to next line (in clearFloats() func)
</span><span class="cx">     if (m_floatsFitOnLine &amp;&amp; m_width.fitsOnLineExcludingTrailingWhitespace(m_block.logicalWidthForFloat(floatingObject))) {
</span><span class="cx">         m_lineBreaker.positionNewFloatOnLine(floatingObject, m_lastFloatFromPreviousLine, m_lineInfo, m_width);
</span><del>-        if (m_lineBreak.renderer() == m_current.renderer()) {
-            ASSERT(!m_lineBreak.offset());
-            m_lineBreak.increment();
</del><ins>+        if (m_lineBreakHistory.renderer() == m_current.renderer()) {
+            ASSERT(!m_lineBreakHistory.offset());
+            m_lineBreakHistory.increment();
</ins><span class="cx">         }
</span><span class="cx">     } else
</span><span class="cx">         m_floatsFitOnLine = false;
</span><span class="lines">@@ -610,9 +678,9 @@
</span><span class="cx">         auto&amp; combineRenderer = downcast&lt;RenderCombineText&gt;(*m_current.renderer());
</span><span class="cx">         combineRenderer.combineText();
</span><span class="cx">         // The length of the renderer's text may have changed. Increment stale iterator positions
</span><del>-        if (iteratorIsBeyondEndOfRenderCombineText(m_lineBreak, combineRenderer)) {
</del><ins>+        if (iteratorIsBeyondEndOfRenderCombineText(m_lineBreakHistory.current(), combineRenderer)) {
</ins><span class="cx">             ASSERT(iteratorIsBeyondEndOfRenderCombineText(m_resolver.position(), combineRenderer));
</span><del>-            m_lineBreak.increment();
</del><ins>+            m_lineBreakHistory.increment();
</ins><span class="cx">             m_resolver.increment();
</span><span class="cx">         }
</span><span class="cx">     }
</span><span class="lines">@@ -760,30 +828,34 @@
</span><span class="cx">                     // additional whitespace.
</span><span class="cx">                     if (!m_width.fitsOnLineIncludingExtraWidth(charWidth)) {
</span><span class="cx">                         lineWasTooWide = true;
</span><del>-                        m_lineBreak.moveTo(m_current.renderer(), m_current.offset(), m_current.nextBreakablePosition());
-                        m_lineBreaker.skipTrailingWhitespace(m_lineBreak, m_lineInfo);
</del><ins>+                        m_lineBreakHistory.push([&amp;](InlineIterator&amp; modifyMe) {
+                            modifyMe.moveTo(m_current.renderer(), m_current.offset(), m_current.nextBreakablePosition());
+                            m_lineBreaker.skipTrailingWhitespace(modifyMe, m_lineInfo);
+                        });
</ins><span class="cx">                     }
</span><span class="cx">                 }
</span><span class="cx">                 if (lineWasTooWide || !m_width.fitsOnLine()) {
</span><span class="cx">                     if (canHyphenate &amp;&amp; !m_width.fitsOnLine()) {
</span><del>-                        tryHyphenating(&amp;renderText, font, style.locale(), consecutiveHyphenatedLines, m_blockStyle.hyphenationLimitLines(), style.hyphenationLimitBefore(), style.hyphenationLimitAfter(), lastSpace, m_current.offset(), m_width.currentWidth() - additionalTempWidth, m_width.availableWidth(), isFixedPitch, m_collapseWhiteSpace, lastSpaceWordSpacing, m_lineBreak, m_current.nextBreakablePosition(), m_lineBreaker.m_hyphenated);
</del><ins>+                        m_lineBreakHistory.push([&amp;](InlineIterator&amp; modifyMe) {
+                            tryHyphenating(&amp;renderText, font, style.locale(), consecutiveHyphenatedLines, m_blockStyle.hyphenationLimitLines(), style.hyphenationLimitBefore(), style.hyphenationLimitAfter(), lastSpace, m_current.offset(), m_width.currentWidth() - additionalTempWidth, m_width.availableWidth(), isFixedPitch, m_collapseWhiteSpace, lastSpaceWordSpacing, modifyMe, m_current.nextBreakablePosition(), m_lineBreaker.m_hyphenated);
+                        });
</ins><span class="cx">                         if (m_lineBreaker.m_hyphenated) {
</span><span class="cx">                             m_atEnd = true;
</span><span class="cx">                             return false;
</span><span class="cx">                         }
</span><span class="cx">                     }
</span><del>-                    if (m_lineBreak.atTextParagraphSeparator()) {
</del><ins>+                    if (m_lineBreakHistory.atTextParagraphSeparator()) {
</ins><span class="cx">                         if (!stoppedIgnoringSpaces &amp;&amp; m_current.offset() &gt; 0)
</span><span class="cx">                             ensureCharacterGetsLineBox(m_lineMidpointState, m_current);
</span><del>-                        m_lineBreak.increment();
</del><ins>+                        m_lineBreakHistory.increment();
</ins><span class="cx">                         m_lineInfo.setPreviousLineBrokeCleanly(true);
</span><del>-                        wordMeasurement.endOffset = m_lineBreak.offset();
</del><ins>+                        wordMeasurement.endOffset = m_lineBreakHistory.offset();
</ins><span class="cx">                     }
</span><del>-                    if (m_lineBreak.offset() &amp;&amp; downcast&lt;RenderText&gt;(m_lineBreak.renderer()) &amp;&amp; downcast&lt;RenderText&gt;(*m_lineBreak.renderer()).textLength() &amp;&amp; downcast&lt;RenderText&gt;(*m_lineBreak.renderer()).characterAt(m_lineBreak.offset() - 1) == softHyphen &amp;&amp; style.hyphens() != HyphensNone)
</del><ins>+                    if (m_lineBreakHistory.offset() &amp;&amp; downcast&lt;RenderText&gt;(m_lineBreakHistory.renderer()) &amp;&amp; downcast&lt;RenderText&gt;(*m_lineBreakHistory.renderer()).textLength() &amp;&amp; downcast&lt;RenderText&gt;(*m_lineBreakHistory.renderer()).characterAt(m_lineBreakHistory.offset() - 1) == softHyphen &amp;&amp; style.hyphens() != HyphensNone)
</ins><span class="cx">                         hyphenated = true;
</span><del>-                    if (m_lineBreak.offset() &amp;&amp; m_lineBreak.offset() != (unsigned)wordMeasurement.endOffset &amp;&amp; !wordMeasurement.width) {
</del><ins>+                    if (m_lineBreakHistory.offset() &amp;&amp; m_lineBreakHistory.offset() != (unsigned)wordMeasurement.endOffset &amp;&amp; !wordMeasurement.width) {
</ins><span class="cx">                         if (charWidth) {
</span><del>-                            wordMeasurement.endOffset = m_lineBreak.offset();
</del><ins>+                            wordMeasurement.endOffset = m_lineBreakHistory.offset();
</ins><span class="cx">                             wordMeasurement.width = charWidth;
</span><span class="cx">                         }
</span><span class="cx">                     }
</span><span class="lines">@@ -807,7 +879,7 @@
</span><span class="cx">                 if (!stoppedIgnoringSpaces &amp;&amp; m_current.offset())
</span><span class="cx">                     ensureCharacterGetsLineBox(m_lineMidpointState, m_current);
</span><span class="cx">                 commitLineBreakAtCurrentWidth(*m_current.renderer(), m_current.offset(), m_current.nextBreakablePosition());
</span><del>-                m_lineBreak.increment();
</del><ins>+                m_lineBreakHistory.increment();
</ins><span class="cx">                 m_lineInfo.setPreviousLineBrokeCleanly(true);
</span><span class="cx">                 return true;
</span><span class="cx">             }
</span><span class="lines">@@ -823,7 +895,7 @@
</span><span class="cx">             if (midWordBreak &amp;&amp; !U16_IS_TRAIL(c) &amp;&amp; !(U_GET_GC_MASK(c) &amp; U_GC_M_MASK)) {
</span><span class="cx">                 // Remember this as a breakable position in case
</span><span class="cx">                 // adding the end width forces a break.
</span><del>-                m_lineBreak.moveTo(m_current.renderer(), m_current.offset(), m_current.nextBreakablePosition());
</del><ins>+                m_lineBreakHistory.moveTo(m_current.renderer(), m_current.offset(), m_current.nextBreakablePosition());
</ins><span class="cx">                 midWordBreak &amp;= (breakWords || breakAll);
</span><span class="cx">             }
</span><span class="cx"> 
</span><span class="lines">@@ -879,7 +951,7 @@
</span><span class="cx"> 
</span><span class="cx">         if (!m_currentCharacterIsWS &amp;&amp; previousCharacterIsWS) {
</span><span class="cx">             if (m_autoWrap &amp;&amp; m_currentStyle-&gt;breakOnlyAfterWhiteSpace())
</span><del>-                m_lineBreak.moveTo(m_current.renderer(), m_current.offset(), m_current.nextBreakablePosition());
</del><ins>+                m_lineBreakHistory.moveTo(m_current.renderer(), m_current.offset(), m_current.nextBreakablePosition());
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (m_collapseWhiteSpace &amp;&amp; m_currentCharacterIsSpace &amp;&amp; !m_ignoringSpaces)
</span><span class="lines">@@ -917,10 +989,13 @@
</span><span class="cx">     m_includeEndWidth = false;
</span><span class="cx"> 
</span><span class="cx">     if (!m_width.fitsOnLine()) {
</span><del>-        if (canHyphenate)
-            tryHyphenating(&amp;renderText, font, style.locale(), consecutiveHyphenatedLines, m_blockStyle.hyphenationLimitLines(), style.hyphenationLimitBefore(), style.hyphenationLimitAfter(), lastSpace, m_current.offset(), m_width.currentWidth() - additionalTempWidth, m_width.availableWidth(), isFixedPitch, m_collapseWhiteSpace, lastSpaceWordSpacing, m_lineBreak, m_current.nextBreakablePosition(), m_lineBreaker.m_hyphenated);
</del><ins>+        if (canHyphenate) {
+            m_lineBreakHistory.push([&amp;](InlineIterator&amp; modifyMe) {
+                tryHyphenating(&amp;renderText, font, style.locale(), consecutiveHyphenatedLines, m_blockStyle.hyphenationLimitLines(), style.hyphenationLimitBefore(), style.hyphenationLimitAfter(), lastSpace, m_current.offset(), m_width.currentWidth() - additionalTempWidth, m_width.availableWidth(), isFixedPitch, m_collapseWhiteSpace, lastSpaceWordSpacing, modifyMe, m_current.nextBreakablePosition(), m_lineBreaker.m_hyphenated);
+            });
+        }
</ins><span class="cx"> 
</span><del>-        if (!hyphenated &amp;&amp; m_lineBreak.previousInSameNode() == softHyphen &amp;&amp; style.hyphens() != HyphensNone) {
</del><ins>+        if (!hyphenated &amp;&amp; m_lineBreakHistory.previousInSameNode() == softHyphen &amp;&amp; style.hyphens() != HyphensNone) {
</ins><span class="cx">             hyphenated = true;
</span><span class="cx">             m_atEnd = true;
</span><span class="cx">         }
</span><span class="lines">@@ -1008,13 +1083,12 @@
</span><span class="cx"> 
</span><span class="cx">     if (!m_current.renderer()-&gt;isFloatingOrOutOfFlowPositioned()) {
</span><span class="cx">         m_lastObject = m_current.renderer();
</span><del>-        if (m_lastObject-&gt;isReplaced() &amp;&amp; m_autoWrap &amp;&amp; !m_lastObject-&gt;isRubyRun() &amp;&amp; (!m_lastObject-&gt;isImage() || m_allowImagesToBreak) &amp;&amp; (!is&lt;RenderListMarker&gt;(*m_lastObject) || downcast&lt;RenderListMarker&gt;(*m_lastObject).isInside())) {
</del><ins>+        if (m_lastObject-&gt;isReplaced() &amp;&amp; m_autoWrap &amp;&amp; !m_lastObject-&gt;isRubyRun() &amp;&amp; (!m_lastObject-&gt;isImage() || m_allowImagesToBreak) &amp;&amp; (!is&lt;RenderListMarker&gt;(*m_lastObject) || downcast&lt;RenderListMarker&gt;(*m_lastObject).isInside()))
</ins><span class="cx">             commitLineBreakAtCurrentWidth(*m_nextObject);
</span><del>-        }
</del><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-inline TrailingObjects::CollapseFirstSpaceOrNot checkMidpoints(LineMidpointState&amp; lineMidpointState, InlineIterator&amp; lBreak)
</del><ins>+inline TrailingObjects::CollapseFirstSpaceOrNot checkMidpoints(LineMidpointState&amp; lineMidpointState, const InlineIterator&amp; lBreak)
</ins><span class="cx"> {
</span><span class="cx">     // Check to see if our last midpoint is a start point beyond the line break. If so,
</span><span class="cx">     // shave it off the list, and shave off a trailing space if the previous end point doesn't
</span><span class="lines">@@ -1040,12 +1114,12 @@
</span><span class="cx"> 
</span><span class="cx"> inline InlineIterator BreakingContext::handleEndOfLine()
</span><span class="cx"> {
</span><del>-    if (m_lineBreak == m_resolver.position()) {
-        if (!m_lineBreak.renderer() || !m_lineBreak.renderer()-&gt;isBR()) {
</del><ins>+    if (m_lineBreakHistory.current() == m_resolver.position()) {
+        if (!m_lineBreakHistory.renderer() || !m_lineBreakHistory.renderer()-&gt;isBR()) {
</ins><span class="cx">             // we just add as much as possible
</span><span class="cx">             if (m_blockStyle.whiteSpace() == PRE &amp;&amp; !m_current.offset())
</span><span class="cx">                 commitLineBreakAtCurrentWidth(*m_lastObject, m_lastObject-&gt;isText() ? m_lastObject-&gt;length() : 0);
</span><del>-            else if (m_lineBreak.renderer()) {
</del><ins>+            else if (m_lineBreakHistory.renderer()) {
</ins><span class="cx">                 // Don't ever break in the middle of a word if we can help it.
</span><span class="cx">                 // There's no room at all. We just have to be on this line,
</span><span class="cx">                 // even though we'll spill out.
</span><span class="lines">@@ -1053,33 +1127,76 @@
</span><span class="cx">             }
</span><span class="cx">         }
</span><span class="cx">         // make sure we consume at least one char/object.
</span><del>-        if (m_lineBreak == m_resolver.position())
-            m_lineBreak.increment();
</del><ins>+        if (m_lineBreakHistory.current() == m_resolver.position())
+            m_lineBreakHistory.increment();
</ins><span class="cx">     } else if (!m_current.offset() &amp;&amp; !m_width.committedWidth() &amp;&amp; m_width.uncommittedWidth() &amp;&amp; !m_hadUncommittedWidthBeforeCurrent) {
</span><span class="cx">         // Do not push the current object to the next line, when this line has some content, but it is still considered empty.
</span><span class="cx">         // Empty inline elements like &lt;span&gt;&lt;/span&gt; can produce such lines and now we just ignore these break opportunities
</span><span class="cx">         // at the start of a line, if no width has been committed yet.
</span><span class="cx">         // Behave as if it was actually empty and consume at least one object.
</span><del>-        m_lineBreak.increment();
</del><ins>+        m_lineBreakHistory.increment();
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     // Sanity check our midpoints.
</span><del>-    TrailingObjects::CollapseFirstSpaceOrNot collapsed = checkMidpoints(m_lineMidpointState, m_lineBreak);
</del><ins>+    TrailingObjects::CollapseFirstSpaceOrNot collapsed = checkMidpoints(m_lineMidpointState, m_lineBreakHistory.current());
</ins><span class="cx"> 
</span><del>-    m_trailingObjects.updateMidpointsForTrailingBoxes(m_lineMidpointState, m_lineBreak, collapsed);
</del><ins>+    m_trailingObjects.updateMidpointsForTrailingBoxes(m_lineMidpointState, m_lineBreakHistory.current(), collapsed);
</ins><span class="cx"> 
</span><span class="cx">     // We might have made lineBreak an iterator that points past the end
</span><span class="cx">     // of the object. Do this adjustment to make it point to the start
</span><span class="cx">     // of the next object instead to avoid confusing the rest of the
</span><span class="cx">     // code.
</span><del>-    if (m_lineBreak.offset()) {
-        m_lineBreak.setOffset(m_lineBreak.offset() - 1);
-        m_lineBreak.increment();
</del><ins>+    if (m_lineBreakHistory.offset()) {
+        m_lineBreakHistory.update([](InlineIterator&amp; modifyMe) {
+            modifyMe.setOffset(modifyMe.offset() - 1);
+            modifyMe.increment();
+        });
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    return m_lineBreak;
</del><ins>+#if ENABLE(CSS_TRAILING_WORD)
+    if (m_blockStyle.trailingWord() == TrailingWord::PartiallyBalanced)
+        return optimalLineBreakLocationForTrailingWord();
+#endif
+    return m_lineBreakHistory.current();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+#if ENABLE(CSS_TRAILING_WORD)
+inline InlineIterator BreakingContext::optimalLineBreakLocationForTrailingWord()
+{
+    const unsigned longTrailingWordLength = 20;
+    const float optimalTrailingLineRatio = 0.1;
+    InlineIterator lineBreak = m_lineBreakHistory.current();
+    if (!lineBreak.renderer() || !m_lineInfo.isFirstLine() || bidiNextSkippingEmptyInlines(*lineBreak.root(), lineBreak.renderer()) || !is&lt;RenderText&gt;(lineBreak.renderer()))
+        return lineBreak;
+    RenderText&amp; renderText = downcast&lt;RenderText&gt;(*lineBreak.renderer());
+    // Don't even bother measuring if our remaining line has many characters
+    if (renderText.textLength() == lineBreak.offset() || renderText.textLength() - lineBreak.offset() &gt; longTrailingWordLength)
+        return lineBreak;
+    bool isLooseCJKMode = m_renderTextInfo.m_text != &amp;renderText &amp;&amp; m_renderTextInfo.m_lineBreakIterator.isLooseCJKMode();
+    bool breakNBSP = m_autoWrap &amp;&amp; m_currentStyle-&gt;nbspMode() == SPACE;
+    int nextBreakablePosition = lineBreak.nextBreakablePosition();
+    isBreakable(m_renderTextInfo.m_lineBreakIterator, lineBreak.offset() + 1, nextBreakablePosition, breakNBSP, isLooseCJKMode);
+    if (nextBreakablePosition &lt; 0 || static_cast&lt;unsigned&gt;(nextBreakablePosition) != renderText.textLength())
+        return lineBreak;
+    const RenderStyle&amp; style = lineStyle(renderText, m_lineInfo);
+    const FontCascade&amp; font = style.fontCascade();
+    HashSet&lt;const Font*&gt; dummyFonts;
+    InlineIterator best = lineBreak;
+    for (size_t i = 1; i &lt; m_lineBreakHistory.historyLength(); ++i) {
+        const InlineIterator&amp; candidate = m_lineBreakHistory.get(i);
+        if (candidate.renderer() != lineBreak.renderer())
+            return best;
+        float width = textWidth(&amp;renderText, candidate.offset(), renderText.textLength() - candidate.offset(), font, 0, font.isFixedPitch(), m_collapseWhiteSpace, dummyFonts);
+        if (width &gt; m_width.availableWidth())
+            return best;
+        if (width / m_width.availableWidth() &gt; optimalTrailingLineRatio) // Subsequent line is long enough
+            return candidate;
+        best = candidate;
+    }
+    return best;
</ins><span class="cx"> }
</span><ins>+#endif
</ins><span class="cx"> 
</span><ins>+}
+
</ins><span class="cx"> #endif // BreakingContextInlineHeaders_h
</span></span></pre>
</div>
</div>

</body>
</html>