]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/toggle.pm
only darcs add files not yet in version control
[git.ikiwiki.info.git] / IkiWiki / Plugin / toggle.pm
index f969d7686709da5a06410a260c09fbc598530f4b..610d38e3ac0d8e39c43d56e727f098441862b5ce 100644 (file)
@@ -39,23 +39,28 @@ function toggle(s) {
                style.display = "none";
 }
 
                style.display = "none";
 }
 
-function getElementsByClass(class) {
+function getElementsByClass(cls, node, tag) {
+       if (document.getElementsByClass)
+               return document.getElementsByClass(cls, node, tag);
+       if (! node) node = document;
+       if (! tag) tag = '*';
        var ret = new Array();
        var ret = new Array();
-       var pattern = new RegExp("(^|\\s)"+class+"(\\s|$)");
-       var els = document.getElementsByTagName('*');
-       for (i = 0, j = 0; i < els.length; i++) {
+       var pattern = new RegExp("(^|\\s)"+cls+"(\\s|$)");
+       var els = node.getElementsByTagName(tag);
+       for (i = 0; i < els.length; i++) {
                if ( pattern.test(els[i].className) ) {
                if ( pattern.test(els[i].className) ) {
-                       ret[j] = els[i];
-                       j++;
+                       ret.push(els[i]);
                }
        }
        return ret;
 }
                }
        }
        return ret;
 }
+
 //-->
 </script>
 EOF
 
 sub import { #{{{
 //-->
 </script>
 EOF
 
 sub import { #{{{
+       hook(type => "getsetup", id => "toggle", call => \&getsetup);
        hook(type => "preprocess", id => "toggle",
                call => \&preprocess_toggle);
        hook(type => "preprocess", id => "toggleable",
        hook(type => "preprocess", id => "toggle",
                call => \&preprocess_toggle);
        hook(type => "preprocess", id => "toggleable",
@@ -63,6 +68,14 @@ sub import { #{{{
        hook(type => "format", id => "toggle", call => \&format);
 } # }}}
 
        hook(type => "format", id => "toggle", call => \&format);
 } # }}}
 
+sub getsetup () { #{{{
+       return
+               plugin => {
+                       safe => 1,
+                       rebuild => undef,
+               },
+} #}}}
+
 sub genid ($$) { #{{{
        my $page=shift;
        my $id=shift;
 sub genid ($$) { #{{{
        my $page=shift;
        my $id=shift;
@@ -106,11 +119,11 @@ sub preprocess_toggleable (@) { #{{{
 sub format (@) { #{{{
         my %params=@_;
 
 sub format (@) { #{{{
         my %params=@_;
 
-       if ($params{content}=~s!(<div class="toggleable(?:-open)?" id="[^"]+">)</div>!$1!g) {
+       if ($params{content}=~s!(<div class="toggleable(?:-open)?" id="[^"]+">\s*)</div>!$1!g) {
                $params{content}=~s/<div class="toggleableend">//g;
                $params{content}=~s/<div class="toggleableend">//g;
-               if (! ($params{content}=~s!^<\/body>!$javascript</body>!m)) {
+               if (! ($params{content}=~s!^<body>!<body>$javascript!m)) {
                        # no </body> tag, probably in preview mode
                        # no </body> tag, probably in preview mode
-                       $params{content}.=$javascript;
+                       $params{content}=$javascript.$params{content};
                }
        }
        return $params{content};
                }
        }
        return $params{content};