11package iterator
22
33import (
4- "net/url"
4+ nurl "net/url"
55 "strings"
66 "unicode"
77 "unicode/utf8"
88
99 "github.com/readium/go-toolkit/pkg/content/element"
1010 iutil "github.com/readium/go-toolkit/pkg/internal/util"
1111 "github.com/readium/go-toolkit/pkg/manifest"
12- "github.com/readium/go-toolkit/pkg/util"
12+ "github.com/readium/go-toolkit/pkg/mediatype"
13+ "github.com/readium/go-toolkit/pkg/util/url"
1314 "golang.org/x/net/html"
1415 "golang.org/x/net/html/atom"
1516)
@@ -72,14 +73,15 @@ func getAttr(n *html.Node, key string) string {
7273 return ""
7374}
7475
75- func srcRelativeToHref (n * html.Node , base string ) * string {
76+ func srcRelativeToHref (n * html.Node , base url. URL ) url. URL {
7677 if n == nil {
7778 return nil
7879 }
7980
8081 if v := getAttr (n , "src" ); v != "" {
81- h , _ := util .NewHREF (v , base ).String ()
82- return & h
82+ if u , _ := url .URLFromString (v ); u != nil {
83+ return base .Resolve (u )
84+ }
8385 }
8486 return nil
8587}
@@ -336,10 +338,10 @@ func (c *HTMLConverter) Head(n *html.Node, depth int) {
336338 cssSelector = & cs
337339 }
338340 elementLocator := manifest.Locator {
339- Href : c .baseLocator .Href ,
340- Type : c .baseLocator .Type ,
341- Title : c .baseLocator .Title ,
342- Text : c .baseLocator .Text ,
341+ Href : c .baseLocator .Href ,
342+ MediaType : c .baseLocator .MediaType ,
343+ Title : c .baseLocator .Title ,
344+ Text : c .baseLocator .Text ,
343345 Locations : manifest.Locations {
344346 OtherLocations : map [string ]interface {}{
345347 "cssSelector" : cssSelector ,
@@ -361,7 +363,7 @@ func (c *HTMLConverter) Head(n *html.Node, depth int) {
361363 c .elements = append (c .elements , element .NewImageElement (
362364 elementLocator ,
363365 manifest.Link {
364- Href : * href ,
366+ Href : manifest . NewHREF ( href ) ,
365367 },
366368 "" , // FIXME: Get the caption from figcaption
367369 atlist ,
@@ -372,18 +374,20 @@ func (c *HTMLConverter) Head(n *html.Node, depth int) {
372374 var link * manifest.Link
373375 if href != nil {
374376 link = & manifest.Link {
375- Href : * href ,
377+ Href : manifest . NewHREF ( href ) ,
376378 }
377379 } else {
378380 sourceNodes := childrenOfType (n , atom .Source , 1 )
379381 sources := make ([]manifest.Link , len (sourceNodes ))
380382 for _ , source := range sourceNodes {
381383 if src := srcRelativeToHref (source , c .baseLocator .Href ); src != nil {
382384 l := manifest.Link {
383- Href : * src ,
385+ Href : manifest . NewHREF ( href ) ,
384386 }
385387 if typ := getAttr (source , "type" ); typ != "" {
386- l .Type = typ
388+ if mt , err := mediatype .NewOfString (typ ); err == nil {
389+ l .MediaType = & mt
390+ }
387391 }
388392 sources = append (sources , l )
389393 }
@@ -495,7 +499,7 @@ func (c *HTMLConverter) flushText() {
495499 quote := element.Quote {}
496500 for _ , at := range el .Attr {
497501 if at .Key == "cite" {
498- quote .ReferenceURL , _ = url .Parse (at .Val )
502+ quote .ReferenceURL , _ = nurl .Parse (at .Val )
499503 }
500504 if at .Key == "title" {
501505 quote .ReferenceTitle = at .Val
@@ -512,9 +516,9 @@ func (c *HTMLConverter) flushText() {
512516 }
513517 el := element .NewTextElement (
514518 manifest.Locator {
515- Href : c .baseLocator .Href ,
516- Type : c .baseLocator .Type ,
517- Title : c .baseLocator .Title ,
519+ Href : c .baseLocator .Href ,
520+ MediaType : c .baseLocator .MediaType ,
521+ Title : c .baseLocator .Title ,
518522 Locations : manifest.Locations {
519523 OtherLocations : map [string ]interface {}{},
520524 },
@@ -563,9 +567,9 @@ func (c *HTMLConverter) flushSegment() {
563567 }
564568 seg := element.TextSegment {
565569 Locator : manifest.Locator {
566- Href : c .baseLocator .Href ,
567- Type : c .baseLocator .Type ,
568- Title : c .baseLocator .Title ,
570+ Href : c .baseLocator .Href ,
571+ MediaType : c .baseLocator .MediaType ,
572+ Title : c .baseLocator .Title ,
569573 Locations : manifest.Locations {
570574 // TODO fix: needs to use baseLocator locations too!
571575 OtherLocations : map [string ]interface {}{},
0 commit comments