diff --git a/parser/html/nsHtml5Highlighter.cpp b/parser/html/nsHtml5Highlighter.cpp index 892918920f3..c7c509ab94a 100644 --- a/parser/html/nsHtml5Highlighter.cpp +++ b/parser/html/nsHtml5Highlighter.cpp @@ -45,6 +45,10 @@ using namespace mozilla; +// The old code had a limit of 16 tokens. 1300 is a number picked my measuring +// the size of 16 tokens on cnn.com. +#define NS_HTML5_HIGHLIGHTER_PRE_BREAK_THRESHOLD 1300 + PRUnichar nsHtml5Highlighter::sComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 }; @@ -78,6 +82,7 @@ nsHtml5Highlighter::nsHtml5Highlighter(nsAHtml5TreeOpSink* aOpSink) , mCStart(PR_INT32_MAX) , mPos(0) , mLineNumber(1) + , mUnicharsInThisPre(0) , mInlinesOpen(0) , mInCharacters(false) , mBuffer(nsnull) @@ -619,10 +624,25 @@ nsHtml5Highlighter::FlushChars() case '\n': { ++i; if (mCStart < i) { - AppendCharacters(buf, mCStart, i - mCStart); + PRInt32 len = i - mCStart; + AppendCharacters(buf, mCStart, len); mCStart = i; + mUnicharsInThisPre += len; } ++mLineNumber; + if (mUnicharsInThisPre > NS_HTML5_HIGHLIGHTER_PRE_BREAK_THRESHOLD && + !mInlinesOpen && mInCharacters) { + mUnicharsInThisPre = 0; + // Split the pre. See bug 86355. + Pop(); // span + Pop(); // pre + Push(nsGkAtoms::pre, nsnull); + nsHtml5TreeOperation* treeOp = mOpQueue.AppendElement(); + NS_ASSERTION(treeOp, "Tree op allocation failed."); + treeOp->InitAddLineNumberId(CurrentNode(), mLineNumber); + Push(nsGkAtoms::span, nsnull); + break; + } Push(nsGkAtoms::span, nsnull); nsHtml5TreeOperation* treeOp = mOpQueue.AppendElement(); NS_ASSERTION(treeOp, "Tree op allocation failed."); @@ -636,8 +656,10 @@ nsHtml5Highlighter::FlushChars() } } if (mCStart < mPos) { - AppendCharacters(buf, mCStart, mPos - mCStart); + PRInt32 len = mPos - mCStart; + AppendCharacters(buf, mCStart, len); mCStart = mPos; + mUnicharsInThisPre += len; } } } diff --git a/parser/html/nsHtml5Highlighter.h b/parser/html/nsHtml5Highlighter.h index 8007664528d..3def948c9d5 100644 --- a/parser/html/nsHtml5Highlighter.h +++ b/parser/html/nsHtml5Highlighter.h @@ -316,6 +316,12 @@ class nsHtml5Highlighter */ PRInt32 mLineNumber; + /** + * The number of PRUnichars flushed since the start of the current pre + * block. + */ + PRInt32 mUnicharsInThisPre; + /** * The number of inline elements open inside the
excluding the * span potentially wrapping a run of characters.