aboutsummaryrefslogtreecommitdiff
path: root/compute_scripts.awk
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2022-01-03 21:26:12 +0100
committerWojtek Kosior <koszko@koszko.org>2022-01-03 21:26:12 +0100
commita00926f143147ac70656a995f3c97959567c3884 (patch)
tree3e798f9bc49ce1db40e5fdf745d1e041b76a3df9 /compute_scripts.awk
parent3840192d67a38604cfd6738c4f07d181a668ae68 (diff)
downloadbrowser-extension-a00926f143147ac70656a995f3c97959567c3884.tar.gz
browser-extension-a00926f143147ac70656a995f3c97959567c3884.zip
redesign CSS loading
Diffstat (limited to 'compute_scripts.awk')
-rwxr-xr-xcompute_scripts.awk29
1 files changed, 27 insertions, 2 deletions
diff --git a/compute_scripts.awk b/compute_scripts.awk
index e5efc49..2d7ea72 100755
--- a/compute_scripts.awk
+++ b/compute_scripts.awk
@@ -56,6 +56,7 @@ BEGIN {
directive_args_patterns["EXPORT"] = "^(" EXPORT_AS_re "|" identifier_re ")$"
directive_args_patterns["LOADJS"] = "^[^[:space:]]+$"
+ directive_args_patterns["LOADCSS"] = "^[^[:space:]]+$"
}
function validate_path(read_path, path, line) {
@@ -125,6 +126,7 @@ function process_file(path, read_path, mode,
if (mode == "html" && path == read_path) {
clear_array(page_js_deps)
page_js_deps_count = 0
+ clear_array(page_css_deps)
}
modes[path] = mode
@@ -200,7 +202,7 @@ function process_file(path, read_path, mode,
if (directive !~ \
/^(IF|ENDIF|ELSE|ELIF|ERROR|INCLUDE|INCLUDE_VERBATIM|COPY_FILE)$/ &&
(mode != "js" || directive !~ /^(IMPORT|FROM|EXPORT)$/) &&
- (mode != "html" || directive !~ /^LOADJS$/) &&
+ (mode != "html" || directive !~ /^(LOADJS|LOADCSS)$/) &&
(mode != "manifest" || directive !~ /^(LOADJS|LOADHTML)$/)) {
printf "ERROR: Invalid # directive in %s: %s\n",
read_path, line > "/dev/stderr"
@@ -300,6 +302,10 @@ function process_file(path, read_path, mode,
if (load_js_file(path, read_path, directive_args, line) < 1)
return 1
}
+ } else if (directive == "LOADCSS") {
+ if (load_css_file(path, read_path, directive_args, line,
+ page_css_deps))
+ return 1
} else if (directive == "LOADHTML") {
if (load_html_file(path, read_path, directive_args, line))
return 1
@@ -537,7 +543,7 @@ function compute_deps(js_path, dependencies, count, dependencies_added,
}
# Here js_deps and js_deps_count are optional args, used when loading scripts
-# into an HTML page to avoid having th same script loaded twice in multiple
+# into an HTML page to avoid having the same script loaded twice in multiple
# places.
function load_js_file(root_path, read_path, loaded_path, line,
js_deps, js_deps_count,
@@ -576,6 +582,25 @@ function load_js_file(root_path, read_path, loaded_path, line,
return js_deps_count
}
+# css_deps is an array used to avoid having the same stylesheet loaded twice in
+# multiple places in a single HTML page.
+function load_css_file(root_path, read_path, loaded_path, line, css_deps) {
+ delete css_deps[""]
+
+ if (validate_path(read_path, loaded_path, line))
+ return 1
+
+ if (!(loaded_path in css_deps)) {
+ css_deps[loaded_path]
+ to_copy[loaded_path]
+ added_line = ("<link rel=\"stylesheet\" type=\"text/css\" " \
+ "href=\"/" loaded_path "\" />")
+ add_line(root_path, added_line)
+ }
+
+ return 0
+}
+
function load_html_file(root_path, read_path, loaded_path, line) {
if (validate_path(read_path, loaded_path, line))
return 1