]> sipb.mit.edu Git - ikiwiki.git/blob - doc/bugs/map_fails_to_close_ul_element_for_empty_list.mdwn
patch overcoming the issue of <ul> with no <li>
[ikiwiki.git] / doc / bugs / map_fails_to_close_ul_element_for_empty_list.mdwn
1 [[!tag plugins/map patch]]
2
3 input:
4
5     before.
6     \[[!map pages="sdfsdfsdfsd/*"]]
7     after.
8
9 Presuming that the pagespec does not match, output:
10
11     <p>before.
12     <div class="map">
13     <ul>
14     </div></p>
15
16 The UL element is not closed.
17
18 Patch:
19
20     --- /usr/share/perl5/IkiWiki/Plugin/map.pm  2009-05-06 00:56:55.000000000 +0100
21     +++ IkiWiki/Plugin/map.pm   2009-06-15 12:23:54.000000000 +0100
22     @@ -137,11 +137,11 @@
23             $openli=1;
24             $parent=$item;
25         }
26     -   while ($indent > 0) {
27     +   while ($indent > 1) {
28             $indent--;
29             $map .= "</li>\n</ul>\n";
30         }
31     -   $map .= "</div>\n";
32     +   $map .= "</ul>\n</div>\n";
33         return $map;
34      }
35      
36
37 -- [[Jon]]
38
39 > Strictly speaking, a `<ul>` with no `<li>`s isn't valid HTML either...
40 > could `map` instead delay emitting the first `<ul>` until it determines that
41 > it will have at least one item? Perhaps refactoring that function into
42 > something easier to regression-test would be useful. --[[smcv]]
43
44 >> You are right (just checked 4.01 DTD to confirm). I suspect refactoring
45 >> the function would be wise. From my brief look at it to formulate the
46 >> above I thought it was a bit icky.  I'm not a good judge of what would
47 >> be regression-test friendly but I might have a go at reworking it. With
48 >> this variety of problem I have a strong inclination to use HOFs like map,
49 >> grep. - [[Jon]]
50
51 >>> The patch in [[map/discussion|plugins/map/discussion]] has the same
52 >>> problem, but does suggest a simpler approach to solving it (bail out
53 >>> early if the map has no items at all). --[[smcv]]
54
55 >>>> Thanks for pointing out the problem. I guess this patch should solve it.
56 >>>> --[[harishcm]]
57
58 Patch:
59
60     --- /usr/local/share/perl/5.8.8/IkiWiki/Plugin/map.pm
61     +++ map.pm
62     @@ -80,7 +80,17 @@
63         my $indent=0;
64         my $openli=0;
65         my $addparent="";
66     -   my $map = "<div class='map'>\n<ul>\n";
67     +   my $map = "<div class='map'>\n";
68     +
69     +   # Return empty div if %mapitems is empty
70     +   if (!scalar(keys %mapitems)) {
71     +           $map .= "</div>\n";
72     +           return $map; 
73     +   } 
74     +   else { # continue populating $map
75     +           $map .= "<ul>\n";
76     +   }
77     +
78         foreach my $item (sort keys %mapitems) {
79                 my @linktext = (length $mapitems{$item} ? (linktext => $mapitems{$item}) : ());
80                 $item=~s/^\Q$common_prefix\E\///