why is contenteditable beforeinput event not cancelable?

烈酒焚心 提交于 2020-05-15 11:17:13

问题


Reading this webkit document, It says the beforeinput event when used with contenteditable is cancelable. But I run the code in the document on a chrome browser and the event is not cancelable.

Am I mistaken? How to enable the beforeinput event to be cancelable?

<body onload="setup()">
  <div id="editor" contenteditable>
    <p>This is some regular content.</p>
    <p>This text is fully editable.</p>
    <div class="quote" style="background-color: #EFFEFE;">
      <p>This is some quoted content.</p>
      <p>You can only change the format of this text.</p>
    </div>
    <p>This is some more regular content.</p>
    <p>This text is also fully editable.</p>
  </div>
</body>

<script>
  function setup()
  {
    editor.addEventListener("beforeinput", event =>
    {
      if (event.inputType.match(/^format/))
        return;

      for (let staticRange of event.getTargetRanges())
      {
        if (nodeIsInsideQuote(staticRange.startContainer)
          || nodeIsInsideQuote(staticRange.endContainer))
        {
          event.preventDefault();
          return;
        }
      }
    });

    function nodeIsInsideQuote(node)
    {
      let currentElement = node.nodeType == Node.ELEMENT_NODE ? node : node.parentElement;
      while (currentElement)
      {
        if (currentElement.classList.contains("quote"))
          return true;
        currentElement = currentElement.parentElement;
      }
      return false;
    }
  }
</script>
  

来源:https://stackoverflow.com/questions/53140803/why-is-contenteditable-beforeinput-event-not-cancelable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!