public void Mark(IHtmlElement element) { Contract.RequiresNotNull(element, "element"); if (Element != null) { // unmark first - maybe it was marked with another color before Unmark(); } Element = element; var markerCountAttr = Element.GetAttribute(Attribute_MarkerCount); if (string.IsNullOrEmpty(markerCountAttr)) { Element.SetAttribute(Attribute_OrigStyle, Element.Style); myMarkerIdx = 0; } else { myMarkerIdx = int.Parse(markerCountAttr); } var markupStyle = string.Format(";color:black;background-color:{0}", ColorTranslator.ToHtml(Color)); Element.SetAttribute(Attribute_Marker + myMarkerIdx, markupStyle); Element.SetAttribute(Attribute_MarkerCount, (myMarkerIdx + 1).ToString()); Element.Style = Element.GetAttribute(Attribute_OrigStyle) + markupStyle; }
/// <summary> /// Submits a HTML form to the server /// </summary> /// <param name="client">HTTP client to use for submitting the form.</param> /// <param name="form">HTML Form to submit.</param> /// <param name="submitButton">Submit button to use for submitting the form.</param> /// <returns>Returns the HTTP response received after submitting the form.</returns> public static Task <HttpResponseMessage> SubmitFormAsync(this HttpClient client, IHtmlFormElement form, IHtmlElement submitButton) { if (form == null) { throw new ArgumentNullException(nameof(form)); } if (submitButton == null) { throw new ArgumentNullException(nameof(submitButton)); } var formSubmission = form.GetSubmission(submitButton); var targetUri = (Uri)formSubmission.Target; if (submitButton.HasAttribute("formaction")) { var formAction = submitButton.GetAttribute("formaction"); targetUri = new Uri(formAction, UriKind.Relative); } var submitFormRequest = new HttpRequestMessage(new HttpMethod(formSubmission.Method.ToString()), targetUri) { Content = new StreamContent(formSubmission.Body) }; foreach (var header in formSubmission.Headers) { submitFormRequest.Headers.TryAddWithoutValidation(header.Key, header.Value); submitFormRequest.Content.Headers.TryAddWithoutValidation(header.Key, header.Value); } return(client.SendAsync(submitFormRequest)); }
public static Task <HttpResponseMessage> SendAsync( this HttpClient client, IHtmlFormElement form, IHtmlElement submitButton, IEnumerable <KeyValuePair <string, string> > formValues) { foreach (var kvp in formValues) { var element = Assert.IsAssignableFrom <IHtmlInputElement>(form[kvp.Key]); element.Value = kvp.Value; } var submit = form.GetSubmission(submitButton); var target = (Uri)submit.Target; if (submitButton.HasAttribute("formaction")) { var formaction = submitButton.GetAttribute("formaction"); target = new Uri(formaction, UriKind.Relative); } var submision = new HttpRequestMessage(new HttpMethod(submit.Method.ToString()), target) { Content = new StreamContent(submit.Body) }; foreach (var header in submit.Headers) { submision.Headers.TryAddWithoutValidation(header.Key, header.Value); submision.Content.Headers.TryAddWithoutValidation(header.Key, header.Value); } return(client.SendAsync(submision)); }
public void Inject(HtmlNodeElement element) { IHtmlElement input = element.Children.FirstOrDefault(x => x.TagName == "input"); if (input != null) { HtmlParser parser = new HtmlParser(); if (input.GetAttribute("name")?.Value != null) { IHtmlElement validationMessageTag; using (var writer = new System.IO.StringWriter()) { _htmlHelper.ValidationMessage(input.GetAttribute("name").Value, _options?.ValidationMessageAttributes?.ToDictionary()).WriteTo(writer, HtmlEncoder.Default); validationMessageTag = parser.ParseString(writer.ToString()).FirstOrDefault(); } using (var writer = new System.IO.StringWriter()) { _htmlHelper.TextBox(input.GetAttribute("name").Value).WriteTo(writer, HtmlEncoder.Default); IHtmlElement textBox = parser.ParseString(writer.ToString()).FirstOrDefault(); foreach (var attr in textBox.Attributes) { if (attr.Name.StartsWith("data")) { input.AddAttribute(attr); } } } element.AddAfter(input.UId, validationMessageTag); } } int index = 0; while (element.Children.Count > index) { if (element.Children[index] is IHtmlNodeElement) { Inject((HtmlNodeElement)element.Children[index]); } index++; } }
public override IHtmlElement Check(IHtmlElement elem,IHtmlElement root = null) { var attr = elem.GetAttribute(this.AttributeName); if (attr == null) return null; if (this.AttributeValue != null) { return this.AttributeValue == attr?elem:null; } return elem; }
/// <summary> /// If the given element is a html link or it contains /// a child which is a html link then the url of the first /// link found is returned. Otherwise the InnerText of the element /// is returned. /// </summary> public static string FirstLinkOrInnerText(this IHtmlElement element) { if (element.TagName == "A") { return(element.GetAttribute("HREF")); } IHtmlElement link = element.Children.FirstOrDefault(child => child.TagName == "A"); return(link != null ? link.GetAttribute("HREF") : element.InnerText); }
internal static bool IsMarked(IHtmlElement element) { var attr = element.GetAttribute(Attribute_MarkerCount); if (string.IsNullOrEmpty(attr)) { return(false); } return(int.Parse(attr) > 0); }
public static Task <HttpResponseMessage> SendAsync( this HttpClient client, IHtmlFormElement form, IHtmlElement submitButton, IEnumerable <KeyValuePair <string, string> > formValues) { foreach (var kvp in formValues) { switch (form[kvp.Key]) { case IHtmlInputElement input: input.Value = kvp.Value; if (bool.TryParse(kvp.Value, out var isChecked)) { input.IsChecked = isChecked; } break; case IHtmlSelectElement select: select.Value = kvp.Value; break; default: throw new Exception($"Unknown form element: '{kvp.Key}'"); } } var submit = form.GetSubmission(submitButton); var target = (Uri)submit.Target; if (submitButton.HasAttribute("formaction")) { var formaction = submitButton.GetAttribute("formaction"); target = new Uri(formaction, UriKind.Relative); } var submision = new HttpRequestMessage(new HttpMethod(submit.Method.ToString()), target) { Content = new StreamContent(submit.Body) }; foreach (var header in submit.Headers) { submision.Headers.TryAddWithoutValidation(header.Key, header.Value); submision.Content.Headers.TryAddWithoutValidation(header.Key, header.Value); } return(client.SendAsync(submision)); }
public static Task <HttpResponseMessage> SendAsync( this HttpClient client, IHtmlFormElement form, IHtmlElement submitButton, IEnumerable <KeyValuePair <string, string> > formValues) { foreach (var formValue in formValues) { if (form[formValue.Key] is IHtmlInputElement elementAsInput) { elementAsInput.Value = formValue.Value; } else if (form[formValue.Key] is IHtmlSelectElement) { var matchingSelect = form.QuerySelectorAll("select") .OfType <IHtmlSelectElement>() .FirstOrDefault(s => s.Children.OfType <IHtmlOptionElement>().Any(o => o.Value == formValue.Value)); if (matchingSelect != null) { matchingSelect.Value = formValue.Value; } } } var documentRequest = form.GetSubmission(submitButton); var requestUri = submitButton.HasAttribute("formaction") ? new Uri(submitButton.GetAttribute("formaction"), UriKind.Relative) : documentRequest.Target; var request = new HttpRequestMessage(new HttpMethod(documentRequest.Method.ToString()), requestUri) { Content = new StreamContent(documentRequest.Body) }; foreach (var header in documentRequest.Headers) { request.Headers.TryAddWithoutValidation(header.Key, header.Value); request.Content.Headers.TryAddWithoutValidation(header.Key, header.Value); } return(client.SendAsync(request)); }
/// <summary> /// Sanitizes the style. /// </summary> /// <param name="element">The element.</param> /// <param name="baseUrl">The base URL.</param> protected void SanitizeStyle(IHtmlElement element, string baseUrl) { // filter out invalid CSS declarations // see https://github.com/AngleSharp/AngleSharp/issues/101 if (element.GetAttribute("style") == null) { return; } element.SetAttribute("style", element.Style.ToCss()); var styles = element.Style; if (styles == null || styles.Length == 0) { return; } SanitizeStyleDeclaration(element, styles, baseUrl); }
private Inline GenerateAbbreviation(IHtmlElement node) { var span = new Span() { TextDecorations = TextDecorations.Underline, }; AddInlineChildren(node, span.Inlines); if (node.HasAttribute("title")) { ToolTip toolTip = new ToolTip() { Content = node.GetAttribute("title"), }; ToolTipService.SetToolTip(span, toolTip); } return(span); }
internal object GetUserName() => _userNameInput.GetAttribute("value");
internal string GetEmail() => _emailInput.GetAttribute("value");
private static TextElement ConvertAnchorElement(IHtmlElement anchorHtmlElement) { IEnumerable<Inline> hyperlinkContent = anchorHtmlElement.ChildNodes.Select(child => HtmlConverter.ConvertHtmlNode(child)).OfType<Inline>(); Hyperlink hyperlink = new Hyperlink(); hyperlink.Inlines.AddRange(hyperlinkContent); string hyperReference = anchorHtmlElement.GetAttribute("href"); Uri navigationUri; if (Uri.TryCreate(hyperReference, UriKind.RelativeOrAbsolute, out navigationUri)) hyperlink.NavigateUri = navigationUri; return hyperlink; }
/// <summary> /// Sanitizes the style. /// </summary> /// <param name="element">The element.</param> /// <param name="baseUrl">The base URL.</param> protected void SanitizeStyle(IHtmlElement element, string baseUrl) { // filter out invalid CSS declarations // see https://github.com/FlorianRappl/AngleSharp/issues/101 if (element.GetAttribute("style") == null) return; element.SetAttribute("style", element.Style.ToCss()); var styles = element.Style; if (styles == null || styles.Length == 0) return; var removeStyles = new List<Tuple<ICssProperty, RemoveReason>>(); var setStyles = new Dictionary<string, string>(); foreach (var style in styles) { var key = DecodeCss(style.Name); var val = DecodeCss(style.Value); if (!AllowedCssProperties.Contains(key)) { removeStyles.Add(new Tuple<ICssProperty, RemoveReason>(style, RemoveReason.NotAllowedStyle)); continue; } if(CssExpression.IsMatch(val) || DisallowCssPropertyValue.IsMatch(val)) { removeStyles.Add(new Tuple<ICssProperty, RemoveReason>(style, RemoveReason.NotAllowedValue)); continue; } var urls = CssUrl.Matches(val); if (urls.Count > 0) { if (urls.Cast<Match>().Any(m => GetSafeUri(m.Groups[2].Value) == null || SanitizeUrl(m.Groups[2].Value, baseUrl) == null)) removeStyles.Add(new Tuple<ICssProperty, RemoveReason>(style, RemoveReason.NotAllowedUrlValue)); else { var s = CssUrl.Replace(val, m => "url(" + m.Groups[1].Value + SanitizeUrl(m.Groups[2].Value, baseUrl) + m.Groups[3].Value); if (s != val) { if (key != style.Name) { removeStyles.Add(new Tuple<ICssProperty, RemoveReason>(style, RemoveReason.NotAllowedUrlValue)); } setStyles[key] = s; } } } } foreach (var style in removeStyles) { RemoveStyle(element, styles, style.Item1, style.Item2); } foreach (var style in setStyles) { styles.SetProperty(style.Key, style.Value); } }
private UIElement GeneratePreformattedCodeBlock(IHtmlElement node) { string language = node.GetAttribute("class"); HighlightLanguage highlightLanguage = HighlightLanguage.PlainText; switch (language) { case "python": highlightLanguage = HighlightLanguage.Python; break; case "javascript": case "js": case "jsx": highlightLanguage = HighlightLanguage.JavaScript; break; case "json": highlightLanguage = HighlightLanguage.JSON; break; case "cs": case "csharp": highlightLanguage = HighlightLanguage.CSharp; break; case "c": case "c++": case "cc": case "cpp": highlightLanguage = HighlightLanguage.CPlusPlus; break; case "css": highlightLanguage = HighlightLanguage.CSS; break; case "php": highlightLanguage = HighlightLanguage.PHP; break; case "ruby": case "rb": highlightLanguage = HighlightLanguage.Ruby; break; case "html": case "xml": case "xhtml": case "rss": highlightLanguage = HighlightLanguage.XML; break; case "java": case "jsp": highlightLanguage = HighlightLanguage.Java; break; case "sql": highlightLanguage = HighlightLanguage.SQL; break; default: break; } return(new CodeHighlightedTextBlock() { Code = CleanText(node.InnerHtml), HighlightLanguage = highlightLanguage, }); }
internal object GetNewEmail() => _newEmailInput.GetAttribute("value");
internal object GetEmail() => _emailInput.GetAttribute("value");
public static Task <HttpResponseMessage> SendFormAsync( this HttpClient client, IHtmlFormElement form, IHtmlElement submitButton, IEnumerable <KeyValuePair <string, string> > formValues) { if (client is null) { throw new ArgumentNullException(nameof(client)); } if (form is null) { throw new ArgumentNullException(nameof(form)); } if (submitButton is null) { throw new ArgumentNullException(nameof(submitButton)); } if (formValues is null) { throw new ArgumentNullException(nameof(formValues)); } foreach (var kvp in formValues) { var element = form[kvp.Key]; switch (element) { case IHtmlInputElement input: input.Value = kvp.Value; break; case IHtmlSelectElement select: select.Value = kvp.Value; break; } } Assert.True(form.CheckValidity(), "Form contains invalid data"); var submit = form.GetSubmission(submitButton); var target = (Uri)submit.Target; if (submitButton.HasAttribute("formaction")) { var formaction = submitButton.GetAttribute("formaction"); target = new Uri(formaction, UriKind.Relative); } #pragma warning disable CA2000 // Dispose objects before losing scope var submision = new HttpRequestMessage(new HttpMethod(submit.Method.ToString()), target) { Content = new StreamContent(submit.Body) }; #pragma warning restore CA2000 // Dispose objects before losing scope foreach (var header in submit.Headers) { submision.Headers.TryAddWithoutValidation(header.Key, header.Value); submision.Content.Headers.TryAddWithoutValidation(header.Key, header.Value); } return(client.SendAsync(submision)); }
private static bool HasSummaryCellAttributes(IHtmlElement cell) { return cell.GetAttribute("COLSPAN") == "3"; }