Skip to content
Open
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
2 changes: 1 addition & 1 deletion pkg/content/iterator/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewHTML(resource fetcher.Resource, locator manifest.Locator) *HTMLContentIt

func HTMLFactory() ResourceContentIteratorFactory {
return func(resource fetcher.Resource, locator manifest.Locator) Iterator {
if resource.Link().MediaType.Matches(&mediatype.HTML, &mediatype.XHTML) {
if mt := resource.Link().MediaType; mt != nil && mt.Matches(&mediatype.HTML, &mediatype.XHTML) {
return NewHTML(resource, locator)
}
return nil
Expand Down
16 changes: 8 additions & 8 deletions pkg/manifest/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (ll LinkList) FilterByRel(rel string) LinkList {
// Finds the first link matching the given media type.
func (ll LinkList) FirstWithMediaType(mt *mediatype.MediaType) *Link {
for _, link := range ll {
if link.MediaType.Matches(mt) {
if link.MediaType != nil && link.MediaType.Matches(mt) {
return &link
}
}
Expand All @@ -268,7 +268,7 @@ func (ll LinkList) FirstWithMediaType(mt *mediatype.MediaType) *Link {
func (ll LinkList) FilterByMediaType(mt ...*mediatype.MediaType) LinkList {
flinks := make(LinkList, 0)
for _, link := range ll {
if link.MediaType.Matches(mt...) {
if link.MediaType != nil && link.MediaType.Matches(mt...) {
flinks = append(flinks, link)
}
}
Expand All @@ -278,7 +278,7 @@ func (ll LinkList) FilterByMediaType(mt ...*mediatype.MediaType) LinkList {
// Returns whether all the resources in the collection are bitmaps.
func (ll LinkList) AllAreBitmap() bool {
for _, link := range ll {
if !link.MediaType.IsBitmap() {
if link.MediaType == nil || !link.MediaType.IsBitmap() {
return false
}
}
Expand All @@ -288,7 +288,7 @@ func (ll LinkList) AllAreBitmap() bool {
// Returns whether all the resources in the collection are audio clips.
func (ll LinkList) AllAreAudio() bool {
for _, link := range ll {
if !link.MediaType.IsAudio() {
if link.MediaType == nil || !link.MediaType.IsAudio() {
return false
}
}
Expand All @@ -298,7 +298,7 @@ func (ll LinkList) AllAreAudio() bool {
// Returns whether all the resources in the collection are video clips.
func (ll LinkList) AllAreVideo() bool {
for _, link := range ll {
if !link.MediaType.IsVideo() {
if link.MediaType == nil || !link.MediaType.IsVideo() {
return false
}
}
Expand All @@ -308,7 +308,7 @@ func (ll LinkList) AllAreVideo() bool {
// Returns whether all the resources in the collection are bitmaps or video clips.
func (ll LinkList) AllAreVisual() bool {
for _, link := range ll {
if !link.MediaType.IsBitmap() && !link.MediaType.IsVideo() {
if link.MediaType == nil || (!link.MediaType.IsBitmap() && !link.MediaType.IsVideo()) {
return false
}
}
Expand All @@ -318,7 +318,7 @@ func (ll LinkList) AllAreVisual() bool {
// Returns whether all the resources in the collection are HTML documents.
func (ll LinkList) AllAreHTML() bool {
for _, link := range ll {
if !link.MediaType.IsHTML() {
if link.MediaType == nil || !link.MediaType.IsHTML() {
return false
}
}
Expand All @@ -328,7 +328,7 @@ func (ll LinkList) AllAreHTML() bool {
// Returns whether all the resources in the collection are matching the given media type.
func (ll LinkList) AllMatchMediaType(mt ...*mediatype.MediaType) bool {
for _, link := range ll {
if !link.MediaType.Matches(mt...) {
if link.MediaType == nil || !link.MediaType.Matches(mt...) {
return false
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/audio/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (p AudioParser) accepts(ctx context.Context, asset asset.PublicationAsset,
if extensions.IsHiddenOrThumbs(path) {
continue
}
if link.MediaType.IsBitmap() {
if link.MediaType != nil && link.MediaType.IsBitmap() {
continue
}
fext := filepath.Ext(strings.ToLower(path))
Expand Down
4 changes: 2 additions & 2 deletions pkg/parser/image/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (p ImageParser) Parse(ctx context.Context, asset asset.PublicationAsset, fe
path := link.URL(nil, nil).Path()

// Filter out all irrelevant files
if extensions.IsHiddenOrThumbs(path) || !link.MediaType.IsBitmap() {
if extensions.IsHiddenOrThumbs(path) || link.MediaType == nil || !link.MediaType.IsBitmap() {
continue
}
readingOrder = append(readingOrder, link)
Expand Down Expand Up @@ -94,7 +94,7 @@ func (p ImageParser) accepts(ctx context.Context, asset asset.PublicationAsset,
if extensions.IsHiddenOrThumbs(path) {
continue
}
if link.MediaType.IsBitmap() {
if link.MediaType != nil && link.MediaType.IsBitmap() {
continue
}
fext := filepath.Ext(strings.ToLower(path))
Expand Down
9 changes: 6 additions & 3 deletions pkg/streamer/a11y_infer.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func inferA11yMetadataInPublicationManifest(ctx context.Context, pub *pub.Public

for _, link := range allResources {
mt := link.MediaType
if mt == nil {
continue
}
if mt.IsAudio() ||
mt.IsVideo() ||
mt.Matches(&mediatype.PDF) {
Expand Down Expand Up @@ -109,7 +112,7 @@ func inferA11yMetadataInPublicationManifest(ctx context.Context, pub *pub.Public
// audio or video resource (inspect "resources" and "readingOrder" in
// RWPM).
for _, link := range allResources {
if link.MediaType.IsAudio() || link.MediaType.IsVideo() {
if link.MediaType != nil && (link.MediaType.IsAudio() || link.MediaType.IsVideo()) {
inferredA11y.AccessModes = append(inferredA11y.AccessModes, manifest.A11yAccessModeAuditory)
break
}
Expand All @@ -119,7 +122,7 @@ func inferA11yMetadataInPublicationManifest(ctx context.Context, pub *pub.Public
// or a video resource (inspect "resources" and "readingOrder" in
// RWPM).
for _, link := range allResources {
if link.MediaType.IsBitmap() || link.MediaType.IsVideo() {
if link.MediaType != nil && (link.MediaType.IsBitmap() || link.MediaType.IsVideo()) {
inferredA11y.AccessModes = append(inferredA11y.AccessModes, manifest.A11yAccessModeVisual)
break
}
Expand Down Expand Up @@ -170,7 +173,7 @@ func inferA11yMetadataInPublicationManifest(ctx context.Context, pub *pub.Public
}

for _, link := range mf.Resources {
if link.MediaType.Matches(&mediatype.SMIL) {
if link.MediaType != nil && link.MediaType.Matches(&mediatype.SMIL) {
addFeature(manifest.A11yFeatureSynchronizedAudioText)
break
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/streamer/a11y_infer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,34 @@ func newLink(mt mediatype.MediaType, extension string) manifest.Link {
}
}

// A resource with an unparseable media type is treated as unclassified.
func TestInferA11yMetadataTreatsNilMediaTypeAsUnclassified(t *testing.T) {
nilMediaTypeLink := manifest.Link{
Href: manifest.MustNewHREFFromString("file.ttf", false),
}
require.Nil(t, nilMediaTypeLink.MediaType)

m := manifest.Manifest{
Metadata: manifest.Metadata{
ConformsTo: manifest.Profiles{manifest.ProfileEPUB},
Layout: manifest.LayoutReflowable,
},
ReadingOrder: []manifest.Link{
newLink(mediatype.HTML, "html"),
},
Resources: []manifest.Link{
nilMediaTypeLink,
},
}

require.NotPanics(t, func() {
res, err := inferA11yMetadataInPublicationManifest(context.TODO(), pub.New(m, nil, nil), nil)
require.NoError(t, err)
assert.NotNil(t, res)
assert.Contains(t, res.AccessModes, manifest.A11yAccessModeTextual)
})
}

// If the publication contains a reference to an audio or video resource
// (inspect "resources" and "readingOrder" in RWPM).
func TestInferAuditoryAccessMode(t *testing.T) {
Expand Down