1
0
mirror of https://github.com/AllanWang/Frost-for-Facebook.git synced 2024-11-10 04:52:38 +01:00

Feature/compact formatter (#337)

* Make components private and thus jvm fields

* Split converter and decoder

* Update changelog

* Only decode nonquery portions of links

* Add comments and fix test
This commit is contained in:
Allan Wang 2017-09-24 17:53:07 -04:00 committed by GitHub
parent 38d7b144cd
commit 952d4e41ef
3 changed files with 40 additions and 11 deletions

View File

@ -11,16 +11,25 @@ val String.formattedFbUrl: String
get() = FbUrlFormatter(this).toString() get() = FbUrlFormatter(this).toString()
class FbUrlFormatter(url: String) { class FbUrlFormatter(url: String) {
val queries = mutableMapOf<String, String>() private val queries = mutableMapOf<String, String>()
val cleaned: String private val cleaned: String
/**
* Formats all facebook urls
*
* The order is very important:
* 1. Wrapper links (discardables) are stripped away, resulting in the actual link
* 2. CSS encoding is converted to normal encoding
* 3. Query portions are separated from the cleaned url
* 4. The cleaned url is decoded. Queries are kept as is!
*/
init { init {
if (url.isBlank()) cleaned = "" if (url.isBlank()) cleaned = ""
else { else {
var cleanedUrl = url var cleanedUrl = url
discardable.forEach { cleanedUrl = cleanedUrl.replace(it, "", true) } discardable.forEach { cleanedUrl = cleanedUrl.replace(it, "", true) }
val changed = cleanedUrl != url //note that discardables strip away the first ? val changed = cleanedUrl != url //note that discardables strip away the first '?'
decoder.forEach { (k, v) -> cleanedUrl = cleanedUrl.replace(k, v, true) } converter.forEach { (k, v) -> cleanedUrl = cleanedUrl.replace(k, v, true) }
val qm = cleanedUrl.indexOf(if (changed) "&" else "?") val qm = cleanedUrl.indexOf(if (changed) "&" else "?")
if (qm > -1) { if (qm > -1) {
cleanedUrl.substring(qm + 1).split("&").forEach { cleanedUrl.substring(qm + 1).split("&").forEach {
@ -29,6 +38,8 @@ class FbUrlFormatter(url: String) {
} }
cleanedUrl = cleanedUrl.substring(0, qm) cleanedUrl = cleanedUrl.substring(0, qm)
} }
//only decode non query portion of the url
decoder.forEach { (k, v) -> cleanedUrl = cleanedUrl.replace(k, v, true) }
discardableQueries.forEach { queries.remove(it) } discardableQueries.forEach { queries.remove(it) }
if (cleanedUrl.startsWith("#!")) cleanedUrl = cleanedUrl.substring(2) if (cleanedUrl.startsWith("#!")) cleanedUrl = cleanedUrl.substring(2)
if (cleanedUrl.startsWith("/")) cleanedUrl = FB_URL_BASE + cleanedUrl.substring(1) if (cleanedUrl.startsWith("/")) cleanedUrl = FB_URL_BASE + cleanedUrl.substring(1)
@ -83,15 +94,29 @@ class FbUrlFormatter(url: String) {
"%60" to "`", "%3B" to ";", "%2F" to "/", "%3F" to "?", "%60" to "`", "%3B" to ";", "%2F" to "/", "%3F" to "?",
"%3A" to ":", "%40" to "@", "%3D" to "=", "%26" to "&", "%3A" to ":", "%40" to "@", "%3D" to "=", "%26" to "&",
"%24" to "$", "%2B" to "+", "%22" to "\"", "%2C" to ",", "%24" to "$", "%2B" to "+", "%22" to "\"", "%2C" to ",",
"%20" to " ", "%20" to " "
//css )
@JvmStatic
val cssDecoder = mapOf(
"\\3C " to "<", "\\3E " to ">", "\\23 " to "#", "\\25 " to "%", "\\3C " to "<", "\\3E " to ">", "\\23 " to "#", "\\25 " to "%",
"\\7B " to "{", "\\7D " to "}", "\\7C " to "|", "\\5C " to "\\", "\\7B " to "{", "\\7D " to "}", "\\7C " to "|", "\\5C " to "\\",
"\\5E " to "^", "\\7E " to "~", "\\5B " to "[", "\\5D " to "]", "\\5E " to "^", "\\7E " to "~", "\\5B " to "[", "\\5D " to "]",
"\\60 " to "`", "\\3B " to ";", "\\2F " to "/", "\\3F " to "?", "\\60 " to "`", "\\3B " to ";", "\\2F " to "/", "\\3F " to "?",
"\\3A " to ":", "\\40 " to "@", "\\3D " to "=", "\\26 " to "&", "\\3A " to ":", "\\40 " to "@", "\\3D " to "=", "\\26 " to "&",
"\\24 " to "$", "\\2B " to "+", "\\22 " to "\"", "\\2C " to ",", "\\24 " to "$", "\\2B " to "+", "\\22 " to "\"", "\\2C " to ",",
"\\20 " to " " "%20" to " "
)
@JvmStatic
val converter = mapOf(
"\\3C " to "%3C", "\\3E " to "%3E", "\\23 " to "%23", "\\25 " to "%25",
"\\7B " to "%7B", "\\7D " to "%7D", "\\7C " to "%7C", "\\5C " to "%5C",
"\\5E " to "%5E", "\\7E " to "%7E", "\\5B " to "%5B", "\\5D " to "%5D",
"\\60 " to "%60", "\\3B " to "%3B", "\\2F " to "%2F", "\\3F " to "%3F",
"\\3A " to "%3A", "\\40 " to "%40", "\\3D " to "%3D", "\\26 " to "%26",
"\\24 " to "%24", "\\2B " to "%2B", "\\22 " to "%22", "\\2C " to "%2C",
"\\20 " to "%20"
) )
} }
} }

View File

@ -45,8 +45,8 @@ class FbUrlTest {
@Test @Test
fun css() { fun css() {
val expected = "https://test.com?efg=hi&oh=bye&oe=apple" val expected = "https://test.com?efg=hi&oh=bye&oe=apple%3Fornot"
val orig = "https\\3a //test.com?efg\\3d hi\\26 oh\\3d bye\\26 oe\\3d apple" val orig = "https\\3a //test.com?efg=hi&oh=bye&oe=apple\\3F ornot"
assertFbFormat(expected, orig) assertFbFormat(expected, orig)
} }

View File

@ -1,11 +1,15 @@
# Changelog # Changelog
## v1.5.4 ## v1.5.4
* Numerous bug fixes in KAU
* Set background back to white on non facebook pages
* Make read notification/message colors more obvious
* Clean up and small bug fixes
## v1.5.2
* Add default download manager to download all files * Add default download manager to download all files
* Limit notification sounds when multiple notifications come in * Limit notification sounds when multiple notifications come in
* Check that job scheduler exists before scheduling notifications * Check that job scheduler exists before scheduling notifications
* Make read notification/message colors more obvious
* Clean up and small bug fixes
## v1.5.1 ## v1.5.1
* Release day is here! * Release day is here!