diff --git a/js/core/utils.js b/js/core/utils.js index a9523bf7b8..af431431c6 100644 --- a/js/core/utils.js +++ b/js/core/utils.js @@ -44,16 +44,18 @@ define( $.fn.getDfnTitles = function ( args ) { var titles = []; var theAttr = ""; - var titleString = ""; - if (this.attr("data-lt")) { + var titleString = ""; + var normalizedTextContent = utils.normalizeString(this.text()); + // allow @lt to be consistent with bikeshed + if (this.attr("data-lt") || this.attr("lt")) { + theAttr = this.attr("data-lt") ? "data-lt" : "lt"; // prefer @data-lt for the list of title aliases - titleString = this.attr("data-lt"); - theAttr = "data-lt"; - } - else if (this.attr("lt")) { - // allow @lt to be consistent with bikeshed - titleString = this.attr("lt"); - theAttr = "lt"; + titleString = this.attr(theAttr); + // Use the definition itself as first item, so to avoid + // having to declare the definition twice. + if(!this.attr(theAttr).startsWith(normalizedTextContent)) { + titleString = normalizedTextContent + "|" + titleString; + } } else if (this.attr("title")) { // allow @title for backward compatibility @@ -70,11 +72,7 @@ define( titleString = this.text(); } // now we have a string of one or more titles - titleString = titleString.toLowerCase() // mask to lower case - .replace(/^\s+/, "") // strip out any leading whitespace - .replace(/\s+$/, "") // and any trailing whitespace - .split(/\s+/) // split on any whitepace - .join(" "); // and replace with a single space + titleString = utils.normalizeString(titleString); if (args && args.isDefinition === true) { // if it came from an attribute, replace that with data-lt as per contract with Shepherd if (theAttr) { @@ -199,7 +197,12 @@ define( } return ret; } - + , normalizeString: function(str) { + return str.toLowerCase() // mask to lower case + .trim() // trim trailing/leading whitespace + .split(/\s+/) // split on any whitespace + .join(" "); // and replace with a single space + } // Takes a string, applies some XML escapes, and returns the escaped string. // Note that overall using either Handlebars' escaped output or jQuery is much // preferred to operating on strings directly. diff --git a/tests/spec/core/utils-spec.js b/tests/spec/core/utils-spec.js index 671cd4b177..76d532da68 100644 --- a/tests/spec/core/utils-spec.js +++ b/tests/spec/core/utils-spec.js @@ -134,14 +134,31 @@ describe("Core - Utils", function () { }); }); + // $.getDfnTitles() + it("should find the data-lts", function () { + runs(function () { + var $dfn = $("TEXT").appendTo($("body")); + var titles = $dfn.getDfnTitles( { isDefinition: true } ); + expect(titles[0]).toEqual("text"); + expect(titles[1]).toEqual("dfn"); + expect(titles[2]).toEqual("dfn2"); + expect(titles[3]).toEqual("dfn3"); + $dfn.removeAttr("data-lt"); + expect($dfn.getDfnTitles()[0]).toEqual("abbr"); + $dfn.find("abbr").removeAttr("title"); + expect($dfn.getDfnTitles()[0]).toEqual("text"); + $dfn.remove(); + }); + }); // $.getDfnTitles() it("should find the definition title", function () { runs(function () { var $dfn = $("TEXT").appendTo($("body")); var titles = $dfn.getDfnTitles( { isDefinition: true } ); - expect(titles[0]).toEqual("dfn"); - expect(titles[1]).toEqual("dfn2"); - expect(titles[2]).toEqual("dfn3"); + expect(titles[0]).toEqual("text"); + expect(titles[1]).toEqual("dfn"); + expect(titles[2]).toEqual("dfn2"); + expect(titles[3]).toEqual("dfn3"); $dfn.removeAttr("data-lt"); expect($dfn.getDfnTitles()[0]).toEqual("abbr"); $dfn.find("abbr").removeAttr("title"); @@ -155,11 +172,13 @@ describe("Core - Utils", function () { runs(function () { var $dfn = $("TEXT").appendTo($("body")); var titles = $dfn.getDfnTitles( { isDefinition: true } ); - expect(titles[0]).toEqual("dfn"); - expect(titles[1]).toEqual("dfn2"); - expect(titles[2]).toEqual("dfn3"); - expect($dfn.attr("data-lt")).toEqual('dfn|dfn2|dfn3'); - expect($dfn.getDfnTitles()[0]).toEqual("dfn"); + expect(titles[0]).toEqual("text"); + expect(titles[1]).toEqual("dfn"); + expect(titles[2]).toEqual("dfn2"); + expect(titles[3]).toEqual("dfn3"); + console.log(titles, $dfn.attr("data-lt"), $dfn.getDfnTitles()) + expect($dfn.attr("data-lt")).toEqual("text|dfn|dfn2|dfn3"); + expect($dfn.getDfnTitles()[1]).toEqual("dfn"); $dfn.remove(); }); });