xpages

How can I test if a CK Editor field is empty


I want to test if a CKEditor ( Rich Text ) field is empty as part of some business logic. I do not want to use the built in validation features.

If a CK Editor field has previously had text and then this text is deleted there is still content e.g.

<p dir="ltr">
 &nbsp;</p>

I can get a handle to this text string using :

dataVar = xspdoc.getDocument().getMIMEEntity(dataNamevar).getContentAsText();

Is there a way to test if the CKEditor field is empty of visible text ?


Solution

  • Technically speaking, if it has what amounts to a a single visible newline in it as you've shown in your question, it isn't really "empty".

    Realistically, you'll have to parse the content value to find out if there is content that is not either inside tags or the few special characters like   and so on.

    I tend to do this in js, if I have to, by taking the whole string of text and splitting it into an array based on "<" then taking each element of the array and removing an text to the left of an ">", then trim. That leaves me an array of either empty strings or text that is outside any tags. From there it's easy enough check for any of strings in the array to see if they are not empty, and not " ".

    This may be more cumbersome then some built in parser that I don't know, but it's fairly reliable and quick. (and a very similar method can be used in formula language as well).

    In ssjs formula you could:

    var checkString = @trim(@replacesubstring(@implode( @trim (@right( @explode( sourceHTMLstring , "<" ) , ">" ) ) , " "), "&nbsp;" , ""));
    
    if(checkstring == "") {
            //  *** You have no content 
    } else {
            // ***  you have content
    }
    

    Obviously this could be done just as easily in pure javascript, but the old formula language is so ingrained in my head, I'd go this way just out of habit.

    ** Also note: You may want to check for an <img> tag in there somewhere in case someone has done absolutely nothing other than put an image in the rich text.