Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

fix(select): handle corner case of adding options via a custom directive #13878

Closed

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented Jan 28, 2016

Under specific circumstances (e.g. adding options via a directive with replace: true and a structural directive in its template), an error occurred when trying to call hasAttribute() on a comment node (which doesn't support that method).
This commit fixes it by first checking if hasAttribute() is available.

Fixes #13874

Under specific circumstances (e.g. adding options via a directive with `replace: true` and a
structural directive in its template), an error occurred when trying to call `hasAttribute()` on a
comment node (which doesn't support that method).
This commit fixes it by first checking if `hasAttribute()` is available.

Fixes angular#13874
@gkalpak gkalpak force-pushed the fix-select-chromeHack-on-comment branch from ba177c1 to f0e1eb0 Compare January 28, 2016 19:53
scope: {myOptions: '='},
replace: true,
template:
'<option value="{{ ::option.value }}" ng-repeat="option in ::myOptions">' +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reasonw why this uses one-time binding?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No particular reason - force of habbit :)

@Narretz Narretz assigned gkalpak and unassigned Narretz Jan 29, 2016
@gkalpak
Copy link
Member Author

gkalpak commented Jan 29, 2016

@Narretz, I made the suggested changes. PTAL

@@ -80,6 +80,9 @@ var SelectController =

// Tell the select control that an option, with the given value, has been added
self.addOption = function(value, element) {
// Skip comment nodes, as they only pollute the `optionsMap`
if (element.prop('nodeType') === NODE_TYPE_COMMENT) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

element[0].nodeType?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my first "reaction", but then I thought: "What if element[0] is undefined ?"
So, instead of element[0] && element[0].nodeType, I went for element.prop(...) (I'm not even sure if the element passed to the linking function can be empty...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it can't. Fixed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AddOption is a private Api so we guarantee that element will always be jq
wrapped. I'm a bit worried about the overhead of calling prop on every
option.
Am 29.01.2016 13:28 schrieb "Georgios Kalpakas" notifications@github.com:

In src/ng/directive/select.js
#13878 (comment):

@@ -80,6 +80,9 @@ var SelectController =

// Tell the select control that an option, with the given value, has been added
self.addOption = function(value, element) {

  • // Skip comment nodes, as they only pollute the optionsMap
  • if (element.prop('nodeType') === NODE_TYPE_COMMENT) return;

That was my first "reaction", but then I thought: "What if element[0] is
undefined ?"
So, instead of element[0] && element[0].nodeType, I went for
element.prop(...) (I'm not even sure if the element passed to the linking
function can be empty...)


Reply to this email directly or view it on GitHub
https://github.com/angular/angular.js/pull/13878/files#r51255240.

@Narretz
Copy link
Contributor

Narretz commented Jan 29, 2016

small suggestion, otherwise LGTM

@gkalpak gkalpak closed this in ca5b27b Jan 29, 2016
gkalpak added a commit that referenced this pull request Jan 29, 2016
Under specific circumstances (e.g. adding options via a directive with `replace: true` and a
structural directive in its template), an error occurred when trying to call `hasAttribute()` on a
comment node (which doesn't support that method).
This commit fixes it by filtering out comment nodes in the `addOption()` method.

Fixes #13874
Closes #13878
@gkalpak
Copy link
Member Author

gkalpak commented Jan 29, 2016

Backported to v1.4.x as df6e731.

Thx for the review, @Narretz.

@gkalpak gkalpak deleted the fix-select-chromeHack-on-comment branch January 29, 2016 23:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

chromeHack method throws TypeError: optionElement[0].hasAttribute is not a function
3 participants