From adb13dde5e026553fab49dc8b76292a74bc317e9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 24 Apr 2026 12:11:32 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=9A=A7=20HTML=20filter=20can=20now=20extra?= =?utf8?q?ct=20HTML=20from=20additional=20states,=20too?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/rhynodge/filters/HtmlFilter.java | 9 ++++++- .../rhynodge/filters/HtmlFilterTest.kt | 29 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/net/pterodactylus/rhynodge/filters/HtmlFilterTest.kt diff --git a/src/main/java/net/pterodactylus/rhynodge/filters/HtmlFilter.java b/src/main/java/net/pterodactylus/rhynodge/filters/HtmlFilter.java index 9c83745..d314abd 100644 --- a/src/main/java/net/pterodactylus/rhynodge/filters/HtmlFilter.java +++ b/src/main/java/net/pterodactylus/rhynodge/filters/HtmlFilter.java @@ -50,7 +50,14 @@ public class HtmlFilter implements Filter { checkState(state instanceof HttpState, "state is not a HttpState but a %s", state.getClass().getName()); logger.trace(String.format("Got HTML: %s, %s", ((HttpState) state).contentType(), ((HttpState) state).content())); Document document = Jsoup.parse(((HttpState) state).content(), ((HttpState) state).uri()); - return new HtmlState(((HttpState) state).uri(), document); + var htmlState = new HtmlState(((HttpState) state).uri(), document); + state.getAdditionalStates().forEach(additionalState -> { + if (additionalState instanceof HttpState) { + var additionalDocument = Jsoup.parse(((HttpState) additionalState).content(), ((HttpState) additionalState).uri()); + htmlState.addState(new HtmlState(((HttpState) additionalState).uri(), additionalDocument)); + } + }); + return htmlState; } } diff --git a/src/test/kotlin/net/pterodactylus/rhynodge/filters/HtmlFilterTest.kt b/src/test/kotlin/net/pterodactylus/rhynodge/filters/HtmlFilterTest.kt new file mode 100644 index 0000000..e5f482e --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/rhynodge/filters/HtmlFilterTest.kt @@ -0,0 +1,29 @@ +package net.pterodactylus.rhynodge.filters + +import net.pterodactylus.rhynodge.states.HtmlState +import net.pterodactylus.rhynodge.states.HttpState +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.jupiter.api.Test + +class HtmlFilterTest { + + @Test + fun `html filter can extract html from http state`() { + val httpState = HttpState("uri", 123, "content/test", "test".toByteArray()) + val htmlFilter = HtmlFilter() + val htmlState = htmlFilter.filter(httpState) as HtmlState + assertThat(htmlState.document().select("body.test").text(), equalTo("test")) + } + + @Test + fun `html filter can extract html from multi-http state`() { + val httpState = HttpState("uri", 123, "content/test", "bar".toByteArray()) + httpState.addState(HttpState("uri", 123, "content/test", "quo".toByteArray())) + val htmlFilter = HtmlFilter() + val htmlState = htmlFilter.filter(httpState) as HtmlState + assertThat(htmlState.document().select("body.foo").text(), equalTo("bar")) + assertThat((htmlState.additionalStates.first() as HtmlState).document().select("body.baz").text(), equalTo("quo")) + } + +} -- 2.7.4