I'm not actually convinced that this is Drupal's problem, but these guys don't seem to want to fix it.
Xinha adds two functions to every array, and when Drupal's autocomplete returns no results, it tries to display these two functions instead. To reproduce, install the htmlarea module and try to create a node, of a type with free tagging enabled. Enter some text with no matching terms, and you will see two javascript functions appear in the autocomplete dropdown.
Attached is a change that could be made to autocomplete.js, to make this problem go away.
Comments
Comment #1
john morahan commentedComment #2
m3avrck commentedThis is indeed a bug but not related to Xinha. I've seen this on sites I run and I have had users complain about. *very bad* for users to see errors & code.
Here is the patch I run...
Comment #3
drummWhy are you using
data.length? Is having more than 4 characters really a good way of checking if we have a valid JSON response, or am I missing something?Is removing the error handling the best thing to do? The current errors can get a bit ugly, so they should be improved, not completely removed.
I think I like John Morahan's patch in the original post better. Anyone else want to test it?
Comment #4
m3avrck commentedI was testing the length to make sure the type was right. John's patch does that a bit cleaner indeed.
I stripped out the HTTP error not found because users should **never** see this error. Some were complaining about it and what did it mean.
If for some reason your JavaScript autocomplete callback is broken, why should it show that to users who might be creating nodes and tagging it?
Yes, it should show only the error to admins and/or log it, but we don't have a mechanism for that.
In my case, not showing the error was the best course of action.
Comment #5
samo commentedThanks for the patch. The original patch from John is working well on a Drupal 5 (up-to-date branch) install that needs HTMLArea (Xinha).
Comment #6
drummThe original patch by John Morahan still applies without errors to HEAD. This issue should be resolved there and backported as needed. I think the code looks good, but needs testing on HEAD.
Comment #7
catchThis really needs a comment for data.length > 4
Comment #8
solipsist commentedI've fixed it by simply overriding the AC object's prototype method. That means I don't, for the purpose of a workaround, have to hack core (and cause the death of young felines!). I do think however that this has to be fixed in core.
If I may be so bold as to say that my solution is a bit prettier than the ones already suggested here. Instead of checking type or string length, I suggest using hasOwnProperty method. Using it allows you to determine whether the iterator has handed us a property of the object or a method inherited down through the prototype chain.
Comment #9
solipsist commentedRemade patch using diff instead of NetBeans.
Comment #10
john morahan commentedright?
Comment #12
agileware commentedI like the approach in #9.
Here are drupal 6 & 7 versions of the patch that should pass the tests.
I have not tested the D7 version but I have tested the D6 one and it fixed my getting javascript code in the autocomplete results wherever there should have been no results.
Comment #13
c960657 commentedIsn't it better/simpler to just iterate using "for (var i = 0; i < matches.length; i++)" or the more jQuery'ish $.each()? This sees to be the suggested approach here.
This is the same issue as reported in #784670: autocomplete.js can iterate over functions in Array objects. Let's continue the discussion there.