public void TestStructuredElements() { var @expected = @"<meta property=""og:image"" content=""img1.png""><meta property=""og:image:height"" content=""30""><meta property=""og:image:width"" content=""60"">"; var element = new StructuredMetadata(this.ns, "image", "img1.png"); element.AddProperty(new PropertyMetadata("height", "30")); element.AddProperty(new PropertyMetadata("width", "60")); Assert.Equal("img1.png", element); Assert.Equal(expected, element.ToString()); }
public void TestStructuredElementWithArrayProperty() { var @expected = @"<meta property=""og:locale"" content=""es""><meta property=""og:locale:alternate"" content=""es_ES""><meta property=""og:locale:alternate"" content=""es_US"">"; var element = new StructuredMetadata(this.ns, "locale", "es"); element.AddProperty(new PropertyMetadata("alternate", "es_ES")); element.AddProperty(new PropertyMetadata("alternate", "es_US")); Assert.Equal("es", element); Assert.Equal(expected, element.ToString()); Assert.Equal("es", element.Value); Assert.Equal("es_ES", element.Properties["alternate"][0].Value); Assert.Equal("es_US", element.Properties["alternate"][1].Value); }
/// <summary> /// Parses the HTML. /// </summary> /// <param name="result">The result.</param> /// <param name="content">The content.</param> /// <param name="validateSpecification">if set to <c>true</c> [validate specification].</param> /// <returns><see cref="OpenGraph"/>.</returns> /// <exception cref="OpenGraphNet.InvalidSpecificationException">The parsed HTML does not meet the open graph specification.</exception> private static OpenGraph ParseHtml(OpenGraph result, string content, bool validateSpecification = false) { HtmlDocument document = MakeDocumentToParse(content); ParseNamespaces(result, document); HtmlNodeCollection allMeta = document.DocumentNode.SelectNodes("//meta"); var openGraphMetaTags = from meta in allMeta ?? new HtmlNodeCollection(null) where (meta.Attributes.Contains("property") && MatchesNamespacePredicate(meta.Attributes["property"].Value)) || (meta.Attributes.Contains("name") && MatchesNamespacePredicate(meta.Attributes["name"].Value)) select meta; StructuredMetadata lastElement = null; foreach (HtmlNode metaTag in openGraphMetaTags) { var prefix = GetOpenGraphPrefix(metaTag); SetNamespace(result, prefix); if (!result.Namespaces.ContainsKey(prefix)) { continue; } string value = GetOpenGraphValue(metaTag); string property = GetOpenGraphKey(metaTag); var cleanProperty = CleanOpenGraphKey(prefix, property); value = HtmlDecodeUrl(property, value); if (lastElement != null && lastElement.IsMyProperty(property)) { lastElement.AddProperty(cleanProperty, value); } else if (IsChildOfExistingElement(result.internalOpenGraphData, property)) { var matchingElement = result.internalOpenGraphData.First(kvp => kvp.Value.First().IsMyProperty(property)); var element = matchingElement.Value.FirstOrDefault(e => !e.Properties.ContainsKey(cleanProperty)); element?.AddProperty(cleanProperty, value); } else { lastElement = new StructuredMetadata(result.Namespaces[prefix], cleanProperty, value); result.AddMetadata(lastElement); } } result.Type = string.Empty; if (result.internalOpenGraphData.TryGetValue("og:type", out var type)) { result.Type = (type.FirstOrDefault() ?? new NullMetadata()).Value ?? string.Empty; } result.Title = string.Empty; if (result.internalOpenGraphData.TryGetValue("og:title", out var title)) { result.Title = (title.FirstOrDefault() ?? new NullMetadata()).Value ?? string.Empty; } result.Image = GetUri(result, "og:image"); result.Url = GetUri(result, "og:url"); if (validateSpecification) { ValidateSpecification(result); } return(result); }