diff --git a/src/main/kotlin/nl/lengrand/opengraphkt/Parser.kt b/src/main/kotlin/nl/lengrand/opengraphkt/Parser.kt index 3f6d3b6..4fc88c0 100644 --- a/src/main/kotlin/nl/lengrand/opengraphkt/Parser.kt +++ b/src/main/kotlin/nl/lengrand/opengraphkt/Parser.kt @@ -50,11 +50,18 @@ class Parser { println(tags) println(cleanTags) - // Extract the basic required Open Graph properties - val title = tags.select("meta[property=og:title]").attr("content") - val image = tags.select("meta[property=og:image]").attr("content") - val url = tags.select("meta[property=og:url]").attr("content") - val type = tags.select("meta[property=og:type]").attr("content") + val title = + if (tags.select("meta[property=og:title]").isEmpty()) null + else tags.select("meta[property=og:title]").attr("content") + val image = + if (tags.select("meta[property=og:image]").isEmpty()) null + else tags.select("meta[property=og:image]").attr("content") + val url = + if (tags.select("meta[property=og:url]").isEmpty()) null + else tags.select("meta[property=og:url]").attr("content") + val type = + if (tags.select("meta[property=og:type]").isEmpty()) null + else tags.select("meta[property=og:type]").attr("content") return OpenGraph( tags, diff --git a/src/test/kotlin/nl/lengrand/opengraphkt/ParserTest.kt b/src/test/kotlin/nl/lengrand/opengraphkt/ParserTest.kt index 07efa72..162a186 100644 --- a/src/test/kotlin/nl/lengrand/opengraphkt/ParserTest.kt +++ b/src/test/kotlin/nl/lengrand/opengraphkt/ParserTest.kt @@ -92,21 +92,15 @@ class ParserTest { val document = fetcher.fromString(incompleteHtml) val openGraph = parser.extractOpenGraphTags(document) - // Verify that the available property is extracted correctly assertEquals("The Rock", openGraph.title) - // Verify that missing properties are empty strings - assertEquals("", openGraph.type) - assertEquals("", openGraph.url) - assertEquals("", openGraph.image) + assertEquals(null, openGraph.type) + assertEquals(null, openGraph.url) + assertEquals(null, openGraph.image) - // Verify that the OpenGraph object is not valid due to missing required properties assertFalse(openGraph.isValid()) - - // Verify that only the available tags are extracted assertEquals(2, openGraph.tags.size) - // Verify specific tag content val descriptionTag = openGraph.tags.find { it.property == "description" } assertNotNull(descriptionTag) assertEquals("An action movie about a rock", descriptionTag.content) @@ -117,16 +111,13 @@ class ParserTest { val document = fetcher.fromString(noOgHtml) val openGraph = parser.extractOpenGraphTags(document) - // Verify that all properties are empty strings - assertEquals("", openGraph.title) - assertEquals("", openGraph.type) - assertEquals("", openGraph.url) - assertEquals("", openGraph.image) + // Verify that all properties are null + assertEquals(null, openGraph.title) + assertEquals(null, openGraph.type) + assertEquals(null, openGraph.url) + assertEquals(null, openGraph.image) - // Verify that the OpenGraph object is not valid assertFalse(openGraph.isValid()) - - // Verify that no tags are extracted assertEquals(0, openGraph.tags.size) } }