Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

FIX suggestion of ip when subnet id is not the same as array index #34

Merged
merged 1 commit into from

2 participants

@tjikkun

No description provided.

@ohadlevy
Owner

this looks good, in which context would the error happen?

@ohadlevy
Owner

and also, is there an open bug about this? thanks!

@tjikkun

New Host: Network tab. I select a domain, then a subnet. I expect an ip address to be autosugested, but without my patch I only get a javascript error:
drop_text is undefined

Our select box looks like:

<select onchange="subnet_selected(this);" name="host[subnet_id]" id="host_subnet_id"><option value=""></option>
<option value="3">kelder (10.255.9.0/24)</option></select>

and drop_tet is defined as

$(element).text().split("\n")[subnet_id]

subnet_id will be 3 in this case, but there is no 3th array index

I hope you can make sense out of this :)

@tjikkun

No open bug that I know of, just something we ran into. Should I always file a bug report, or is just fixing it ok as well?

@ohadlevy
Owner

hmm.. I can't reproduce, which browser is it?

@tjikkun

Firefox 11
To reproduce, create a few Subnets, 3 for example. Then delete the first two subnets. Then you should be able to reproduce.

@ohadlevy
Owner

OK, I want to try and reproduce to understand the root of the problem, thanks for the patch!

@tjikkun

If it makes it easier to understand:
instead of:

-  var drop_text = $(element).text().split("\n")[subnet_id]
+  var drop_text = $(element).children(":selected").text();

I could also write:

-  var drop_text = $(element).text().split("\n")[subnet_id]
+  var drop_text = $(element).children("[value=" + subnet_id + "]").text();

The important thing is that if some subnets are removed, then you are missing some \n's to split on, so array indexing is off.

@ohadlevy
Owner

what I'm not getting, is why would the returned array is wrong?

@tjikkun

Ok, another try:
we have this selectbox:

    <option value=""></option>
    <option value="1">choosing this option will work (10.255.8.0/24)</option>
    <option value="3">choosing this option will break (10.255.9.0/24)</option>
</select>```
what you see here is that we once had a subnet with id of 2, but it is removed.
Now the current code does
```var drop_text = $(element).text().split("\n")[subnet_id]```
$(element).text() will be:
 ```<empty line>
choosing this option will work (10.255.8.0/24)
choosing this option will break (10.255.9.0/24)

Now you split it, and you get the array:
```0 => ''
1 => 'choosing this option will work (10.255.8.0/24)'
2 => 'choosing this option will break (10.255.9.0/24)'

but since 'value' of the last option is 3, subnet_id will be 3, and
```var drop_text = $(element).text().split("\n")[subnet_id]```
means we try to get index 3 from the array, which doesn't exist.
What you want is to get the text of the option where value==3, not the 3th in the list:
```var drop_text = $(element).children("[value=" + subnet_id + "]").text();```
Or even better, just get the text of the selected element, which is what we are trying to do anyway:
```var drop_text = $(element).children(":selected").text();```
This last one is what my patch does
@ohadlevy ohadlevy merged commit dfcfbae into from
@rbirnie rbirnie referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 16, 2012
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 2 deletions.
  1. +2 −2 public/javascripts/host_edit.js
View
4 public/javascripts/host_edit.js
@@ -123,9 +123,9 @@ function subnet_selected(element){
if (subnet_id == '' || $('#host_ip').size() == 0) return false;
// We do not query the proxy if the host_ip field is filled in and contains an
// IP that is in the selected subnet
- var drop_text = $(element).text().split("\n")[subnet_id]
+ var drop_text = $(element).children(":selected").text();
if (drop_text.length !=0 && drop_text.search(/^.+ \([0-9\.\/]+\)/) != -1) {
- var details = $(element).text().split("\n")[subnet_id].replace(/^[^(]+\(/, "").replace(")","").split("/");
+ var details = drop_text.replace(/^[^(]+\(/, "").replace(")","").split("/");
if (subnet_contains(details[0], details[1], $('#host_ip').val()))
return false;
}
Something went wrong with that request. Please try again.