Skip to content
Permalink
Browse files
Return boolean from DOMTokenList's replace() method
https://bugs.webkit.org/show_bug.cgi?id=183567

Reviewed by Youenn Fablet.

LayoutTests/imported/w3c:

Rebaseline web platform test now that all checks are passing.

* web-platform-tests/dom/nodes/Element-classlist-expected.txt:

Source/WebCore:

Have DOMTokenList's replace() method return a boolean indicating if the
token was replaced, as per:
- https://dom.spec.whatwg.org/#dom-domtokenlist-replace

This is a recent addition to the DOM specification:
- whatwg/dom#582

No new tests, rebaselined existing test.

* html/DOMTokenList.cpp:
(WebCore::replaceInOrderedSet):
(WebCore::DOMTokenList::replace):
* html/DOMTokenList.h:
* html/DOMTokenList.idl:


Canonical link: https://commits.webkit.org/199253@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@229566 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
cdumez committed Mar 13, 2018
1 parent 22b77c1 commit d807e9e01ad7ccdab789ef6ba3e959f4d60bc797
Showing 6 changed files with 190 additions and 142 deletions.
@@ -1,3 +1,14 @@
2018-03-12 Chris Dumez <cdumez@apple.com>

Return boolean from DOMTokenList's replace() method
https://bugs.webkit.org/show_bug.cgi?id=183567

Reviewed by Youenn Fablet.

Rebaseline web platform test now that all checks are passing.

* web-platform-tests/dom/nodes/Element-classlist-expected.txt:

2018-03-12 Ryan Haddad <ryanhaddad@apple.com>

Unreviewed, rebaseline two cssom tests after r229544.

Large diffs are not rendered by default.

@@ -1,3 +1,25 @@
2018-03-12 Chris Dumez <cdumez@apple.com>

Return boolean from DOMTokenList's replace() method
https://bugs.webkit.org/show_bug.cgi?id=183567

Reviewed by Youenn Fablet.

Have DOMTokenList's replace() method return a boolean indicating if the
token was replaced, as per:
- https://dom.spec.whatwg.org/#dom-domtokenlist-replace

This is a recent addition to the DOM specification:
- https://github.com/whatwg/dom/pull/582

No new tests, rebaselined existing test.

* html/DOMTokenList.cpp:
(WebCore::replaceInOrderedSet):
(WebCore::DOMTokenList::replace):
* html/DOMTokenList.h:
* html/DOMTokenList.idl:

2018-03-12 Brian Burg <bburg@apple.com>

Ignore some deprecation warnings encountered when compiling with newer versions of ICU
@@ -157,33 +157,48 @@ ExceptionOr<bool> DOMTokenList::toggle(const AtomicString& token, std::optional<
return true;
}

static inline void replaceInOrderedSet(Vector<AtomicString>& tokens, size_t tokenIndex, const AtomicString& newToken)
{
ASSERT(tokenIndex != notFound);
ASSERT(tokenIndex < tokens.size());

auto newTokenIndex = tokens.find(newToken);
if (newTokenIndex == notFound) {
tokens[tokenIndex] = newToken;
return;
}

if (newTokenIndex == tokenIndex)
return;

if (newTokenIndex > tokenIndex) {
tokens[tokenIndex] = newToken;
tokens.remove(newTokenIndex);
} else
tokens.remove(tokenIndex);
}

// https://dom.spec.whatwg.org/#dom-domtokenlist-replace
ExceptionOr<void> DOMTokenList::replace(const AtomicString& item, const AtomicString& replacement)
ExceptionOr<bool> DOMTokenList::replace(const AtomicString& token, const AtomicString& newToken)
{
if (item.isEmpty() || replacement.isEmpty())
if (token.isEmpty() || newToken.isEmpty())
return Exception { SyntaxError };

if (tokenContainsHTMLSpace(item) || tokenContainsHTMLSpace(replacement))
if (tokenContainsHTMLSpace(token) || tokenContainsHTMLSpace(newToken))
return Exception { InvalidCharacterError };

auto& tokens = this->tokens();

auto matchesItemOrReplacement = [&](auto& token) {
return token == item || token == replacement;
};

size_t index = tokens.findMatching(matchesItemOrReplacement);
if (index == notFound)
return { };
auto tokenIndex = tokens.find(token);
if (tokenIndex == notFound)
return false;

tokens[index] = replacement;
tokens.removeFirstMatching(matchesItemOrReplacement, index + 1);
ASSERT(item == replacement || tokens.find(item) == notFound);
ASSERT(tokens.reverseFind(replacement) == index);
replaceInOrderedSet(tokens, tokenIndex, newToken);
ASSERT(token == newToken || tokens.find(token) == notFound);

updateAssociatedAttributeFromTokens();

return { };
return true;
}

// https://dom.spec.whatwg.org/#concept-domtokenlist-validation
@@ -49,7 +49,7 @@ class DOMTokenList {
ExceptionOr<void> remove(const Vector<String>&);
ExceptionOr<void> remove(const AtomicString&);
WEBCORE_EXPORT ExceptionOr<bool> toggle(const AtomicString&, std::optional<bool> force);
ExceptionOr<void> replace(const AtomicString& token, const AtomicString& newToken);
ExceptionOr<bool> replace(const AtomicString& token, const AtomicString& newToken);
ExceptionOr<bool> supports(StringView token);

Element& element() const { return m_element; }
@@ -33,7 +33,7 @@
[CEReactions, MayThrowException] void add(DOMString... tokens);
[CEReactions, MayThrowException] void remove(DOMString... tokens);
[CEReactions, MayThrowException] boolean toggle(DOMString token, optional boolean force);
[CEReactions, MayThrowException] void replace(DOMString token, DOMString newToken);
[CEReactions, MayThrowException] boolean replace(DOMString token, DOMString newToken);
[MayThrowException] boolean supports(DOMString token);
[CEReactions] stringifier attribute DOMString value;
iterable<DOMString>;

0 comments on commit d807e9e

Please sign in to comment.