summaryrefslogtreecommitdiff
path: root/src/c/urweb.c
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adam@chlipala.net>2012-04-27 09:52:16 -0400
committerGravatar Adam Chlipala <adam@chlipala.net>2012-04-27 09:52:16 -0400
commit2f1297ee97bcd0bea48d6e8c0af3fecf40744193 (patch)
tree5dca2c2df7a7666a08d5a0bce5878d0e4b49bb2c /src/c/urweb.c
parentc4daee27278ae99dd4d2cd0507f7ffc555d266f4 (diff)
Avoid generating empty <script> tags
Diffstat (limited to 'src/c/urweb.c')
-rw-r--r--src/c/urweb.c80
1 files changed, 51 insertions, 29 deletions
diff --git a/src/c/urweb.c b/src/c/urweb.c
index ab14144d..93635fd1 100644
--- a/src/c/urweb.c
+++ b/src/c/urweb.c
@@ -3223,38 +3223,60 @@ void uw_commit(uw_context ctx) {
} else if (!strncmp(s, "<head>", 6)) {
// <head> is present. Let's add the <script> tags immediately after it.
- size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script);
- size_t lenP = lenH + 40 + len;
- char *start = s + 6, *oldPage = ctx->page.start;
-
- ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
- start += ctx->page.start - oldPage;
- memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
- ctx->page.front += lenP;
- memcpy(start, ctx->script_header, lenH);
- memcpy(start + lenH, "<script type=\"text/javascript\">", 31);
- memcpy(start + lenH + 31, ctx->script.start, len);
- memcpy(start + lenH + 31 + len, "</script>", 9);
+ // Any freeform JavaScript to include?
+ if (uw_buffer_used(&ctx->script) > 0) {
+ size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script);
+ size_t lenP = lenH + 40 + len;
+ char *start = s + 6, *oldPage = ctx->page.start;
+
+ ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
+ start += ctx->page.start - oldPage;
+ memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
+ ctx->page.front += lenP;
+ memcpy(start, ctx->script_header, lenH);
+ memcpy(start + lenH, "<script type=\"text/javascript\">", 31);
+ memcpy(start + lenH + 31, ctx->script.start, len);
+ memcpy(start + lenH + 31 + len, "</script>", 9);
+ } else {
+ size_t lenH = strlen(ctx->script_header);
+ char *start = s + 6, *oldPage = ctx->page.start;
+
+ ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenH);
+ start += ctx->page.start - oldPage;
+ memmove(start + lenH, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
+ ctx->page.front += lenH;
+ memcpy(start, ctx->script_header, lenH);
+ }
} else {
// No <head>. At this point, add it, with <script> tags inside.
- size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script);
- size_t lenP = lenH + 53 + len;
- char *start = s, *oldPage = ctx->page.start;
-
- printf("start = %ld\n", start - ctx->page.start);
-
- ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
- start += ctx->page.start - oldPage;
- printf("page1 = %s\n", ctx->page.start);
- memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
- printf("page2 = %s\n", ctx->page.start);
- ctx->page.front += lenP;
- memcpy(start, "<head>", 6);
- memcpy(start + 6, ctx->script_header, lenH);
- memcpy(start + 6 + lenH, "<script type=\"text/javascript\">", 31);
- memcpy(start + 6 + lenH + 31, ctx->script.start, len);
- memcpy(start + 6 + lenH + 31 + len, "</script></head>", 16);
+ if (uw_buffer_used(&ctx->script) > 0) {
+ size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script);
+ size_t lenP = lenH + 53 + len;
+ char *start = s, *oldPage = ctx->page.start;
+
+ ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
+ start += ctx->page.start - oldPage;
+ memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
+ ctx->page.front += lenP;
+ memcpy(start, "<head>", 6);
+ memcpy(start + 6, ctx->script_header, lenH);
+ memcpy(start + 6 + lenH, "<script type=\"text/javascript\">", 31);
+ memcpy(start + 6 + lenH + 31, ctx->script.start, len);
+ memcpy(start + 6 + lenH + 31 + len, "</script></head>", 16);
+ } else {
+ size_t lenH = strlen(ctx->script_header);
+ size_t lenP = lenH + 13;
+ char *start = s, *oldPage = ctx->page.start;
+
+ ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
+ start += ctx->page.start - oldPage;
+ memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
+ ctx->page.front += lenP;
+ memcpy(start, "<head>", 6);
+ memcpy(start + 6, ctx->script_header, lenH);
+ memcpy(start + 6 + lenH, "</head>", 7);
+ }
}
}
}