Closed Bug 495277 Opened 15 years ago Closed 12 years ago

autocomplete.xml should not use new Function()

Categories

(Toolkit :: Autocomplete, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla13

People

(Reporter: vlad, Assigned: felix)

References

Details

Attachments

(1 file, 3 obsolete files)

autocomplete should not use new Function, especially not in code that's executed each time the user types a key in the urlbar:

  http://hg.mozilla.org/mozilla-central/file/tip/toolkit/content/widgets/autocomplete.xml#l403

fireEvent() is only called from 4 different places, to handle onsearchbegin, onsearchcomplete, ontextreverted, ontextentered.  We should just have properties on autocomplete to store those, and if a string is set we should turn it into a function object once.  Then we should just get rid of fireEvent and just call the right event handling function directly.
Flags: wanted1.9.2+
Performance-wise, it would be best to just ignore dynamic changes to these attributes (onsearchbegin, onsearchcomplete, ontextreverted, and ontextentered). Would that be an acceptable trade-off?
- Took to suggestion in comment 1 and ignored setting these properties after autocomplete is constructed. As far as I can tell from searching the codebase, none of these properties are modified dynamically.
Assignee: nobody → ffung
Attachment #572664 - Flags: review?(vladimir)
Comment on attachment 572664 [details] [diff] [review]
Reduce calls to new Function in Autocomplete.xml

>diff --git a/toolkit/content/widgets/autocomplete.xml b/toolkit/content/widgets/autocomplete.xml

>       <constructor><![CDATA[
>         mController = Components.classes["@mozilla.org/autocomplete/controller;1"].
>                         getService(Components.interfaces.nsIAutoCompleteController);
>+
>+        mSearchBeginHandler = this.initEventHandler("searchbegin");
>+        mSearchCompleteHandler = this.initEventHandler("searchcomplete");
>+        mTextEnteredHandler = this.initEventHandler("textentered");
>+        mTextRevertedHandler = this.initEventHandler("textreverted");

Use this.mSearchBeginHandler, etc. And fix mController while you're here too?

>+      <method name="initEventHandler">

>+          let handlerString = this.getAttribute("on" + aEventType);
>+          if (handlerString) {
>+            return new Function("eventType", "param", handlerString);

You could return:

(new Function("eventType", "param", handlerString)).bind(this, aEventType);

and then just call the function directly (without .apply, at least for those without additional parameters) from the handlers.

Are there tests for this functionality? There should be, shouldn't be too hard to add.

Cancelling the review request pending response to these comments.
Attachment #572664 - Flags: review?(vladimir)
Additional Changes:
- Addressed above comments

Notes:
- toolkit/content/tests/chrome/test_autocomplete[2..4].xul seem to cover the relevant functionality.
Attachment #572664 - Attachment is obsolete: true
Attachment #581613 - Flags: review?(gavin.sharp)
(In reply to Felix Fung (:felix) from comment #4)
> - toolkit/content/tests/chrome/test_autocomplete[2..4].xul seem to cover the
> relevant functionality.

None of those tests appear to cover e.g. onsearchbegin="foo()" attributes on an xul:autocomplete input, which is what you're changing here.
Additional Changes:
- Added tests
Attachment #581613 - Attachment is obsolete: true
Attachment #581928 - Flags: review?(gavin.sharp)
Attachment #581613 - Flags: review?(gavin.sharp)
Could you please use _ instead of the m prefix for new fields?
Additional Changes:
- Prefixed new fields with _ instead of m.
Attachment #581928 - Attachment is obsolete: true
Attachment #584245 - Flags: review?(gavin.sharp)
Attachment #581928 - Flags: review?(gavin.sharp)
Attachment #584245 - Flags: review?(gavin.sharp) → review+
Where should we comment that making dynamic changes on these properties (e.g. onsearchbegin) does not work as one would expect and instead would do nothing.
https://hg.mozilla.org/mozilla-central/rev/f01f200f798a
Status: NEW → RESOLVED
Closed: 12 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla13
Depends on: 729244
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: