Available in a git repository branch.
Branch: anarcat/js-newline
Author: anarcat

I am not sure why -- it may be my fault -- but it seems like plugins loads there Javascript resources after the <body> and <html> tags are closed. Here's an example from my sandbox:

<script src="../ikiwiki/ikiwiki.js" type="text/javascript" charset="utf-8"></script>
<script src="../ikiwiki/relativedate.js" type="text/javascript" charset="utf-8"></script>  </body>
</html>
<script src="/ikiwiki/ikiwiki.js" type="text/javascript" charset="utf-8"></script>
<script src="/ikiwiki/toggle.js" type="text/javascript" charset="utf-8"></script>

Here, toggle.js appaars after the <html> tag! Interestingly, this is not specific to that one plugin: the same thing happens, in reverse, in my recent changes page:

<script src="../ikiwiki/ikiwiki.js" type="text/javascript" charset="utf-8"></script>
<script src="../ikiwiki/toggle.js" type="text/javascript" charset="utf-8"></script>  </body>
</html>
<script src="/ikiwiki/ikiwiki.js" type="text/javascript" charset="utf-8"></script>
<script src="/ikiwiki/relativedate.js" type="text/javascript" charset="utf-8"></script>

I am not sure what determines the order in the end.

I'm not sure what's going on, but it seems like the second time a include_javascript-like pattern is used, it fails to detect the </body> tag and appends the resources at the end instead. That could be because the </body> tag is not alone on its own line, which is actually fine in terms of comformity but confuses the regex...

I think the simplest solution is to add a newline when we add the javascript blob, to keep the assertion that body is on its own line. I have done so in the aforementioned patch, which is reproduced here for simplicty:

diff --git a/IkiWiki/Plugin/recentchangesdiff.pm b/IkiWiki/Plugin/recentchangesdiff.pm
index 0093c655e..ac8a61895 100644
--- a/IkiWiki/Plugin/recentchangesdiff.pm
+++ b/IkiWiki/Plugin/recentchangesdiff.pm
@@ -60,7 +60,7 @@ sub pagetemplate (@) {
 sub format (@) {
         my %params=@_;

-   if (! ($params{content}=~s!^(\s*</body[^>]*>)!include_javascript($params{page}).$1!em)) {
+   if (! ($params{content}=~s!^(\s*</body[^>]*>)!include_javascript($params{page})."\n".$1!em)) {
        # no <body> tag, probably in preview mode
        $params{content}=$params{content}.include_javascript(undef);
    }
diff --git a/IkiWiki/Plugin/relativedate.pm b/IkiWiki/Plugin/relativedate.pm
index 083966ad2..1ef6b2e2a 100644
--- a/IkiWiki/Plugin/relativedate.pm
+++ b/IkiWiki/Plugin/relativedate.pm
@@ -26,7 +26,7 @@ sub getsetup () {
 sub format (@) {
         my %params=@_;

-   if (! ($params{content}=~s!^(\s*</body[^>]*>)!include_javascript($params{page}).$1!em)) {
+   if (! ($params{content}=~s!^(\s*</body[^>]*>)!include_javascript($params{page})."\n".$1!em)) {
        # no <body> tag, probably in preview mode
        $params{content}=$params{content}.include_javascript(undef);
    }
diff --git a/IkiWiki/Plugin/toggle.pm b/IkiWiki/Plugin/toggle.pm
index eea6e8861..9f74f6029 100644
--- a/IkiWiki/Plugin/toggle.pm
+++ b/IkiWiki/Plugin/toggle.pm
@@ -68,7 +68,7 @@ sub format (@) {

    if ($params{content}=~s!(<div class="toggleable(?:-open)?" id="[^"]+">\s*)</div>!$1!g) {
        $params{content}=~s/<div class="toggleableend">//g;
-       if (! ($params{content}=~s!^(\s*</body[^>]*>)!include_javascript($params{page}).$1!em)) {
+       if (! ($params{content}=~s!^(\s*</body[^>]*>)!include_javascript($params{page})."\n".$1!em)) {
            # no <body> tag, probably in preview mode
            $params{content}=$params{content}.include_javascript(undef);
        }

Sorry for the trouble, HTH! :)

PS: Note that it seems like browsers are happy to ignore the spec and load stuff outside of the <html> tag, thankfully. So this is mostly a cosmetic/conformity thing...