├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── fixed.html ├── index.js ├── origin.html ├── package.json └── tests ├── index.html └── tests.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | tests/* 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 明非 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |  2 | 3 | # array-reverse-polyfill 4 | Fixed Array `reverse()` Bug of Safari 12.0 on iOS 12 and MacOS. 5 | 6 | ## About the Bug 7 | 8 | https://bugs.webkit.org/show_bug.cgi?id=188794 9 | 10 | https://stackoverflow.com/q/52390368/7606327 11 | 12 | https://www.v2ex.com/t/490590 (in Chinese) 13 | 14 | https://zhuanlan.zhihu.com/p/44869607 (in Chinese) 15 | 16 | A simple visualization of this bug: 17 | 18 |  19 | 20 | (Thank @yisibl for his magical realism style drawing / 感谢灵魂画手一丝姐姐) 21 | 22 | ## Usage 23 | 24 | ### CDN 25 | 26 | jsDelivr 27 | ```html 28 | 29 | ``` 30 | 31 | unpkg 32 | ```html 33 | 34 | ``` 35 | 36 | Alibaba 37 | ```html 38 | 39 | ``` 40 | 41 | ### npm package 42 | [array-reverse-polyfill](https://www.npmjs.com/package/array-reverse-polyfill) 43 | 44 | ### Download releases 45 | [fanmingfei/array-reverse-ios12](https://github.com/fanmingfei/array-reverse-ios12/releases) 46 | 47 | 48 | ## UA sniffing 49 | 50 | This polyfill use *[feature (bug 🙃) detection](https://en.wikipedia.org/wiki/Feature_detection_(web_development))* instead of *[UA sniffing](https://en.wikipedia.org/wiki/User_agent#User_agent_sniffing)*. 51 | 52 | Some may want to load the polyfill in client-side or add the polyfill script in server-side only for Safari 12.0, and save the request to the polyfill for all other browsers. We assume the next version of iOS/Safari (12.1) will fix the bug, so we suggest you search "Version/12.0" and "Safari/" substring in user-agent string. You could also use regexp `/Version\/12\.0.*Safari\//`. 53 | 54 | ### Client on-demand load 55 | ```html 56 |
57 | 62 | ... 63 | ``` 64 | **NOTE**: The polyfill should be loaded and executed before all other scripts. So be careful about the `async` `defer` or `type=module`attributes and make sure your `loadScript()` implementation will keep the order. 65 | 66 | ### Server-side adaption (use PHP as sample) 67 | ```php 68 | 69 | if (preg_match('#Version/12\.0.*Safari/#', $_SERVER['HTTP_USER_AGENT'])): ?> 70 | 71 | endif; ?> 72 | ... 73 | ``` 74 | 75 | ## Test 76 | 77 | Test page: [https://fanmingfei.github.io/array-reverse-ios12/origin.html](https://fanmingfei.github.io/array-reverse-ios12/origin.html) 78 | 79 | 80 | Test page with this polyfill: [https://fanmingfei.github.io/array-reverse-ios12/fixed.html](https://fanmingfei.github.io/array-reverse-ios12/fixed.html) 81 | 82 | Clicking the button to test. 83 | 84 | 85 | For Unit Tests: [https://fanmingfei.github.io/array-reverse-ios12/tests/](https://fanmingfei.github.io/array-reverse-ios12/tests/) 86 | 87 | 88 | -------------------------------- 89 | 90 | ## Hope you guys luck 91 | 92 | -------------------------------------------------------------------------------- /fixed.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |
23 |
24 | Use iOS12 test. <br/>
25 | <button onclick="test()" style="border: 1px solid #ccc;">Click Refresh</button>
26 | <script>
27 | function test() {
28 | var arr = [1, 2, 3, 4]
29 | document.body.innerHTML += '<br/>'+arr.join(',')
30 | arr.reverse()
31 | }
32 | </script>
33 |
34 |
35 |
21 |
22 | Use iOS12 test. <br/>
23 | <button onclick="test()" style="border: 1px solid #ccc;">Click Refresh</button>
24 | <script>
25 | function test() {
26 | var arr = [1, 2, 3, 4]
27 | document.body.innerHTML += '<br/>'+arr.join(',')
28 | arr.reverse()
29 | }
30 | </script>
31 |
32 |
33 |