comparison src/c/urweb.c @ 1729:6817ddd6cf1f

Avoid generating empty <script> tags
author Adam Chlipala <adam@chlipala.net>
date Fri, 27 Apr 2012 09:52:16 -0400
parents 95d3b4f26f59
children e9587120831a
comparison
equal deleted inserted replaced
1728:95d3b4f26f59 1729:6817ddd6cf1f
3221 uw_check(ctx, 1); 3221 uw_check(ctx, 1);
3222 *ctx->page.front = 0; 3222 *ctx->page.front = 0;
3223 } else if (!strncmp(s, "<head>", 6)) { 3223 } else if (!strncmp(s, "<head>", 6)) {
3224 // <head> is present. Let's add the <script> tags immediately after it. 3224 // <head> is present. Let's add the <script> tags immediately after it.
3225 3225
3226 size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script); 3226 // Any freeform JavaScript to include?
3227 size_t lenP = lenH + 40 + len; 3227 if (uw_buffer_used(&ctx->script) > 0) {
3228 char *start = s + 6, *oldPage = ctx->page.start; 3228 size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script);
3229 3229 size_t lenP = lenH + 40 + len;
3230 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP); 3230 char *start = s + 6, *oldPage = ctx->page.start;
3231 start += ctx->page.start - oldPage; 3231
3232 memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1); 3232 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
3233 ctx->page.front += lenP; 3233 start += ctx->page.start - oldPage;
3234 memcpy(start, ctx->script_header, lenH); 3234 memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
3235 memcpy(start + lenH, "<script type=\"text/javascript\">", 31); 3235 ctx->page.front += lenP;
3236 memcpy(start + lenH + 31, ctx->script.start, len); 3236 memcpy(start, ctx->script_header, lenH);
3237 memcpy(start + lenH + 31 + len, "</script>", 9); 3237 memcpy(start + lenH, "<script type=\"text/javascript\">", 31);
3238 memcpy(start + lenH + 31, ctx->script.start, len);
3239 memcpy(start + lenH + 31 + len, "</script>", 9);
3240 } else {
3241 size_t lenH = strlen(ctx->script_header);
3242 char *start = s + 6, *oldPage = ctx->page.start;
3243
3244 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenH);
3245 start += ctx->page.start - oldPage;
3246 memmove(start + lenH, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
3247 ctx->page.front += lenH;
3248 memcpy(start, ctx->script_header, lenH);
3249 }
3238 } else { 3250 } else {
3239 // No <head>. At this point, add it, with <script> tags inside. 3251 // No <head>. At this point, add it, with <script> tags inside.
3240 3252
3241 size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script); 3253 if (uw_buffer_used(&ctx->script) > 0) {
3242 size_t lenP = lenH + 53 + len; 3254 size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script);
3243 char *start = s, *oldPage = ctx->page.start; 3255 size_t lenP = lenH + 53 + len;
3244 3256 char *start = s, *oldPage = ctx->page.start;
3245 printf("start = %ld\n", start - ctx->page.start); 3257
3246 3258 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
3247 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP); 3259 start += ctx->page.start - oldPage;
3248 start += ctx->page.start - oldPage; 3260 memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
3249 printf("page1 = %s\n", ctx->page.start); 3261 ctx->page.front += lenP;
3250 memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1); 3262 memcpy(start, "<head>", 6);
3251 printf("page2 = %s\n", ctx->page.start); 3263 memcpy(start + 6, ctx->script_header, lenH);
3252 ctx->page.front += lenP; 3264 memcpy(start + 6 + lenH, "<script type=\"text/javascript\">", 31);
3253 memcpy(start, "<head>", 6); 3265 memcpy(start + 6 + lenH + 31, ctx->script.start, len);
3254 memcpy(start + 6, ctx->script_header, lenH); 3266 memcpy(start + 6 + lenH + 31 + len, "</script></head>", 16);
3255 memcpy(start + 6 + lenH, "<script type=\"text/javascript\">", 31); 3267 } else {
3256 memcpy(start + 6 + lenH + 31, ctx->script.start, len); 3268 size_t lenH = strlen(ctx->script_header);
3257 memcpy(start + 6 + lenH + 31 + len, "</script></head>", 16); 3269 size_t lenP = lenH + 13;
3270 char *start = s, *oldPage = ctx->page.start;
3271
3272 ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP);
3273 start += ctx->page.start - oldPage;
3274 memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1);
3275 ctx->page.front += lenP;
3276 memcpy(start, "<head>", 6);
3277 memcpy(start + 6, ctx->script_header, lenH);
3278 memcpy(start + 6 + lenH, "</head>", 7);
3279 }
3258 } 3280 }
3259 } 3281 }
3260 } 3282 }
3261 3283
3262 3284