diff options
author | John MacFarlane <jgm@berkeley.edu> | 2017-02-24 11:55:15 +0100 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2017-02-24 11:55:50 +0100 |
commit | 7c0a80c323f81e6262848bfcfc922301e3f406e0 (patch) | |
tree | fda43daa2a75e1a7d8b85972b313cfbdbf13ce5a /src/Text/Pandoc | |
parent | 0e8b19e709c61b0eda8ba2191d99991ad3f50458 (diff) | |
download | pandoc-7c0a80c323f81e6262848bfcfc922301e3f406e0.tar.gz |
SelfContained: don't use data URIs for script or style.
Instead, just use script or style tags with the content inside.
The old method with data URIs prevents certain optimizations
outside pandoc.
Exception: data URIs are still used when a script contains
`</script>` or a style contains `</`.
Closes #3423.
Also, in MIME, use application/javascript (not
application/x-javascript).
Diffstat (limited to 'src/Text/Pandoc')
-rw-r--r-- | src/Text/Pandoc/MIME.hs | 2 | ||||
-rw-r--r-- | src/Text/Pandoc/SelfContained.hs | 46 |
2 files changed, 37 insertions, 11 deletions
diff --git a/src/Text/Pandoc/MIME.hs b/src/Text/Pandoc/MIME.hs index 328dbf928..6fe3d6b20 100644 --- a/src/Text/Pandoc/MIME.hs +++ b/src/Text/Pandoc/MIME.hs @@ -244,7 +244,7 @@ mimeTypesList = -- List borrowed from happstack-server. ,("jpeg","image/jpeg") ,("jfif","image/jpeg") ,("jpg","image/jpeg") - ,("js","application/x-javascript") + ,("js","application/javascript") ,("kar","audio/midi") ,("key","application/pgp-keys") ,("kil","application/x-killustrator") diff --git a/src/Text/Pandoc/SelfContained.hs b/src/Text/Pandoc/SelfContained.hs index 6038baa2f..8cbd70e26 100644 --- a/src/Text/Pandoc/SelfContained.hs +++ b/src/Text/Pandoc/SelfContained.hs @@ -67,12 +67,12 @@ makeDataURI (mime, raw) = then mime ++ ";charset=utf-8" else mime -- mime type already has charset -convertTag :: PandocMonad m => Maybe String -> Tag String -> m (Tag String) +convertTag :: PandocMonad m => Maybe String -> Tag String -> m [Tag String] convertTag sourceURL t@(TagOpen tagname as) | tagname `elem` ["img", "embed", "video", "input", "audio", "source", "track"] = do as' <- mapM processAttribute as - return $ TagOpen tagname as' + return [TagOpen tagname as'] where processAttribute (x,y) = if x == "src" || x == "data-src" || x == "href" || x == "poster" then do @@ -81,17 +81,43 @@ convertTag sourceURL t@(TagOpen tagname as) else return (x,y) convertTag sourceURL t@(TagOpen "script" as) = case fromAttrib "src" t of - [] -> return t + [] -> return [t] src -> do - enc <- getDataURI sourceURL (fromAttrib "type" t) src - return $ TagOpen "script" (("src",enc) : [(x,y) | (x,y) <- as, x /= "src"]) + let typeAttr = fromAttrib "type" t + res <- getData sourceURL typeAttr src + case res of + Left dataUri -> return [TagOpen "script" + (("src",dataUri) : [(x,y) | (x,y) <- as, x /= "src"])] + Right (mime, bs) + | (mime == "text/javascript" || + mime == "application/javascript" || + mime == "application/x-javascript") && + not ("</" `B.isInfixOf` bs) -> + return [ + TagOpen "script" [("type", typeAttr)|not (null typeAttr)] + , TagText (toString bs) + , TagClose "script" ] + | otherwise -> return [TagOpen "script" + (("src",makeDataURI (mime, bs)) : + [(x,y) | (x,y) <- as, x /= "src"])] convertTag sourceURL t@(TagOpen "link" as) = case fromAttrib "href" t of - [] -> return t + [] -> return [t] src -> do - enc <- getDataURI sourceURL (fromAttrib "type" t) src - return $ TagOpen "link" (("href",enc) : [(x,y) | (x,y) <- as, x /= "href"]) -convertTag _ t = return t + res <- getData sourceURL (fromAttrib "type" t) src + case res of + Left dataUri -> return [TagOpen "link" + (("href",dataUri) : [(x,y) | (x,y) <- as, x /= "href"])] + Right (mime, bs) + | mime == "text/css" && not ("</" `B.isInfixOf` bs) -> + return [ + TagOpen "style" [("type", "text/css")] + , TagText (toString bs) + , TagClose "style" ] + | otherwise -> return [TagOpen "link" + (("href",makeDataURI (mime, bs)) : + [(x,y) | (x,y) <- as, x /= "href"])] +convertTag _ t = return [t] cssURLs :: PandocMonad m => Maybe String -> FilePath -> ByteString -> m ByteString @@ -184,5 +210,5 @@ getData sourceURL mimetype src = do makeSelfContained :: PandocMonad m => WriterOptions -> String -> m String makeSelfContained opts inp = do let tags = parseTags inp - out' <- mapM (convertTag (writerSourceURL opts)) tags + out' <- concat <$> mapM (convertTag (writerSourceURL opts)) tags return $ renderTags' out' |