Changeset 207497 in webkit


Ignore:
Timestamp:
Oct 18, 2016 4:18:20 PM (8 years ago)
Author:
Chris Dumez
Message:

Leverage new union type support for HTMLSelectElement.add() / HTMLOptionsCollection.add()
https://bugs.webkit.org/show_bug.cgi?id=163608

Reviewed by Ryosuke Niwa.

LayoutTests/imported/w3c:

Rebaseline W3C test now that one more check is passing. This is because we are now
correctly throwing a TypeError when calling add() with an unexpected HTMLElement type.

  • web-platform-tests/html/infrastructure/common-dom-interfaces/collections/htmloptionscollection-expected.txt:

Source/WebCore:

Leverage new union type support for HTMLSelectElement.add() / HTMLOptionsCollection.add():

No new tests, rebaseline existing test.

  • bindings/scripts/CodeGeneratorJS.pm:

(GenerateDefaultValue):
(GenerateParametersCheck):

  • bindings/scripts/test/JS/JSTestObj.cpp:

(WebCore::jsTestObjPrototypeFunctionMethodWithOptionalSequenceIsEmptyCaller):

  • bindings/scripts/test/JS/JSTestOverloadedConstructorsWithSequence.cpp:

(WebCore::constructJSTestOverloadedConstructorsWithSequence1):

  • bindings/scripts/test/JS/JSTestTypedefs.cpp:

(WebCore::jsTestTypedefsPrototypeFunctionFuncCaller):

  • html/HTMLOptGroupElement.idl:
  • html/HTMLOptionsCollection.cpp:

(WebCore::HTMLOptionsCollection::add):

  • html/HTMLOptionsCollection.h:
  • html/HTMLOptionsCollection.idl:
  • html/HTMLSelectElement.cpp:

(WebCore::HTMLSelectElement::add):
(WebCore::HTMLSelectElement::setOption):
(WebCore::HTMLSelectElement::setLength):

  • html/HTMLSelectElement.h:
  • html/HTMLSelectElement.idl:

Source/WebKit/mac:

Update add() call sites now that it takes std::variant types in.

  • DOM/DOMHTMLOptionsCollection.mm:

(-[DOMHTMLOptionsCollection add:index:]):

  • DOM/DOMHTMLSelectElement.mm:

(-[DOMHTMLSelectElement add:before:]):

LayoutTests:

Rebaseline existing tests now that exception messages are different.

  • fast/dom/HTMLSelectElement/add-expected.txt:
  • fast/dom/HTMLSelectElement/options-collection-add-expected.txt:
  • fast/dom/incompatible-operations-expected.txt:
Location:
trunk
Files:
20 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r207486 r207497  
     12016-10-18  Chris Dumez  <cdumez@apple.com>
     2
     3        Leverage new union type support for HTMLSelectElement.add() / HTMLOptionsCollection.add()
     4        https://bugs.webkit.org/show_bug.cgi?id=163608
     5
     6        Reviewed by Ryosuke Niwa.
     7
     8        Rebaseline existing tests now that exception messages are different.
     9
     10        * fast/dom/HTMLSelectElement/add-expected.txt:
     11        * fast/dom/HTMLSelectElement/options-collection-add-expected.txt:
     12        * fast/dom/incompatible-operations-expected.txt:
     13
    1142016-10-18  Ryosuke Niwa  <rniwa@webkit.org>
    215
  • trunk/LayoutTests/fast/dom/HTMLSelectElement/add-expected.txt

    r204028 r207497  
    1010Call HTMLSelectElement.add() with one argument:
    1111PASS testAdd(createOption("Y1")) is "0,1,2,Y1"
    12 PASS testAdd("foo") threw exception TypeError: Argument 1 ('element') to HTMLSelectElement.add must be an instance of HTMLElement.
    13 PASS testAdd(undefined) threw exception TypeError: Argument 1 ('element') to HTMLSelectElement.add must be an instance of HTMLElement.
    14 PASS testAdd(null) threw exception TypeError: Argument 1 ('element') to HTMLSelectElement.add must be an instance of HTMLElement.
     12PASS testAdd("foo") threw exception TypeError: Type error.
     13PASS testAdd(undefined) threw exception TypeError: Type error.
     14PASS testAdd(null) threw exception TypeError: Type error.
    1515
    1616Call HTMLSelectElement.add() with two arguments:
     
    4141PASS testAdd(createOption("X"), mySelect.options[1]) is "0,X,1,2"
    4242PASS testAdd(createOption("X"), mySelect.options[2]) is "0,1,X,2"
    43 PASS testAdd("foo", 0) threw exception TypeError: Argument 1 ('element') to HTMLSelectElement.add must be an instance of HTMLElement.
    44 PASS testAdd(undefined, 0) threw exception TypeError: Argument 1 ('element') to HTMLSelectElement.add must be an instance of HTMLElement.
    45 PASS testAdd(null, 0) threw exception TypeError: Argument 1 ('element') to HTMLSelectElement.add must be an instance of HTMLElement.
     43PASS testAdd("foo", 0) threw exception TypeError: Type error.
     44PASS testAdd(undefined, 0) threw exception TypeError: Type error.
     45PASS testAdd(null, 0) threw exception TypeError: Type error.
    4646
    4747Call HTMLSelectElement.add() with three arguments (when it only takes two arguments):
  • trunk/LayoutTests/fast/dom/HTMLSelectElement/options-collection-add-expected.txt

    r204028 r207497  
    1010Call HTMLOptionsCollection.add() with only one argument:
    1111PASS testAdd(createOption("Y9")) is "0,1,2,Y9"
    12 PASS testAdd("foo") threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
    13 PASS testAdd(undefined) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
    14 PASS testAdd(null) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     12PASS testAdd("foo") threw exception TypeError: Type error.
     13PASS testAdd(undefined) threw exception TypeError: Type error.
     14PASS testAdd(null) threw exception TypeError: Type error.
    1515
    1616Call HTMLOptionsCollection.add() with two arguments:
     
    5353PASS testAdd(createOption("Y25"), 0, 1) is "Y25,0,1,2"
    5454PASS testAdd(createOption("Y25"), mySelect.options[0], 1) is "Y25,0,1,2"
    55 PASS testAdd("foo", 0) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
    56 PASS testAdd(undefined, 0) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
    57 PASS testAdd(null, 0) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     55PASS testAdd("foo", 0) threw exception TypeError: Type error.
     56PASS testAdd(undefined, 0) threw exception TypeError: Type error.
     57PASS testAdd(null, 0) threw exception TypeError: Type error.
    5858
    5959Call HTMLOptionsCollection.add() with three arguments (when it only takes two arguments):
  • trunk/LayoutTests/fast/dom/incompatible-operations-expected.txt

    r204028 r207497  
    1313PASS aNode.lookupPrefix(void 0) is null
    1414PASS aNode.cloneNode(aDOMImplementation) instanceof HTMLDivElement is true
    15 PASS aSelect.add(aDOMImplementation, aDOMImplementation) threw exception TypeError: Argument 1 ('element') to HTMLSelectElement.add must be an instance of HTMLElement.
    16 PASS aSelect.add(aDOMImplementation, anOption) threw exception TypeError: Argument 1 ('element') to HTMLSelectElement.add must be an instance of HTMLElement.
     15PASS aSelect.add(aDOMImplementation, aDOMImplementation) threw exception TypeError: Type error.
     16PASS aSelect.add(aDOMImplementation, anOption) threw exception TypeError: Type error.
    1717PASS aSelect.add(anOption, aDOMImplementation) is undefined.
    18 PASS aSelect.add(void 0, void 0) threw exception TypeError: Argument 1 ('element') to HTMLSelectElement.add must be an instance of HTMLElement.
    19 PASS aSelect.add(void 0, anOption) threw exception TypeError: Argument 1 ('element') to HTMLSelectElement.add must be an instance of HTMLElement.
     18PASS aSelect.add(void 0, void 0) threw exception TypeError: Type error.
     19PASS aSelect.add(void 0, anOption) threw exception TypeError: Type error.
    2020PASS aSelect.add(anOption, void 0) is undefined.
    2121PASS successfullyParsed is true
  • trunk/LayoutTests/imported/w3c/ChangeLog

    r207468 r207497  
     12016-10-18  Chris Dumez  <cdumez@apple.com>
     2
     3        Leverage new union type support for HTMLSelectElement.add() / HTMLOptionsCollection.add()
     4        https://bugs.webkit.org/show_bug.cgi?id=163608
     5
     6        Reviewed by Ryosuke Niwa.
     7
     8        Rebaseline W3C test now that one more check is passing. This is because we are now
     9        correctly throwing a TypeError when calling add() with an unexpected HTMLElement type.
     10
     11        * web-platform-tests/html/infrastructure/common-dom-interfaces/collections/htmloptionscollection-expected.txt:
     12
    1132016-10-18  Chris Dumez  <cdumez@apple.com>
    214
  • trunk/LayoutTests/imported/w3c/web-platform-tests/html/infrastructure/common-dom-interfaces/collections/htmloptionscollection-expected.txt

    r207181 r207497  
    2424PASS HTMLOptionsCollection.add method insert HTMLOptionElement Option element
    2525PASS HTMLOptionsCollection.remove method remove Option element by index
    26 FAIL Add non-option to collection assert_throws: function "function () {b_opts.add(add);}" did not throw
     26PASS Add non-option to collection
    2727
  • trunk/LayoutTests/js/dom/script-tests/select-options-add.js

    r178097 r207497  
    5454
    5555debug("1.4 Add a non-Option element");
    56 option1 = document.createElement("DIV");
    57 shouldBeUndefined("select1.options.add(option1)");
     56div = document.createElement("DIV");
     57shouldThrowErrorName("select1.options.add(div)", "TypeError");
    5858shouldBe("select1.options.length", "3");
    5959shouldBe("select1.selectedIndex", "0");
     
    265265
    266266debug("2.12 Add a non-Option element");
    267 option2 = document.createElement("DIV");
    268 shouldBeUndefined("select2.options.add(option2, 1)");
     267div = document.createElement("DIV");
     268shouldThrowErrorName("select2.options.add(div, 1)", "TypeError");
    269269shouldBe("select2.options.length", "11");
    270270shouldBe("select2.selectedIndex", "4");
  • trunk/LayoutTests/js/dom/select-options-add-expected.txt

    r204028 r207497  
    3030
    31311.4 Add a non-Option element
    32 PASS select1.options.add(option1) is undefined.
     32PASS select1.options.add(div) threw exception TypeError: Type error.
    3333PASS select1.options.length is 3
    3434PASS select1.selectedIndex is 0
    3535
    36361.5 Add a non-element (string)
    37 PASS select1.options.add(option1) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     37PASS select1.options.add(option1) threw exception TypeError: Type error.
    3838PASS select1.options.length is 3
    3939PASS select1.selectedIndex is 0
    4040
    41411.6 Add a non-element (number)
    42 PASS select1.options.add(option1) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     42PASS select1.options.add(option1) threw exception TypeError: Type error.
    4343PASS select1.options.length is 3
    4444PASS select1.selectedIndex is 0
    4545
    46461.7 Add a non-element (boolean)
    47 PASS select1.options.add(option1) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     47PASS select1.options.add(option1) threw exception TypeError: Type error.
    4848PASS select1.options.length is 3
    4949PASS select1.selectedIndex is 0
    5050
    51511.8 Add undefined
    52 PASS select1.options.add(option1) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     52PASS select1.options.add(option1) threw exception TypeError: Type error.
    5353PASS select1.options.length is 3
    5454PASS select1.selectedIndex is 0
    5555
    56561.9 Add null
    57 PASS select1.options.add(option1) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     57PASS select1.options.add(option1) threw exception TypeError: Type error.
    5858PASS select1.options.length is 3
    5959PASS select1.selectedIndex is 0
    6060
    61611.10 Add negative infinity
    62 PASS select1.options.add(option1) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     62PASS select1.options.add(option1) threw exception TypeError: Type error.
    6363PASS select1.options.length is 3
    6464PASS select1.selectedIndex is 0
    6565
    66661.11 Add NaN
    67 PASS select1.options.add(option1) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     67PASS select1.options.add(option1) threw exception TypeError: Type error.
    6868PASS select1.options.length is 3
    6969PASS select1.selectedIndex is 0
    7070
    71711.12 Add positive infinity
    72 PASS select1.options.add(option1) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     72PASS select1.options.add(option1) threw exception TypeError: Type error.
    7373PASS select1.options.length is 3
    7474PASS select1.selectedIndex is 0
     
    179179
    1801802.12 Add a non-Option element
    181 PASS select2.options.add(option2, 1) is undefined.
     181PASS select2.options.add(div, 1) threw exception TypeError: Type error.
    182182PASS select2.options.length is 11
    183183PASS select2.selectedIndex is 4
    184184
    1851852.13 Add a non-element (string)
    186 PASS select2.options.add(option2, 1) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     186PASS select2.options.add(option2, 1) threw exception TypeError: Type error.
    187187PASS select2.options.length is 11
    188188PASS select2.selectedIndex is 4
    189189
    1901902.14 Add a non-element (number)
    191 PASS select2.options.add(option2, 1) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     191PASS select2.options.add(option2, 1) threw exception TypeError: Type error.
    192192PASS select2.options.length is 11
    193193PASS select2.selectedIndex is 4
    194194
    1951952.15 Add a non-element (boolean)
    196 PASS select2.options.add(option2, 1) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     196PASS select2.options.add(option2, 1) threw exception TypeError: Type error.
    197197PASS select2.options.length is 11
    198198PASS select2.selectedIndex is 4
    199199
    2002002.16 Add undefined
    201 PASS select2.options.add(option2, 1) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     201PASS select2.options.add(option2, 1) threw exception TypeError: Type error.
    202202PASS select2.options.length is 11
    203203PASS select2.selectedIndex is 4
    204204
    2052052.17 Add null
    206 PASS select2.options.add(option2, 1) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     206PASS select2.options.add(option2, 1) threw exception TypeError: Type error.
    207207PASS select2.options.length is 11
    208208PASS select2.selectedIndex is 4
    209209
    2102102.18 Add negative infinity
    211 PASS select2.options.add(option2, 1) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     211PASS select2.options.add(option2, 1) threw exception TypeError: Type error.
    212212PASS select2.options.length is 11
    213213PASS select2.selectedIndex is 4
    214214
    2152152.19 Add NaN
    216 PASS select2.options.add(option2, 1) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     216PASS select2.options.add(option2, 1) threw exception TypeError: Type error.
    217217PASS select2.options.length is 11
    218218PASS select2.selectedIndex is 4
    219219
    2202202.20 Add positive infinity
    221 PASS select2.options.add(option2, 1) threw exception TypeError: Argument 1 ('element') to HTMLOptionsCollection.add must be an instance of HTMLElement.
     221PASS select2.options.add(option2, 1) threw exception TypeError: Type error.
    222222PASS select2.options.length is 11
    223223PASS select2.selectedIndex is 4
  • trunk/Source/WebCore/ChangeLog

    r207496 r207497  
     12016-10-18  Chris Dumez  <cdumez@apple.com>
     2
     3        Leverage new union type support for HTMLSelectElement.add() / HTMLOptionsCollection.add()
     4        https://bugs.webkit.org/show_bug.cgi?id=163608
     5
     6        Reviewed by Ryosuke Niwa.
     7
     8        Leverage new union type support for HTMLSelectElement.add() / HTMLOptionsCollection.add():
     9        - https://html.spec.whatwg.org/#htmlselectelement
     10        - https://html.spec.whatwg.org/#htmloptionscollection
     11
     12        No new tests, rebaseline existing test.
     13
     14        * bindings/scripts/CodeGeneratorJS.pm:
     15        (GenerateDefaultValue):
     16        (GenerateParametersCheck):
     17        * bindings/scripts/test/JS/JSTestObj.cpp:
     18        (WebCore::jsTestObjPrototypeFunctionMethodWithOptionalSequenceIsEmptyCaller):
     19        * bindings/scripts/test/JS/JSTestOverloadedConstructorsWithSequence.cpp:
     20        (WebCore::constructJSTestOverloadedConstructorsWithSequence1):
     21        * bindings/scripts/test/JS/JSTestTypedefs.cpp:
     22        (WebCore::jsTestTypedefsPrototypeFunctionFuncCaller):
     23        * html/HTMLOptGroupElement.idl:
     24        * html/HTMLOptionsCollection.cpp:
     25        (WebCore::HTMLOptionsCollection::add):
     26        * html/HTMLOptionsCollection.h:
     27        * html/HTMLOptionsCollection.idl:
     28        * html/HTMLSelectElement.cpp:
     29        (WebCore::HTMLSelectElement::add):
     30        (WebCore::HTMLSelectElement::setOption):
     31        (WebCore::HTMLSelectElement::setLength):
     32        * html/HTMLSelectElement.h:
     33        * html/HTMLSelectElement.idl:
     34
    1352016-10-18  Aaron Chu  <aaron_chu@apple.com>
    236
  • trunk/Source/WebCore/bindings/scripts/CodeGeneratorJS.pm

    r207462 r207497  
    997997sub GenerateDefaultValue
    998998{
    999     my ($interface, $member) = @_;
    1000 
    1001     my $defaultValue = $member->default;
    1002 
    1003     if ($codeGenerator->IsEnumType($member->type)) {
     999    my ($interface, $signature) = @_;
     1000
     1001    my $defaultValue = $signature->default;
     1002
     1003    if ($codeGenerator->IsEnumType($signature->type)) {
    10041004        # FIXME: Would be nice to report an error if the value does not have quote marks around it.
    10051005        # FIXME: Would be nice to report an error if the value is not one of the enumeration values.
    1006         my $className = GetEnumerationClassName($member->type, $interface);
     1006        my $className = GetEnumerationClassName($signature->type, $interface);
    10071007        my $enumerationValueName = GetEnumerationValueName(substr($defaultValue, 1, -1));
    10081008        return $className . "::" . $enumerationValueName;
    10091009    }
    10101010    if ($defaultValue eq "null") {
    1011         return "jsNull()" if $member->type eq "any";
    1012         return "nullptr" if $codeGenerator->IsWrapperType($member->type) || $codeGenerator->IsTypedArrayType($member->type);
    1013         return "String()" if $codeGenerator->IsStringType($member->type);
     1011        return "Nullopt" if $signature->idlType->isUnion;
     1012        return "jsNull()" if $signature->type eq "any";
     1013        return "nullptr" if $codeGenerator->IsWrapperType($signature->type) || $codeGenerator->IsTypedArrayType($signature->type);
     1014        return "String()" if $codeGenerator->IsStringType($signature->type);
    10141015        return "Nullopt";
    10151016    }
    1016     return "{ }" if $defaultValue eq "[]";
     1017    if ($defaultValue eq "[]") {
     1018        my $nativeType = GetNativeTypeFromSignature($interface, $signature);
     1019        return "$nativeType()"
     1020    }
     1021
    10171022    return "jsUndefined()" if $defaultValue eq "undefined";
     1023    return "PNaN" if $defaultValue eq "NaN";
    10181024
    10191025    return $defaultValue;
     
    43434349                        }
    43444350                    } else {
    4345                         $defaultValue = "nullptr" if $defaultValue eq "null";
    4346                         $defaultValue = "PNaN" if $defaultValue eq "NaN";
    4347                         $defaultValue = "$nativeType()" if $defaultValue eq "[]";
    4348                         $defaultValue = "JSValue::JSUndefined" if $defaultValue eq "undefined";
     4351                        $defaultValue = GenerateDefaultValue($interface, $parameter);
    43494352                    }
    43504353
  • trunk/Source/WebCore/html/HTMLOptGroupElement.idl

    r131172 r207497  
    1818 */
    1919
    20 interface HTMLOptGroupElement : HTMLElement {
     20[
     21    JSGenerateToNativeObject,
     22] interface HTMLOptGroupElement : HTMLElement {
    2123    [Reflect] attribute boolean disabled;
    2224    [Reflect] attribute DOMString label;
  • trunk/Source/WebCore/html/HTMLOptionsCollection.cpp

    r199335 r207497  
    3737}
    3838
    39 void HTMLOptionsCollection::add(HTMLElement& element, HTMLElement* beforeElement, ExceptionCode& ec)
     39ExceptionOr<void> HTMLOptionsCollection::add(const OptionOrOptGroupElement& element, Optional<HTMLElementOrInt> before)
    4040{
    41     selectElement().add(element, beforeElement, ec);
    42 }
    43 
    44 void HTMLOptionsCollection::add(HTMLElement& element, int beforeIndex, ExceptionCode& ec)
    45 {
    46     add(element, item(beforeIndex), ec);
     41    return selectElement().add(element, before);
    4742}
    4843
  • trunk/Source/WebCore/html/HTMLOptionsCollection.h

    r207181 r207497  
    4242    HTMLOptionElement* namedItem(const AtomicString& name) const final;
    4343
    44     WEBCORE_EXPORT void add(HTMLElement&, HTMLElement* beforeElement, ExceptionCode&);
    45     WEBCORE_EXPORT void add(HTMLElement&, int beforeIndex, ExceptionCode&);
     44    using OptionOrOptGroupElement = std::experimental::variant<RefPtr<HTMLOptionElement>, RefPtr<HTMLOptGroupElement>>;
     45    using HTMLElementOrInt = std::experimental::variant<RefPtr<HTMLElement>, int>;
     46    WEBCORE_EXPORT ExceptionOr<void> add(const OptionOrOptGroupElement&, Optional<HTMLElementOrInt> before);
    4647    WEBCORE_EXPORT void remove(int index);
    4748    void remove(HTMLOptionElement&);
  • trunk/Source/WebCore/html/HTMLOptionsCollection.idl

    r206723 r207497  
    3131    getter HTMLOptionElement? namedItem(DOMString name);
    3232
    33     // FIXME: Should be:
    34     // void add((HTMLOptionElement or HTMLOptGroupElement) element, optional (HTMLElement or long)? before = null);
    35     [MayThrowLegacyException] void add(HTMLElement element, optional HTMLElement? before = null);
    36     [MayThrowLegacyException] void add(HTMLElement element, long index);
     33    [MayThrowException] void add((HTMLOptionElement or HTMLOptGroupElement) element, optional (HTMLElement or long)? before = null);
    3734
    3835    void remove(long index);
  • trunk/Source/WebCore/html/HTMLSelectElement.cpp

    r207458 r207497  
    222222}
    223223
    224 void HTMLSelectElement::add(HTMLElement& element, HTMLElement* beforeElement, ExceptionCode& ec)
    225 {
    226     if (!(is<HTMLOptionElement>(element) || is<HTMLHRElement>(element) || is<HTMLOptGroupElement>(element)))
    227         return;
    228     insertBefore(element, beforeElement, ec);
    229 }
    230 
    231 void HTMLSelectElement::add(HTMLElement& element, int beforeIndex, ExceptionCode& ec)
    232 {
    233     add(element, item(beforeIndex), ec);
     224ExceptionOr<void> HTMLSelectElement::add(const OptionOrOptGroupElement& element, Optional<HTMLElementOrInt> before)
     225{
     226    HTMLElement* beforeElement = nullptr;
     227    if (before) {
     228        auto visitor = WTF::makeVisitor(
     229            [](const RefPtr<HTMLElement>& element) -> HTMLElement* { return element.get(); },
     230            [this](int index) -> HTMLElement* { return item(index); }
     231        );
     232
     233        beforeElement = std::experimental::visit(visitor, before.value());
     234    }
     235    HTMLElement& toInsert = std::experimental::visit([](const auto& htmlElement) -> HTMLElement& {
     236        return *htmlElement;
     237    }, element);
     238
     239
     240    ExceptionCode ec = 0;
     241    insertBefore(toInsert, beforeElement, ec);
     242    if (ec)
     243        return Exception { ec };
     244    return { };
    234245}
    235246
     
    441452    // Finally add the new element.
    442453    if (!ec) {
    443         add(option, before.get(), ec);
     454        auto exception = add(&option, HTMLElementOrInt(before.get()));
     455        if (exception.hasException())
     456            ec = exception.releaseException().code();
    444457        if (diff >= 0 && option.selected())
    445458            optionSelectionStateChanged(option, true);
     
    457470        do {
    458471            auto option = document().createElement(optionTag, false);
    459             add(downcast<HTMLElement>(option.get()), nullptr, ec);
    460             if (ec)
     472            auto exception = add(downcast<HTMLOptionElement>(option.ptr()), Nullopt);
     473            if (exception.hasException()) {
     474                ec = exception.releaseException().code();
    461475                break;
     476        }
    462477        } while (++diff);
    463478    } else {
  • trunk/Source/WebCore/html/HTMLSelectElement.h

    r205249 r207497  
    5252    bool usesMenuList() const;
    5353
    54     WEBCORE_EXPORT void add(HTMLElement&, HTMLElement* beforeElement, ExceptionCode&);
    55     void add(HTMLElement&, int beforeIndex, ExceptionCode&);
     54    using OptionOrOptGroupElement = std::experimental::variant<RefPtr<HTMLOptionElement>, RefPtr<HTMLOptGroupElement>>;
     55    using HTMLElementOrInt = std::experimental::variant<RefPtr<HTMLElement>, int>;
     56    WEBCORE_EXPORT ExceptionOr<void> add(const OptionOrOptGroupElement&, Optional<HTMLElementOrInt> before);
    5657
    5758    using Node::remove;
  • trunk/Source/WebCore/html/HTMLSelectElement.idl

    r207426 r207497  
    4141    HTMLOptionElement? namedItem(DOMString name);
    4242
    43     [MayThrowLegacyException] void add(HTMLElement element, optional HTMLElement? before = null);
    44     // FIXME: Should be:
    45     // void add((HTMLOptionElement or HTMLOptGroupElement) element, optional (HTMLElement or long)? before = null);
    46     [MayThrowLegacyException] void add(HTMLElement element, long index);
     43    [MayThrowException] void add((HTMLOptionElement or HTMLOptGroupElement) element, optional (HTMLElement or long)? before = null);
    4744
    4845    [MayThrowLegacyException] void remove(); // ChildNode overload
  • trunk/Source/WebKit/mac/ChangeLog

    r207493 r207497  
     12016-10-18  Chris Dumez  <cdumez@apple.com>
     2
     3        Leverage new union type support for HTMLSelectElement.add() / HTMLOptionsCollection.add()
     4        https://bugs.webkit.org/show_bug.cgi?id=163608
     5
     6        Reviewed by Ryosuke Niwa.
     7
     8        Update add() call sites now that it takes std::variant types in.
     9
     10        * DOM/DOMHTMLOptionsCollection.mm:
     11        (-[DOMHTMLOptionsCollection add:index:]):
     12        * DOM/DOMHTMLSelectElement.mm:
     13        (-[DOMHTMLSelectElement add:before:]):
     14
    1152016-10-18  Anders Carlsson  <andersca@apple.com>
    216
  • trunk/Source/WebKit/mac/DOM/DOMHTMLOptionsCollection.mm

    r204717 r207497  
    3030#import "DOMNodeInternal.h"
    3131#import "ExceptionHandlers.h"
     32#import <WebCore/HTMLOptGroupElement.h>
    3233#import <WebCore/HTMLOptionElement.h>
    3334#import <WebCore/HTMLOptionsCollection.h>
     
    3940#import <WebCore/WebScriptObjectPrivate.h>
    4041#import <wtf/GetPtr.h>
     42#import <wtf/Variant.h>
    4143
    4244#define IMPL reinterpret_cast<WebCore::HTMLOptionsCollection*>(_internal)
     
    9193    if (!option)
    9294        raiseTypeErrorException();
    93     WebCore::ExceptionCode ec = 0;
    94     IMPL->add(*core(option), index, ec);
    95     raiseOnDOMError(ec);
     95    auto exception = IMPL->add(core(option), Optional<WebCore::HTMLOptionsCollection::HTMLElementOrInt>(static_cast<int>(index)));
     96    if (exception.hasException())
     97        raiseOnDOMError(exception.releaseException().code());
    9698}
    9799
  • trunk/Source/WebKit/mac/DOM/DOMHTMLSelectElement.mm

    r204717 r207497  
    3737#import <WebCore/HTMLFormElement.h>
    3838#import <WebCore/HTMLNames.h>
     39#import <WebCore/HTMLOptGroupElement.h>
    3940#import <WebCore/HTMLOptionsCollection.h>
    4041#import <WebCore/HTMLSelectElement.h>
     
    183184    if (!element)
    184185        raiseTypeErrorException();
    185     WebCore::ExceptionCode ec = 0;
    186     IMPL->add(*core(element), core(before), ec);
    187     raiseOnDOMError(ec);
     186
     187    auto& coreElement = *core(element);
     188    std::experimental::variant<RefPtr<WebCore::HTMLOptionElement>, RefPtr<WebCore::HTMLOptGroupElement>> variantElement;
     189    if (is<WebCore::HTMLOptionElement>(coreElement))
     190        variantElement = &downcast<WebCore::HTMLOptionElement>(coreElement);
     191    else if (is<WebCore::HTMLOptGroupElement>(coreElement))
     192        variantElement = &downcast<WebCore::HTMLOptGroupElement>(coreElement);
     193    else {
     194        raiseTypeErrorException();
     195        return;
     196    }
     197    auto exception = IMPL->add(WTFMove(variantElement), WebCore::HTMLSelectElement::HTMLElementOrInt(core(before)));
     198    if (exception.hasException())
     199        raiseOnDOMError(exception.releaseException().code());
    188200}
    189201
Note: See TracChangeset for help on using the changeset viewer.