Skip to content

🐛 fix element.append() creates undefined TextNode #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Cweili
Copy link
Contributor

@Cweili Cweili commented Sep 3, 2021

fn.call( this, el, ...args ) 写法会被 babel 转换为 fn.call.apply(fn, [this, el].concat(args)) ,

el 为 undefined 时, fn apply 的参数列表为 [undefined] ,

调用 append 时会造成元素被 append 一个内容为 undefined 的 TextNode .

比如下面这种用法

https://github.com/microsoft/vscode/blob/136adbed5bdcbca1510f8e4dd5eacd1f4ea63c49/src/vs/base/browser/dom.ts#L1068

@Boelroy
Copy link
Contributor

Boelroy commented Sep 8, 2021

问题我明白了 不过感觉解法上有点问题,等我看一下

@Boelroy
Copy link
Contributor

Boelroy commented Sep 15, 2021

根据如下 DOM API 的几个函数签名:

element.insertBefore(newNode, referenceNode)

element.appendChild(aChild)

element.append(...nodesOrDOMStrings)

其实这里比较会出问题的是在于 append 这个方法, insertBefore, appendChild 都是针对单个 element 做调用 所以并不会出现问题。

append 现在实现应该是有问题的,因为它接受的参数是一组 element. 所以实际上应该把 append 转化成 批量地调用 appendChild 的方法

这里你可以修改一下 对 append 方法的实现。如果着急我来改也行

@Cweili
Copy link
Contributor Author

Cweili commented Sep 17, 2021

感谢回复~

目前我的项目中暂时用这个 PR 的代码修复了这个问题,目前没发现其他问题。

您这边有时间的话可以看下是否有更佳的解决方案,可以随时讨论,再次感谢。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants