Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Does anyone know exactly what ddwrt:EcmaScriptEncode does? (Which char mappings, etc.?)

M.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Well, basically it calls this method SPHttpUtility.EcmaScriptStringLiteralEncode. As far as that method goes, I'm not really sure. According to the msdn article, you can go to a website that explains what ecma is.

Or if this helps, here is the reflected code:

private static readonly ushort[] ScriptCharMap = new ushort[] { 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 3, 0, 0, 4, 5, 6, 7, 8, 0, 9, 0, 0, 0, 10, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 12, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0
     };
private static readonly string[] ScriptEncodedChars = new string[] { "", @"\n", @"\r", @"\u0022", @"\u0025", @"\u0026", @"\u0027", @"\u0028", @"\u0029", @"\u002b", @"\u002f", @"\u003c", @"\u003e", @"\\" };

public static void EcmaScriptStringLiteralEncode(string scriptLiteralToEncode, TextWriter output)
{
    int num;
    int num2;
    int length;
    int num4;
    if (scriptLiteralToEncode != null)
    {
        if (scriptLiteralToEncode.Length == 0)
        {
            return;
        }
        if (output != null)
        {
            num = 0;
            num2 = 0;
            length = scriptLiteralToEncode.Length;
            num4 = 0;
            goto Label_0058;
        }
    }
    return;
Label_0058:
    if (num4 < length)
    {
        int index = scriptLiteralToEncode[num4];
        if (index <= 0x7f)
        {
            ushort num5;
            if (index < 0x5f)
            {
                num5 = ScriptCharMap[index];
            }
            else
            {
                num5 = 0;
            }
            if (num5 <= 0)
            {
                num2++;
            }
            else
            {
                if (num2 > 0)
                {
                    output.Write(scriptLiteralToEncode.Substring(num, num2));
                    num2 = 0;
                }
                num = num4 + 1;
                output.Write(ScriptEncodedChars[num5]);
            }
        }
        else
        {
            if (num2 > 0)
            {
                output.Write(scriptLiteralToEncode.Substring(num, num2));
                num2 = 0;
            }
            num = num4 + 1;
            output.Write(@"\u");
            int num7 = index >> 8;
            if (num7 != 0)
            {
                if (num7 >= 0x10)
                {
                    output.Write(num7.ToString("X", CultureInfo.InvariantCulture));
                }
                else
                {
                    output.Write('0');
                    output.Write(num7.ToString("X", CultureInfo.InvariantCulture));
                }
            }
            else
            {
                output.Write("00");
            }
            num7 = index & 0xff;
            if (num7 >= 0x10)
            {
                output.Write(num7.ToString("X", CultureInfo.InvariantCulture));
            }
            else
            {
                output.Write('0');
                output.Write(num7.ToString("X", CultureInfo.InvariantCulture));
            }
        }
    }
    else
    {
        if (num < length)
        {
            output.Write(scriptLiteralToEncode.Substring(num));
        }
        return;
    }
    num4++;
    goto Label_0058;
}
share|improve this answer
    
So basicly if its one of the ones defined in ScriptEncodedChars (new-line, return, " (quote), %, &, ' (ampersand), (, ), +, /, <, >, ) it finds its value in ScriptCharMap and writes it out, if its a higher charmap value it is hex value is written out prefixed with \u (unicodeescape) and appropriate 0's –  Anders Rask Dec 23 '09 at 8:25
    
Thanks! I just didn't have the mental capacity to go through reflector code at 11:30 pm when I'm on Holiday. –  Steve Lineberry Dec 23 '09 at 12:32
    
Certainly a long string of code to do that! Thanks, guys. –  Marc D Anderson Dec 24 '09 at 16:27

To be honest I don't know; but judging by the name I wonder if perhaps it's nothing more than an xslt equivalent of the javascript escape() function?

share|improve this answer
    
Hmm. Maybe, but escape seems stronger. I can certainly just try passing in every ASCII character and see what I get back, I suppose. It's a shame that there is no documentation on the ddwrt: functions other thane the old post from Serge van den Oever: msdn.microsoft.com/en-us/library/dd583143(office.11).aspx –  Marc D Anderson Dec 23 '09 at 1:35
    
I agree, I wish there was more documentation. The best help I've had with it is that I reflected the class and refactored it to use in my own webparts. I learned a lot by doing that. I needed to use it so that people can use SP Designer to create xslt and then I can still transform that xslt in my webparts. Or just use some of the advanced xslt functionality it provides in the xslt I write. –  Steve Lineberry Dec 23 '09 at 12:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.