summaryrefslogtreecommitdiff
path: root/Source/WebCore/dom/DatasetDOMStringMap.cpp
diff options
context:
space:
mode:
authorKonstantin Tokarev <annulen@yandex.ru>2016-08-25 19:20:41 +0300
committerKonstantin Tokarev <annulen@yandex.ru>2017-02-02 12:30:55 +0000
commit6882a04fb36642862b11efe514251d32070c3d65 (patch)
treeb7959826000b061fd5ccc7512035c7478742f7b0 /Source/WebCore/dom/DatasetDOMStringMap.cpp
parentab6df191029eeeb0b0f16f127d553265659f739e (diff)
downloadqtwebkit-6882a04fb36642862b11efe514251d32070c3d65.tar.gz
Imported QtWebKit TP3 (git b57bc6801f1876c3220d5a4bfea33d620d477443)
Change-Id: I3b1d8a2808782c9f34d50240000e20cb38d3680f Reviewed-by: Konstantin Tokarev <annulen@yandex.ru>
Diffstat (limited to 'Source/WebCore/dom/DatasetDOMStringMap.cpp')
-rw-r--r--Source/WebCore/dom/DatasetDOMStringMap.cpp111
1 files changed, 60 insertions, 51 deletions
diff --git a/Source/WebCore/dom/DatasetDOMStringMap.cpp b/Source/WebCore/dom/DatasetDOMStringMap.cpp
index adf3b781e..df31d9006 100644
--- a/Source/WebCore/dom/DatasetDOMStringMap.cpp
+++ b/Source/WebCore/dom/DatasetDOMStringMap.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010 Apple Inc. All rights reserved.
+ * Copyright (C) 2010, 2014 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -26,11 +26,12 @@
#include "config.h"
#include "DatasetDOMStringMap.h"
-#include "Attribute.h"
#include "Element.h"
#include "ExceptionCode.h"
#include <wtf/ASCIICType.h>
+#include <wtf/text/AtomicString.h>
#include <wtf/text/StringBuilder.h>
+#include <wtf/text/WTFString.h>
namespace WebCore {
@@ -106,75 +107,88 @@ static bool isValidPropertyName(const String& name)
return true;
}
-static String convertPropertyNameToAttributeName(const String& name)
+template<typename CharacterType>
+static inline AtomicString convertPropertyNameToAttributeName(const StringImpl& name)
{
- StringBuilder builder;
- builder.append("data-");
+ const CharacterType dataPrefix[] = { 'd', 'a', 't', 'a', '-' };
+
+ Vector<CharacterType, 32> buffer;
unsigned length = name.length();
+ buffer.reserveInitialCapacity(WTF_ARRAY_LENGTH(dataPrefix) + length);
+
+ buffer.append(dataPrefix, WTF_ARRAY_LENGTH(dataPrefix));
+
+ const CharacterType* characters = name.characters<CharacterType>();
for (unsigned i = 0; i < length; ++i) {
- UChar character = name[i];
+ CharacterType character = characters[i];
if (isASCIIUpper(character)) {
- builder.append('-');
- builder.append(toASCIILower(character));
+ buffer.append('-');
+ buffer.append(toASCIILower(character));
} else
- builder.append(character);
+ buffer.append(character);
}
+ return AtomicString(buffer.data(), buffer.size());
+}
- return builder.toString();
+static AtomicString convertPropertyNameToAttributeName(const String& name)
+{
+ if (name.isNull())
+ return nullAtom;
+
+ StringImpl* nameImpl = name.impl();
+ if (nameImpl->is8Bit())
+ return convertPropertyNameToAttributeName<LChar>(*nameImpl);
+ return convertPropertyNameToAttributeName<UChar>(*nameImpl);
}
void DatasetDOMStringMap::ref()
{
- m_element->ref();
+ m_element.ref();
}
void DatasetDOMStringMap::deref()
{
- m_element->deref();
+ m_element.deref();
}
void DatasetDOMStringMap::getNames(Vector<String>& names)
{
- if (!m_element->hasAttributes())
+ if (!m_element.hasAttributes())
return;
- unsigned length = m_element->attributeCount();
- for (unsigned i = 0; i < length; i++) {
- const Attribute* attribute = m_element->attributeItem(i);
- if (isValidAttributeName(attribute->localName()))
- names.append(convertAttributeNameToPropertyName(attribute->localName()));
- }
-}
-
-String DatasetDOMStringMap::item(const String& name)
-{
- if (!m_element->hasAttributes())
- return String();
-
- unsigned length = m_element->attributeCount();
- for (unsigned i = 0; i < length; i++) {
- const Attribute* attribute = m_element->attributeItem(i);
- if (propertyNameMatchesAttributeName(name, attribute->localName()))
- return attribute->value();
+ for (const Attribute& attribute : m_element.attributesIterator()) {
+ if (isValidAttributeName(attribute.localName()))
+ names.append(convertAttributeNameToPropertyName(attribute.localName()));
}
-
- return String();
}
-bool DatasetDOMStringMap::contains(const String& name)
+const AtomicString& DatasetDOMStringMap::item(const String& propertyName, bool& isValid)
{
- if (!m_element->hasAttributes())
- return false;
-
- unsigned length = m_element->attributeCount();
- for (unsigned i = 0; i < length; i++) {
- const Attribute* attribute = m_element->attributeItem(i);
- if (propertyNameMatchesAttributeName(name, attribute->localName()))
- return true;
+ isValid = false;
+ if (m_element.hasAttributes()) {
+ AttributeIteratorAccessor attributeIteratorAccessor = m_element.attributesIterator();
+
+ if (attributeIteratorAccessor.attributeCount() == 1) {
+ // If the node has a single attribute, it is the dataset member accessed in most cases.
+ // Building a new AtomicString in that case is overkill so we do a direct character comparison.
+ const Attribute& attribute = *attributeIteratorAccessor.begin();
+ if (propertyNameMatchesAttributeName(propertyName, attribute.localName())) {
+ isValid = true;
+ return attribute.value();
+ }
+ } else {
+ AtomicString attributeName = convertPropertyNameToAttributeName(propertyName);
+ for (const Attribute& attribute : attributeIteratorAccessor) {
+ if (attribute.localName() == attributeName) {
+ isValid = true;
+ return attribute.value();
+ }
+ }
+ }
}
- return false;
+ return nullAtom;
}
void DatasetDOMStringMap::setItem(const String& name, const String& value, ExceptionCode& ec)
@@ -184,17 +198,12 @@ void DatasetDOMStringMap::setItem(const String& name, const String& value, Excep
return;
}
- m_element->setAttribute(convertPropertyNameToAttributeName(name), value, ec);
+ m_element.setAttribute(convertPropertyNameToAttributeName(name), value, ec);
}
-void DatasetDOMStringMap::deleteItem(const String& name, ExceptionCode& ec)
+bool DatasetDOMStringMap::deleteItem(const String& name)
{
- if (!isValidPropertyName(name)) {
- ec = SYNTAX_ERR;
- return;
- }
-
- m_element->removeAttribute(convertPropertyNameToAttributeName(name));
+ return m_element.removeAttribute(convertPropertyNameToAttributeName(name));
}
} // namespace WebCore