You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ta...@apache.org on 2023/08/16 18:48:05 UTC
[tika] branch main updated: TIKA-3109 (#1291)
This is an automated email from the ASF dual-hosted git repository.
tallison pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tika.git
The following commit(s) were added to refs/heads/main by this push:
new 7dabca968 TIKA-3109 (#1291)
7dabca968 is described below
commit 7dabca968202cefcafa089570c19cb11809e82cb
Author: Tim Allison <ta...@apache.org>
AuthorDate: Wed Aug 16 14:47:58 2023 -0400
TIKA-3109 (#1291)
* TIKA-3109 -- parse iframe's srcdoc as an embedded document
---
CHANGES.txt | 2 +
.../org/apache/tika/parser/html/HtmlHandler.java | 22 +++++++++++
.../org/apache/tika/parser/html/SrcDocTest.java | 43 ++++++++++++++++++++++
.../test/resources/test-documents/testSrcDoc.html | 22 +++++++++++
4 files changed, 89 insertions(+)
diff --git a/CHANGES.txt b/CHANGES.txt
index 015a55e43..df3c6b573 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -7,6 +7,8 @@ Release 2.8.1 - ???
* Fix bug that led to duplicate extraction of macros from some OLE2 containers (TIKA-4116).
+ * Parse iframe's srcdoc as an embedded file (TIKA-3109).
+
* Add detection of warc.gz as a specialization of gz and parse as if a standard WARC (TIKA-4048).
* Allow users to modify the attachment limit size in the /unpack resource (TIKA-4039)
diff --git a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-html-module/src/main/java/org/apache/tika/parser/html/HtmlHandler.java b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-html-module/src/main/java/org/apache/tika/parser/html/HtmlHandler.java
index ae2de95d5..3050422c4 100644
--- a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-html-module/src/main/java/org/apache/tika/parser/html/HtmlHandler.java
+++ b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-html-module/src/main/java/org/apache/tika/parser/html/HtmlHandler.java
@@ -187,6 +187,12 @@ class HtmlHandler extends TextContentHandler {
handleDataURIScheme(value);
}
}
+ if ("IFRAME".equals(name)) {
+ String srcDoc = atts.getValue("srcdoc");
+ if (!StringUtils.isBlank(srcDoc)) {
+ handleSrcDoc(srcDoc);
+ }
+ }
}
/**
@@ -339,6 +345,22 @@ class HtmlHandler extends TextContentHandler {
discardLevel--;
}
}
+ private void handleSrcDoc(String string) throws SAXException {
+ Metadata m = new Metadata();
+ m.set(TikaCoreProperties.EMBEDDED_RESOURCE_TYPE,
+ TikaCoreProperties.EmbeddedResourceType.INLINE.toString());
+ m.set(TikaCoreProperties.CONTENT_TYPE_PARSER_OVERRIDE, "text/html");
+ //TODO add metadata about iframe content?
+ EmbeddedDocumentExtractor embeddedDocumentExtractor =
+ EmbeddedDocumentUtil.getEmbeddedDocumentExtractor(context);
+ if (embeddedDocumentExtractor.shouldParseEmbedded(m)) {
+ try (InputStream stream = new UnsynchronizedByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8))) {
+ embeddedDocumentExtractor.parseEmbedded(stream, xhtml, m, true);
+ } catch (IOException e) {
+ EmbeddedDocumentUtil.recordEmbeddedStreamException(e, metadata);
+ }
+ }
+ }
private void handleDataURIScheme(String string) throws SAXException {
DataURIScheme dataURIScheme = null;
diff --git a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-html-module/src/test/java/org/apache/tika/parser/html/SrcDocTest.java b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-html-module/src/test/java/org/apache/tika/parser/html/SrcDocTest.java
new file mode 100644
index 000000000..e7f6d1c70
--- /dev/null
+++ b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-html-module/src/test/java/org/apache/tika/parser/html/SrcDocTest.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.tika.parser.html;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
+
+import org.apache.tika.TikaTest;
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.metadata.TikaCoreProperties;
+
+public class SrcDocTest extends TikaTest {
+
+
+ @Test
+ public void testBasic() throws Exception {
+ List<Metadata> metadataList = getRecursiveMetadata("testSrcDoc.html");
+ assertEquals(2, metadataList.size());
+ assertContains("outside", metadataList.get(0).get(TikaCoreProperties.TIKA_CONTENT));
+ assertContains("this is the iframe content",
+ metadataList.get(1).get(TikaCoreProperties.TIKA_CONTENT));
+ assertEquals(TikaCoreProperties.EmbeddedResourceType.INLINE.toString(),
+ metadataList.get(1).get(TikaCoreProperties.EMBEDDED_RESOURCE_TYPE));
+ }
+}
diff --git a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-html-module/src/test/resources/test-documents/testSrcDoc.html b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-html-module/src/test/resources/test-documents/testSrcDoc.html
new file mode 100644
index 000000000..8d33de7d8
--- /dev/null
+++ b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-html-module/src/test/resources/test-documents/testSrcDoc.html
@@ -0,0 +1,22 @@
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<html>
+<body>
+outside
+<iframe srcdoc="<p>this is the iframe content</p>"></iframe>
+</body>
+</html>
\ No newline at end of file