2013-10-01 15:59:29 -04:00
|
|
|
package transform
|
|
|
|
|
|
|
|
import (
|
|
|
|
htmltran "code.google.com/p/go-html-transform/html/transform"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
2013-11-01 01:14:11 -04:00
|
|
|
func AbsURL(absURL string) (trs []*htmltran.Transform, err error) {
|
|
|
|
var baseURL *url.URL
|
2013-10-01 15:59:29 -04:00
|
|
|
|
2013-11-01 01:14:11 -04:00
|
|
|
if baseURL, err = url.Parse(absURL); err != nil {
|
2013-10-01 15:59:29 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-11-01 01:14:11 -04:00
|
|
|
if trs, err = absUrlify(baseURL, elattr{"a", "href"}, elattr{"script", "src"}); err != nil {
|
2013-10-01 15:59:29 -04:00
|
|
|
return
|
|
|
|
}
|
2013-11-01 01:14:11 -04:00
|
|
|
return
|
2013-10-01 15:59:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type elattr struct {
|
|
|
|
tag, attr string
|
|
|
|
}
|
|
|
|
|
2013-11-01 01:14:11 -04:00
|
|
|
func absUrlify(baseURL *url.URL, selectors ...elattr) (trs []*htmltran.Transform, err error) {
|
|
|
|
var inURL *url.URL
|
2013-10-01 15:59:29 -04:00
|
|
|
|
|
|
|
replace := func(in string) string {
|
|
|
|
if inURL, err = url.Parse(in); err != nil {
|
|
|
|
return in + "?"
|
|
|
|
}
|
|
|
|
if fragmentOnly(inURL) {
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
return baseURL.ResolveReference(inURL).String()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, el := range selectors {
|
2013-11-01 01:14:11 -04:00
|
|
|
mt := htmltran.MustTrans(htmltran.TransformAttrib(el.attr, replace), el.tag)
|
|
|
|
trs = append(trs, mt)
|
2013-10-01 15:59:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func fragmentOnly(u *url.URL) bool {
|
|
|
|
return u.Fragment != "" && u.Scheme == "" && u.Opaque == "" && u.User == nil && u.Host == "" && u.Path == "" && u.Path == "" && u.RawQuery == ""
|
|
|
|
}
|