google / closure-compiler

A JavaScript checker and optimizer.
https://developers.google.com/closure/compiler/
Apache License 2.0
7.36k stars 1.14k forks source link

cannot make an HTMLOptionElement #1759

Closed myphysicslab closed 8 years ago

myphysicslab commented 8 years ago

After updating to the latest compiler (previously I was using version from Nov 2015) I had to change this line

this.selectMenu_.options[i] = new Option(this.choices_[i]);

to this:

var opt =  /** @type {HTMLOptionElement} */(document.createElement('option'));
opt.text = this.choices_[i];
this.selectMenu_.options[i] = opt;

I tried doing this:

var opt = new HTMLOptionElement();
opt.text = this.choices_[i];
this.selectMenu_.options[i] = opt;

and that compiled OK, but results in runtime errors:

ERROR: TypeError: HTMLOptionElementConstructor is not a constructor 
(evaluating 'new HTMLOptionElement')
file:///Users/erikn/Documents/Programming/jssimlab/build/sims/engine2D/BilliardsApp_en.js:1007

Note also that new HTMLOptionElement() does not allow arguments in the constructor.

Here are some of the relevant online docs:

http://www.w3.org/TR/2012/WD-html5-20121025/the-option-element.html#dom-option

https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement/Option

https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement

That last reference seems to imply that the Option constructor should be available like this:

new HTMLOptionElement.Option();

and that it should take four values.

Dominator008 commented 8 years ago
new HTMLOptionElement.Option();

gives a TypeError in Chrome. I think new Option() should still be used: https://github.com/google/closure-compiler/blob/42a3cc26b3588450770a450027aad0c56a2698dd/externs/browser/deprecated.js#L33-L40 What error did you see when you had it the previous way?

myphysicslab commented 8 years ago

Here is the error:

found   : Option
required: (HTMLOptionElement|null)
    this.selectMenu_.options[i] = new Option(this.choices_[i]);
    ^

1 error(s), 0 warning(s), 99.4% typed

Here is the definition of selectMenu_

  /**
  * @type {!HTMLSelectElement}
  * @private
  */
  this.selectMenu_ =
      /** @type {!HTMLSelectElement} */(document.createElement('select'));

This was compiling with the previous compiler version from Nov 2015.

Dominator008 commented 8 years ago

@myphysicslab Please see if https://github.com/google/closure-compiler/pull/1760 fixes it. Technically it should be an alias but I think @extends will work in most cases. This is a non-standard deprecated API anyway.

myphysicslab commented 8 years ago

Yes, #1760 fixes it. I made that change in my local version of the compiler, rebuilt the compiler, and now the original version of the line is compiling with no warning or error:

this.selectMenu_.options[i] = new Option(this.choices_[i]);
Dominator008 commented 8 years ago

@myphysicslab Great, thanks. I don't expect #1760 to cause breakages internally at Google but we will see how it goes :)