kswedberg / jquery-expander

Expand and Collapse HTML content
https://kswedberg.github.io/jquery-expander/
Other
459 stars 167 forks source link

Breaking in a table #57

Closed tpg198 closed 10 years ago

tpg198 commented 11 years ago

When the slice point is in the middle of a table, the read more link appears before the top of table.

Example of this behaviour here http://jsfiddle.net/fFdDW/4/

I fixed this locally by modifying the backup function to not break in the middle of a table.

function backup(txt, preserveWords) { if ( txt.lastIndexOf('<') > txt.lastIndexOf('>') ) { txt = txt.slice( 0, txt.lastIndexOf('<') ); } if ( txt.toLowerCase().indexOf('<table') > -1 ) { txt = txt.slice( 0, txt.toLowerCase().indexOf('<table') ); } if (preserveWords) { txt = txt.replace(rAmpWordEnd,''); } return $.trim(txt); }

A more generalised solution would be to allow a user to configure any elements that should not be broken by the slice.

Thanks for jqueryexpander :-)

JamesMcFall commented 10 years ago

I'm having this issue too where the read more link was being output and then another table was being output beneath it. I don't know what the previous poster meant by their code, because the only bit he added is mangled.

I've hacked this locally to get a dirty quick fix out and all I did was:

Modify the backup() method

If the splitter point is in the middle of a table, drop that table from the text that's going to be output.

    function backup(txt, preserveWords) {
            if ( txt.lastIndexOf('<') > txt.lastIndexOf('>') ) {
                txt = txt.slice( 0, txt.lastIndexOf('<') );
            }

            // Check if we're in the middle of a table, and if so, cut out the table
            lastOpenTable = txt.lastIndexOf('<table');
            lastClosedTable = txt.lastIndexOf('</table>');
            if (lastOpenTable > 0 && lastClosedTable > 0) {
                if (lastOpenTable > lastClosedTable) {
                    txt = txt.slice( 0, lastOpenTable );
                }
            }

            if (preserveWords) {
                txt = txt.replace(rAmpWordEnd,'');
            }

            return $.trim(txt);
        }

Modify the buildHTML() method

buildHTML() was putting the read more link before the tag (the end of my content finished with a closing table tag) so I've just made buildHTML() always append the read more tag (instead of tucking it inside the closing tag).

My solution is definitely a quick hack and isn't a "solution"... but it's got me out of trouble in this instance.

Thanks for your work on the plugin. Hopefully you can resolve this issue properly at some stage.

travco commented 10 years ago

@JamesMcFall I wasn't able to reproduce this in the current state of the plugin. It closed out the table, tr, and td without an issue.

If you still have the HTML (or have a rough idea of what you were doing) where you were applying expander, I'd really appreciate it if you could check it again or post it so I can start looking for a more global solution.

JamesMcFall commented 10 years ago

Hi @travco. I don't remember which project this was on (this is going back 8-9 months) sorry. I'll have a look tonight and see if I can dig up what it was on. Thanks.

travco commented 10 years ago

Judging by the lack of a reply the search didn't yield well. Thank you regardless for making an effort to look @JamesMcFall, I don't blame you for not remembering which project used which plugin almost a year back.

There were some changes to how the plugin handles where it slices recently, and they -should- have resolved most of the issues involving slicing inside of HTML elements.

If you suddenly stumble across anything, feel free to post it in here so Karl can reopen the issue.

@kswedberg Close issue?

JamesMcFall commented 10 years ago

Hi Travis, Apologies on not getting back to you. I couldn't find the specific project sorry. It's nestled in somewhere amongst hundreds of projects on our GitLab setup at work :s

Feel free to close the issue. If I notice anything next time I use it I'll let you know.

Thanks for the plugin.

kswedberg commented 10 years ago

thanks, @JamesMcFall . Closing this.