Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.hotwire.navigation.fragments

// Android's WebView falls back to the formatted URL when a page has no <title>.
// Chromium drops the scheme, a leading "www." and a bare host's trailing slash,
// and decodes spaces, so compare both sides with the same parts removed.
internal fun displayTitle(title: String?, url: String?): String {
val pageTitle = title.orEmpty()
return if (pageTitle.asDisplayUrl() == url.orEmpty().asDisplayUrl()) "" else pageTitle
}

private fun String.asDisplayUrl(): String {
return substringAfter("://")
.removePrefix("www.")
.replace("%20", " ")
.trimEnd('/')
}
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ internal class HotwireWebFragmentDelegate(
}

private fun title(): String {
return webView.title ?: ""
return displayTitle(webView.title, webView.url)
}

private fun registerFileChooserLauncher(): ActivityResultLauncher<Intent> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.hotwire.navigation.fragments

import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class DisplayTitleTest {
@Test
fun `shows the page title`() {
assertThat(displayTitle("Dashboard", "https://example.com/dashboard")).isEqualTo("Dashboard")
}

@Test
fun `hides a missing title`() {
assertThat(displayTitle(null, "https://example.com/dashboard")).isEmpty()
assertThat(displayTitle(null, null)).isEmpty()
}

@Test
fun `hides the fallback title for an https url`() {
assertThat(displayTitle("example.com/dashboard", "https://example.com/dashboard")).isEmpty()
}

@Test
fun `hides the fallback title for an http url with a port`() {
assertThat(displayTitle("192.168.42.51:3000/dashboard", "http://192.168.42.51:3000/dashboard")).isEmpty()
}

@Test
fun `hides the fallback title without www`() {
assertThat(displayTitle("example.com/dashboard", "https://www.example.com/dashboard")).isEmpty()
}

@Test
fun `hides the fallback title for a bare host`() {
assertThat(displayTitle("example.com", "https://example.com/")).isEmpty()
}

@Test
fun `hides the fallback title with decoded spaces`() {
assertThat(displayTitle("example.com/my page", "https://example.com/my%20page")).isEmpty()
}

@Test
fun `hides the fallback title for about blank`() {
assertThat(displayTitle("about:blank", "about:blank")).isEmpty()
}

@Test
fun `shows a title that differs from the url only by path`() {
assertThat(displayTitle("example.com", "https://example.com/dashboard")).isEqualTo("example.com")
}
}
Loading