Filtering Pasted Content

Sometimes you need to go beyond the options that EditLive! provides for pasting content into the editor and filter the content yourself. For example, to remove images, update or change the URL of links or just forbid certain tags from being used. You can achieve this through our advanced APIs by setting a paste filter.

To provide a simple example, let's write a paste filter for a cat lovers site to change any reference to the word "dog" to be "cat". It may be silly, but it shows the important concepts to let you filter pasted content however you want.

Firstly, we need a class that implements com.ephox.editlive.PasteFilter:

public class SimplePasteFilter implements PasteFilter {

Next, we need the usual constructor that all plugins have, which accepts an ELJBean instance and we'll simply set the current class as the paste filter:

public SimplePasteFilter(ELJBean bean) {
  bean.setPasteFilter(this);
}

Finally, the one method that PasteFilter specifies in String filterIn(String) which is called whenever text or HTML content is pasted to allow the paste filter to make any required changes before the content is inserted into the document. In our case, we do a simple replaceAll:

public String filterIn(String source) {
  return source.replaceAll("dog", "cat");
}

That's it. Now whenever you paste content that contains the string "dog", it is replaced with "cat". You can download the complete source code for the SimplePasteFilter class as a starting point for whatever filter you need.

Adrian spends his days working out ways to make life easier for Ephox clients through initiatives like LiveWorks! Previously a senior engineer, he has now moved on to the fancier sounding title of CTO.

4 Responses to “Filtering Pasted Content”

  1. Paolo Says:

    Using this method seems to break the functionality of clicking the right-click menu paste. It also breaks the functionality of the toolbar button’s paste. Any ideas why this is happening?

    Paolo

  2. Adrian Sutton Says:

    Hi Polo,
    There’s something odd going on there as both worked when I wrote the code for this article. Have you checked the console output to see if there are any exceptions? Are there any cases where it does work? Is the jar file that your code in signed correctly (this is important and can lead to some very unusual problems)?

    It might be worth jumping onto the LiveWorks! mailing list (http://liveworks.ephox.com/mailing-list/) - it tends to be easier to discuss these things via email.

  3. Paolo Says:

    Adrian,

    Two things that might be causing the problem:

    1. The jar is not being signed. What alias do I need to use to sign the jar files? (Does ePhox have documentation on how to do this)?
    2. The manifest file is an empty text file. I just used an empty file to get the code into a jar file.

    Do you think #2 could be causing the problem?

    Paolo

  4. Adrian Sutton Says:

    Hi Paolo,
    Sorry for the delay in replying my internet access is fairly sporadic at the moment. The problem is definitely that the jar is unsigned. The manifest shouldn’t be a problem and you shouldn’t need to specify one at all (use the jar tool that comes with the Java SDK and it handles all the little details).

    You can sign the jar with a self signed certificate easily enough. keytool and jarsigner are the key tools to use. Take a look at http://java.sun.com/j2se/1.3/docs/tooldocs/win32/keytool.html as a starting point (self signed certificates are discussed half way down the page).

Leave a Reply