From 5b4764eeb9c8801474ea917993c79bcab87e83e5 Mon Sep 17 00:00:00 2001 From: Ay1nDas Date: Sun, 30 Nov 2025 10:04:34 +0530 Subject: [PATCH 1/2] Fix Suprious Unsupported URI warning for web hyperlinks Silenced 'UnsupportedURIException' so that 'Unsupported URI' doesn't get reported on anchor links. Fixes: https://gitlab.com/inkscape/inkscape/-/issues/5784 --- src/object/uri-references.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/object/uri-references.cpp b/src/object/uri-references.cpp index 7b74c86216..eb4273f0ad 100644 --- a/src/object/uri-references.cpp +++ b/src/object/uri-references.cpp @@ -199,6 +199,9 @@ bool URIReference::try_attach(char const *uri) try { attach(Inkscape::URI(uri)); return true; + } catch (Inkscape::UnsupportedURIException &e) { + // Explicitly SILENCE this (no g_warning). + // Just fall through to detach() and return false. } catch (Inkscape::BadURIException &e) { g_warning("%s", e.what()); } -- GitLab From 7af66868d993672c7b059d0141b9dcc5f427cb37 Mon Sep 17 00:00:00 2001 From: Ay1nDas Date: Tue, 2 Dec 2025 23:50:26 +0530 Subject: [PATCH 2/2] Add tests for try_attach() from uri-reference.cpp Add unit tests for URIReference external link handling. Verify that try_attach silences UnsupportedURIException for web links but warns on malformed inputs. --- testfiles/CMakeLists.txt | 1 + testfiles/src/uri-references-test.cpp | 129 ++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 testfiles/src/uri-references-test.cpp diff --git a/testfiles/CMakeLists.txt b/testfiles/CMakeLists.txt index 64678ca849..d98babd056 100644 --- a/testfiles/CMakeLists.txt +++ b/testfiles/CMakeLists.txt @@ -68,6 +68,7 @@ set(TEST_SOURCES colors/color-set-test colors/xml-color-test uri-test + uri-references-test util-test util-units-test util-expression-evaluator-test diff --git a/testfiles/src/uri-references-test.cpp b/testfiles/src/uri-references-test.cpp new file mode 100644 index 0000000000..c56ca32f8c --- /dev/null +++ b/testfiles/src/uri-references-test.cpp @@ -0,0 +1,129 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** + * Unit tests for URIReference + * @file + * Test URI Reference from src/object/ + */ + +#include +#include +#include +#include +#include +#include + +#include "document.h" +#include "inkscape.h" +#include "inkscape-application.h" +#include "object/sp-root.h" +#include "object/uri-references.h" + +using namespace std::literals; + +class URIReferenceTest : public ::testing::Test { +public: + static void SetUpTestCase() { + if (!Inkscape::Application::exists()) { + Inkscape::Application::create(false); + } + } + +protected: + std::unique_ptr doc; + SPObject *root = nullptr; + + void SetUp() override { + constexpr auto svg_data = R"A( + + + + )A"sv; + + auto span = std::span(svg_data.data(), svg_data.size()); + doc = SPDocument::createNewDocFromMem(span, "uri-ref-test.svg"); + + ASSERT_NE(doc, nullptr) << "Failed to create test document"; + + root = doc->getRoot(); + ASSERT_NE(root, nullptr); + } +}; + +/* +* Helper: Intercepts GLib warnings and turns them into GTest failures +*/ +void FailOnWarning(const gchar *log_domain, GLogLevelFlags log_level, + const gchar *message, gpointer user_data) { + // Record a failure, DO NOT abort/crash the program + ADD_FAILURE() << "Unexpected Warning: " << message; +} + +/* +* Helper: If it matches "Malformed URI", it marks success. +* If it's something else it records a FAILURE. +*/ +void ExpectMalformedOrFail(const gchar *log_domain, GLogLevelFlags log_level, + const gchar *message, gpointer user_data) { + bool* saw_expected = static_cast(user_data); + + if (std::string(message).find("Malformed URI") != std::string::npos) { + *saw_expected = true; + } else { + // It was the WRONG/NO warning. Fail the test. + ADD_FAILURE() << "Unexpected Warning received: " << message; + } +} + +/** + * Test Case: Internal Links + * Expectation: Returns TRUE and finds the object. + */ +TEST_F(URIReferenceTest, AcceptsInternalLinks) +{ + Inkscape::URIReference ref(root); + + bool result = ref.try_attach("#rect1"); + EXPECT_TRUE(result) << "try_attach should return true for valid internal ID"; + + if (ref.isAttached()) { + EXPECT_STREQ(ref.getObject()->getId(), "rect1"); + } +} + +/** + * Test Case: Web Links + * Expectation: Returns FALSE (did not attach), but NO console warning. + */ +TEST_F(URIReferenceTest, SilencesUnsupportedURI) +{ + guint handler_id = g_log_set_handler(nullptr, G_LOG_LEVEL_WARNING, FailOnWarning, nullptr); + + Inkscape::URIReference ref(root); + + // HTTP + bool result = ref.try_attach("http://example.com"); + EXPECT_FALSE(result) << "try_attach should return false silently for http"; + + // HTTPS + bool result_https = ref.try_attach("https://inkscape.org"); + EXPECT_FALSE(result_https) << "try_attach should return false silently for https"; + + g_log_remove_handler(nullptr, handler_id); +} + +/** + * Test Case: Malformed URIs + * Expectation: Returns FALSE and REPORTS a console warning. + */ +TEST_F(URIReferenceTest, WarnsOnMalformed) +{ + Inkscape::URIReference ref(root); + + bool saw_expected = false; + guint handler_id = g_log_set_handler(nullptr, G_LOG_LEVEL_WARNING, ExpectMalformedOrFail, &saw_expected); + + ref.try_attach("#xpointer(id(broken"); + g_log_remove_handler(nullptr, handler_id); + + EXPECT_TRUE(saw_expected) << "Test finished but expected 'Malformed URI' warning was never seen."; +} -- GitLab