editor.pasteHtml stripping whitespace

6 posts, 1 answers
  1. Lyle
    Lyle avatar
    3 posts
    Member since:
    Oct 2012

    Posted 07 May 2012 Link to this post

    Hello,

    Javascript such as this:

     var editor = $find("RadEditor1"); 
     editor.pasteHtml('test    test');


    Is stripping all the extra consecutive spaces between the two words test. Anyway to preserve the extra spaces?
  2. Rumen
    Admin
    Rumen avatar
    14417 posts

    Posted 10 May 2012 Link to this post

    Hello,

    My suggestion is to replace the empty spaces with   or use

    editor.setFocus()
    editor.getSelection().getRange().pasteHTML('test    test');

    to paste the content.

    Kind regards,
    Rumen
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. Lyle
    Lyle avatar
    3 posts
    Member since:
    Oct 2012

    Posted 11 May 2012 Link to this post

    Hello, thank for you the suggestions. However neither method will work because both introduce   into the html file.

    Let me try to explain the situation further. We use Radeditor to allow our customers to design their own HTML templates for some documents. In these documents, we have what we call tokens. The token is processed via string replacement, very much like a mail merge.

    Before the HTML document is sent to the browser, these tokens are replaced with actual data.

    The problem is when the token has consecutive spaces. The original token in the database and what is used for lookups contains a double space. The HTML document does not preserve that double space and so the token is ignored and the token is visible to the user and users complain. The PasteHTML javascript function seems to massage the text string before actually adding it to the document. I need an PasteText function that adds the text verbatim.
     
    I will try replacing all spaces with   and maybe if I do that during token replacement as well, that may solve my problem. However a much more direct solution (and is what I have been doing manually) is to reintroduce the double space into the HTML document. As long as nobody tries to re-write the HTML document and re-create the token, the double space is preserved.

    I realize the implementation of tokens inside an HTML document is probably not ideal. We are striving to refactor that as well, however it is a low priority since the current implementation works for 99% of cases. Another fix would be to prevent double space from being introduced into the source token text.

    PS:

    After working on this some more, i'm running into more unexpected behavior from PasteHTML. This is the exact string I have been struggling with:
     
    [[Indicate the method used to test temper water chlorination.  If system not installed, type “Chlorination system not Installed”:1013:4407]]

    When this string is inserted via PasteHTML, the double space between the period and If is converted into a single space. Plus the quotes are converted into “ and ”. This alone is fine, however when I'm processing these tokens, HttpUtility.HTMLEncode is NOT converting the quotes into their “ and ” equivlants.

    I think my question has transformed into:

    What is the difference between PasteHTML's encoding routine versus ASP.NET 4.0 HttpUtility.HTMLEncode?
  4. Lyle
    Lyle avatar
    3 posts
    Member since:
    Oct 2012

    Posted 14 May 2012 Link to this post

    After stepping through the Telerik javascript,

    The culprit seems to be a function called getStoredOriginalPathsAndAttributes. It runs some regex against my string that is removing double space, and a couple other things. Hard to tell because its obfuscated.

    If I could call these functions from server side code, that might work as well. I just need a way to re-encode my string in the same style as PasteHTML but from my code behind.

    Further Info:

    I see now this is a problem that is browser specific, as Firefox correctly pastes the double space into the document. I think my only solution is to validate input much sooner in the process, and not allow double spaces and the other characters that may cause a problem here.
  5. Answer
    Rumen
    Admin
    Rumen avatar
    14417 posts

    Posted 16 May 2012 Link to this post

    Hi,

    The function that are looking for restoreOriginalPathsAndAttributes is located in the RadEditor\0EditorUtils.js source code file and you can overwrite and modify its regular expressions using the code below:

    <telerik:RadEditor ID="RadEditor1" Runat="server"></telerik:RadEditor>
    <script type="text/javascript">
        Telerik.Web.UI.Editor.Utils.getStoredOriginalPathsAndAttributes = function (content) {
            alert(1);
            var matchEvaluator = function (match, g1, g2, g3, g4, g5, g6, matchIndex, wholeText) {
                //"$1 $2=$3$4$3 originalAttribute=\"$2\" originalPath=\"$4\"$6"
                if (!g3) {
                    g3 = "";
                    g4 = g4 + g6;
                    //find first space or > and break g4
                    var indmatch = g4.search(/(\s|>)/gi);
                    if (indmatch > 0) {
                        g6 = g4.substring(indmatch, g4.length);
                        g4 = g4.substring(0, indmatch);
                        if (g4 == "\"\"") {
                            //handle case when href/src is empty
                            g4 = "";
                            g3 = "\"";
                        }
                    }
                    else {
     
                        return match;
                    }
                }
                return g1 + " " + g2 + "=" + g3 + g4 + g3 + " originalAttribute=\"" + g2 + "\" originalPath=\"" + g4 + "\"" + g6;
            };
            var pathsRegExp = new RegExp("(<[^>]*?)\\s(href|src)\\s*=\\s*('|\")?([^>]+?)(\\3)([^>]*?>)", "ig");
            content = content.replace(pathsRegExp, matchEvaluator);
            //do not touch src and hrefs that are inside comments (saved tags)
            var commentsRegExp = new RegExp("(<!--[\\s\\S]*?) originalAttribute=\"(?:href|src)\" originalPath=\"[^\"]+\"([\\s\\S]*?-->)", "ig");
            var contentLength = content.length + 1;
            while (content.length < contentLength) {
                contentLength = content.length;
                content = content.replace(commentsRegExp, "$1$2");
            }
            return content;
        }
    </script>



    Kind regards,
    Rumen
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  6. vikas
    vikas avatar
    11 posts
    Member since:
    Dec 2008

    Posted 17 May 2012 Link to this post

    dear telerik
Back to Top