Commit a38d0db1 authored by Fergal Daly's avatar Fergal Daly Committed by Commit Bot

Move DOMTokenList:SerializeToString

Make it a public method on SpaceSplitString so that it can be used in
unit tests etc.

Also add some unit tests.

Change-Id: I2169259c29bb6e5a5a62c8dc6245ba8af31bc053
Reviewed-on: https://chromium-review.googlesource.com/991693Reviewed-by: default avatarKent Tamura <tkent@chromium.org>
Reviewed-by: default avatarTakayoshi Kochi <kochi@chromium.org>
Commit-Queue: Fergal Daly <fergal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#547656}
parent b13129e4
......@@ -29,7 +29,6 @@
#include "core/dom/ExceptionCode.h"
#include "core/html/parser/HTMLParserIdioms.h"
#include "platform/wtf/AutoReset.h"
#include "platform/wtf/text/StringBuilder.h"
namespace blink {
......@@ -250,30 +249,10 @@ void DOMTokenList::RemoveTokens(const Vector<String>& tokens) {
UpdateWithTokenSet(token_set_);
}
// https://dom.spec.whatwg.org/#concept-ordered-set-serializer
// The ordered set serializer takes a set and returns the concatenation of the
// strings in set, separated from each other by U+0020, if set is non-empty, and
// the empty string otherwise.
AtomicString DOMTokenList::SerializeTokenSet(
const SpaceSplitString& token_set) {
size_t size = token_set.size();
if (size == 0)
return g_empty_atom;
if (size == 1)
return token_set[0];
StringBuilder builder;
builder.Append(token_set[0]);
for (size_t i = 1; i < size; ++i) {
builder.Append(' ');
builder.Append(token_set[i]);
}
return builder.ToAtomicString();
}
// https://dom.spec.whatwg.org/#concept-dtl-update
void DOMTokenList::UpdateWithTokenSet(const SpaceSplitString& token_set) {
AutoReset<bool> updating(&is_in_update_step_, true);
setValue(SerializeTokenSet(token_set));
setValue(token_set.SerializeToString());
}
void DOMTokenList::setValue(const AtomicString& value) {
......
......@@ -85,7 +85,6 @@ class CORE_EXPORT DOMTokenList : public ScriptWrappable {
void AddTokens(const Vector<String>&);
void RemoveTokens(const Vector<String>&);
void UpdateWithTokenSet(const SpaceSplitString&);
static AtomicString SerializeTokenSet(const SpaceSplitString&);
SpaceSplitString token_set_;
AtomicString value_;
......
......@@ -23,6 +23,7 @@
#include "core/html/parser/HTMLParserIdioms.h"
#include "platform/wtf/HashSet.h"
#include "platform/wtf/text/AtomicStringHash.h"
#include "platform/wtf/text/StringBuilder.h"
#include "platform/wtf/text/StringHash.h"
namespace blink {
......@@ -146,11 +147,26 @@ void SpaceSplitString::Remove(size_t index) {
}
void SpaceSplitString::ReplaceAt(size_t index, const AtomicString& token) {
DCHECK_LT(index, size());
DCHECK_LT(index, data_->size());
EnsureUnique();
(*data_)[index] = token;
}
AtomicString SpaceSplitString::SerializeToString() const {
size_t size = data_->size();
if (size == 0)
return g_empty_atom;
if (size == 1)
return (*data_)[0];
StringBuilder builder;
builder.Append((*data_)[0]);
for (size_t i = 1; i < size; ++i) {
builder.Append(' ');
builder.Append((*data_)[i]);
}
return builder.ToAtomicString();
}
SpaceSplitString::DataMap& SpaceSplitString::SharedDataMap() {
DEFINE_STATIC_LOCAL(DataMap, map, ());
return map;
......
......@@ -54,6 +54,12 @@ class CORE_EXPORT SpaceSplitString {
void Remove(size_t index);
void ReplaceAt(size_t index, const AtomicString&);
// https://dom.spec.whatwg.org/#concept-ordered-set-serializer
// The ordered set serializer takes a set and returns the concatenation of the
// strings in set, separated from each other by U+0020, if set is non-empty,
// and the empty string otherwise.
AtomicString SerializeToString() const;
size_t size() const { return data_ ? data_->size() : 0; }
bool IsNull() const { return !data_; }
const AtomicString& operator[](size_t i) const { return (*data_)[i]; }
......
......@@ -32,4 +32,22 @@ TEST(SpaceSplitStringTest, Set) {
EXPECT_EQ(AtomicString("foo"), tokens[0]);
EXPECT_EQ(AtomicString("bar"), tokens[1]);
}
TEST(SpaceSplitStringTest, SerializeToString) {
SpaceSplitString tokens;
tokens.Set("foo");
EXPECT_EQ("foo", tokens.SerializeToString());
tokens.Set("foo bar");
EXPECT_EQ("foo bar", tokens.SerializeToString());
tokens.Set("foo");
tokens.Add("bar");
EXPECT_EQ("foo bar", tokens.SerializeToString());
tokens.Set("bar");
tokens.Add("foo");
EXPECT_EQ("bar foo", tokens.SerializeToString());
}
}
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