Hi there,
I came across a situation where an assumption in uc_taxes.js was causing an inifinite loop to take place in the javascript.
Recently, I implemented a couple of ubercart plugins for using web-services to compute taxes. (see http://drupal.org/project/stc_taxes and http://drupal.org/project/so_taxes)
One of the requirements for the return value of the hook_calculate_tax() requires that each tax object returned have a unique id.
Now, the way the checkout screen works is that it calls a bunch of hooks via ajax to add line items on an order. Each time a line-item is added via ajax, another call is triggered to check if the most recently added line-items themselves would cause any hooks to add any additional line-items. It does this by getting the latest line-items, adding them to the list, and if there are any new line items, making another call. It does this by comparing the id of any new line items to the next round of line items and seeing if there is anything new.
In this process, it prepends the line-item type to the type returned by the hooks. It then goes through and splits off the prepended portion (in this case: 'tax_') and seeing if the remainder matches up with any of the new ids returned in the last ajax call. It does so by making a call to the js function split(), using '_' as the delimiter. The problem is that if '_' is used in the returned id itself, it chops off the rest of the id. It then makes another call. Because the return values for the ids from my tax module include '_', it does not recognize the entire id (since it is matching it to only the first portion).
In my case, the second to last ajax call would get back a tax id of "stc_tax" or "so_tax" and, in the javascript, prepend it so it would become "tax_stc_tax" or "tax_so_tax". It would then make another call to see if these items triggered any new line-items/taxes to be added. In my case, it would use split with '_' as the delimiter to get the original id to compare it to the id returned by what should've been the last call. Because of this, the id it would compare against from the latest call's returned ids would be "stc" to "stc_tax" (or "so" to "so_tax"). This would then cause it to make another call. This would result in an infinite recursion.
My guess is that either the documentation should make it clear that the ids should not include underscores ('_') but hyphens ('-') instead. OR, it should use substring() instead of split() to determine the original id that was returned by the previous ajax call before comparing it to the ids returned by the next ajax call.
I've included a patch in the case that the latter is decided upon. In the meantime, I've updated my tax modules to use hyphens instead of underscores, just so we can get this show on the road.
| Comment | File | Size | Author |
|---|---|---|---|
| uc_taxes.js_.diff | 945 bytes | ankur |
Comments
Comment #1
rszrama commentedComment #2
Island Usurper commentedI should have figured that JavaScript split() would be different from PHP explode(). If you tell explode() to only return two values, the second piece contains the rest of the string after the first separator.
Patch looks like it works, so it's committed.