Skip to content

Select .options attribute should be an HTMLOptionsCollection #9334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jasongrout opened this issue Jun 23, 2016 · 7 comments
Closed

Select .options attribute should be an HTMLOptionsCollection #9334

jasongrout opened this issue Jun 23, 2016 · 7 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@jasongrout
Copy link

jasongrout commented Jun 23, 2016

TypeScript Version:

1.9.0-dev (commit be6c34f)

Description

In https://github.com/Microsoft/TypeScript/blob/be6c34f1c79ec998c56ae127e572ffa9d7794b51/lib/lib.dom.d.ts#L6797, .options should be an HTMLOptionsCollection, which would have typed add/remove methods. We have an HTMLOptionsCollection defined at https://github.com/Microsoft/TypeScript/blob/be6c34f1c79ec998c56ae127e572ffa9d7794b51/lib/lib.dom.d.ts#L6633, but I think it should probably inherit from HTMLCollectionOf<HTMLOptionElement> (possibly unioned with an optgroup type too) for the typed index accessors, etc.

Also, HTMLOptionsCollection shouldn't have a selectedIndex: number; attribute according to the spec at https://www.w3.org/TR/2011/WD-html5-20110525/common-dom-interfaces.html#htmloptionscollection-0

Code

// A self-contained demonstration of the problem follows...
document.createElement('select').options

cf. #8220

@jasongrout jasongrout changed the title HTMLOptionsCollection doesn't recognize elements of HTMLOptionElements Select .options parameter should be HTMLOptionsCollection Jun 23, 2016
@jasongrout jasongrout changed the title Select .options parameter should be HTMLOptionsCollection Select .options attribute should be an HTMLOptionsCollection Jun 23, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Jun 23, 2016

I am not sure i understand why this changed but, 9a55fac should show the fixed definition.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 23, 2016

should be in typescript@next later tonight.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jun 23, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Jun 23, 2016

Also, HTMLOptionsCollection shouldn't have a selectedIndex: number; attribute according to the spec at https://www.w3.org/TR/2011/WD-html5-20110525/common-dom-interfaces.html#htmloptionscollection-0

I am a bit confused:

from https://www.w3.org/TR/2011/WD-html5-20110525/common-dom-interfaces.html#htmloptionscollection-0

interface HTMLOptionsCollection : HTMLCollection {
  // inherits item()
  attribute unsigned long length; // overrides inherited length
  caller getter object namedItem(in DOMString name); // overrides inherited namedItem()
  void add(in HTMLElement element, in optional HTMLElement before);
  void add(in HTMLElement element, in long before);
  void remove(in long index);
  attribute long selectedIndex;
};

from lib.d.ts:

interface HTMLOptionsCollection extends HTMLCollectionOf<HTMLOptionElement> {
    length: number;
    selectedIndex: number;
    add(element: HTMLOptionElement | HTMLOptGroupElement, before?: HTMLElement | number): void;
    remove(index: number): void;
}

and both has selectedIndex.

@jasongrout
Copy link
Author

and both has selectedIndex.

You're right! My mistake.

Should HTMLOptionsCollection extend HTMLCollectionOf<HTMLOptionElement | HTMLOptGroupElement> to account for optgroup elements? (I'm not sure if .options will list option group elements alongside option elements).

@jasongrout
Copy link
Author

Thanks for the fast response on this!

@mhegazy
Copy link
Contributor

mhegazy commented Jun 24, 2016

Can you explain why you think it should be a union?

@aluanhaddad
Copy link
Contributor

I suppose it is because both types are valid elements in the collection, but neither is declared as a subtype of the other. Actually, the instance side of both types is identical, but this is not clear from reading their definitions.

var x: HTMLOptGroupElement;
var y: HTMLOptionElement = x;
x = y;

is perfectly valid, but they are not explicitly related. A union would be clearer, but it would not affect typing.

@mhegazy mhegazy added Needs More Info The issue still hasn't been fully clarified and removed Fixed A PR has been merged for this issue labels Jun 28, 2016
@mhegazy mhegazy closed this as completed Feb 28, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

3 participants