예제 #1
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="jsonName"></param>
        public JsonNameAttribute(string jsonName)
        {
#if !NO_ECMASCRIPT
            this.jsonName = EcmaScriptIdentifier.EnsureValidIdentifier(jsonName, false);
#else
            this.jsonName = jsonName;
#endif
        }
예제 #2
0
 protected override void WriteObjectPropertyName(string name)
 {
     if (EcmaScriptIdentifier.IsValidIdentifier(name, false))
     {
         // write out without quoting
         this.TextWriter.Write(name);
     }
     else
     {
         // write out as an escaped string
         base.WriteObjectPropertyName(name);
     }
 }
예제 #3
0
        /// <summary>
        /// Verifies is a valid EcmaScript variable expression.
        /// </summary>
        /// <param name="varExpr">the variable expression</param>
        /// <returns>varExpr</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 varExpr, bool nested)
        {
            if (String.IsNullOrEmpty(varExpr))
            {
                return(false);
            }

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

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

            bool indentPart = false;

            foreach (char ch in varExpr)
            {
                if (indentPart && Char.IsDigit(ch))
                {
                    // digits are only allowed after first char
                    continue;
                }

                // can be start or part
                if (Char.IsLetter(ch) || ch == '_' || ch == '$')
                {
                    indentPart = true;
                    continue;
                }

                return(false);
            }

            return(true);
        }
예제 #4
0
        /// <summary>
        /// Compares the identifiers.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            EcmaScriptIdentifier that = obj as EcmaScriptIdentifier;

            if (that == null)
            {
                return(base.Equals(obj));
            }

            if (String.IsNullOrEmpty(this.identifier) && String.IsNullOrEmpty(that.identifier))
            {
                // null and String.Empty are equivalent
                return(true);
            }

            return(StringComparer.Ordinal.Equals(this.identifier, that.identifier));
        }
예제 #5
0
        /// <summary>
        /// Ensures is a valid EcmaScript variable expression.
        /// </summary>
        /// <param name="varExpr">the variable expression</param>
        /// <returns>varExpr</returns>
        public static string EnsureValidIdentifier(string varExpr, bool nested, bool throwOnEmpty)
        {
            if (String.IsNullOrEmpty(varExpr))
            {
                if (throwOnEmpty)
                {
                    throw new ArgumentException("Variable expression is empty.");
                }
                return(String.Empty);
            }

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

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

            return(varExpr);
        }
예제 #6
0
 /// <summary>
 /// Ensures is a valid EcmaScript variable expression.
 /// </summary>
 /// <param name="varExpr">the variable expression</param>
 /// <returns>varExpr</returns>
 public static string EnsureValidIdentifier(string varExpr, bool nested)
 {
     return(EcmaScriptIdentifier.EnsureValidIdentifier(varExpr, nested, true));
 }
예제 #7
0
 /// <summary>
 /// Ctor
 /// </summary>
 public EcmaScriptIdentifier(string ident)
 {
     this.identifier = String.IsNullOrEmpty(ident) ? String.Empty :
                       EcmaScriptIdentifier.EnsureValidIdentifier(ident, true);
 }
예제 #8
0
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="jsonName"></param>
 public JsonNameAttribute(string jsonName)
 {
     this.jsonName = EcmaScriptIdentifier.EnsureValidIdentifier(jsonName, false);
 }