示例#1
0
        public void ScriptContent_type_is_formatted()
        {
            var script   = new ScriptContent("alert('hello');");
            var mimeType = Formatter.PreferredMimeTypeFor(script.GetType());

            var formattedValue = new FormattedValue(
                mimeType,
                script.ToDisplayString(mimeType));

            formattedValue.MimeType.Should().Be("text/html");
            formattedValue.Value.Should().Be(@"<script type=""text/javascript"">alert('hello');</script>");
        }
示例#2
0
        public void JArray_is_formatted_as_application_json()
        {
            JArray obj = JArray.FromObject(new object[] { "one", 1 });

            var mimeType = Formatter.PreferredMimeTypeFor(obj.GetType());

            mimeType.Should().Be(JsonFormatter.MimeType);

            obj.ToDisplayString(JsonFormatter.MimeType)
            .Should()
            .Be(@"[""one"",1]");
        }
示例#3
0
        public void MathString_type_is_formatted()
        {
            var latex = new MathString(@"F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx");

            var mimeType = Formatter.PreferredMimeTypeFor(latex.GetType());

            var formattedValue = new FormattedValue(
                mimeType,
                latex.ToDisplayString(mimeType));

            formattedValue.MimeType.Should().Be("text/latex");
            formattedValue.Value.Should().Be(@"$$F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$$");
        }
        public void ScriptContent_type_with_possible_html_characters_is_not_HTML_encoded()
        {
            var scriptText = "if (true && false) { alert('hello with embedded <>\" escapes'); };";
            var script     = new ScriptContent(scriptText);
            var mimeType   = Formatter.GetPreferredMimeTypeFor(script.GetType());

            var formattedValue = new FormattedValue(
                mimeType,
                script.ToDisplayString(mimeType));

            formattedValue.MimeType.Should().Be("text/html");
            formattedValue.Value.Should().Be($"<script type=\"text/javascript\">{scriptText}</script>");
        }
示例#5
0
        public void JObject_is_formatted_as_application_json()
        {
            JObject obj = JObject.FromObject(new { value = 123, OtherValue = 456 });

            var mimeType = Formatter.PreferredMimeTypeFor(obj.GetType());
            var output   = obj.ToDisplayString(JsonFormatter.MimeType);

            mimeType.Should().Be(JsonFormatter.MimeType);

            output
            .Should()
            .Be(@"{""value"":123,""OtherValue"":456}");
        }
示例#6
0
        public static void SetupDefaultMimeTypes()
        {
            Formatter <LaTeXString> .Register((laTeX, writer) => writer.Write(laTeX.ToString()), "text/latex");

            Formatter <MathString> .Register((math, writer) => writer.Write(math.ToString()), "text/latex");

            Formatter.SetPreferredMimeTypeFor(typeof(LaTeXString), "text/latex");
            Formatter.SetPreferredMimeTypeFor(typeof(MathString), "text/latex");

            Formatter.SetPreferredMimeTypeFor(typeof(string), PlainTextFormatter.MimeType);

            Formatter.SetDefaultMimeType(HtmlFormatter.MimeType);
        }
示例#7
0
        public static void SetUpFormatters()
        {
            Formatter.DefaultMimeType = HtmlFormatter.MimeType;
            Formatter.SetPreferredMimeTypeFor(typeof(string), PlainTextFormatter.MimeType);
            Formatter.SetPreferredMimeTypeFor(typeof(ScriptContent), HtmlFormatter.MimeType);

            Formatter.Register <ScriptContent>((script, writer) =>
            {
                IHtmlContent content =
                    PocketViewTags.script[type: "text/javascript"](script.ScriptValue.ToHtmlContent());
                content.WriteTo(writer, HtmlEncoder.Default);
            }, HtmlFormatter.MimeType);
        }
示例#8
0
        public void LatexString_type_is_formatted()
        {
            var latex = new LaTeXString(@"F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx");

            var mimeType = Formatter.GetPreferredMimeTypesFor(latex.GetType()).FirstOrDefault();

            var formattedValue = new FormattedValue(
                mimeType,
                latex.ToDisplayString(mimeType));

            formattedValue.MimeType.Should().Be("text/latex");
            formattedValue.Value.Should().Be(@"F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx");
        }
        public void js_wrapping_formatter_fails_if_apiUri_is_not_configured_within_the_configured_timeout()
        {
            var frontendEnvironment = new HtmlNotebookFrontedEnvironment();

            CommandLineParser.SetUpFormatters(frontendEnvironment, new StartupOptions(httpPort: new HttpPort(4242)), 1.Seconds());
            var script   = new ScriptContent("alert('hello');");
            var mimeType = Formatter.GetPreferredMimeTypeFor(script.GetType());

            Action formatting = () => script.ToDisplayString(mimeType);

            formatting.Should()
            .Throw <TimeoutException>()
            .Which
            .Message
            .Should().Be("Timeout resolving the kernel's HTTP endpoint. Please try again.");
        }
        public void ScriptContent_type_is_formatted()
        {
            var script   = new ScriptContent("alert('hello');");
            var mimeType = Formatter.PreferredMimeTypeFor(script.GetType());

            var formattedValue = new FormattedValue(
                mimeType,
                script.ToDisplayString(mimeType));

            formattedValue.MimeType.Should().Be("text/html");
            formattedValue.Value.Should().Be($@"<script type=""text/javascript"">if (typeof window.createDotnetInteractiveClient === typeof Function) {{
createDotnetInteractiveClient('http://12.12.12.12:4242/').then(function (interactive) {{
let notebookScope = getDotnetInteractiveScope('http://12.12.12.12:4242/');
alert('hello');
}});
}}</script>");
        }
示例#11
0
        public void ScriptContent_type_is_wrapped_when_http_and_the_frontendEnvironment_is_JupyterFrontedEnvironment()
        {
            var frontendEnvironment = new HtmlNotebookFrontedEnvironment(new Uri("http://12.12.12.12:4242"));

            CommandLineParser.SetUpFormatters(frontendEnvironment, new StartupOptions(httpPort: new HttpPort(4242)), 10.Seconds());
            var script   = new ScriptContent("alert('hello');");
            var mimeType = Formatter.GetPreferredMimeTypeFor(script.GetType());

            var formattedValue = new FormattedValue(
                mimeType,
                script.ToDisplayString(mimeType));

            formattedValue.MimeType.Should().Be("text/html");
            formattedValue.Value.Should().Be(@"<script type=""text/javascript"">if (typeof window.createDotnetInteractiveClient === typeof Function) {
createDotnetInteractiveClient('http://12.12.12.12:4242/').then(function (interactive) {
let notebookScope = getDotnetInteractiveScope('http://12.12.12.12:4242/');
alert('hello');
});
}</script>");
        }
示例#12
0
        internal static HtmlFormatter <T> CreateForAnyEnumerable(bool includeInternals)
        {
            Func <T, IEnumerable> getKeys   = null;
            Func <T, IEnumerable> getValues = instance => (IEnumerable)instance;

            var dictType =
                typeof(T).GetAllInterfaces()
                .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary <,>))
                ??
                typeof(T).GetAllInterfaces()
                .FirstOrDefault(i => i == typeof(IDictionary));

            if (dictType is not null)
            {
                var keysProperty = dictType.GetProperty("Keys");
                getKeys = instance => (IEnumerable)keysProperty.GetValue(instance, null);

                var valuesProperty = dictType.GetProperty("Values");
                getValues = instance => (IEnumerable)valuesProperty.GetValue(instance, null);
            }

            return(new HtmlFormatter <T>(BuildTable));

            bool BuildTable(T source, FormatContext context)
            {
                using var _ = context.IncrementTableDepth();

                if (context.TableDepth > 1)
                {
                    HtmlFormatter.FormatAndStyleAsPlainText(source, context);
                    return(true);
                }

                var canCountRemainder = source is ICollection;

                var(rowData, remainingCount) = getValues(source)
                                               .Cast <object>()
                                               .Select((v, i) => (v, i))
                                               .TakeAndCountRemaining(Formatter.ListExpansionLimit, canCountRemainder);

                if (rowData.Count == 0)
                {
                    context.Writer.Write(i("(empty)"));
                    return(true);
                }

                var valuesByHeader    = new Dictionary <string, Dictionary <int, object> >();
                var headerToSortIndex = new Dictionary <string, (int, int)>();
                var typesAreDifferent = false;
                var types             = new Dictionary <Type, int>();

                foreach (var(value, index) in rowData)
                {
                    IDictionary <string, object> keysAndValues;

                    if (value is { } &&
                        Formatter.GetPreferredFormatterFor(value.GetType(), HtmlFormatter.MimeType) is { } formatter&&
                        formatter.Type == typeof(object))
                    {
                        var destructurer = Destructurer.GetOrCreate(value?.GetType());

                        keysAndValues = destructurer.Destructure(value);
                    }