Skip to content

Commit a064adb

Browse files
committed
Support for data-tzoffset mutations
1 parent 45d35d4 commit a064adb

File tree

4 files changed

+191
-1
lines changed

4 files changed

+191
-1
lines changed

src/time.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@ TER.prototype._initialize = function () {
1616
for (var i = 0; i < elsL; i++) {
1717
var el = els[i];
1818
if (!el) break; /* If <time> is nested */
19-
this._replaceTimeContent (el);
19+
this._initTimeElement (el);
2020
}
2121
}; // TER.prototype._initialize
2222

23+
TER.prototype._initTimeElement = function (el) {
24+
var self = this;
25+
this._replaceTimeContent (el);
26+
new MutationObserver (function (mutations) {
27+
self._replaceTimeContent (el);
28+
}).observe (el, {attributeFilter: ['data-tzoffset']});
29+
}; // _initTimeElement
30+
2331
TER.prototype._replaceTimeContent = function (el) {
2432
var date = this._getDate (el);
2533
if (isNaN (date.valueOf ())) return;

t/runtests.js

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
(function () {
2+
var envKey = new URLSearchParams (location.search).get ("env");
3+
4+
var link0 = document.createElement ('link');
5+
link0.rel = "stylesheet";
6+
link0.href = "https://code.jquery.com/qunit/qunit-2.2.0.css";
7+
document.head.appendChild (link0);
8+
9+
var link = document.createElement ('link');
10+
link.rel = "stylesheet";
11+
link.href = "test.css";
12+
document.head.appendChild (link);
13+
14+
var meta = document.createElement ('viewport');
15+
meta.name = 'viewport';
16+
meta.content = "width=device-width";
17+
document.head.appendChild (meta);
18+
19+
var a = document.createElement ('a');
20+
a.href = '/';
21+
a.textContent = 'Top';
22+
document.body.appendChild (a);
23+
24+
var qunitLoaded = new Promise (function (ok, error) {
25+
var script = document.createElement ('script');
26+
script.src = 'https://code.jquery.com/qunit/qunit-2.2.0.js';
27+
script.onload = ok;
28+
script.onerror = error;
29+
document.body.appendChild (script);
30+
});
31+
32+
var scriptLoaded = new Promise (function (ok, error) {
33+
var script = document.createElement ('script');
34+
script.src = '../src/time.js';
35+
script.onload = ok;
36+
script.onerror = error;
37+
document.body.appendChild (script);
38+
});
39+
40+
qunitLoaded.then (function () {
41+
QUnit.config.current = {ignoreGlobalErrors: true};
42+
document.querySelectorAll ('test-code').forEach (function (e) {
43+
QUnit.test (e.getAttribute ('name'), function (assert) {
44+
var AsyncFunction = Object.getPrototypeOf (async function (){}).constructor;
45+
var code = new AsyncFunction (e.textContent);
46+
var context = {
47+
currentScript: e,
48+
wait: (n) => new Promise ((ok) => setTimeout (ok, n || 0)),
49+
assertEqualError: function (actual, expected, name) {
50+
this.assert.throws (() => { throw actual }, expected, name);
51+
}, // assertEqualError
52+
assertWindowError: function (code, expected, name) {
53+
var onerror = window.onerror;
54+
var error = undefined;
55+
window.onerror = function (a, b, c, d, e) {
56+
error = e || arguments;
57+
return true;
58+
};
59+
code ();
60+
window.onerror = onerror;
61+
this.assertEqualError (error, expected, name);
62+
}, // assertWindowError
63+
env: envKey,
64+
};
65+
var originalOnError;
66+
return scriptLoaded.then (function () {
67+
context.assert = assert;
68+
originalOnError = window.onerror;
69+
if (e.hasAttribute ('ignoreerrors')) window.onerror = undefined;
70+
return code.apply (context);
71+
}).then (assert.async (), function (e) {
72+
assert.equal (true, false, "Should not be rejected");
73+
assert.equal (e, null, "Exception");
74+
}).then (() => window.onerror = originalOnError);
75+
});
76+
});
77+
});
78+
79+
var div2 = document.createElement ('div');
80+
div2.id = "qunit";
81+
div2.dir = 'ltr';
82+
document.body.appendChild (div2);
83+
}) ();
84+
/*
85+
86+
Per CC0 <https://creativecommons.org/publicdomain/zero/1.0/>, to the
87+
extent possible under law, the author has waived all copyright and
88+
related or neighboring rights to this work.
89+
90+
*/

t/test.css

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@import "../css/default.css";
2+
3+
html {
4+
overflow: scroll;
5+
}
6+
7+
test-code {
8+
position: absolute;
9+
visibility: hidden;
10+
}
11+
12+
/*
13+
14+
Per CC0 <https://creativecommons.org/publicdomain/zero/1.0/>, to the
15+
extent possible under law, the author has waived all copyright and
16+
related or neighboring rights to this work.
17+
18+
*/

t/tzoffset.html

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Test</title>
6+
</head>
7+
<body>
8+
<test-code name="tzoffset static">
9+
/*
10+
<time data-tzoffset=2342>2012-05-01T00:12:51Z</time>
11+
*/
12+
13+
var e = this.currentScript.querySelector ('time');
14+
new TER (e.parentNode);
15+
await this.wait ();
16+
this.assert.equal (e.textContent, {
17+
"UTC:en-US": "5/1/2012, 0:51:53 AM",
18+
"UTC:ja-JP": "2012/5/1 0:51:53",
19+
"Asia/Tokyo:en-US": "5/1/2012, 0:51:53 AM",
20+
"Asia/Tokyo:ja-JP": "2012/5/1 0:51:53",
21+
"America/Los_Angeles:en-US": "5/1/2012, 0:51:53 AM",
22+
"America/Los_Angeles:ja-JP": "2012/5/1 0:51:53",
23+
}[this.env]);
24+
</test-code>
25+
26+
<test-code name="tzoffset dynamic">
27+
/*
28+
<time>2012-05-01T00:12:51Z</time>
29+
*/
30+
31+
var e = this.currentScript.querySelector ('time');
32+
e.setAttribute ('data-tzoffset', '2342');
33+
new TER (e.parentNode);
34+
await this.wait ();
35+
this.assert.equal (e.textContent, {
36+
"UTC:en-US": "5/1/2012, 0:51:53 AM",
37+
"UTC:ja-JP": "2012/5/1 0:51:53",
38+
"Asia/Tokyo:en-US": "5/1/2012, 0:51:53 AM",
39+
"Asia/Tokyo:ja-JP": "2012/5/1 0:51:53",
40+
"America/Los_Angeles:en-US": "5/1/2012, 0:51:53 AM",
41+
"America/Los_Angeles:ja-JP": "2012/5/1 0:51:53",
42+
}[this.env]);
43+
</test-code>
44+
45+
<test-code name="tzoffset dynamic change">
46+
/*
47+
<time>2012-05-01T00:12:51Z</time>
48+
*/
49+
50+
var e = this.currentScript.querySelector ('time');
51+
new TER (e.parentNode);
52+
await this.wait ();
53+
e.setAttribute ('data-tzoffset', '-3600');
54+
await this.wait ();
55+
this.assert.equal (e.textContent, {
56+
"UTC:en-US": "4/30/2012, 11:12:51 PM",
57+
"UTC:ja-JP": "2012/4/30 23:12:51",
58+
"Asia/Tokyo:en-US": "4/30/2012, 11:12:51 PM",
59+
"Asia/Tokyo:ja-JP": "2012/4/30 23:12:51",
60+
"America/Los_Angeles:en-US": "4/30/2012, 11:12:51 PM",
61+
"America/Los_Angeles:ja-JP": "2012/4/30 23:12:51",
62+
}[this.env]);
63+
</test-code>
64+
65+
<script src=runtests.js></script>
66+
</body>
67+
</html>
68+
<!--
69+
70+
Per CC0 <https://creativecommons.org/publicdomain/zero/1.0/>, to the
71+
extent possible under law, the author has waived all copyright and
72+
related or neighboring rights to this work.
73+
74+
-->

0 commit comments

Comments
 (0)