예제 #1
0
        public void TestMetaElement()
        {
            var element = new StructuredMetadata(this.ns, "title", "my title");

            Assert.Equal("my title", element);
            Assert.Equal(@"<meta property=""og:title"" content=""my title"">", element.ToString());
        }
예제 #2
0
        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());
        }
예제 #3
0
        /// <summary>
        /// Adds the metadata.
        /// </summary>
        /// <param name="prefix">The prefix.</param>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        /// <exception cref="InvalidOperationException">The prefix {prefix} does not exist in the NamespaceRegistry.</exception>
        public void AddMetadata(string prefix, string name, string value)
        {
            if (!NamespaceRegistry.Instance.Namespaces.ContainsKey(prefix))
            {
                throw new InvalidOperationException($"The prefix {prefix} does not exist in the {nameof(NamespaceRegistry)}");
            }

            var ns = NamespaceRegistry.Instance.Namespaces[prefix];

            var metadata = new StructuredMetadata(ns, name, value);

            this.AddMetadata(metadata);
        }
예제 #4
0
        /// <summary>
        /// Adds the meta element.
        /// </summary>
        /// <param name="element">The element.</param>
        public void AddMetadata(StructuredMetadata element)
        {
            var key = string.Concat(element.Namespace.Prefix, ":", element.Name);

            if (this.internalOpenGraphData.ContainsKey(key))
            {
                this.internalOpenGraphData[key].Add(element);
            }
            else
            {
                this.internalOpenGraphData.Add(key, new List <StructuredMetadata> {
                    element
                });
            }
        }
예제 #5
0
        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);
        }
예제 #6
0
        /// <summary>
        /// Adds the meta element.
        /// </summary>
        /// <param name="element">The element.</param>
        public void AddMetadata(StructuredMetadata element)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }

            var key = string.Concat(element.Namespace.Prefix, ":", element.Name);

            if (this.internalOpenGraphData.ContainsKey(key))
            {
                this.internalOpenGraphData[key].Add(element);
            }
            else
            {
                this.internalOpenGraphData.Add(key, new List <StructuredMetadata> {
                    element
                });
            }
        }
예제 #7
0
        /// <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);
        }