Skip to content
Browse files

Merge pull request #75 from jgraichen/title

Preserve already present title attributes in time tags.
  • Loading branch information...
2 parents c613a87 + 92e8705 commit b78598f73f8dd0c22925ce3c38b0a5f6fa8271d3 @rmm5t committed May 10, 2012
Showing with 17 additions and 4 deletions.
  1. +6 −4 jquery.timeago.js
  2. +11 −0 test/index.html
View
10 jquery.timeago.js
@@ -96,10 +96,12 @@
return new Date(s);
},
datetime: function(elem) {
- // jQuery's `is()` doesn't play well with HTML5 in IE
- var isTime = $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time");
- var iso8601 = isTime ? $(elem).attr("datetime") : $(elem).attr("title");
+ var iso8601 = $t.isTime(elem) ? $(elem).attr("datetime") : $(elem).attr("title");
return $t.parse(iso8601);
+ },
+ isTime: function(elem) {
+ // jQuery's `is()` doesn't play well with HTML5 in IE
+ return $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time");
}
});
@@ -127,7 +129,7 @@
if (!element.data("timeago")) {
element.data("timeago", { datetime: $t.datetime(element) });
var text = $.trim(element.text());
- if (text.length > 0) {
+ if (text.length > 0 && !($t.isTime(element) && element.attr("title"))) {
element.attr("title", text);
}
}
View
11 test/index.html
@@ -73,6 +73,9 @@ <h2 id="qunit-userAgent"></h2>
<p>Date only (default tooltip): <abbr id="defaultTooltip" class="timeago" title="2008-02-26"> </abbr>.</p>
<p>Timestsamp (with millis): <abbr class="timeago" title="1978-12-18T17:17:00.021Z">(you shouldn't see this)</abbr>.</p>
+ <p><time id="testTimeTitle" class="timeago" datetime="2012-05-07T10:06:02+02:00" title="May 10, 2012 10:06">May 10, 2012</time></p>
+ <p><time id="testTimeTitle2" class="timeago" datetime="2012-05-07T10:06:02+02:00">May 10, 2012</time></p>
+
<h2>Errors</h2>
<p>Bad (letters): <abbr class="bad timeago" title="bleh">(this should be displayed)</abbr>.</p>
@@ -302,6 +305,14 @@ <h2 id="qunit-userAgent"></h2>
ok($("#defaultTooltip").attr("title") == "2008-02-26", "correctly set");
});
+ test("should preserve title in time tags", function () {
+ ok(($("#testTimeTitle").attr('title') === "May 10, 2012 10:06"), "title preserved")
+ });
+
+ test("should set title in time tags without a title", function () {
+ ok(($("#testTimeTitle2").attr('title') === "May 10, 2012"), "title set")
+ });
+
module("Parsing");
// Note, different browsers behave slightly different

0 comments on commit b78598f

Please sign in to comment.
Something went wrong with that request. Please try again.