|
16 | 16 | if (content_length <= opts.max_length)
|
17 | 17 | return; // bail early if not overlong
|
18 | 18 |
|
19 |
| - var actual_max_length = opts.max_length - opts.more.length - 3; // 3 for " ()" |
| 19 | + var actual_max_length = opts.max_length - opts.more.length - 3; // 3 for " ()" |
20 | 20 | var truncated_node = recursivelyTruncate(this, actual_max_length);
|
21 |
| - var full_node = $(this); |
| 21 | + var full_node = $(this).hide(); |
22 | 22 |
|
23 | 23 | truncated_node.insertAfter(full_node);
|
24 |
| - // This is an ugly approximation for getting the last block tag: |
25 |
| - // we just pick the last <p> or else the container itself. |
26 |
| - truncated_node.find('p:last').add(truncated_node).eq(0). |
27 |
| - append(' (<a href="#show more content">'+opts.more+'</a>)'); |
28 |
| - |
29 |
| - full_node.hide(); |
30 |
| - full_node.find('p:last').add(full_node).eq(0). |
31 |
| - append(' (<a href="#show less content">'+opts.less+'</a>)'); |
32 |
| - |
| 24 | + |
| 25 | + findNodeForMore(truncated_node).append(' (<a href="#show more content">'+opts.more+'</a>)'); |
| 26 | + findNodeForLess(full_node).append(' (<a href="#show less content">'+opts.less+'</a>)'); |
| 27 | + |
33 | 28 | truncated_node.find('a:last').click(function() {
|
34 | 29 | truncated_node.hide(); full_node.show(); return false;
|
35 | 30 | });
|
|
81 | 76 | function squeeze(string) {
|
82 | 77 | return string.replace(/\s+/g, ' ');
|
83 | 78 | }
|
| 79 | + |
| 80 | + // Finds the last, innermost block-level element |
| 81 | + function findNodeForMore(node) { |
| 82 | + var $node = $(node); |
| 83 | + var last_child = $node.children(":last"); |
| 84 | + if (!last_child) return node; |
| 85 | + var display = last_child.css('display'); |
| 86 | + if (!display || display=='inline') return $node; |
| 87 | + return findNodeForMore(last_child); |
| 88 | + }; |
| 89 | + |
| 90 | + // Finds the last child if it's a p; otherwise the parent |
| 91 | + function findNodeForLess(node) { |
| 92 | + var $node = $(node); |
| 93 | + var last_child = $node.children(":last"); |
| 94 | + if (last_child && last_child.is('p')) return last_child; |
| 95 | + return node; |
| 96 | + }; |
84 | 97 |
|
85 | 98 | })(jQuery);
|
0 commit comments