diff options
author | Noah Campbell <noahcampbell@gmail.com> | 2013-11-09 18:35:09 +0400 |
---|---|---|
committer | Noah Campbell <noahcampbell@gmail.com> | 2013-11-09 18:35:09 +0400 |
commit | ef595aedfce66caf2e8560b3605d38e95872e1ca (patch) | |
tree | a39d7bb419db8598f06dae75d90306474a7cf1f7 | |
parent | 90a902c843758bb0d4c6da5ace4b9a813ab3a10e (diff) |
Handle schema-less urls when apply absurl
Fixes #114
-rw-r--r-- | transform/absurl.go | 15 | ||||
-rw-r--r-- | transform/posttrans_test.go | 4 |
2 files changed, 13 insertions, 6 deletions
diff --git a/transform/absurl.go b/transform/absurl.go index 80716f1b7..8bdd36515 100644 --- a/transform/absurl.go +++ b/transform/absurl.go @@ -23,15 +23,22 @@ func AbsURL(absURL string) (trs []link, err error) { hrefsq = []byte(" href='" + base + "/") ) trs = append(trs, func(content []byte) []byte { - content = bytes.Replace(content, []byte(" src=\"/"), srcdq, -1) - content = bytes.Replace(content, []byte(" src='/"), srcsq, -1) - content = bytes.Replace(content, []byte(" href=\"/"), hrefdq, -1) - content = bytes.Replace(content, []byte(" href='/"), hrefsq, -1) + content = guardReplace(content, []byte(" src=\"//"), []byte(" src=\"/"), srcdq) + content = guardReplace(content, []byte(" src='//"), []byte(" src='/"), srcsq) + content = guardReplace(content, []byte(" href=\"//"), []byte(" href=\"/"), hrefdq) + content = guardReplace(content, []byte(" href='//"), []byte(" href='/"), hrefsq) return content }) return } +func guardReplace(content, guard, match, replace []byte) []byte { + if !bytes.Contains(content, guard) { + content = bytes.Replace(content, match, replace, -1) + } + return content +} + type elattr struct { tag, attr string } diff --git a/transform/posttrans_test.go b/transform/posttrans_test.go index d7d06fee7..297b4f66e 100644 --- a/transform/posttrans_test.go +++ b/transform/posttrans_test.go @@ -12,8 +12,7 @@ const H5_JS_CONTENT_SINGLE_QUOTE = "<!DOCTYPE html><html><head><script src='foob const H5_JS_CONTENT_ABS_URL = "<!DOCTYPE html><html><head><script src=\"http://user@host:10234/foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"https://host/foobar\">foobar</a>. Follow up</article></body></html>" -// URL doesn't recognize authorities. BUG? -//const H5_JS_CONTENT_ABS_URL = "<!DOCTYPE html><html><head><script src=\"//host/foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"https://host/foobar\">foobar</a>. Follow up</article></body></html>" +const H5_JS_CONTENT_ABS_URL_SCHEMALESS = "<!DOCTYPE html><html><head><script src=\"//host/foobar.js\"></script><script src='//host2/barfoo.js'></head><body><nav><h1>title</h1></nav><article>content <a href=\"//host/foobar\">foobar</a>. <a href='//host2/foobar'>Follow up</a></article></body></html>" const CORRECT_OUTPUT_SRC_HREF_DQ = "<!DOCTYPE html><html><head><script src=\"foobar.js\"></script><script src=\"http://base/barfoo.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"foobar\">foobar</a>. <a href=\"http://base/foobar\">Follow up</a></article></body></html>" @@ -34,6 +33,7 @@ var abs_url_tests = []test{ {H5_JS_CONTENT_DOUBLE_QUOTE, CORRECT_OUTPUT_SRC_HREF_DQ}, {H5_JS_CONTENT_SINGLE_QUOTE, CORRECT_OUTPUT_SRC_HREF_SQ}, {H5_JS_CONTENT_ABS_URL, H5_JS_CONTENT_ABS_URL}, + {H5_JS_CONTENT_ABS_URL_SCHEMALESS, H5_JS_CONTENT_ABS_URL_SCHEMALESS}, } type errorf func(string, ...interface{}) |