Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sniffing of CSS and JS files #455

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -15,10 +15,12 @@ import org.readium.r2.shared.util.format.ArchiveSniffer
import org.readium.r2.shared.util.format.AudioSniffer
import org.readium.r2.shared.util.format.BitmapSniffer
import org.readium.r2.shared.util.format.CompositeFormatSniffer
import org.readium.r2.shared.util.format.CssSniffer
import org.readium.r2.shared.util.format.EpubDrmSniffer
import org.readium.r2.shared.util.format.EpubSniffer
import org.readium.r2.shared.util.format.FormatSniffer
import org.readium.r2.shared.util.format.HtmlSniffer
import org.readium.r2.shared.util.format.JavaScriptSniffer
import org.readium.r2.shared.util.format.JsonSniffer
import org.readium.r2.shared.util.format.LcpLicenseSniffer
import org.readium.r2.shared.util.format.LpfSniffer
Expand Down Expand Up @@ -92,5 +94,7 @@ public class DefaultFormatSniffer(
LcpLicenseSniffer,
EpubDrmSniffer,
W3cWpubSniffer,
RwpmSniffer
RwpmSniffer,
CssSniffer,
JavaScriptSniffer
)
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,11 @@ public object Opds1EntrySpecification : Specification
public object Opds2CatalogSpecification : Specification
public object Opds2PublicationSpecification : Specification
public object OpdsAuthenticationSpecification : Specification

/*
* Language specifications
*/

public object JavaScriptSpecification : Specification

public object CssSpecification : Specification
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,55 @@ public object EpubDrmSniffer : FormatSniffer {
}
}

/**
* Sniffs CSS.
*/

public object CssSniffer : FormatSniffer {
override fun sniffHints(format: Format, hints: FormatHints): Format {
if (format.hasAnySpecification()) {
return format
}

if (hints.hasFileExtension("css") ||
hints.hasMediaType("text/css")
) {
return Format(
specification = FormatSpecification(CssSpecification),
mediaType = MediaType.CSS,
fileExtension = FileExtension("css")
)
}

return format
}
}

/**
* Sniffs JavaScript.
*/

public object JavaScriptSniffer : FormatSniffer {
override fun sniffHints(format: Format, hints: FormatHints): Format {
if (format.hasAnySpecification()) {
return format
}

if (hints.hasFileExtension("js") ||
hints.hasMediaType("text/javascript") ||
hints.hasMediaType("application/javascript")
) {
return Format(
specification = FormatSpecification(JavaScriptSpecification),
mediaType = MediaType.JAVASCRIPT,
fileExtension = FileExtension("js")
)
}

return format
}
}

private suspend fun Readable.canReadWholeBlob() =
length().getOrDefault(0) < 5 * 1000 * 1000

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.readium.r2.shared.util.FileExtension
import org.readium.r2.shared.util.Url
import org.readium.r2.shared.util.asset.DefaultFormatSniffer
import org.readium.r2.shared.util.checkSuccess
import org.readium.r2.shared.util.mediatype.MediaType
import org.robolectric.RobolectricTestRunner
Expand Down Expand Up @@ -114,4 +115,28 @@ class DefaultSniffersTest {
).checkSuccess()
)
}

private val cssFormat = Format(
specification = FormatSpecification(CssSpecification),
mediaType = MediaType.CSS,
fileExtension = FileExtension("css")
)

@Test
fun `Sniff CSS`() = runBlocking {
val sniffer = DefaultFormatSniffer()
assertEquals(cssFormat, sniffer.sniffHints(format = cssFormat, hints = FormatHints()))
}

private val jsFormat = Format(
specification = FormatSpecification(JavaScriptSpecification),
mediaType = MediaType.JAVASCRIPT,
fileExtension = FileExtension("js")
)

@Test
fun `Sniff JS`() = runBlocking {
val sniffer = DefaultFormatSniffer()
assertEquals(jsFormat, sniffer.sniffHints(format = jsFormat, hints = FormatHints()))
}
}
Loading