Esempio n. 1
0
 protected override void WritePropertyName(TextWriter writer, string propertyName)
 {
     if (EcmaScriptIdentifier.IsValidIdentifier(propertyName, false))
     {
         // write out without quoting
         writer.Write(propertyName);
     }
     else
     {
         // write out as an escaped string
         base.WritePropertyName(writer, propertyName);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Verifies is a valid EcmaScript variable expression
        /// </summary>
        /// <param name="ident">the identifier</param>
        /// <returns>identifier</returns>
        /// <remarks>
        /// http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
        ///
        /// IdentifierName =
        ///		IdentifierStart | IdentifierName IdentifierPart
        /// IdentifierStart =
        ///		Letter | '$' | '_'
        /// IdentifierPart =
        ///		IdentifierStart | Digit
        /// </remarks>
        public static bool IsValidIdentifier(string ident, bool nested)
        {
            if (String.IsNullOrEmpty(ident))
            {
                return(false);
            }

            if (nested)
            {
                string[] parts = ident.Split('.');
                foreach (string part in parts)
                {
                    if (!EcmaScriptIdentifier.IsValidIdentifier(part, false))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            if (EcmaScriptIdentifier.IsReservedWord(ident))
            {
                return(false);
            }

            bool indentPart = false;

            for (int i = 0, length = ident.Length; i < length; i++)
            {
                char ch = ident[i];
                if (indentPart && ((ch >= '0') && (ch <= '9')))
                {
                    // digits are only allowed after first char
                    continue;
                }

                // can be start or part
                if (((ch >= 'a') && (ch <= 'z')) ||
                    ((ch >= 'A') && (ch <= 'Z')) ||
                    (ch == '_') || (ch == '$'))
                {
                    indentPart = true;
                    continue;
                }

                return(false);
            }

            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Verifies is a valid EcmaScript identifier
        /// </summary>
        /// <param name="ident">the identifier</param>
        /// <returns>identifier</returns>
        public static string VerifyIdentifier(string ident, bool nested, bool throwOnEmpty)
        {
            if (String.IsNullOrEmpty(ident))
            {
                if (throwOnEmpty)
                {
                    throw new ArgumentException("Identifier is empty.");
                }
                return(String.Empty);
            }

            ident = ident.Replace(" ", "");

            if (!EcmaScriptIdentifier.IsValidIdentifier(ident, nested))
            {
                throw new ArgumentException("Identifier \"" + ident + "\" is not supported.");
            }

            return(ident);
        }