Commit 7a33897e authored by kouhei@chromium.org's avatar kouhei@chromium.org

SegmentedString::push() should always push a char in front

Before this CL, SegmentedString::push() had an exotic behavior, where
two consecutive push would swap its order.

This CL changes the push() implementation so that it would always push
the new char in front of the SegmentedString.
This CL also updates the HTMLEntityParser code which relied on the behavior.

BUG=None
TEST={SegmentedStringTest,HTMLEntityParserTest}.*

Review URL: https://codereview.chromium.org/1308573006

git-svn-id: svn://svn.chromium.org/blink/trunk@201294 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent c23859a6
......@@ -3814,6 +3814,7 @@
'html/TimeRangesTest.cpp',
'html/canvas/CanvasFontCacheTest.cpp',
'html/forms/FileInputTypeTest.cpp',
'html/parser/HTMLEntityParserTest.cpp',
'html/parser/HTMLParserThreadTest.cpp',
'html/parser/HTMLPreloadScannerTest.cpp',
'html/parser/HTMLResourcePreloaderTest.cpp',
......
......@@ -95,8 +95,8 @@ static void unconsumeCharacters(SegmentedString& source, ConsumedCharacterBuffer
if (consumedCharacters.size() == 1)
source.push(consumedCharacters[0]);
else if (consumedCharacters.size() == 2) {
source.push(consumedCharacters[0]);
source.push(consumedCharacters[1]);
source.push(consumedCharacters[0]);
} else
source.prepend(SegmentedString(String(consumedCharacters)));
}
......@@ -212,8 +212,8 @@ bool consumeHTMLEntity(SegmentedString& source, DecodedHTMLEntity& decodedEntity
entityState = Hex;
continue;
}
source.push('#');
source.push('x');
source.push('#');
return false;
}
case MaybeHexUpperCaseX: {
......@@ -221,8 +221,8 @@ bool consumeHTMLEntity(SegmentedString& source, DecodedHTMLEntity& decodedEntity
entityState = Hex;
continue;
}
source.push('#');
source.push('X');
source.push('#');
return false;
}
case Hex: {
......
......@@ -27,6 +27,7 @@
#ifndef HTMLEntityParser_h
#define HTMLEntityParser_h
#include "core/CoreExport.h"
#include "platform/text/SegmentedString.h"
#include "wtf/Allocator.h"
......@@ -63,11 +64,11 @@ public:
UChar data[kMaxLength];
};
bool consumeHTMLEntity(SegmentedString&, DecodedHTMLEntity& decodedEntity, bool& notEnoughCharacters, UChar additionalAllowedCharacter = '\0');
CORE_EXPORT bool consumeHTMLEntity(SegmentedString&, DecodedHTMLEntity& decodedEntity, bool& notEnoughCharacters, UChar additionalAllowedCharacter = '\0');
// Used by the XML parser. Not suitable for use in HTML parsing. Use consumeHTMLEntity instead.
size_t decodeNamedEntityToUCharArray(const char*, UChar result[4]);
}
} // namespace blink
#endif
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "config.h"
#include "core/html/parser/HTMLEntityParser.h"
#include <gtest/gtest.h>
namespace blink {
TEST(HTMLEntityParserTest, ConsumeHTMLEntityIncomplete)
{
String original("am"); // Incomplete by purpose.
SegmentedString src(original);
DecodedHTMLEntity entity;
bool notEnoughCharacters;
bool success = consumeHTMLEntity(src, entity, notEnoughCharacters);
EXPECT_TRUE(notEnoughCharacters);
EXPECT_FALSE(success);
// consumeHTMLEntity should recover the original SegmentedString state if failed.
EXPECT_STREQ(original.ascii().data(), src.toString().ascii().data());
}
} // namespace blink
......@@ -190,13 +190,15 @@ public:
void push(UChar c)
{
ASSERT(c);
if (!m_pushedChar1) {
m_pushedChar1 = c;
m_currentChar = m_pushedChar1 ? m_pushedChar1 : m_currentString.getCurrentChar();
m_currentChar = m_pushedChar1 = c;
updateSlowCaseFunctionPointers();
} else {
ASSERT(!m_pushedChar2);
m_pushedChar2 = c;
m_pushedChar2 = m_pushedChar1;
m_currentChar = m_pushedChar1 = c;
}
}
......
......@@ -47,20 +47,18 @@ TEST(SegmentedStringTest, CurrentChar)
EXPECT_EQ('c', copied.currentChar());
EXPECT_EQ('c', assigned.currentChar());
}
original.push('a');
original.push('b');
{
SegmentedString copied(original);
SegmentedString assigned;
assigned = original;
EXPECT_EQ("acde", original.toString());
EXPECT_EQ('a', original.currentChar());
EXPECT_EQ('a', copied.currentChar());
EXPECT_EQ('a', assigned.currentChar());
EXPECT_EQ("bcde", original.toString());
EXPECT_EQ('b', original.currentChar());
EXPECT_EQ('b', copied.currentChar());
EXPECT_EQ('b', assigned.currentChar());
}
original.push('b');
original.push('a');
{
// Two consecutive push means to push the second char *after* the
// first char in SegmentedString.
SegmentedString copied(original);
SegmentedString assigned;
assigned = original;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment