Skip to content

Commit

Permalink
Implement sniffing of CSS and JS files (readium#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
io7m authored Jan 31, 2024
1 parent 5e807f6 commit 9dffa11
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
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 @@ -20,6 +20,7 @@ import org.readium.r2.shared.util.data.EmptyContainer
import org.readium.r2.shared.util.file.FileResource
import org.readium.r2.shared.util.format.AvifSpecification
import org.readium.r2.shared.util.format.BmpSpecification
import org.readium.r2.shared.util.format.CssSpecification
import org.readium.r2.shared.util.format.EpubSpecification
import org.readium.r2.shared.util.format.Format
import org.readium.r2.shared.util.format.FormatHints
Expand All @@ -28,6 +29,7 @@ import org.readium.r2.shared.util.format.GifSpecification
import org.readium.r2.shared.util.format.HtmlSpecification
import org.readium.r2.shared.util.format.InformalAudiobookSpecification
import org.readium.r2.shared.util.format.InformalComicSpecification
import org.readium.r2.shared.util.format.JavaScriptSpecification
import org.readium.r2.shared.util.format.JpegSpecification
import org.readium.r2.shared.util.format.JsonSpecification
import org.readium.r2.shared.util.format.JxlSpecification
Expand Down Expand Up @@ -840,4 +842,44 @@ class AssetSnifferTest {
).checkSuccess()
)
}

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

@Test
fun `sniff CSS`() = runBlocking {
assertEquals(
cssFormat,
sniffer.sniffFileExtension("css").checkSuccess()
)
assertEquals(
cssFormat,
sniffer.sniffMediaType("text/css").checkSuccess()
)
}

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

@Test
fun `sniff JavaScript`() = runBlocking {
assertEquals(
jsFormat,
sniffer.sniffFileExtension("js").checkSuccess()
)
assertEquals(
jsFormat,
sniffer.sniffMediaType("text/javascript").checkSuccess()
)
assertEquals(
jsFormat,
sniffer.sniffMediaType("application/javascript").checkSuccess()
)
}
}

0 comments on commit 9dffa11

Please sign in to comment.