diff --git a/autotests/structureddata/empty-microdata-with-json-ld-inside.html b/autotests/structureddata/empty-microdata-with-json-ld-inside.html new file mode 100644 --- /dev/null +++ b/autotests/structureddata/empty-microdata-with-json-ld-inside.html @@ -0,0 +1,34 @@ + + + + + + + + +
+

Konqi Resort and Spa

+

+ Discover a 4 star hotel in the heart of Paris. +

+ + Come visit us + + +
+ + + + diff --git a/autotests/structureddata/empty-microdata-with-json-ld-inside.json b/autotests/structureddata/empty-microdata-with-json-ld-inside.json new file mode 100644 --- /dev/null +++ b/autotests/structureddata/empty-microdata-with-json-ld-inside.json @@ -0,0 +1,13 @@ +[ + { + "@context": "http://schema.org", + "@type": "Hotel", + "description": "Discover a 4 star hotel in the heart of Paris.", + "image": { + "@type": "ImageObject", + "url": "https://community.kde.org/images.community/thumb/4/40/Mascot_konqi.png/144px-Mascot_konqi.png" + }, + "name": "Konqi Resort and Spa", + "url": "https://www.kde.org" + } +] diff --git a/src/generic/structureddataextractor.cpp b/src/generic/structureddataextractor.cpp --- a/src/generic/structureddataextractor.cpp +++ b/src/generic/structureddataextractor.cpp @@ -29,6 +29,11 @@ using namespace KItinerary; +static bool isJsonLdTag(const HtmlElement &elem) +{ + return elem.name() == QLatin1String("script") && elem.attribute(QStringLiteral("type")) == QLatin1String("application/ld+json"); +} + static QByteArray fixupJson(const QByteArray &data) { auto output(data); @@ -112,7 +117,10 @@ } } else if (!prop.isEmpty()) { obj.insert(prop, valueForItemProperty(child)); - } else { + // Maybe there is more JSON-LD inside this microdata tree + } else if (isJsonLdTag(child)) { + parseJson(child.content().toUtf8(), result); + } else { // skip intermediate nodes without Microdata annotations parseMicroData(child, obj, result); } @@ -123,7 +131,7 @@ static void extractRecursive(const HtmlElement &elem, QJsonArray &result) { // JSON-LD - if (elem.name() == QLatin1String("script") && elem.attribute(QStringLiteral("type")) == QLatin1String("application/ld+json")) { + if (isJsonLdTag(elem)) { parseJson(elem.content().toUtf8(), result); return; }