protected static IEnumerable <IElement> Single(IElement?element) { if (element != null) { yield return(element); } }
static void ExtractText(StringBuilder sb, IElement?block) { if (block == null) { return; } }
public Boolean Match(IElement element, IElement?scope) { var parent = element.ParentElement; if (parent != null) { var n = Math.Sign(Step); var k = 0; for (var i = parent.ChildNodes.Length - 1; i >= 0; i--) { if (parent.ChildNodes[i] is IElement child && child.NodeName.Is(element.NodeName)) { k += 1; if (child == element) { var diff = k - Offset; return(diff == 0 || (Math.Sign(diff) == n && diff % Step == 0)); } } } } return(false); }
internal static async Task <bool?> IsDiscoveryQueueAvailable(Bot bot) { Uri request = new(SteamStoreURL, "/explore?l=english"); HtmlDocumentResponse?response = await bot.ArchiWebHandler.UrlGetToHtmlDocumentWithSession(request).ConfigureAwait(false); IDocument?htmlDocument = response?.Content; if (htmlDocument == null) { return(null); } IElement?htmlNode = htmlDocument.SelectSingleNode("//div[@class='subtext']"); if (htmlNode == null) { // Valid, no cards for exploring the queue available return(false); } string text = htmlNode.TextContent; if (string.IsNullOrEmpty(text)) { bot.ArchiLogger.LogNullError(nameof(text)); return(null); } bot.ArchiLogger.LogGenericTrace(text); // It'd make more sense to check against "Come back tomorrow", but it might not cover out-of-the-event queue return(text.StartsWith("You can get ", StringComparison.Ordinal)); }
#pragma warning disable CS8764 // Nullability of return type doesn't match overridden member (possibly because of nullability attributes). public override IElement?ReadJson(JsonReader reader, Type objectType, IElement?existingValue, bool hasExistingValue, JsonSerializer serializer) #pragma warning restore CS8764 // Nullability of return type doesn't match overridden member (possibly because of nullability attributes). { if (reader.TokenType != JsonToken.StartObject) { return(null); } serializer.ContractResolver = new SubclassResolver <IElement>(); var element = JObject.Load(reader); if (element.ContainsKey(nameof(RichTextElement.Components).ToLower())) { return(element.ToObject <RichTextElement>(serializer)); } if (element.ContainsKey(nameof(CustomElement.Searchable_Value).ToLower())) { return(element.ToObject <CustomElement>(serializer)); } if (element.ContainsKey(nameof(UrlSlugElement.Mode).ToLower())) { return(element.ToObject <UrlSlugElement>(serializer)); } return((element["value"]?.Type) switch { JTokenType.Float => element.ToObject <NumberElement>(serializer), JTokenType.Date => element.ToObject <DateTimeElement>(serializer), JTokenType.Array => element.ToObject <AbstractReferenceListElement>(serializer), _ => element.ToObject <TextElement>(serializer), });
private async Task <bool?> IsDiscoveryQueueAvailable() { using IDocument? htmlDocument = await Bot.ArchiWebHandler.GetDiscoveryQueuePage().ConfigureAwait(false); if (htmlDocument == null) { return(null); } IElement?htmlNode = htmlDocument.SelectSingleNode("//div[@class='subtext']"); if (htmlNode == null) { // Valid, no cards for exploring the queue available return(false); } string text = htmlNode.TextContent; if (string.IsNullOrEmpty(text)) { Bot.ArchiLogger.LogNullError(nameof(text)); return(null); } if (Debugging.IsUserDebugging) { Bot.ArchiLogger.LogGenericDebug(string.Format(CultureInfo.CurrentCulture, Strings.Content, text)); } // It'd make more sense to check against "Come back tomorrow", but it might not cover out-of-the-event queue return(text.StartsWith("You can get ", StringComparison.Ordinal)); }
internal Article(Uri uri, string title, string byline, string dir, string?language, string?author, IElement article, Metadata metadata, bool readable, Reader reader) { Uri = uri; Title = title; Byline = string.IsNullOrWhiteSpace(byline) ? metadata.Author : byline; Dir = dir; Content = Serializer(article); TextContent = Converter(article); Excerpt = metadata.Excerpt; Length = article.TextContent.Length; Language = string.IsNullOrWhiteSpace(metadata.Language) ? language : metadata.Language; DateTime test = new DateTime(); DateTime.TryParse(metadata.PublicationDate, out test); PublicationDate = test; Author = string.IsNullOrWhiteSpace(metadata.Author) ? author : metadata.Author; SiteName = metadata.SiteName; IsReadable = readable; // based on http://iovs.arvojournals.org/article.aspx?articleid=2166061 TimeToRead = TimeSpan.FromMinutes(article.TextContent.Count(x => x != ' ' && !char.IsPunctuation(x)) / GetWeightTimeToRead()) > TimeSpan.Zero ? TimeSpan.FromMinutes(article.TextContent.Count(x => x != ' ' && !char.IsPunctuation(x)) / GetWeightTimeToRead()) : TimeSpan.FromMinutes(1); FeaturedImage = metadata.FeaturedImage; _article = article; _reader = reader; }
public static T CreateControl <T>(IElement?parent, IElementLocator elementLocator) where T : class, IElement { var control = Activator.CreateInstance(typeof(T), parent, elementLocator) as T; return(Browser.Instance.CreateControl <T>(control)); }
//解析购物车页 internal static CartResponse?ParseCertPage(HtmlDocumentResponse response) { if (response == null) { return(null); } IEnumerable <IElement?> gameNodes = response.Content.SelectNodes("//div[@class='cart_item_list']/div"); List <CartData> cartGames = new(); foreach (IElement gameNode in gameNodes) { IElement?eleName = gameNode.SelectSingleElementNode("//div[@class='cart_item_desc']/a"); IElement?elePrice = gameNode.SelectSingleElementNode("//div[@class='price']"); string gameName = eleName.TextContent.Trim() ?? "出错"; string gameLink = eleName.GetAttribute("href") ?? "出错"; Match match = Regex.Match(gameLink, @"\w+\/\d+"); string gamePath = match.Success ? match.Value : "出错"; match = Regex.Match(elePrice.TextContent, @"\d+([.,]\d+)?"); string strPrice = match.Success ? match.Value : "-1"; bool success = float.TryParse(strPrice, out float gamePrice); if (!success) { gamePrice = -1; } cartGames.Add(new CartData(gamePath, gameName, (int)gamePrice * 100)); } int totalPrice = 0; bool purchaseSelf = false, purchaseGift = false; if (cartGames.Count > 0) { IElement?eleTotalPrice = response.Content.SelectSingleNode("//div[@id='cart_estimated_total']"); Match match = Regex.Match(eleTotalPrice.TextContent, @"\d+([.,]\d+)?"); string strPrice = match.Success ? match.Value : "0"; bool success = float.TryParse(strPrice, out float totalProceFloat); if (!success) { totalProceFloat = -1; } totalPrice = (int)totalProceFloat * 100; purchaseSelf = response.Content.SelectSingleNode("//a[@id='btn_purchase_self']") != null; purchaseGift = response.Content.SelectSingleNode("//a[@id='btn_purchase_gift']") != null; } return(new CartResponse(cartGames, totalPrice, purchaseSelf, purchaseGift)); }
public Element() { Tag = string.Empty; Content = string.Empty; Children = new List <Element>(); Attributes = new Dictionary <string, string?>(); Styles = new Dictionary <string, SortedDictionary <int, string> >(); Parent = null; }
public Boolean Match(IElement element, IElement?scope) { if (!String.IsNullOrEmpty(_value)) { var actual = element.GetAttribute(Name) ?? String.Empty; return(actual.HasHyphen(_value, _comparison)); } return(false); }
public ListElement(IElement?parent, IElementLocator locator) : base(parent, locator) { this.parent = parent; this.locator = locator; if (locator.LocatorType != By.XPath && locator.LocatorType != By.CssSelector) { throw new ArgumentException("SpecDrill: For ListElement<> only Css or XPath locators are accepted!"); } }
//解析商店页 internal static StoreResponse?ParseStorePage(HtmlDocumentResponse response) { if (response == null) { return(null); } IEnumerable <IElement> gameNodes = response.Content.SelectNodes("//div[@id='game_area_purchase']/div"); List <SubData> subInfos = new(); foreach (IElement gameNode in gameNodes) { IElement?eleName = gameNode.SelectSingleElementNode("//h1"); IElement?eleForm = gameNode.SelectSingleElementNode("//form"); IElement?elePrice = gameNode.SelectSingleElementNode("//div[@data-price-final]"); if (elePrice == null)//DLC的按钮,无价格 { continue; } string subName = eleName.TextContent.Trim() ?? "出错"; string formName = eleForm.GetAttribute("name") ?? "出错"; string finalPrice = elePrice.GetAttribute("data-price-final") ?? "出错"; Match match = Regex.Match(formName, @"\d+$"); if (uint.TryParse(match.Value, out uint subID) && uint.TryParse(finalPrice, out uint gamePrice)) { bool bundle = formName.IndexOf("bundle") != -1; subInfos.Add(new SubData(bundle, subID, subName, gamePrice)); } else { ASF.ArchiLogger.LogGenericWarning(subName); ASF.ArchiLogger.LogGenericWarning(formName); ASF.ArchiLogger.LogGenericWarning(finalPrice); } } IElement?eleGameName = response.Content.SelectSingleNode("//div[@id='appHubAppName']"); string gameName = eleGameName?.TextContent.Trim() ?? "出错"; if (subInfos.Count == 0) { IElement eleError = response.Content.SelectSingleNode("//div[@id='error_box']/span"); if (eleError != null) { gameName = eleError.TextContent.Trim(); } } return(new StoreResponse(subInfos, gameName)); }
public Boolean Match(IElement element, IElement?scope) { var last = _combinators.Count - 1; if (_combinators[last].Selector.Match(element, scope)) { return(last > 0 ? MatchCascade(last - 1, element, scope) : true); } return(false); }
public DiffContext(SourceCollection controlSources, SourceCollection testSources) { if (controlSources.Count > 0 && controlSources[0].Node.GetRoot() is IElement r1) { _controlRoot = r1; } if (testSources.Count > 0 && testSources[0].Node.GetRoot() is IElement r2) { _testRoot = r2; } }
void DisconnectHandler(object nativeView) { OnDisconnectHandler(nativeView); // VirtualView has already been changed over to a new handler if (VirtualView != null && VirtualView.Handler == this) { VirtualView.Handler = null; } VirtualView = null; }
public Boolean Match(IElement element, IElement?scope) { for (var i = 0; i < _selectors.Count; i++) { if (_selectors[i].Match(element, scope)) { return(true); } } return(false); }
public static IElement upsertHeadElement(IDocument document, string elementName, string nameAttributeName, string nameAttributeValue, string contentAttributeValue) { IElement?el = document.Head.QuerySelector($"{elementName}[{nameAttributeName} = '{nameAttributeValue}']"); if (el == null) { el = document.CreateElement(elementName); el.SetAttribute(nameAttributeName, nameAttributeValue); findEditableRegion(document.Head, "head")?.end.InsertBefore(new INode[] { el, document.CreateTextNode("\n") }); } el.SetAttribute(CONTENT, contentAttributeValue); return(el); }
private Boolean MatchCascade(Int32 pos, IElement element, IElement?scope) { var newElements = _combinators[pos].Transform !(element); foreach (var newElement in newElements) { if (_combinators[pos].Selector.Match(newElement, scope) && (pos == 0 || MatchCascade(pos - 1, newElement, scope))) { return(true); } } return(false); }
/// <summary> /// ページを解析してJSONを取得する /// </summary> /// <param name="source"></param> /// <returns></returns> private RemotePlaylistInfo AnalyzePage(string source) { var series = new RemotePlaylistInfo(); IHtmlDocument?document = HtmlParser.ParseDocument(source); IHtmlCollection <IElement>?videos = document?.QuerySelectorAll(".SeriesVideoListContainer-video"); if (videos is null) { return(series); } IElement?ownerElm = document?.QuerySelector(".SeriesAdditionalContainer-ownerName"); string ownerName = ownerElm?.InnerHtml ?? string.Empty; int ownerID = int.Parse((ownerElm?.GetAttribute("href")?.Split("/")[^ 1]) ?? "0");
/// <summary> /// Determine whether element has any children block level elements. /// </summary> /// <param name="element">Element to operate on</param> /// <returns>bool</returns> internal static bool HasChildBlockElement(IElement?element) { if (element is null) { return(false); } foreach (var child in element.ChildNodes) { if (child is Element el && (divToPElems.Contains(el.TagName) || HasChildBlockElement(el))) { return(true); } } return(false); }
public virtual void SetVirtualView(IElement view) { _ = view ?? throw new ArgumentNullException(nameof(view)); if (VirtualView == view) { return; } var oldVirtualView = VirtualView; if (oldVirtualView?.Handler != null) { oldVirtualView.Handler = null; } bool setupNativeView = oldVirtualView == null; VirtualView = view; NativeView ??= CreateNativeElement(); if (VirtualView.Handler != this) { VirtualView.Handler = this; } if (setupNativeView) { ConnectHandler(NativeView); } _mapper = _defaultMapper; if (VirtualView is IPropertyMapperView imv) { var map = imv.GetPropertyMapperOverrides(); if (map is not null) { map.Chained = new[] { _defaultMapper }; _mapper = map; } } _mapper.UpdateProperties(this, VirtualView); }
internal Article(Uri uri, string title, string?byline, string?dir, string?language, string?author, IElement element, Metadata metadata, bool readable, Reader reader) { _element = element; _reader = reader; Uri = uri; Title = title; Byline = string.IsNullOrWhiteSpace(byline) ? metadata.Author : byline; Dir = dir; Content = Serializer(element); Excerpt = metadata.Excerpt; Language = string.IsNullOrWhiteSpace(metadata.Language) ? language : metadata.Language; PublicationDate = metadata.PublicationDate; Author = string.IsNullOrWhiteSpace(metadata.Author) ? author : metadata.Author; SiteName = metadata.SiteName; IsReadable = readable; FeaturedImage = metadata.FeaturedImage; }
private List <IElement> DiscoverElementContainers() { List <IElement> elementContainers = new List <IElement>(); IElement?current = this.Parent; if (current != null) { do { if (current is IControl || current is IPage) { elementContainers.Add(current); } current = current.Parent; } while (current != null); } return(elementContainers); }
public HtmlFormControlsCollection(IElement form, IElement?root = null) { if (root is null) { root = form.Owner !.DocumentElement; } _elements = root.GetNodes <HtmlFormControlElement>().Where(m => { if (Object.ReferenceEquals(m.Form, form)) { if (m is not IHtmlInputElement input || !input.Type.Is(InputTypeNames.Image)) { return(true); } } return(false); }); }
/// <summary> /// documentを受け取ってJSっぽく解析する /// </summary> /// <param name="document"></param> /// <returns></returns> private INiconicoIchibaInfo GetNiconicoIchibaInfo(IHtmlDocument?document) { IHtmlCollection <IElement>?items = document?.QuerySelectorAll(".IchibaMainItem"); var info = new NiconicoIchibaInfo(); if (items is null) { return(info); } foreach (var item in items) { IElement?titleElm = item.QuerySelector(".IchibaMainItem_Name"); string? title = titleElm?.InnerHtml; string? link = titleElm?.GetAttribute("href"); string? category = item?.QuerySelector(".IchibaMainItem_Info_Category")?.InnerHtml; string price = item?.QuerySelector(".IchibaMainItem_Price_Number")?.InnerHtml ?? "NaN"; string? thumb = item?.QuerySelector(".IchibaMainItem_Thumbnail img")?.GetAttribute("src"); if (title is null || link is null || category is null || price is null || thumb is null) { continue; } var itemInfo = new IchibaItem() { Name = title, LinkUrl = link, Category = category, Price = price, ThumbUrl = thumb, }; info.IchibaItems.Add(itemInfo); } return(info); }
public Boolean Match(IElement element, IElement?scope) { var parent = element.ParentElement; if (parent != null) { var n = Math.Sign(Step); var k = 0; for (var i = parent.ChildNodes.Length - 1; i >= 0; i--) { if (parent.ChildNodes[i] is IHtmlTableCellElement child) { var span = child.ColumnSpan; k += span; if (child == element) { var diff = k - Offset; for (var index = 0; index < span; index++, diff--) { if (diff == 0 || (Math.Sign(diff) == n && diff % Step == 0)) { return(true); } } return(false); } } } } return(false); }
public Boolean Match(IElement element, IElement?scope) => element.HasAttribute(Name);
public Boolean Match(IElement element, IElement?scope) => _action.Invoke(element);
public Element Create(string tag, IElement?parent) { return(tag switch { // Animation "animate" => new SvgAnimate() { Tag = tag }, "animateColor" => new SvgAnimateColor() { Tag = tag }, "animateMotion" => new SvgAnimateMotion() { Tag = tag }, "animateTransform" => new SvgAnimateTransform() { Tag = tag }, "set" => new SvgSet() { Tag = tag }, "mpath" => new SvgMotionPath() { Tag = tag }, // Basic Shapes "circle" => new SvgCircle() { Tag = tag }, "ellipse" => new SvgEllipse() { Tag = tag }, "line" => new SvgLine() { Tag = tag }, "polygon" => new SvgPolygon() { Tag = tag }, "polyline" => new SvgPolyline() { Tag = tag }, "rect" => new SvgRectangle() { Tag = tag }, // Clipping and Masking "clipPath" => new SvgClipPath() { Tag = tag }, "mask" => new SvgMask() { Tag = tag }, // Color "color-profile" => new SvgColorProfile() { Tag = tag }, // Document Structure "defs" => new SvgDefinitionList() { Tag = tag }, "desc" => new SvgDescription() { Tag = tag }, "metadata" => new SvgDocumentMetadata() { Tag = tag }, "svg" => (parent == null) ? new SvgDocument() { Tag = tag } : new SvgFragment() { Tag = tag }, "g" => new SvgGroup() { Tag = tag }, "image" => new SvgImage() { Tag = tag }, "switch" => new SvgSwitch() { Tag = tag }, "symbol" => new SvgSymbol() { Tag = tag }, "title" => new SvgTitle() { Tag = tag }, "use" => new SvgUse() { Tag = tag }, // Extensibility "foreignObject" => new SvgForeignObject() { Tag = tag }, // Filter Effects "filter" => new FilterEffects.SvgFilter() { Tag = tag }, "feBlend" => new FilterEffects.Primitives.SvgBlend() { Tag = tag }, "feColorMatrix" => new FilterEffects.Primitives.SvgColourMatrix() { Tag = tag }, "feComponentTransfer" => new FilterEffects.Primitives.SvgComponentTransfer() { Tag = tag }, "feComposite" => new FilterEffects.Primitives.SvgComposite() { Tag = tag }, "feConvolveMatrix" => new FilterEffects.Primitives.SvgConvolveMatrix() { Tag = tag }, "feDiffuseLighting" => new FilterEffects.Primitives.SvgDiffuseLighting() { Tag = tag }, "feDisplacementMap" => new FilterEffects.Primitives.SvgDisplacementMap() { Tag = tag }, "feDistantLight" => new FilterEffects.Primitives.SvgDistantLight() { Tag = tag }, "feFlood" => new FilterEffects.Primitives.SvgFlood() { Tag = tag }, "feFuncA" => new FilterEffects.Primitives.SvgFuncA() { Tag = tag }, "feFuncB" => new FilterEffects.Primitives.SvgFuncB() { Tag = tag }, "feFuncG" => new FilterEffects.Primitives.SvgFuncG() { Tag = tag }, "feFuncR" => new FilterEffects.Primitives.SvgFuncR() { Tag = tag }, "feGaussianBlur" => new FilterEffects.Primitives.SvgGaussianBlur() { Tag = tag }, "feImage" => new FilterEffects.Primitives.SvgImage() { Tag = tag }, "feMerge" => new FilterEffects.Primitives.SvgMerge() { Tag = tag }, "feMergeNode" => new FilterEffects.Primitives.SvgMergeNode() { Tag = tag }, "feMorphology" => new FilterEffects.Primitives.SvgMorphology() { Tag = tag }, "feOffset" => new FilterEffects.Primitives.SvgOffset() { Tag = tag }, "fePointLight" => new FilterEffects.Primitives.SvgPointLight() { Tag = tag }, "feSpecularLighting" => new FilterEffects.Primitives.SvgSpecularLighting() { Tag = tag }, "feSpotLight" => new FilterEffects.Primitives.SvgSpotLight() { Tag = tag }, "feTile" => new FilterEffects.Primitives.SvgTile() { Tag = tag }, "feTurbulence" => new FilterEffects.Primitives.SvgTurbulence() { Tag = tag }, // Interactivity "cursor" => new SvgCursor() { Tag = tag }, // Linking "a" => new SvgAnchor() { Tag = tag }, "view" => new SvgView() { Tag = tag }, // Painting "stop" => new SvgGradientStop() { Tag = tag }, "linearGradient" => new SvgLinearGradientServer() { Tag = tag }, "marker" => new SvgMarker() { Tag = tag }, "pattern" => new SvgPatternServer() { Tag = tag }, "radialGradient" => new SvgRadialGradientServer() { Tag = tag }, // Paths "path" => new SvgPath() { Tag = tag }, // Scripting "script" => new SvgScript() { Tag = tag }, // Styling "style" => new SvgStyle() { Tag = tag }, // Text "altGlyph" => new SvgAltGlyph() { Tag = tag }, "altGlyphDef" => new SvgAltGlyphDef() { Tag = tag }, "altGlyphItem" => new SvgAltGlyphItem() { Tag = tag }, "font" => new SvgFont() { Tag = tag }, "font-face" => new SvgFontFace() { Tag = tag }, "font-face-format" => new SvgFontFaceFormat() { Tag = tag }, "font-face-name" => new SvgFontFaceName() { Tag = tag }, "font-face-src" => new SvgFontFaceSrc() { Tag = tag }, "font-face-uri" => new SvgFontFaceUri() { Tag = tag }, "glyph" => new SvgGlyph() { Tag = tag }, "glyphRef" => new SvgGlyphRef() { Tag = tag }, "hkern" => new SvgHorizontalKern() { Tag = tag }, "vkern" => new SvgVerticalKern() { Tag = tag }, "missing-glyph" => new SvgMissingGlyph() { Tag = tag }, "text" => new SvgText() { Tag = tag }, "textPath" => new SvgTextPath() { Tag = tag }, "tref" => new SvgTextRef() { Tag = tag }, "tspan" => new SvgTextSpan() { Tag = tag }, // Unknown _ => new UnknownElement() { Tag = tag } });