Skip to content
This repository was archived by the owner on Aug 21, 2022. It is now read-only.

Commit a111131

Browse files
committed
history API polyfill (draft)
1 parent 5204f86 commit a111131

File tree

7 files changed

+86
-0
lines changed

7 files changed

+86
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ global | `FormData()`<sup>[12](#FormData)</sup>, `Set()`, `Map()`, `WeakSet()`,
8787
`HTMLScriptElement.prototype` | `.onload()`<sup>[10](#HTMLScriptElement.prototype.onload)</sup>, `.onerror()`<sup>[10](#HTMLScriptElement.prototype.onload)</sup>
8888
`CSSStyleDeclaration.prototype` | `.getPropertyValue()`, `.removeProperty()`, `.setProperty()`, `.cssFloat`, `.opacity`
8989
`document` | `.head`, `.createEvent()`<sup>[11](#document.createEvent)</sup>
90+
`history` | `.pushState()`, `.replaceState()`, `.state`
9091
`FormData.prototype` | `.append()`
9192
`Event.prototype`<sup>[13](#Event.prototype)</sup> | `.initEvent()`<sup>[14](#Event.prototype.initEvent)</sup>, `.initUIEvent()`<sup>[14](#Event.prototype.initEvent)</sup>, `.initMouseEvent()`<sup>[14](#Event.prototype.initEvent)</sup>, `.initCustomEvent()`
9293
`XMLHttpRequest.prototype` | `.send()`<sup>[15](#XMLHttpRequest)</sup>, `.onload()`, `.onerror()`, `.onabort()`, `.addEventListener()`, `.removeEventListener()`, `.dispatchEvent()`

build/file-order.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
"polyfill/ltie10/ie8/getcomputedstyle.js",
4444

4545

46+
"polyfill/ltie10/history.js",
47+
48+
4649
"lib/namespace.js",
4750
"lib/is.js",
4851
"lib/class.js",

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- fix: the settled promise bug
66
- fix: a bug with settled promise value changes
7+
- new: history API polyfill
78

89
##v0.4.5
910

index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ <h1>jsCore</h1>
7070
<script src="src/polyfill/ltie10/ie8/getcomputedstyle.js"></script>
7171

7272

73+
<script src="src/polyfill/ltie10/history.js"></script>
74+
75+
7376
<script src="src/lib/namespace.js"></script>
7477
<script src="src/lib/is.js"></script>
7578
<script src="src/lib/class.js"></script>

src/polyfill/ltie10/history.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
history.pushState || new function () {
3+
4+
console.log('history polyfill');
5+
6+
var proto = history.constructor.prototype,
7+
loc = window.location,
8+
states = {},
9+
state = null,
10+
skip = {};
11+
12+
function onPopState() {
13+
var event = document.createEvent('CustomEvent');
14+
event.initEvent('popstate', false, false);
15+
event.state = state;
16+
window.dispatchEvent(event);
17+
}
18+
19+
proto.pushState = function (state, title, hash) {
20+
if (!hash.startsWith('#')) {
21+
hash = '#' + hash;
22+
}
23+
states[hash] = state;
24+
skip[hash] = true;
25+
loc.hash = hash;
26+
};
27+
28+
proto.replaceState = function (state, title, hash) {
29+
throw Error('history.replaceState not implemented');
30+
};
31+
32+
Object.defineProperty(proto, 'state', {
33+
get: function () {
34+
return state;
35+
}
36+
});
37+
38+
window.addEventListener('hashchange', function (event) {
39+
var hash = loc.hash;
40+
if (skip[hash]) {
41+
delete skip[hash];
42+
} else {
43+
state = states[hash] || null;
44+
onPopState();
45+
}
46+
});
47+
48+
};

test/test-polyfill.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ document.addEventListener('DOMContentLoaded', function () {
2020
});
2121

2222
var event = document.createEvent('MouseEvent');
23+
/*todo fix github.com/cubiq/iscroll/issues/501
2324
event.initMouseEvent('mousemove', true, true, window, 'test',
2425
t, t, t, t, false, false, false, false, 0, null);
26+
*/
27+
event.initEvent('mousemove', true, true);
2528
document.dispatchEvent(event);
2629

2730
});

test/test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,36 @@ lib.dom.ready().then(function () {
123123

124124
window.requestAnimationFrame(function (t) {
125125
var event = document.createEvent('MouseEvent');
126+
/*todo fix github.com/cubiq/iscroll/issues/501
126127
event.initMouseEvent('mousemove', true, true, window, 'test',
127128
t, t, t, t, false, false, false, false, 0, null);
129+
*/
130+
event.initEvent('mousemove', true, true);
128131
document.dispatchEvent(event);
129132
});
130133

131134
});
135+
136+
/*
137+
window.addEventListener('popstate', function (event) {
138+
console.log(JSON.stringify(event.state));
139+
});
140+
setTimeout(function () {
141+
history.pushState({test: 1}, '', '#test-1');
142+
setTimeout(function () {
143+
history.pushState({test: 2}, '', '#test-2');
144+
setTimeout(function () {
145+
history.pushState({test: 3}, '', '#test-2');
146+
setTimeout(function () {
147+
history.back();
148+
setTimeout(function () {
149+
history.back();
150+
setTimeout(function () {
151+
history.back();
152+
}, 100);
153+
}, 100);
154+
}, 100);
155+
}, 100);
156+
}, 100);
157+
}, 100);
158+
*/

0 commit comments

Comments
 (0)