# Emoji选择器导致Android/Chrome中的消息编辑器崩溃

**URL:** <https://meta.discourse.org/t/emoji-selector-crashes-the-message-editor-in-android-chrome/187009>\
**Category:** Bug\
**Tags:** composer\
**Created:** [2021 年4 月 15 日 20:06 UTC](https://meta.discourse.org/t/emoji-selector-crashes-the-message-editor-in-android-chrome/187009 "2021-04-15T20:06:52Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

作者： ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
发布日期： [2021 年4 月 15 日 23:10 UTC](https://meta.discourse.org/t/emoji-selector-crashes-the-message-editor-in-android-chrome/187009/3 "2021-04-15T23:10:10Z")

</div>

我可以复现。以下是堆栈跟踪：

```plaintext
_application-bfbda341c2eb6dd7d61c681e17bdccec057c30e045ddc332927a7363150e9b1b.js:16386 Uncaught TypeError: Cannot read property '0' of null
    at HTMLLIElement.<anonymous> (application-bfbda341c2eb6dd7d61c681e17bdccec057c30e045ddc332927a7363150e9b1b.br.js:1)
    at HTMLLIElement.dispatch (ember_jquery-36a23101c869ab0dc53fc908de69adb785731593573d32bdeef416acc1076ef4.br.js:1)
    at HTMLLIElement.d.handle (ember_jquery-36a23101c869ab0dc53fc908de69adb785731593573d32bdeef416acc1076ef4.br.js:1)
(anonymous) @ application-bfbda341c2eb6dd7d61c681e17bdccec057c30e045ddc332927a7363150e9b1b.br.js:1
dispatch @ ember_jquery-36a23101c869ab0dc53fc908de69adb785731593573d32bdeef416acc1076ef4.br.js:1
d.handle @ ember_jquery-36a23101c869ab0dc53fc908de69adb785731593573d32bdeef416acc1076ef4.br.js:1

```

问题出在这一行：

[https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/app/lib/autocomplete.js#L308](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/app/lib/autocomplete.js#L308)

错误发生的原因是 `selectedOption` 为 0（即单个建议，也就是第一个），而 `autocompleteOptions` 不知为何为 null。

我正在调查原因……

目前我还不确定具体原因。起初我怀疑是 @Osama 的这个 PR：

> <https://github.com/discourse/discourse/pull/11637>
>
> When you type # or @ in the search box, a popup appears with
> autocomplete sugge…stions. Currently, when the popup is rendered it has
> the first item selected and upon pressing Enter, the first item is
> inserted into the search box. The problem with this behavior is that the
> first suggestion may not be what you want, and if you are typing quickly
> and hit enter, the first suggestion (which is not what you want) is
> inserted in the search box.
> 
> This PR amends the popup so that it has no suggestions selected by
> default which means the enter key will not insert anything unless you
> select a suggestion via the up or down arrow keys.

但我添加了不少断点，却仍然无法找到究竟是谁在修改 `autocompleteOptions` 并将其设为 null。

`autocompleteOptions` 来自上方两层父级闭包的作用域，这一点也非常奇怪，使得代码更难追踪和调试。

---

_[View the full topic](https://meta.discourse.org/t/emoji-selector-crashes-the-message-editor-in-android-chrome/187009)._
