Ejemplo n.º 1
0
        public void InlineScriptConstructorTest()
        {
            var source = "var s = 'Hello World!';";
            var script = new InlineScript(source);

            Assert.AreEqual(source, script.Source);
        }
Ejemplo n.º 2
0
        public void InlineScriptGroupAddTest()
        {
            var scriptGroup = new InlineScriptGroup();

            var path    = "var s = 'Hello World!';";
            var script1 = new InlineScript(path);
            var script2 = new InlineScript(path);

            scriptGroup.Add(script1);

            try
            {
                scriptGroup.Add(script2);
            }
            catch (ResourceAlreadyAddedException)
            {
                return;
            }
            catch (Exception)
            {
                Assert.Fail("Incorrect exception thrown.");
            }

            Assert.Fail("No exception thrown.");
        }
		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			var o = JObject.Load(reader);
			var dict = o.Properties().ToDictionary(p => p.Name, p => p.Value);
			if (!dict.HasAny()) return null;

			IScript script = null;
			if (dict.ContainsKey("inline"))
			{
				var inline = dict["inline"].ToString();
				script = new InlineScript(inline);
			}
			if (dict.ContainsKey("file"))
			{
				var file = dict["file"].ToString();
				script = new FileScript(file);
			}
			if (dict.ContainsKey("id"))
			{
				var id = dict["id"].ToString();
				script = new IndexedScript(id);
			}

			if (script == null) return null;

			if (dict.ContainsKey("lang"))
				script.Lang = dict["lang"].ToString();
			if (dict.ContainsKey("params"))
				script.Params = dict["params"].ToObject<Dictionary<string, object>>();

			return script;
		}
Ejemplo n.º 4
0
        public void InlineEqualsInlineTest2()
        {
            var script1 = new InlineScript("var s = 'Hello World!';");
            var script2 = new InlineScript("var s = 'Hello Earth!';");

            Assert.IsFalse(script1.Equals(script2));
            Assert.IsFalse(script2.Equals(script1));
        }
Ejemplo n.º 5
0
        public void LinkedEqualsInlineTest()
        {
            var script1 = new LinkedScript("path/to/script");
            var script2 = new InlineScript("path/to/script");

            Assert.IsFalse(script1.Equals(script2));
            Assert.IsFalse(script2.Equals(script1));
        }
Ejemplo n.º 6
0
        public void LinkedEqualsInlineTest2()
        {
            var script1 = new LinkedScript("path/to/script");
            var script2 = new InlineScript("path/to/script");

            var obj = (object)script2;

            Assert.IsFalse(script1.Equals(obj));
            Assert.IsFalse(obj.Equals(script1));
        }
Ejemplo n.º 7
0
        private Task Evaluate(Expression Script, Variables Variables, string Id)
        {
            object Result = this.Evaluate(Script, Variables);

            StringBuilder Html = new StringBuilder();

            InlineScript.GenerateHTML(Result, Html, true, Variables);

            return(ClientEvents.ReportAsynchronousResult(Id, "text/html; charset=utf-8", Encoding.UTF8.GetBytes(Html.ToString())));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Generates Xamarin.Forms XAML for the markdown element.
        /// </summary>
        /// <param name="Output">XAML will be output here.</param>
        /// <param name="TextAlignment">Alignment of text in element.</param>
        /// <param name="Rows">Code rows.</param>
        /// <param name="Language">Language used.</param>
        /// <param name="Indent">Additional indenting.</param>
        /// <param name="Document">Markdown document containing element.</param>
        /// <returns>If content was rendered. If returning false, the default rendering of the code block will be performed.</returns>
        public bool GenerateXamarinForms(XmlWriter Output, TextAlignment TextAlignment, string[] Rows, string Language, int Indent, MarkdownDocument Document)
        {
            Expression Script    = this.BuildExpression(Rows);
            Variables  Variables = Document.Settings.Variables;
            object     Result    = this.Evaluate(Script, Variables);

            InlineScript.GenerateXamarinForms(Result, Output, TextAlignment, true, Variables, Document.Settings.XamlSettings);

            return(true);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Generates Plain Text for the markdown element.
        /// </summary>
        /// <param name="Output">HTML will be output here.</param>
        /// <param name="Rows">Code rows.</param>
        /// <param name="Language">Language used.</param>
        /// <param name="Indent">Additional indenting.</param>
        /// <param name="Document">Markdown document containing element.</param>
        /// <returns>If content was rendered. If returning false, the default rendering of the code block will be performed.</returns>
        public bool GeneratePlainText(StringBuilder Output, string[] Rows, string Language, int Indent, MarkdownDocument Document)
        {
            Expression Script    = this.BuildExpression(Rows);
            Variables  Variables = Document.Settings.Variables;
            object     Result    = this.Evaluate(Script, Variables);

            InlineScript.GeneratePlainText(Result, Output, true);

            return(true);
        }
Ejemplo n.º 10
0
 private static void AssertInlineScript(InlineScript inlineScript)
 {
     inlineScript.Language.Should().Be(ScriptLanguage.Painless);
     inlineScript.Source.Should().Be("doc['field_name'].value * params.factor");
     inlineScript.Options.Should().HaveCount(1);
     inlineScript.Options.TryGetValue("option0", out var optionValue).Should().BeTrue();
     optionValue.Should().Be("option_0");
     inlineScript.Params.Should().HaveCount(1);
     inlineScript.Params.TryGetValue("factor", out var factor).Should().BeTrue();
     factor.Should().Be(1.1);
 }
Ejemplo n.º 11
0
        public void InlineScriptGroupRegisterTest()
        {
            var scriptGroup = new InlineScriptGroup();

            var path    = "var s = 'Hello World!';";
            var script1 = new InlineScript(path);
            var script2 = new InlineScript(path);

            scriptGroup.Register(script1);
            scriptGroup.Register(script2);

            Assert.AreEqual(1, scriptGroup.Count);
        }
    public async Task SerializesRawJson()
    {
        // In this test, we expect the nulls to be ignore when the setting is disabled so the request response serializer is used.

        var script = new InlineScript("source")
        {
            Params = new System.Collections.Generic.Dictionary <string, object> {
                { "person", new RawJsonString(@"{ ""forename"": ""raw_json"" }") }
            }
        };

        var json = SerializeAndGetJsonString(script);

        await Verifier.VerifyJson(json);
    }
Ejemplo n.º 13
0
        public void InlineScriptRenderTest()
        {
            var source = "var s = 'Hello World!';";
            var script = new InlineScript(source);

            var render   = script.Render();
            var expected = "<script type=\"text/javascript\">";

            expected += Environment.NewLine + "// <![CDATA[";
            expected += Environment.NewLine + source;
            expected += Environment.NewLine + "// ]]>";
            expected += Environment.NewLine + "</script>";

            Assert.AreEqual(expected, render);
        }
Ejemplo n.º 14
0
        public void InlineScriptGroupHasRegisteredTest()
        {
            var scriptGroup = new InlineScriptGroup();

            var path    = "var s = 'Hello World!';";
            var script1 = new InlineScript(path);
            var script2 = new InlineScript(path);

            Assert.IsFalse(scriptGroup.HasRegistered(script1));
            Assert.IsFalse(scriptGroup.HasRegistered(script2));

            scriptGroup.Register(script1);

            Assert.IsTrue(scriptGroup.HasRegistered(script1));
            Assert.IsTrue(scriptGroup.HasRegistered(script2));
        }
    public async Task SerializesParamsUsingRequestResponseSerializer_WhenUseSourceSerializerForScriptParameters_IsFalse()
    {
        // In this test, we expect the nulls to be ignore when the setting is disabled so the request response serializer is used.

        var script = new InlineScript("source")
        {
            Params = new System.Collections.Generic.Dictionary <string, object> {
                { "person", new Person {
                      Forename = "has_null_surname", Surname = null
                  } }
            }
        };

        var json = SerializeAndGetJsonString(script);

        await Verifier.VerifyJson(json);
    }
    public async Task SerializesParamsUsingRequestResponseSerializer_WhenUseSourceSerializerForScriptParameters_IsTrue()
    {
        // In this test, we expect the null to be serialized by the default JsonNetSerializer

        var script = new InlineScript("source")
        {
            Params = new System.Collections.Generic.Dictionary <string, object> {
                { "person", new Person {
                      Forename = "has_null_surname", Surname = null
                  } }
            }
        };

        var json = SerializeAndGetJsonString(script, new ElasticsearchClientSettings(new SingleNodePool(new Uri("http://localhost:9200")), sourceSerializer: JsonNetSerializer.Default)
                                             .Experimental(new ExperimentalSettings {
            UseSourceSerializerForScriptParameters = true
        }));

        await Verifier.VerifyJson(json);
    }
Ejemplo n.º 17
0
    /// <summary>
    /// Registers the inline script
    /// </summary>
    private void RegisterInlineScript()
    {
        // Render the inline script
        if (InlineScript.Trim() != string.Empty)
        {
            string inlineScript = InlineScript;

            // Check if script tags must be generated
            if (GenerateScriptTags && (InlineScriptPageLocation.ToLowerCSafe() != "submit"))
            {
                inlineScript = ScriptHelper.GetScript(InlineScript);
            }

            // Switch for script position on the page
            switch (InlineScriptPageLocation.ToLowerCSafe())
            {
            case "header":
                Page.Header.Controls.Add(new LiteralControl(inlineScript));
                break;

            case "beginning":
                ScriptHelper.RegisterClientScriptBlock(Page, typeof(string), ClientID + "inlinescript", inlineScript);
                break;

            case "startup":
                ScriptHelper.RegisterStartupScript(Page, typeof(string), ClientID + "inlinescript", inlineScript);
                break;

            case "submit":
                ScriptHelper.RegisterOnSubmitStatement(Page, typeof(string), ClientID + "inlinescript", inlineScript);
                break;

            case "inline":
            default:
                ltlInlineScript.Text = inlineScript;
                break;
            }
        }
    }
Ejemplo n.º 18
0
        public void InlineScriptGroupRenderTest()
        {
            var scriptGroup = new InlineScriptGroup();

            var source1 = "var s = 'Hello World!';";
            var source2 = "var r = 'Hello Earth!';";
            var script1 = new InlineScript(source1);
            var script2 = new InlineScript(source2);

            scriptGroup.Add(script1);
            scriptGroup.Add(script2);

            var expected = "<script type=\"text/javascript\">" + Environment.NewLine;

            expected += "// <![CDATA[" + Environment.NewLine;
            expected += source1 + Environment.NewLine;
            expected += source2 + Environment.NewLine;
            expected += "// ]]>" + Environment.NewLine;
            expected += "</script>";

            var render = scriptGroup.Render();

            Assert.AreEqual(expected, render);
        }
Ejemplo n.º 19
0
 public void SetUp()
 {
     _tag = new InlineScript(_script);
 }
Ejemplo n.º 20
0
    /// <summary>
    /// Initializes the control properties.
    /// </summary>
    protected void SetupControl()
    {
        if (StopProcessing)
        {
            // Do nothing
        }
        else
        {
            // Include javascript only in live site or preview mode
            if ((CMSContext.ViewMode == ViewModeEnum.LiveSite) || (CMSContext.ViewMode == ViewModeEnum.Preview))
            {
                // Render the inline script
                if (InlineScript.Trim() != string.Empty)
                {
                    string inlineScript = InlineScript;

                    // Check if script tags must be generated
                    if (GenerateScriptTags && (InlineScriptPageLocation.ToLower() != "submit"))
                    {
                        inlineScript = ScriptHelper.GetScript(InlineScript);
                    }
                    // Switch for script position on the page
                    switch (InlineScriptPageLocation.ToLower())
                    {
                    case "header":
                        Page.Header.Controls.Add(new LiteralControl(inlineScript));
                        break;

                    case "beginning":
                        ScriptHelper.RegisterClientScriptBlock(Page, typeof(string), ClientID + "inlinescript", inlineScript);
                        break;

                    case "inline":
                        ltlInlineScript.Text = inlineScript;
                        break;

                    case "startup":
                        ScriptHelper.RegisterStartupScript(Page, typeof(string), ClientID + "inlinescript", inlineScript);
                        break;

                    case "submit":
                        ScriptHelper.RegisterOnSubmitStatement(Page, typeof(string), ClientID + "inlinescript", inlineScript);
                        break;

                    default:
                        ltlInlineScript.Text = inlineScript;
                        break;
                    }
                }

                // Create linked js file
                if (LinkedFile.Trim() != string.Empty)
                {
                    string linkedFile = ScriptHelper.GetIncludeScript(ResolveUrl(LinkedFile));

                    // Switch for script position on the page
                    switch (LinkedFilePageLocation.ToLower())
                    {
                    case "header":
                        Page.Header.Controls.Add(new LiteralControl(linkedFile));
                        break;

                    case "beginning":
                        ScriptHelper.RegisterClientScriptBlock(Page, typeof(string), ClientID + "script", linkedFile);
                        break;

                    case "startup":
                        ScriptHelper.RegisterStartupScript(Page, typeof(string), ClientID + "script", linkedFile);
                        break;

                    default:
                        Page.Header.Controls.Add(new LiteralControl(linkedFile));
                        break;
                    }
                }
            }
        }
    }