public void OnSendNavigating_Navigating_IsInvoked()
        {
            // Arrange
            var label        = new HtmlLabel();
            var url          = Guid.NewGuid().ToString();
            var source       = new UrlWebViewSource();
            var expectedArgs = new WebNavigatingEventArgs(WebNavigationEvent.Forward, source, url);

            label.Navigating += LabelNavigating;

            WebNavigatingEventArgs actualArgs = null;
            object actualSender = null;

            void LabelNavigating(object sender, WebNavigatingEventArgs e)
            {
                actualSender = sender;
                actualArgs   = e;
            }

            // Act
            label.SendNavigating(expectedArgs);

            // Assert
            Assert.Equal(expectedArgs, actualArgs);
            Assert.Equal(label, actualSender);
        }
Exemplo n.º 2
0
            public override void OnClick(global::Android.Views.View widget)
            {
                var args = new WebNavigatingEventArgs(WebNavigationEvent.NewPage, new UrlWebViewSource {
                    Url = _span.URL
                }, _span.URL);

                _label.SendNavigating(args);

                if (args.Cancel)
                {
                    return;
                }

                Device.OpenUri(new Uri(_span.URL));
                _label.SendNavigated(args);
            }
Exemplo n.º 3
0
        private static void ParseText(XElement element, InlineCollection inlines, HtmlLabel label)
        {
            if (element == null)
            {
                return;
            }

            InlineCollection currentInlines = inlines;
            var elementName = element.Name.ToString().ToUpperInvariant();

            switch (elementName)
            {
            case _elementA:
                var        link = new Hyperlink();
                XAttribute href = element.Attribute("href");
                if (href != null)
                {
                    try
                    {
                        link.NavigateUri = new Uri(href.Value);
                    }
                    catch (FormatException) { /* href is not valid */ }
                }
                link.Click += (sender, e) =>
                {
                    sender.NavigateUri = null;
                    if (href == null)
                    {
                        return;
                    }

                    var args = new WebNavigatingEventArgs(WebNavigationEvent.NewPage, new UrlWebViewSource {
                        Url = href.Value
                    }, href.Value);
                    label.SendNavigating(args);

                    if (args.Cancel)
                    {
                        return;
                    }

                    Device.OpenUri(new Uri(href.Value));
                    label.SendNavigated(args);
                };
                inlines.Add(link);
                currentInlines = link.Inlines;
                break;

            case _elementB:
            case _elementStrong:
                var bold = new Bold();
                inlines.Add(bold);
                currentInlines = bold.Inlines;
                break;

            case _elementI:
            case _elementEm:
                var italic = new Italic();
                inlines.Add(italic);
                currentInlines = italic.Inlines;
                break;

            case _elementU:
                var underline = new Underline();
                inlines.Add(underline);
                currentInlines = underline.Inlines;
                break;

            case _elementBr:
                inlines.Add(new LineBreak());
                break;

            case _elementP:
                // Add two line breaks, one for the current text and the second for the gap.
                if (AddLineBreakIfNeeded(inlines))
                {
                    inlines.Add(new LineBreak());
                }

                var paragraphSpan = new Span();
                inlines.Add(paragraphSpan);
                currentInlines = paragraphSpan.Inlines;
                break;

            case _elementLi:
                inlines.Add(new LineBreak());
                inlines.Add(new Run {
                    Text = " • "
                });
                break;

            case _elementUl:
            case _elementDiv:
                _ = AddLineBreakIfNeeded(inlines);
                var divSpan = new Span();
                inlines.Add(divSpan);
                currentInlines = divSpan.Inlines;
                break;
            }
            foreach (XNode node in element.Nodes())
            {
                if (node is XText textElement)
                {
                    currentInlines.Add(new Run {
                        Text = textElement.Value
                    });
                }
                else
                {
                    ParseText(node as XElement, currentInlines, label);
                }
            }
            // Add newlines for paragraph tags
            if (elementName == "ElementP")
            {
                currentInlines.Add(new LineBreak());
            }
        }