简体   繁体   English

是否有 document.execCommand 的替代品? (或者使用 document.execCommand 是否安全?)

[英]Is there a replacement for document.execCommand? (or is it safe to use document.execCommand?)

I am building an amateur rich text editor with vanilla JavaScript and document.execCommand() is essential to enabling the core features of an text editor.我正在使用 vanilla JavaScript 构建一个业余富文本编辑器,而 document.execCommand() 对于启用文本编辑器的核心功能至关重要。

For example bold, italic and unordered list commands:例如粗体、斜体和无序列表命令:

Array.from(toolbarBtn).forEach(btn => {
  btn.addEventListener('click', (e) => {
    e.preventDefault();
    if (e.target.id === "toolbar__btn--bold") {
      format('bold');
    }
    if (e.target.id === "toolbar__btn--italic") {
      format('italic');
    }
    if (e.target.id === "toolbar__btn--unorderedlist") {
      format('insertunorderedlist');
    }
});
});

However, when looking up this command on MDN Web Docs, I saw that this command is considered to be obsolete:但是,在 MDN Web Docs 上查找此命令时,我看到此命令被认为已过时:

Obsolete This feature is obsolete. 已过时此功能已过时。 Although it may still work in some browsers, its use is discouraged since it could be removed at any time.尽管它在某些浏览器中可能仍然有效,但不鼓励使用它,因为它可能随时被删除。 Try to avoid using it.尽量避免使用它。

So, I'm wondering are there any replacement method in vanilla JavaScript, that could create all the Rich Text Editor features like execCommand() does?所以,我想知道香草 JavaScript 中是否有任何替代方法,可以创建像 execCommand() 这样的所有富文本编辑器功能?

The Google search gave me no results, so at the same time, I wonder, how is that possible that the method is announced to be obsolete, but no alternative is suggested.谷歌搜索没有给我任何结果,所以同时我想知道,该方法怎么可能被宣布过时,但没有建议替代方案。

Both the change to MDN marking document.execCommand() as obsolete and a related change at https://github.com/mdn/browser-compat-data/commit/2d3890a were made in part to due to https://w3c.github.io/editing/ActiveDocuments/execCommand.html having a big red warning with the following statements:对 MDN 将document.execCommand()标记为过时的更改和https://github.com/mdn/browser-compat-data/commit/2d3890a的相关更改部分是由于https://w3c。 github.io/editing/ActiveDocuments/execCommand.html带有大红色警告,并带有以下语句:

This spec is incomplete and it is not expected that it will advance beyond draft status.该规范不完整,预计不会超出草案状态。 Authors should not use most of these features directly, but instead use JavaScript editing libraries.作者不应直接使用这些功能中的大部分,而应使用 JavaScript 编辑库。 The features described in this document are not implemented consistently or fully by user agents, and it is not expected that this will change in the foreseeable future.本文档中描述的功能并未由用户代理一致或完全实现,并且预计在可预见的将来不会改变。

As far as any replacement method in vanilla JavaScript, the same warning box says it's:至于香草 JavaScript 中的任何替换方法,相同的警告框表示:

predicted that in the future both specs will be replaced by Content Editable and Input Events预测在未来这两个规范都将被 Content Editable 和 Input Events 取代

…but sadly, we're not there yet. ......但遗憾的是,我们还没有到那里。 So the unfortunate current state of things is, even though we have no replacement yet, we know document.execCommand() as-is now doesn't work cross-browser interoperably — and browser projects aren't gonna be fixing it.因此,不幸的当前 state 是,即使我们还没有替代品,我们知道document.execCommand()现在不能跨浏览器互操作 - 浏览器项目不会修复它。 That's why the MDN warning says:这就是为什么 MDN 警告说:

its use is discouraged… Try to avoid using it.不鼓励使用它……尽量避免使用它。

So, as a comment above says, it's similar to the case of drag-and-drop: It's known to be broken in a number of ways, and it's been that way for a long time because fixing it is basically not practical.因此,正如上面的评论所说,它类似于拖放的情况:众所周知,它会以多种方式被破坏,而且这种方式已经存在很长时间了,因为修复它基本上不切实际。

And that's why the red warning box in the spec also says that developers and authors:这就是为什么规范中的红色警告框还说开发人员和作者:

should not use most of these features directly, but instead use JavaScript editing libraries不应该直接使用这些功能中的大部分,而是使用 JavaScript 编辑库

The available JavaScript editing libraries in online rich-text editor tools such as CKEditor and TinyMCE “paper over” all the underlying brokenness in document.execCommand() for you. CKEditor 和 TinyMCE 等在线富文本编辑器工具中可用的 JavaScript 编辑库为您“覆盖”了document.execCommand()中的所有潜在缺陷。 If you were to try to write your own robust handling for document.execCommand() in vanilla JavaScript from scratch, you'd basically — after a lot of work and time — end up having repeated the work that was done to create the JavaScript libraries underlying those tools.如果您要尝试从头开始在 vanilla JavaScript 中为document.execCommand()编写自己的强大处理,那么您基本上 - 经过大量工作和时间 - 最终会重复创建 JavaScript 库所做的工作这些工具的基础。

So the bottom line is: to save yourself a lot of time and work, use one of the available libraries.所以底线是:为了节省大量时间和工作,请使用可用的库之一。

It looks like the new standard will be Input Events Level 2 .看起来新标准将是Input Events Level 2

The way I see it, it tries to fix the pain points of execCommand.在我看来,它试图解决 execCommand 的痛点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM