gfranko / jquery.tocify.js

A jQuery Table of Contents plugin that can be themed with Twitter Bootstrap or jQueryUI.
http://gregfranko.com/jquery.tocify.js/
MIT License
928 stars 216 forks source link

Quotes in headings produce errors #75

Open kafoso opened 9 years ago

kafoso commented 9 years ago

Headings containing a (double) quote symbol, i.e. ", will produce an error.

This heading, for instance:

<h1>Bill "Bootstrap" Turner</h1>

... produces the error:

 Uncaught Error: Syntax error, unrecognized expression: div[data-unique="Bill"Bootstrap"Turner"]

This is likely due do the quote symbol not being escaped.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

micrology commented 8 years ago

For me, it doesn't produce a Syntax error, but the link does not work, undoubtedly due to having quote marks in the link. I suggest that the hashGenerator functions provided are augmented to include a filter for quotes and apostrophes; this should cure the problem fairy easily.

Tahiche commented 6 years ago

Hi, I was randomly getting those errors and thanks to this post i figured it was because of the quotes in headers. Seeing the advice from @micrology, it was easy to solve thanks to the "hashGenerator" API option. I just added a "replace" to further strip quotes and errors are gone.

//Calls the tocify method on your HTML div.
                $("#toc_content").tocify({
                    context: "#secc_content",
                    ignoreSelector: ".no_tocify",
                    // fix quotes in headings...
                    // function(text, element){} - Your own hash generation function that accepts the text as an
                    // argument, and returns the hash value.
                    hashGenerator:function(text, element){
                        // prettify the text - taken from tocify.js
                        var hashValue = text.toLowerCase().replace(/\s/g, "");
                       // strip single and double quotes...
                        hashValue = hashValue.replace(/["']/g,"");
                    return hashValue;
                    }
                });