commit dc7d92ffbab9cda6929046f9b5fbea1a625d807f
parent 739eca230112f7a88b625f6b72b9a99f40274384
Author: tongong <tongong@gmx.net>
Date: Wed, 29 Jun 2022 17:22:06 +0200
added html->css bundling
Diffstat:
7 files changed, 65 insertions(+), 23 deletions(-)
diff --git a/README.md b/README.md
@@ -48,6 +48,5 @@ the `-p` option. Input and output file name stay relative to the cwd.
`tacker` does not aim to be 100% spec-compliant. The goal is to work in all
common scenarios without laying to much emphasis on obscure edge cases. It is a
tacker after all - not an industrial robot. Though unlike a real-world tacker
-your security should not be at hazard. In the case of javascript malicious
-source files can obviously take over your bundle but they should never take
-over your system.
+your security should not be at hazard. Malicious source files can obviously
+take over your bundled page but they can never take over your system.
diff --git a/bundle_html.ha b/bundle_html.ha
@@ -51,6 +51,38 @@ fn tacker_html(inputpath: str, ofile: io::handle) void = {
tacker_js(src, ofile);
fmt::fprint(ofile, "</script>")!;
};
+ } else if (m == 2) { // stylesheets
+ let tagbuf = bufio::dynamic(io::mode::RDWR);
+ defer io::close(&tagbuf)!;
+ fmt::fprint(&tagbuf, "<link")!;
+ searchio::search(ifile, &tagbuf, p_tagclose);
+ fmt::fprint(&tagbuf, ">")!;
+ const rel = tag_get_attr(tagbuf.buf,
+ strings::toutf8("rel"));
+ let is_style = rel is tag_split;
+ const rel = if (is_style) {
+ const rel = rel: tag_split;
+ const rel = strings::fromutf8(rel.1);
+ if (rel != "stylesheet")
+ is_style = false;
+ yield rel;
+ } else "";
+ if (!is_style) {
+ io::write(ofile, tagbuf.buf)!;
+ } else {
+ const href = tag_get_attr(tagbuf.buf,
+ strings::toutf8("href"));
+ if (href is not_found)
+ fixed_fatalf("broken style tag \"{}\".", strings::fromutf8(tagbuf.buf));
+ const href = href: tag_split;
+ const href = strings::fromutf8(href.1);
+ fmt::fprint(ofile, "<style>\n")!;
+ const href = resolve_path(href,
+ inputpath);
+ defer free(href);
+ tacker_js(href, ofile);
+ fmt::fprint(ofile, "</style>")!;
+ };
} else {
// TODO other embeds
fmt::fatal("[TODO] unimplemented html tag transform");
diff --git a/main.ha b/main.ha
@@ -47,10 +47,7 @@ export fn main() void = {
const defaultfrom = strings::join("", os::getcwd(), "/");
defer free(defaultfrom);
const ifile = resolve_path(ifile, defaultfrom);
- // has to be uncommented again when the bug is fixed
- // https://todo.sr.ht/~sircmpwn/hare/717
- // (because of fmt::fatalf)
- // defer free(ifile);
+ defer free(ifile);
let extstart = lastdotindex(ifile);
if (extstart == -1)
@@ -61,6 +58,13 @@ export fn main() void = {
case "html" => tacker_html(ifile, ofile);
case "js" => tacker_js(ifile, ofile);
case "css" => tacker_css(ifile, ofile);
- case => fmt::fatalf("unknown filetype: \"{}\".", ifile);
+ case => fixed_fatalf("unknown filetype: \"{}\".", ifile);
};
};
+
+// Should be replaced by fmt::fatalf when the issue with the compiler is fixed
+// https://todo.sr.ht/~sircmpwn/hare/717
+// Currently defer free() does not work correctly with @noreturn functions
+fn fixed_fatalf(fmt: str, args: fmt::field...) void = {
+ fmt::fatalf(fmt, args...);
+};
diff --git a/test-page/a.js b/test-page/a.js
@@ -0,0 +1,4 @@
+// let testm = require("./b.js")
+// console.log(testm.hello());
+
+console.log("hi from an imported script!");
diff --git a/test-page/index.html b/test-page/index.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <!-- asdf < test -->
+ <!-- <script src="./a.js"></script> -->
+ <link href="./styles.css" rel="stylesheet">
+ <script attribute=value a2 = 'val2src="wrong"' src="./a.js"></script>
+ <script a3 = "asdf">
+ console.log("inline script");
+ </script>
+ </head>
+ <body>
+ <h1>test page</h1>
+ </body>
+</html>
diff --git a/test-page/styles.css b/test-page/styles.css
@@ -0,0 +1,3 @@
+body {
+ background-color: yellow;
+}
diff --git a/test/index.html b/test/index.html
@@ -1,15 +0,0 @@
-<!DOCTYPE html>
-<html>
- <head>
- <!-- asdf < test -->
- <!-- <script src="./a.js"></script> -->
- <script attribute=value a2 = 'val2src="wrong"' src="./a.js"></script>
- <script a3 = "asdf">
- console.log("test");
- let end = "</script";
- </script>
- </head>
- <body>
-
- </body>
-</html>