Preserve <head> when editing .html file
authorJamie Cameron <jcameron@webmin.com>
Sun, 22 Apr 2007 16:27:27 +0000 (16:27 +0000)
committerJamie Cameron <jcameron@webmin.com>
Sun, 22 Apr 2007 16:27:27 +0000 (16:27 +0000)
file/CHANGELOG
file/file-lib.pl
file/save_html.cgi

index 9d5bfb5..054740c 100644 (file)
@@ -31,3 +31,5 @@ Removed the HTML editing button, and changed the Edit button to detect HTML file
 Added a button for creating a new HTML file.
 ---- Changes since 1.330 ----
 Replaced the HTMLarea widget for editing .html files with Xinha.
+---- Changes since 1.340 ----
+When editing an HTML file, the <head> section is preserved.
index fdee103..c394133 100644 (file)
@@ -447,6 +447,18 @@ else {
        }
 }
 
+# html_extract_head_body(html)
+# Given some HTML, extracts the header, body and stuff after the body
+sub html_extract_head_body
+{
+local ($html) = @_;
+if ($html =~ /^([\000-\377]*<body[^>]*>)([\000-\377]*)(<\/body[^>]*>[\000-\377]*)/i) {
+       return ($1, $2, $3);
+       }
+else {
+       return (undef, $html, undef);
+       }
+}
 
 1;
 
index 04995b8..465c796 100755 (executable)
@@ -6,9 +6,15 @@ $disallowed_buttons{'edit'} && &error($text{'ebutton'});
 &ReadParseMime();
 &error_setup($text{'html_err'});
 
-# Try to write the file
+# Get the original file contents, in case we need to preserve the head
 $p = $in{'file'};
 &switch_acl_uid_and_chroot();
+$olddata = &read_file_contents($p);
+if ($olddata) {
+       ($oldhead, $oldbody, $oldfoot) = &html_extract_head_body($olddata);
+       }
+
+# Try to write the file
 if ($access{'ro'} || !&can_access($p)) {
        &popup_error(&text('edit_eaccess', $p));
        }
@@ -19,7 +25,16 @@ elsif (!&open_tempfile(FILE, ">$p", 1)) {
        &popup_error("$!");
        }
 else {
-       &print_tempfile(FILE, $in{'body'});
+       # Fix up HTML head, and write it out
+       $in{'body'} =~ s/\r//g;
+       $in{'body'} =~ s/^\s+//g;
+       $in{'body'} =~ s/\s+$//g;
+       if ($oldhead && $in{'body'} !~ /<body.*>/i) {
+               &print_tempfile(FILE, $oldhead.$in{'body'}.$oldfoot);
+               }
+       else {
+               &print_tempfile(FILE, $in{'body'});
+               }
        &close_tempfile(FILE);
 
        # Show JS to close page