Example #1
0
 public PreviewMode(IHtmlView html, ITestService service, Test test)
     : base(Mode.Preview)
 {
     _html = html;
     _service = service;
     _test = test;
 }
Example #2
0
        /// <summary>Creates the UI elements for the given HTML node and HTML view.</summary>
        /// <param name="node">The HTML node.</param>
        /// <param name="htmlView">The HTML view.</param>
        /// <returns>The UI elements.</returns>
        public DependencyObject[] CreateControls(HtmlNode node, IHtmlView htmlView)
        {
            var controls = new List<Grid>();
            foreach (var child in node.Children.OfType<HtmlTagNode>().Where(c => c.Name == "li"))
            {
                var grid = new Grid();
                grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(20)});
                grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

                var textBlock = CreateBulletSymbol(htmlView);
                                grid.Children.Add(textBlock);
                Grid.SetColumn(textBlock, 0);

                var panel = new StackPanel();

                child.WrapWithHtmlTag();
                foreach (var c in child.GetChildControls(htmlView).OfType<UIElement>())
                {
                    var frameworkElement = c as FrameworkElement;
                    if (frameworkElement != null)
                        frameworkElement.HorizontalAlignment = HorizontalAlignment.Stretch;

                    panel.Children.Add(c);
                }

                grid.Children.Add(panel);
                Grid.SetColumn(panel, 1);

                controls.Add(grid);
            }

            AdjustMargins(htmlView, controls);
            return controls.OfType<DependencyObject>().ToArray();
        }
Example #3
0
 public EditMode(Test test, ITestStateManager testState, IHtmlView view, ITestEditorBuilder editorBuilder)
     : base(Mode.Edit)
 {
     _test = test;
     _testState = testState;
     _view = view;
     _editorBuilder = editorBuilder;
 }
        /// <summary>Creates the UI elements for the given HTML node and HTML view.</summary>
        /// <param name="node">The HTML node.</param>
        /// <param name="htmlView">The HTML view.</param>
        /// <returns>The UI elements.</returns>
        public DependencyObject[] CreateControls(HtmlNode node, IHtmlView htmlView)
        {
            var control = CreateControl(node, htmlView);
            if (control != null)
                return new [] { control };

            return new DependencyObject[] { };
        }
Example #5
0
        /// <summary>Creates the UI elements for the given HTML node and HTML view.</summary>
        /// <param name="node">The HTML node.</param>
        /// <param name="htmlView">The HTML view.</param>
        /// <returns>The UI elements.</returns>
        public DependencyObject[] CreateControls(HtmlNode node, IHtmlView htmlView)
        {
            try
            {
                var link = node.Attributes["href"];
                var hyperlink = new Hyperlink();

                if (Foreground != null)
                    hyperlink.Foreground = Foreground;

#if !WINRT
                hyperlink.MouseOverForeground = htmlView.Foreground;
                hyperlink.TextDecorations = TextDecorations.Underline;
#endif                
                foreach (var child in node.Children)
                {
                    var leaves = child.GetControls(htmlView).ToArray();
                    if (leaves.Length > 0)
                    {
                        foreach (var item in leaves)
                        {
                            if (item is Inline)
                                hyperlink.Inlines.Add((Inline)item);
                            else
                                hyperlink.Inlines.Add(new InlineUIContainer { Child = (UIElement) item });
                        }
                    }
                    else if (child is HtmlTextNode && !string.IsNullOrEmpty(((HtmlTextNode)child).Text))
                        hyperlink.Inlines.Add(new Run { Text = ((HtmlTextNode)child).Text });
                }

#if WINRT

                var action = CreateLinkAction(hyperlink, link, htmlView);
                hyperlink.Click += (sender, e) =>
                {
                    action();
                    ((Control)htmlView).Focus(FocusState.Programmatic);
                };

#else

                var action = CreateLinkAction(hyperlink, link, htmlView);
                hyperlink.Command = new RelayCommand(delegate
                {
                    if (!PhoneApplication.IsNavigating)
                        action();
                });

#endif
                
                return new DependencyObject[] { hyperlink };
            }
            catch
            {
                return node.GetChildControls(htmlView); // suppress link 
            }
        }
Example #6
0
 private TextBlock CreateBulletSymbol(IHtmlView htmlView)
 {
     var textBlock = new TextBlock();
     textBlock.Foreground = htmlView.Foreground;
     textBlock.FontSize = htmlView.FontSize;
     textBlock.FontFamily = htmlView.FontFamily;
     textBlock.Margin = new Thickness();
     textBlock.Text = BulletSymbol;
     return textBlock;
 }
 /// <summary>Creates the UI elements for the given HTML node and HTML view.</summary>
 /// <param name="node">The HTML node.</param>
 /// <param name="htmlView">The HTML view.</param>
 /// <returns>The UI elements.</returns>
 public DependencyObject[] CreateControls(HtmlNode node, IHtmlView htmlView)
 {
     var controls = node.GetChildControls(htmlView); 
     foreach (var leave in controls)
     {
         var element = leave as TextElement;
         if (element != null)
             element.FontWeight = FontWeights.Bold;
     }
     return controls;
 }
 public static DependencyObject[] GetChildControls(this HtmlNode node, IHtmlView textBlock)
 {
     var list = new List<DependencyObject>();
     foreach (var c in node.Children)
     {
         var ctrl = c.GetControls(textBlock);
         if (ctrl != null)
             list.AddRange(ctrl);
         else
             list.AddRange(c.GetChildControls(textBlock));
     }
     return list.ToArray();
 }
        /// <summary>Creates a single UI element for the given HTML node and HTML view.</summary>
        /// <param name="node">The node.</param>
        /// <param name="htmlView">The text block.</param>
        /// <returns>The UI element.</returns>
        public override DependencyObject CreateControl(HtmlNode node, IHtmlView htmlView)
        {
            try
            {
                var imageUri = node.Attributes["src"];

                var height = 0;
                if (node.Attributes.ContainsKey("height"))
                    int.TryParse(node.Attributes["height"], out height);

                var width = 0;
                if (node.Attributes.ContainsKey("width"))
                    int.TryParse(node.Attributes["width"], out width);

                if (height == 1 && width == 1)
                    return null;

                var image = new Image();
                image.Width = 0;
                image.Height = 0;

                var bitmapImage = new BitmapImage(new Uri(imageUri));

                var imageBlock = new ImageBlock
                {
                    Image = image,
                    UserHeight = height,
                    UserWidth = width,
                    Source = bitmapImage
                };

                bitmapImage.ImageOpened += delegate { imageBlock.Update(htmlView.ActualWidth); };

                image.HorizontalAlignment = HorizontalAlignment.Left;
                image.Source = bitmapImage;
                image.Margin = new Thickness(0, htmlView.ParagraphMargin, 0, htmlView.ParagraphMargin);

                if (width > 0)
                    image.Width = width;
                if (height > 0)
                    image.Height = height;

                htmlView.SizeDependentControls.Add(imageBlock);
                return new ContentPresenter { Content = image };
            }
            catch
            {
                return null;
            }
        }
Example #10
0
        /// <summary>Creates the UI elements for the given HTML node and HTML view.</summary>
        /// <param name="node">The HTML node.</param>
        /// <param name="htmlView">The HTML view.</param>
        /// <returns>The UI elements.</returns>
        public DependencyObject[] CreateControls(HtmlNode node, IHtmlView htmlView)
        {
            var controls = node.GetChildControls(htmlView);
            foreach (var leave in controls)
            {
                var element = leave as TextElement;
                if (element != null)
                {
#if WINRT
                    element.FontStyle = FontStyle.Italic;
#else
                    element.FontStyle = FontStyles.Italic;
#endif
                }
            }
            return controls;
        }
Example #11
0
        /// <summary>Creates the UI elements for the given HTML node and HTML view.</summary>
        /// <param name="node">The HTML node.</param>
        /// <param name="textBlock">The HTML view.</param>
        /// <returns>The UI elements.</returns>
        public DependencyObject[] CreateControls(HtmlNode node, IHtmlView textBlock)
        {
            var leaves = node.GetChildControls(textBlock);
            if (leaves.Length == 1 && leaves[0] is Panel)
                leaves = new[] { leaves[0] };

            if (leaves.Length > 0)
            {
                var element = leaves.First() as FrameworkElement;
                if (element != null)
                    element.Margin = new Thickness(element.Margin.Left, 0, element.Margin.Right, element.Margin.Bottom);

                element = leaves.Last() as FrameworkElement;
                if (element != null)
                    element.Margin = new Thickness(element.Margin.Left, element.Margin.Top, element.Margin.Right, 0);
            }

            return leaves;
        }
        /// <exception cref="InvalidOperationException">Control generator returned null.</exception>
        public static DependencyObject[] GetControls(this HtmlNode node, IHtmlView textBlock)
        {
            if (node.Data == null)
            {
                var value = node is HtmlTextNode ? "text" : ((HtmlTagNode)node).Name;

                var generator = textBlock.Generators.ContainsKey(value) ? textBlock.Generators[value] :
                    (textBlock.Generators.ContainsKey("unknown") ? textBlock.Generators["unknown"] : null);

                if (generator != null)
                {
                    node.Data = generator.CreateControls(node, textBlock);
                    if (node.Data == null)
                        node.Data = new DependencyObject[] { };
                }
                else
                    node.Data = node.GetChildControls(textBlock);
            }

            return (DependencyObject[]) node.Data;
        }
Example #13
0
 /// <summary>Creates a single UI element for the given HTML node and HTML view.</summary>
 /// <param name="node">The node.</param>
 /// <param name="htmlView">The text block.</param>
 /// <returns>The UI element.</returns>
 public override DependencyObject CreateControl(HtmlNode node, IHtmlView htmlView)
 {
     return new Run { Text = ((HtmlTextNode)node).Text };
 }
Example #14
0
 public ResultsMode(IHtmlView view, Test test)
     : base(Mode.Results)
 {
     _view = view;
     _test = test;
 }
Example #15
0
 public static Stream Render(IHtmlView html, IResponseContext response, ChunkedMemoryStream stream)
 {
     stream.Reset();
     response.ContentType = "text/html; charset=UTF-8";
     var sw = stream.GetWriter();
     html.Render(sw);
     sw.Flush();
     response.ContentLength = stream.Position;
     stream.Position = 0;
     return stream;
 }
Example #16
0
        /// <summary>Creates the UI elements for the given HTML node and HTML view.</summary>
        /// <param name="node">The HTML node.</param>
        /// <param name="htmlView">The HTML view.</param>
        /// <returns>The UI elements.</returns>
        public DependencyObject[] CreateControls(HtmlNode node, IHtmlView htmlView)
        {
            var list = new List<DependencyObject>();

            var isFirst = true; 
            var addTopMargin = true; 
            var current = new List<Inline>();

            foreach (var child in node.GetChildControls(htmlView))
            {
                if (isFirst)
                {
                    if (child is Run)
                    {
                        var run = (Run)child;
                        run.Text = run.Text.TrimStart(' ', '\t'); // TODO: Trim all white spaces
                    }
                    isFirst = false;
                }

                if (child is Run && UseTextSplitting && ((Run)child).Text.Contains("\n")) // used to avoid 2048px max control size
                {
                    // split text
                    var run = (Run) child;
                    var splits = run.Text.Split('\n');

                    // join some splits to avoid small junks 
                    var currentSplit = "";
                    var newSplits = new List<string>();
                    for (var i = 0; i < splits.Length; i++)
                    {
                        var split = splits[i];
                        if (i != 0 && currentSplit.Length + split.Length > 16)
                        {
                            newSplits.Add(currentSplit);
                            currentSplit = split;
                        }
                        else
                            currentSplit += (i != 0 ? "\n" : "") + split;
                    }
                    newSplits.Add(currentSplit);

                    // create multiple text blocks
                    splits = newSplits.ToArray();
                    for (var i = 0; i < splits.Length; i++)
                    {
                        var split = splits[i];
                        current.Add(new Run { Text = split });
                        if (i < splits.Length - 1) // dont create for last
                            CreateTextBox(node, list, current, htmlView, i == 0 && addTopMargin, false);
                    }
                    addTopMargin = list.Count == 0; 
                } else if (child is Inline)
                    current.Add((Inline)child);
                else
                {
                    CreateTextBox(node, list, current, htmlView, addTopMargin, true);
                    list.Add(child);
                    addTopMargin = true; 
                }
            }

            CreateTextBox(node, list, current, htmlView, addTopMargin, true);

            if (list.Count == 0)
                return null;

            return list.ToArray();
        }
Example #17
0
        protected virtual RichTextBlock CreateTextBlock(HtmlNode node, IHtmlView htmlView)
        {
            var textBlock = new RichTextBlock();
#endif

            textBlock.FontSize = htmlView.FontSize * FontSize;
            textBlock.Foreground = Foreground ?? htmlView.Foreground;
            textBlock.FontFamily = FontFamily ?? htmlView.FontFamily;
            textBlock.FontStyle = FontStyle;
            textBlock.FontWeight = FontWeight;

            return textBlock;
        }
Example #18
0
        /// <summary>Creates a formatted text block.</summary>
        /// <param name="node">The node.</param>
        /// <param name="htmlView">The HTML view.</param>
#if !WINRT
        protected virtual RichTextBox CreateTextBlock(HtmlNode node, IHtmlView htmlView)
        {
            var textBlock = new RichTextBox();
Example #19
0
        private void CreateTextBox(HtmlNode node, List<DependencyObject> list, List<Inline> current, IHtmlView htmlView, bool addTopMargin, bool addBottomMargin)
        {
            if (current.Count > 0)
            {
                var textBlock = CreateTextBlock(node, htmlView);

                var p = new Paragraph();
                foreach (var r in current)
                    p.Inlines.Add(r);

                textBlock.Blocks.Add(p);

#if !WINRT
                textBlock.Background = textBlock.Background;
                textBlock.Margin = new Thickness(-12, addTopMargin ? htmlView.ParagraphMargin : 0, -12, addBottomMargin ? htmlView.ParagraphMargin : 0);
#else
                textBlock.IsTextSelectionEnabled = false;
                textBlock.Margin = new Thickness(0, addTopMargin ? htmlView.ParagraphMargin : 0, 0, addBottomMargin ? htmlView.ParagraphMargin : 0);
#endif

                list.Add(textBlock);
                current.Clear();
            }
        }
Example #20
0
        private void CreateTextBox(List<DependencyObject> list, List<Inline> current, IHtmlView htmlView, bool addTopMargin, bool addBottomMargin)
        {
            if (current.Count > 0)
            {
                var p = new Paragraph();
                foreach (var r in current)
                    p.Inlines.Add(r);

#if !WINRT
                var textBlock = new RichTextBox();
                textBlock.Background = textBlock.Background;
                textBlock.Margin = new Thickness(-12, addTopMargin ? htmlView.ParagraphMargin : 0, -12, addBottomMargin ? htmlView.ParagraphMargin : 0);
#else
                var textBlock = new RichTextBlock();
                textBlock.IsTextSelectionEnabled = false; 
                textBlock.Margin = new Thickness(0, addTopMargin ? htmlView.ParagraphMargin : 0, 0, addBottomMargin ? htmlView.ParagraphMargin : 0);
#endif

                textBlock.Blocks.Add(p);
                textBlock.FontSize = htmlView.FontSize * FontSize;
                textBlock.Foreground = Foreground ?? htmlView.Foreground;
                textBlock.FontFamily = FontFamily ?? htmlView.FontFamily;
                textBlock.FontStyle = FontStyle;
                textBlock.FontWeight = FontWeight;
                
                list.Add(textBlock);
                current.Clear();
            }
        }
Example #21
0
        /// <summary>Creates the action to open the link after the user clicked on it.</summary>
        /// <param name="hyperlink">The hyperlink.</param>
        /// <param name="link">The link.</param>
        /// <param name="textBlock">The text block.</param>
        /// <returns>The action</returns>
        protected virtual Action CreateLinkAction(Hyperlink hyperlink, string link, IHtmlView textBlock)
        {
            if (link.StartsWith("mailto:"))
                return () => new EmailComposeTask {To = link.Substring(7)}.Show();
            
            // 'tel:' removed because it needs CAP_PHONEDIALER capability! Use PhoneLinkGenerator from "- Other" directory. 

            try
            {
                var uri = link.StartsWith("http://") || link.StartsWith("https://") ?
                    new Uri(link, UriKind.Absolute) : new Uri(textBlock.HtmlBaseUri, link);
                return () => new WebBrowserTask { Uri = uri }.Show();
            }
            catch (Exception)
            {

            }

            return () => { };
        }
Example #22
0
 /// <summary>Creates the action to open the link after the user clicked on it.</summary>
 /// <param name="hyperlink">The hyperlink.</param>
 /// <param name="link">The link.</param>
 /// <param name="textBlock">The text block.</param>
 /// <returns>The action</returns>
 protected virtual Action CreateLinkAction(Hyperlink hyperlink, string link, IHtmlView textBlock)
 {
     return async () =>
     {
         try
         {
             await Launcher.LaunchUriAsync(new Uri(link));
         }
         catch
         {
             // Ignore malformed URIs. 
         }
     };
 }
 /// <summary>Creates a single UI element for the given HTML node and HTML view.</summary>
 /// <param name="node">The node.</param>
 /// <param name="htmlView">The HTML view.</param>
 /// <returns>The UI element.</returns>
 public abstract DependencyObject CreateControl(HtmlNode node, IHtmlView htmlView);
Example #24
0
        private void AdjustMargins(IHtmlView htmlView, List<Grid> controls)
        {
            var firstControl = controls.FirstOrDefault();
            if (firstControl != null)
                firstControl.Margin = new Thickness(0, htmlView.ParagraphMargin, 0, 0);

            var lastControl = controls.LastOrDefault();
            if (lastControl != null)
                lastControl.Margin = new Thickness(0, 0, 0, htmlView.ParagraphMargin);
        }
Example #25
0
 /// <summary>Creates the UI elements for the given HTML node and HTML view.</summary>
 /// <param name="node">The HTML node.</param>
 /// <param name="textBlock">The HTML view.</param>
 /// <returns>The UI elements.</returns>
 public DependencyObject[] CreateControls(HtmlNode node, IHtmlView textBlock)
 {
     return new DependencyObject[] { };
 }