title: How do I restrict the style of the text pasted in a contenteditable area?
body: <p>I came across <a href="https://stackoverflow.com/questions/53186433/restrict-paste-in-contenteditable-html-js">this Stack Overflow post</a> where was discussed the exact thing I needed: to be able to paste text into a contenteditable area, retaining only a few styles. I ran the code snippet there and it work fine. However, when I tried it on my page, all styles were being removed, including those I wanted to keep, like bold and italic. After comparing the codes and a few experimentations, I realized that the reason it was not working was because I was using external CSS, instead of inline.</p>
  <p>Is there any way I can make it work with external CSS? I will never know the origin of the text users will post in that contenteditable, and how was style applied to it, so I am looking to address all possibilities.</p>
  <p>Also, is there a way to make it work with dragged and dropped text, instead of just pasted text? I tried replacing the event it is listening to from &quot;paste&quot; to &quot;drop&quot;, but I get the error <code>e.clipboardData is undefined</code></p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const el = document.querySelector('p');
  
  el.addEventListener('paste', (e) =&gt; {
    // Get user's pasted data
    let data = e.clipboardData.getData('text/html') ||
        e.clipboardData.getData('text/plain');
    
    // Filter out everything except simple text and allowable HTML elements
    let regex = /&lt;(?!(\/\s*)?(b|i|em|strong|u)[&gt;,\s])([^&gt;])*&gt;/g;
    data = data.replace(regex, '');
    
    // Insert the filtered content
    document.execCommand('insertHTML', false, data);
  
    // Prevent the standard paste behavior
    e.preventDefault();
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.editable {
    width: 100%;
    min-height: 20px;
    font-size: 14px;
    color: black;
    font-family: arial;
    line-height: 1.5;
    border: solid 1px black;
    margin-bottom: 30px;
    }
    
  .big {
    font-size: 20px;
  }
  
  .red {
    color: red;
  }
  
  .bold {
    font-weight: bold;
  }
  
  .italic {
    text-decoration: italic;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;p class="editable" contenteditable&gt;&lt;/p&gt;
  
  &lt;p class="notEditable"&gt;
    Try pasting this paragraph into the contenteditable paragraph above. This text includes &lt;b&gt;BOLD&lt;/b&gt;, &lt;i&gt;ITALIC&lt;/i&gt;, &lt;s&gt;STRIKE&lt;/s&gt;, &lt;u&gt;UNDERLINE&lt;/u&gt;, a &lt;a href='#'&gt;LINK&lt;/a&gt;, and &lt;span style="font-size:30px; color:red; font-family:Times New Roman"&gt;a few other styles.&lt;/span&gt; All styles are inline, and it works as expected.
  &lt;/p&gt;
  
  &lt;p&gt;Now, try pasting this paragraph with external styles. &lt;span class="big"&gt;Big&lt;/span &gt; &lt;span class="red"&gt;red&lt;/span&gt; &lt;span class="bold"&gt;bold&lt;/span&gt; &lt;span class="italic"&gt;italic&lt;/span&gt;. It no longer works.&lt;/p&gt; </code></pre>
  </div>
  </div>
  </p>