Exemplo n.º 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
        }
Exemplo n.º 2
0
        public override bool Equals(object obj)
        {
            EcmaScriptIdentifier identifier = obj as EcmaScriptIdentifier;

            if (identifier == null)
            {
                return(base.Equals(obj));
            }
            return((string.IsNullOrEmpty(this.identifier) && string.IsNullOrEmpty(identifier.identifier)) || StringComparer.Ordinal.Equals(this.identifier, identifier.identifier));
        }
 protected override void WriteObjectPropertyName(string name)
 {
     if (EcmaScriptIdentifier.IsValidIdentifier(name, false))
     {
         base.TextWriter.Write(name);
     }
     else
     {
         base.WriteObjectPropertyName(name);
     }
 }
Exemplo n.º 4
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);
     }
 }
        public override bool Equals(object obj)
        {
            EcmaScriptIdentifier ecmaScriptIdentifier = obj as EcmaScriptIdentifier;

            if (ecmaScriptIdentifier == null)
            {
                return(base.Equals(obj));
            }
            if (string.IsNullOrEmpty(identifier) && string.IsNullOrEmpty(ecmaScriptIdentifier.identifier))
            {
                return(true);
            }
            return(StringComparer.Ordinal.Equals(identifier, ecmaScriptIdentifier.identifier));
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Bind the JBST to the provided data.
        /// </summary>
        /// <param name="jbstName"></param>
        /// <param name="dataName">named data to bind</param>
        /// <param name="dataItems">collection of data to emit</param>
        /// <returns></returns>
        public static string Bind(EcmaScriptIdentifier jbstName, EcmaScriptIdentifier dataName, IDictionary<string, object> dataItems)
        {
            StringWriter writer = new StringWriter();
            JbstBuildResult jbst = JbstBuildResult.FindJbst(jbstName);
            jbst.IsDebug = IsDebug;

            if (dataItems != null)
            {
                // render data block
                new DataBlockWriter(jbst.AutoMarkup, jbst.IsDebug).Write(writer, dataItems);
            }

            // render the JBST
            jbst.Write(writer, dataName);

            return writer.ToString();
        }
Exemplo n.º 8
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));
        }
Exemplo n.º 9
0
        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(" ", string.Empty);
            if (!EcmaScriptIdentifier.IsValidIdentifier(varExpr, nested))
            {
                throw new ArgumentException("Variable expression \"" + varExpr + "\" is not supported.");
            }

            return(varExpr);
        }
Exemplo n.º 10
0
        public static bool IsValidIdentifier(string varExpr, bool nested)
        {
            if (string.IsNullOrEmpty(varExpr))
            {
                return(false);
            }

            if (nested)
            {
                string str     = varExpr;
                char[] chArray = new char[1] {
                    '.'
                };
                foreach (string varExpr1 in str.Split(chArray))
                {
                    if (!EcmaScriptIdentifier.IsValidIdentifier(varExpr1, false))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            if (EcmaScriptIdentifier.IsReservedWord(varExpr))
            {
                return(false);
            }

            bool flag = false;

            foreach (char c in varExpr)
            {
                if (!flag || !char.IsDigit(c))
                {
                    if (!char.IsLetter(c) && c != '_' && c != '$')
                    {
                        return(false);
                    }

                    flag = true;
                }
            }
            return(true);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Bind the JBST to the provided data.
        /// </summary>
        /// <param name="jbstName"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Bind(EcmaScriptIdentifier jbstName, object data)
        {
            StringWriter writer = new StringWriter();

            // render the JBST
            JbstBuildResult jbst = JbstBuildResult.FindJbst(jbstName);
            jbst.IsDebug = IsDebug;
            jbst.Write(writer, data);

            return writer.ToString();
        }
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="writer"></param>
 public SimpleJbstBuildResult(EcmaScriptIdentifier jbstName, AutoMarkupType autoMarkup)
     : base(jbstName, autoMarkup)
 {
 }
Exemplo n.º 13
0
 public EcmaScriptIdentifier(string ident)
 {
     identifier = !string.IsNullOrEmpty(ident) ? EcmaScriptIdentifier.EnsureValidIdentifier(ident, true) : string.Empty;
 }
Exemplo n.º 14
0
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="jsonName"></param>
 public JsonNameAttribute(string jsonName)
 {
     this.jsonName = EcmaScriptIdentifier.EnsureValidIdentifier(jsonName, false);
 }
Exemplo n.º 15
0
 public EcmaScriptIdentifier(string ident)
 {
     this.identifier = ((!string.IsNullOrEmpty(ident)) ? EcmaScriptIdentifier.EnsureValidIdentifier(ident, true) : string.Empty);
 }
Exemplo n.º 16
0
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="jbstName"></param>
 /// <param name="autoMarkup"></param>
 public JbstBuildResult(EcmaScriptIdentifier jbstName, AutoMarkupType autoMarkup)
 {
     this.jbstName = jbstName;
     this.autoMarkup = autoMarkup;
 }
Exemplo n.º 17
0
 /// <summary>
 /// Ctor
 /// </summary>
 public EcmaScriptIdentifier(string ident)
 {
     this.identifier = String.IsNullOrEmpty(ident) ? String.Empty :
                       EcmaScriptIdentifier.EnsureValidIdentifier(ident, true);
 }
Exemplo n.º 18
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));
 }
Exemplo n.º 19
0
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="jbstName"></param>
 public JbstBuildResult(EcmaScriptIdentifier jbstName)
     : this(jbstName, AutoMarkupType.None)
 {
 }