public new static string Serialize(object value)
        {
            StringBuilder stringBuilder = new StringBuilder();

            using (EcmaScriptWriter ecmaScriptWriter = new EcmaScriptWriter(stringBuilder))
            {
                ecmaScriptWriter.Write(value);
            }
            return(stringBuilder.ToString());
        }
Пример #2
0
        public new static string Serialize(object value)
        {
            StringBuilder output = new StringBuilder();

            using (EcmaScriptWriter writer = new EcmaScriptWriter(output))
            {
                writer.Write(value);
            }
            return(output.ToString());
        }
 protected override void Write(object value, bool isProperty)
 {
     if (value is Regex)
     {
         if (isProperty && base.Settings.PrettyPrint)
         {
             base.TextWriter.Write(' ');
         }
         EcmaScriptWriter.WriteEcmaScriptRegExp(this, (Regex)value);
         return;
     }
     base.Write(value, isProperty);
 }
Пример #4
0
 /// <summary>
 /// Outputs a .NET Regex as an ECMAScript RegExp literal.
 /// Defaults to global matching off.
 /// </summary>
 /// <param name="writer"></param>
 /// <param name="regex"></param>
 /// <remarks>
 /// http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
 /// </remarks>
 public static void WriteEcmaScriptRegExp(JsonWriter writer, Regex regex)
 {
     EcmaScriptWriter.WriteEcmaScriptRegExp(writer, regex, false);
 }
Пример #5
0
 /// <summary>
 /// Writes dates as ECMAScript Date constructors
 /// </summary>
 /// <param name="value"></param>
 public override void Write(DateTime value)
 {
     EcmaScriptWriter.WriteEcmaScriptDate(this, value);
 }
Пример #6
0
        public void Render(TextWriter writer)
        {
            this.ProcessDirectives();

            // add JSLINT directives
            string globals = this.GetGlobals();
            if (!String.IsNullOrEmpty(globals))
            {
                writer.WriteLine("/*global {0} */", globals);
            }

            if (!EcmaScriptWriter.WriteNamespaceDeclaration(writer, this.JbstName, null, true))
            {
                writer.Write("var ");
            }

            // wrap with ctor and assign
            writer.Write(this.JbstName);
            writer.WriteLine(" = JsonML.BST(");

            // render root node of content (null is OK)
            EcmaScriptWriter jsWriter = new EcmaScriptWriter(writer);
            jsWriter.Settings.PrettyPrint = true;
            jsWriter.Write(this.JbstParseTree);

            writer.WriteLine(");");

            // render any declarations
            if (this.Declarations.HasCode)
            {
                this.Declarations.OwnerName = this.JbstName;
                jsWriter.Write(this.Declarations);
            }
        }
		/// <summary>
		/// A helper method for serializing an object to EcmaScript
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static new string Serialize(object value)
		{
			StringBuilder output = new StringBuilder();

			using (EcmaScriptWriter writer = new EcmaScriptWriter(output))
			{
				writer.Write(value);
			}

			return output.ToString();
		}
Пример #8
0
        /// <summary>
        /// Renders the data items as a block of JavaScript
        /// </summary>
        /// <param name="writer"></param>
        public void Write(TextWriter writer, IDictionary<string, object> data)
        {
            if (data == null || data.Count < 1)
            {
                // emit nothing when empty
                return;
            }

            List<string> namespaces = new List<string>();

            StringWriter markup;
            EcmaScriptWriter jsWriter;
            if (this.AutoMarkup == AutoMarkupType.Data)
            {
                markup = new StringWriter();
                jsWriter = new JsonMarkupWriter(writer, markup);
            }
            else
            {
                markup = null;
                jsWriter = new EcmaScriptWriter(writer);
            }

            if (this.IsDebug)
            {
                jsWriter.Settings.PrettyPrint = true;
                jsWriter.Settings.NewLine = Environment.NewLine;
                jsWriter.Settings.Tab = "\t";
            }

            writer.Write(DataBlockWriter.ScriptOpen);

            foreach (string key in data.Keys)
            {
                if (markup != null)
                {
                    if (this.IsDebug)
                    {
                        markup.WriteLine();
                    }
                    markup.Write("<div title=\"");
                    HttpUtility.HtmlAttributeEncode(key, markup);
                    markup.Write("\">");
                }

                string declaration;
                if (!EcmaScriptWriter.WriteNamespaceDeclaration(writer, key, namespaces, this.IsDebug))
                {
                    declaration = "var "+key;
                }
                else
                {
                    declaration = key;
                }

                if (this.IsDebug)
                {
                    writer.Write(DataBlockWriter.VarAssignmentDebug, declaration);
                    if (data[key] != null &&
                        data[key].GetType().IsClass)
                    {
                        writer.WriteLine();
                    }
                }
                else
                {
                    writer.Write(DataBlockWriter.VarAssignment, declaration);
                }

                // emit the value as JSON
                jsWriter.Write(data[key]);
                writer.Write(DataBlockWriter.VarAssignmentEnd);

                if (markup != null)
                {
                    markup.Write("</div>");
                }

                if (this.IsDebug)
                {
                    writer.WriteLine();
                }
            }

            writer.Write(DataBlockWriter.ScriptClose);

            if (markup != null)
            {
                writer.Write(DataBlockWriter.NoScriptOpen);
                writer.Write(markup.ToString());
                writer.Write(DataBlockWriter.NoScriptClose);
            }
        }
        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            if (this.IsDebug)
            {
                context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            }
            else
            {
                // TODO: make this configurable (default to min-value YSlow! considers useful)
                // Note: Google Page Speed wants 1 month
                context.Response.ExpiresAbsolute = DateTime.UtcNow.AddDays(3);
            }

            string userCulture = context.Request.QueryString[ResourceHandler.GlobalizationQuery];
            if (userCulture != null && userCulture.Length > 1)
            {
                try
                {
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(userCulture);
                }
                catch { }
                try
                {
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(userCulture);
                }
                catch { }
            }

            // get the target
            string targetPath = context.Request.AppRelativeCurrentExecutionFilePath;

            IGlobalizedBuildResult target = BuildManager.CreateInstanceFromVirtualPath(targetPath, typeof(IGlobalizedBuildResult)) as IGlobalizedBuildResult;
            if (target == null)
            {
                return;
            }

            IDictionary<string, object> res = this.GetResourceStrings(target.GlobalizationKeys, targetPath);

            if (!this.IsDebug)
            {
                // TODO: provide a mechanism for disabling compression?
                ResourceHandler.EnableStreamCompression(context);
            }

            HttpResponse response = context.Response;
            response.ContentType = ScriptResourceCodeProvider.MimeType;

            response.AppendHeader(
                "Content-Disposition",
                "inline;filename="+Path.GetFileNameWithoutExtension(targetPath)+".js");

            if (this.IsDebug)
            {
                response.Write(JslintDirective);
            }

            if (res.Count < 1)
            {
                // don't output call
                return;
            }

            response.Write(ResStart);

            EcmaScriptWriter writer = new EcmaScriptWriter(response.Output);
            writer.Settings.PrettyPrint = this.IsDebug;
            writer.Write(res);

            response.Write(",");

            writer = new EcmaScriptWriter(response.Output);
            writer.Settings.PrettyPrint = this.IsDebug;
            writer.Write(Thread.CurrentThread.CurrentCulture.Name);

            response.Write(ResEnd);
        }
Пример #10
0
        /// <summary>
        /// Renders the JBST control reference and any stored data to be used.
        /// </summary>
        /// <param name="writer">output</param>
        /// <param name="data">data to be bound as an object which will be serialized</param>
        /// <param name="index">the data index</param>
        /// <param name="count">the total data count</param>
        /// <param name="inner">a callback for writing inner placeholder content</param>
        internal void Write(TextWriter writer, object data, int index, int count, InnerCallback inner)
        {
            if (String.IsNullOrEmpty(this.JbstName))
            {
                throw new ArgumentNullException("JBST Name must be specified.");
            }

            // generate an ID for controls which do not have explicit
            if (String.IsNullOrEmpty(this.ID))
            {
                // happens with no parents
                this.ID = "_"+Guid.NewGuid().ToString("n");
            }

            bool hasInner = (inner != null);
            string placeholder = hasInner ? "div" : "noscript";

            // render the placeholder hook
            writer.Write('<');
            writer.Write(placeholder);
            writer.Write(" id=\"");
            writer.Write(this.ID);
            writer.Write("\">");

            if (hasInner)
            {
                // render inner as loading/error markup
                inner(writer);
            }

            string inlineData = null;
            if (data != null && !(data is EcmaScriptIdentifier) && this.AutoMarkup == AutoMarkupType.Data)
            {
                if (hasInner)
                {
                    writer.Write("<noscript>");
                }

                // serialize InlineData as a JavaScript literal
                StringBuilder builder = new StringBuilder();

                JsonMarkupWriter jsWriter = new JsonMarkupWriter(builder, writer);
                if (this.IsDebug)
                {
                    jsWriter.Settings.PrettyPrint = true;
                    jsWriter.Settings.NewLine = Environment.NewLine;
                    jsWriter.Settings.Tab = "\t";
                }
                jsWriter.Write(data);

                if (hasInner)
                {
                    writer.Write("</noscript>");
                }

                inlineData = builder.ToString();
            }

            writer.Write("</");
            writer.Write(placeholder);
            writer.Write('>');

            // render the binding script
            writer.Write("<script type=\"text/javascript\">");

            writer.Write(this.JbstName);
            writer.Write(".replace(\"");
            writer.Write(this.ID);
            writer.Write("\",");

            if (!String.IsNullOrEmpty(inlineData))
            {
                writer.Write(inlineData);
            }
            else if (data != null)
            {
                // serialize InlineData as a JavaScript literal
                EcmaScriptWriter jsWriter = new EcmaScriptWriter(writer);
                if (this.IsDebug)
                {
                    jsWriter.Settings.PrettyPrint = true;
                    jsWriter.Settings.NewLine = Environment.NewLine;
                    jsWriter.Settings.Tab = "\t";
                }
                jsWriter.Write(data);
            }
            else
            {
                // smallest most innocuous default data
                writer.Write("{}");
            }

            if (index >= 0)
            {
                writer.Write(",(");
                writer.Write(index);
                writer.Write(')');

                if (count >= 0)
                {
                    writer.Write(",(");
                    writer.Write(count);
                    writer.Write(')');
                }
            }
            writer.Write(");");

            writer.Write("</script>");
        }