├── .gitignore ├── .jshintignore ├── .jshintrc ├── .npmignore ├── .travis.yml ├── README.md ├── bower.json ├── examples ├── animation.html └── aspect-ratio.html ├── extensions ├── fastdom-promised.d.ts ├── fastdom-promised.js └── fastdom-sandbox.js ├── fastdom-strict.js ├── fastdom.d.ts ├── fastdom.js ├── fastdom.min.js ├── package-lock.json ├── package.json ├── src └── fastdom-strict.js ├── test ├── fastdom-promised-test.js ├── fastdom-sandbox-test.js ├── fastdom-strict-test.js ├── fastdom-test.js └── karma.conf.js └── webpack.config.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | npm-debug.log 4 | test/coverage 5 | -------------------------------------------------------------------------------- /.jshintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | fastdom.js 3 | strict.js 4 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "camelcase": false, 3 | "forin": false, 4 | "latedef": "nofunc", 5 | "newcap": false, 6 | "noarg": true, 7 | "node": true, 8 | "nonew": true, 9 | "quotmark": "single", 10 | "undef": true, 11 | "unused": "vars", 12 | "trailing": true, 13 | "maxlen": 80, 14 | "laxbreak": true, 15 | "sub": true, 16 | 17 | "eqnull": true, 18 | "expr": true, 19 | 20 | "maxerr": 1000, 21 | "regexdash": true, 22 | "laxcomma": true, 23 | "proto": true, 24 | "boss": true, 25 | 26 | "esnext": true, 27 | 28 | "browser": true, 29 | "devel": true, 30 | "nonstandard": true, 31 | "worker": true, 32 | 33 | "-W078": true, 34 | 35 | "predef": [ 36 | "define" 37 | ] 38 | } 39 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | **/*. 2 | bower.json 3 | webpack.config.js 4 | node_modules 5 | examples 6 | test 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | dist: trusty 3 | 4 | language: node_js 5 | 6 | node_js: 7 | - 5.1.0 8 | 9 | addons: 10 | firefox: latest 11 | apt: 12 | sources: 13 | - google-chrome 14 | packages: 15 | - google-chrome-stable 16 | 17 | before_script: 18 | - export CHROME_BIN=$(which google-chrome-stable) 19 | - export DISPLAY=:99.0 20 | - sh -e /etc/init.d/xvfb start 21 | 22 | after_script: 23 | - npm run coveralls 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fastdom [![Twitter Follow](https://img.shields.io/twitter/follow/wilsonpage?style=social)](https://twitter.com/wilsonpage) 2 | 3 | [![Build Status](https://travis-ci.org/wilsonpage/fastdom.svg?branch=master)](https://travis-ci.org/wilsonpage/fastdom) [![NPM version](https://badge.fury.io/js/fastdom.svg)](http://badge.fury.io/js/fastdom) [![npm](https://img.shields.io/npm/dm/fastdom.svg?maxAge=2592000)]() [![Coverage Status](https://coveralls.io/repos/wilsonpage/fastdom/badge.svg?branch=master&service=github)](https://coveralls.io/github/wilsonpage/fastdom?branch=master) ![gzip size](http://img.badgesize.io/https://unpkg.com/fastdom/fastdom.min.js?compression=gzip) 4 | 5 | 6 | Eliminates layout thrashing by batching DOM read/write operations (~600 bytes minified gzipped). 7 | 8 | ```js 9 | fastdom.measure(() => { 10 | console.log('measure'); 11 | }); 12 | 13 | fastdom.mutate(() => { 14 | console.log('mutate'); 15 | }); 16 | 17 | fastdom.measure(() => { 18 | console.log('measure'); 19 | }); 20 | 21 | fastdom.mutate(() => { 22 | console.log('mutate'); 23 | }); 24 | ``` 25 | 26 | Outputs: 27 | 28 | ``` 29 | measure 30 | measure 31 | mutate 32 | mutate 33 | ``` 34 | 35 | ## Examples 36 | 37 | - [Animation example](http://wilsonpage.github.io/fastdom/examples/animation.html) 38 | - [Aspect ratio example](http://wilsonpage.github.io/fastdom/examples/aspect-ratio.html) 39 | 40 | ## Installation 41 | 42 | FastDom is CommonJS and AMD compatible, you can install it in one of the following ways: 43 | 44 | ```sh 45 | $ npm install fastdom --save 46 | ``` 47 | 48 | or [download](http://github.com/wilsonpage/fastdom/raw/master/fastdom.js). 49 | 50 | ## How it works 51 | 52 | FastDom works as a regulatory layer between your app/library and the DOM. By batching DOM access we **avoid unnecessary document reflows** and dramatically **speed up layout performance**. 53 | 54 | Each measure/mutate job is added to a corresponding measure/mutate queue. The queues are emptied (reads, then writes) at the turn of the next frame using [`window.requestAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/window.requestAnimationFrame). 55 | 56 | FastDom aims to behave like a singleton across *all* modules in your app. When any module requires `'fastdom'` they get the same instance back, meaning FastDom can harmonize DOM access app-wide. 57 | 58 | Potentially a third-party library could depend on FastDom, and better integrate within an app that itself uses it. 59 | 60 | ## API 61 | 62 | ### FastDom#measure(callback[, context]) 63 | 64 | Schedules a job for the 'measure' queue. Returns a unique ID that can be used to clear the scheduled job. 65 | 66 | ```js 67 | fastdom.measure(() => { 68 | const width = element.clientWidth; 69 | }); 70 | ``` 71 | 72 | ### FastDom#mutate(callback[, context]) 73 | 74 | Schedules a job for the 'mutate' queue. Returns a unique ID that can be used to clear the scheduled job. 75 | 76 | ```js 77 | fastdom.mutate(() => { 78 | element.style.width = width + 'px'; 79 | }); 80 | ``` 81 | 82 | ### FastDom#clear(id) 83 | 84 | Clears **any** scheduled job. 85 | 86 | ```js 87 | const read = fastdom.measure(() => {}); 88 | const write = fastdom.mutate(() => {}); 89 | 90 | fastdom.clear(read); 91 | fastdom.clear(write); 92 | ``` 93 | 94 | ## Strict mode 95 | 96 | It's very important that all DOM mutations or measurements go through `fastdom` to ensure good performance; to help you with this we wrote `fastdom-strict`. When `fastdom-strict.js` is loaded, it will throw errors when sensitive DOM APIs are called at the wrong time. 97 | 98 | This is useful when working with a large team who might not all be aware of `fastdom` or its benefits. It can also prove useful for catching 'un-fastdom-ed' code when migrating an app to `fastdom`. 99 | 100 | ```html 101 | 102 | 103 | ``` 104 | 105 | ```js 106 | element.clientWidth; // throws 107 | fastdom.mutate(function() { element.clientWidth; }); // throws 108 | fastdom.measure(function() { element.clientWidth; }); // does not throw 109 | ``` 110 | 111 | ```js 112 | "Error: Can only get .clientWidth during 'measure' phase" 113 | ``` 114 | 115 | - `fastdom-strict` will not throw if nodes are not attached to the document. 116 | - You should use `fastdom-strict` in development to catch rendering performance issues before they hit production. 117 | - It is not advisable to use `fastdom-strict` in production. 118 | 119 | ## Exceptions 120 | 121 | FastDom is async, this can therefore mean that when a job comes around to being executed, the node you were working with may no longer be there. These errors are usually not critical, but they can cripple your app. 122 | 123 | FastDom allows you to register a `catch` handler. If `fastdom.catch` has been registered, FastDom will catch any errors that occur in your jobs, and run the handler instead. 124 | 125 | ```js 126 | fastdom.catch = (error) => { 127 | // Do something if you want 128 | }; 129 | 130 | ``` 131 | 132 | ## Extensions 133 | 134 | The core `fastdom` library is designed to be as light as possible. Additional functionality can be bolted on in the form of 'extensions'. It's worth noting that `fastdom` is a 'singleton' by design, so all tasks (even those scheduled by extensions) will reach the same global task queue. 135 | 136 | **Fastdom ships with some extensions:** 137 | 138 | - [`fastdom-promised`](extensions/fastdom-promised.js) - Adds Promise based API 139 | - [`fastdom-sandbox`](extensions/fastdom-sandbox.js) - Adds task grouping concepts 140 | 141 | ### Using an extension 142 | 143 | Use the `.extend()` method to extend the current `fastdom` to create a new object. 144 | 145 | ```html 146 | 147 | 148 | ``` 149 | 150 | ```js 151 | // extend fastdom 152 | const myFastdom = fastdom.extend(fastdomPromised); 153 | 154 | // use new api 155 | myFastdom.mutate(...).then(...); 156 | ``` 157 | 158 | Extensions can be chained to construct a fully customised `fastdom`. 159 | 160 | ```js 161 | const myFastdom = fastdom 162 | .extend(fastdomPromised) 163 | .extend(fastdomSandbox); 164 | ``` 165 | 166 | ### Writing an extension 167 | 168 | ```js 169 | const myFastdom = fastdom.extend({ 170 | measure(fn, ctx) { 171 | // do custom stuff ... 172 | 173 | // then call the parent method 174 | return this.fastdom.measure(fn, ctx); 175 | }, 176 | 177 | mutate: ... 178 | }); 179 | ``` 180 | 181 | You'll notice `this.fastdom` references the parent `fastdom`. If you're extending a core API and aren't calling the parent method, you're doing something wrong. 182 | 183 | When distributing an extension only export a plain object to allow users to compose their own `fastdom`. 184 | 185 | ```js 186 | module.exports = { 187 | measure: ..., 188 | mutate: ..., 189 | clear: ... 190 | }; 191 | ``` 192 | 193 | ## Tests 194 | 195 | ```sh 196 | $ npm install 197 | $ npm test 198 | ``` 199 | 200 | ## Author 201 | 202 | - **Wilson Page** - [@wilsonpage](http://twitter.com/wilsonpage) 203 | 204 | ## Contributors 205 | 206 | - **Wilson Page** - [@wilsonpage](http://twitter.com/wilsonpage) 207 | - **Paul Irish** - [@paulirish](http://github.com/paulirish) 208 | - **Kornel Lesinski** - [@pornel](http://github.com/pornel) 209 | - **George Crawford** - [@georgecrawford](http://github.com/georgecrawford) 210 | 211 | ## License 212 | 213 | (The MIT License) 214 | 215 | Copyright (c) 2016 Wilson Page 216 | 217 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 218 | 219 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 220 | 221 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 222 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fastdom", 3 | "description": "Eliminates layout thrashing by batching DOM read/write operations", 4 | "main": "fastdom.js", 5 | "scripts": [ 6 | "fastdom.js" 7 | ], 8 | "ignore": [ 9 | "**/*.", 10 | "webpack.config.js", 11 | "examples/", 12 | "test/", 13 | "README.md" 14 | ], 15 | "license": "MIT", 16 | "_source": "git@github.com/wilsonpage/fastdom.git" 17 | } 18 | -------------------------------------------------------------------------------- /examples/animation.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | FastDom: Animation Example 5 | 6 | 7 | 8 | 9 | 33 | 34 |
An adaptation of the demo from the Google Developers article Diagnosing forced synchronous layouts. In the article, forced synchronous layouts are fixed by simply not doing any reads at all. Rather than this extreme solution, which will not be appropriate for many use cases, we instead use fastdom to intelligently defer and batch the DOM reads and writes, and get similar performance gains as a result.
35 | 36 | 37 | 38 | 39 | 40 | 41 |
42 | 43 | 139 | 140 | 141 | 142 | 143 | -------------------------------------------------------------------------------- /examples/aspect-ratio.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | FastDom: Aspect Ratio Example 6 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 |
28 | 29 | 136 | 137 | 138 | -------------------------------------------------------------------------------- /extensions/fastdom-promised.d.ts: -------------------------------------------------------------------------------- 1 | declare namespace FastdomPromised { 2 | export function clear>(task: T): void; 3 | export function initialize(): void; 4 | export function measure void>(task: T, context?: any): Promise>; 5 | export function mutate void>(task: T, context?: any): Promise>; 6 | } 7 | 8 | export = FastdomPromised; 9 | -------------------------------------------------------------------------------- /extensions/fastdom-promised.js: -------------------------------------------------------------------------------- 1 | !(function() { 2 | 3 | /** 4 | * Wraps fastdom in a Promise API 5 | * for improved control-flow. 6 | * 7 | * @example 8 | * 9 | * // returning a result 10 | * fastdom.measure(() => el.clientWidth) 11 | * .then(result => ...); 12 | * 13 | * // returning promises from tasks 14 | * fastdom.measure(() => { 15 | * var w = el1.clientWidth; 16 | * return fastdom.mutate(() => el2.style.width = w + 'px'); 17 | * }).then(() => console.log('all done')); 18 | * 19 | * // clearing pending tasks 20 | * var promise = fastdom.measure(...) 21 | * fastdom.clear(promise); 22 | * 23 | * @type {Object} 24 | */ 25 | var exports = { 26 | initialize: function() { 27 | this._tasks = new Map(); 28 | }, 29 | 30 | mutate: function(fn, ctx) { 31 | return create(this, 'mutate', fn, ctx); 32 | }, 33 | 34 | measure: function(fn, ctx) { 35 | return create(this, 'measure', fn, ctx); 36 | }, 37 | 38 | clear: function(promise) { 39 | var tasks = this._tasks; 40 | var task = tasks.get(promise); 41 | this.fastdom.clear(task); 42 | tasks.delete(promise); 43 | } 44 | }; 45 | 46 | /** 47 | * Create a fastdom task wrapped in 48 | * a 'cancellable' Promise. 49 | * 50 | * @param {FastDom} fastdom 51 | * @param {String} type - 'measure'|'mutate' 52 | * @param {Function} fn 53 | * @return {Promise} 54 | */ 55 | function create(promised, type, fn, ctx) { 56 | var tasks = promised._tasks; 57 | var fastdom = promised.fastdom; 58 | var task; 59 | 60 | var promise = new Promise(function(resolve, reject) { 61 | task = fastdom[type](function() { 62 | tasks.delete(promise); 63 | try { resolve(ctx ? fn.call(ctx) : fn()); } 64 | catch (e) { reject(e); } 65 | }, ctx); 66 | }); 67 | 68 | tasks.set(promise, task); 69 | return promise; 70 | } 71 | 72 | // Expose to CJS, AMD or global 73 | if ((typeof define)[0] == 'f') define(function() { return exports; }); 74 | else if ((typeof module)[0] == 'o') module.exports = exports; 75 | else window.fastdomPromised = exports; 76 | 77 | })(); -------------------------------------------------------------------------------- /extensions/fastdom-sandbox.js: -------------------------------------------------------------------------------- 1 | (function(exports) { 2 | 3 | /** 4 | * Mini logger 5 | * 6 | * @return {Function} 7 | */ 8 | var debug = 0 ? console.log.bind(console, '[fastdom-sandbox]') : function() {}; 9 | 10 | /** 11 | * Exports 12 | */ 13 | 14 | /** 15 | * Create a new `Sandbox`. 16 | * 17 | * Scheduling tasks via a sandbox is 18 | * useful because you can clear all 19 | * sandboxed tasks in one go. 20 | * 21 | * This is handy when working with view 22 | * components. You can create one sandbox 23 | * per component and call `.clear()` when 24 | * tearing down. 25 | * 26 | * @example 27 | * 28 | * var sandbox = fastdom.sandbox(); 29 | * 30 | * sandbox.measure(function() { console.log(1); }); 31 | * sandbox.measure(function() { console.log(2); }); 32 | * 33 | * fastdom.measure(function() { console.log(3); }); 34 | * fastdom.measure(function() { console.log(4); }); 35 | * 36 | * sandbox.clear(); 37 | * 38 | * // => 3 39 | * // => 4 40 | * 41 | * @return {Sandbox} 42 | * @public 43 | */ 44 | exports.sandbox = function() { 45 | return new Sandbox(this.fastdom); 46 | }; 47 | 48 | /** 49 | * Initialize a new `Sandbox` 50 | * 51 | * @param {FastDom} fastdom 52 | */ 53 | 54 | function Sandbox(fastdom) { 55 | this.fastdom = fastdom; 56 | this.tasks = []; 57 | debug('initialized'); 58 | } 59 | 60 | /** 61 | * Schedule a 'measure' task. 62 | * 63 | * @param {Function} fn 64 | * @param {Object} ctx 65 | * @return {Object} can be passed to .clear() 66 | */ 67 | Sandbox.prototype.measure = function(fn, ctx) { 68 | var tasks = this.tasks; 69 | var task = this.fastdom.measure(function() { 70 | tasks.splice(tasks.indexOf(task)); 71 | return fn.call(ctx); 72 | }); 73 | 74 | tasks.push(task); 75 | return task; 76 | }; 77 | 78 | /** 79 | * Schedule a 'mutate' task. 80 | * 81 | * @param {Function} fn 82 | * @param {Object} ctx 83 | * @return {Object} can be passed to .clear() 84 | */ 85 | Sandbox.prototype.mutate = function(fn, ctx) { 86 | var tasks = this.tasks; 87 | var task = this.fastdom.mutate(function() { 88 | tasks.splice(tasks.indexOf(task)); 89 | return fn.call(ctx); 90 | }); 91 | 92 | this.tasks.push(task); 93 | return task; 94 | }; 95 | 96 | /** 97 | * Clear a single task or is no task is 98 | * passsed, all tasks in the `Sandbox`. 99 | * 100 | * @param {Object} task (optional) 101 | */ 102 | 103 | Sandbox.prototype.clear = function(task) { 104 | if (!arguments.length) clearAll(this.fastdom, this.tasks); 105 | remove(this.tasks, task); 106 | return this.fastdom.clear(task); 107 | }; 108 | 109 | /** 110 | * Clears all the given tasks from 111 | * the given `FastDom`. 112 | * 113 | * @param {FastDom} fastdom 114 | * @param {Array} tasks 115 | * @private 116 | */ 117 | 118 | function clearAll(fastdom, tasks) { 119 | debug('clear all', fastdom, tasks); 120 | var i = tasks.length; 121 | while (i--) { 122 | fastdom.clear(tasks[i]); 123 | tasks.splice(i, 1); 124 | } 125 | } 126 | 127 | /** 128 | * Remove an item from an Array. 129 | * 130 | * @param {Array} array 131 | * @param {*} item 132 | * @return {Boolean} 133 | */ 134 | function remove(array, item) { 135 | var index = array.indexOf(item); 136 | return !!~index && !!array.splice(index, 1); 137 | } 138 | 139 | /** 140 | * Expose 141 | */ 142 | 143 | if ((typeof define)[0] == 'f') define(function() { return exports; }); 144 | else if ((typeof module)[0] == 'o') module.exports = exports; 145 | else window.fastdomSandbox = exports; 146 | 147 | })({}); 148 | -------------------------------------------------------------------------------- /fastdom-strict.js: -------------------------------------------------------------------------------- 1 | (function webpackUniversalModuleDefinition(root, factory) { 2 | if(typeof exports === 'object' && typeof module === 'object') 3 | module.exports = factory(require("fastdom")); 4 | else if(typeof define === 'function' && define.amd) 5 | define(["fastdom"], factory); 6 | else if(typeof exports === 'object') 7 | exports["fastdom"] = factory(require("fastdom")); 8 | else 9 | root["fastdom"] = factory(root["fastdom"]); 10 | })(this, function(__WEBPACK_EXTERNAL_MODULE_2__) { 11 | return /******/ (function(modules) { // webpackBootstrap 12 | /******/ // The module cache 13 | /******/ var installedModules = {}; 14 | 15 | /******/ // The require function 16 | /******/ function __webpack_require__(moduleId) { 17 | 18 | /******/ // Check if module is in cache 19 | /******/ if(installedModules[moduleId]) 20 | /******/ return installedModules[moduleId].exports; 21 | 22 | /******/ // Create a new module (and put it into the cache) 23 | /******/ var module = installedModules[moduleId] = { 24 | /******/ exports: {}, 25 | /******/ id: moduleId, 26 | /******/ loaded: false 27 | /******/ }; 28 | 29 | /******/ // Execute the module function 30 | /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); 31 | 32 | /******/ // Flag the module as loaded 33 | /******/ module.loaded = true; 34 | 35 | /******/ // Return the exports of the module 36 | /******/ return module.exports; 37 | /******/ } 38 | 39 | 40 | /******/ // expose the modules object (__webpack_modules__) 41 | /******/ __webpack_require__.m = modules; 42 | 43 | /******/ // expose the module cache 44 | /******/ __webpack_require__.c = installedModules; 45 | 46 | /******/ // __webpack_public_path__ 47 | /******/ __webpack_require__.p = ""; 48 | 49 | /******/ // Load entry module and return exports 50 | /******/ return __webpack_require__(0); 51 | /******/ }) 52 | /************************************************************************/ 53 | /******/ ([ 54 | /* 0 */ 55 | /***/ (function(module, exports, __webpack_require__) { 56 | 57 | 'use strict'; 58 | 59 | var strictdom = __webpack_require__(1); 60 | var fastdom = __webpack_require__(2); 61 | 62 | /** 63 | * Mini logger 64 | * 65 | * @return {Function} 66 | */ 67 | var debug = 0 ? console.log.bind(console, '[fastdom-strict]') : function() {}; 68 | 69 | /** 70 | * Enabled state 71 | * 72 | * @type {Boolean} 73 | */ 74 | var enabled = false; 75 | 76 | window.fastdom = module.exports = fastdom.extend({ 77 | measure: function(fn, ctx) { 78 | debug('measure'); 79 | var task = !ctx ? fn : fn.bind(ctx); 80 | return this.fastdom.measure(function() { 81 | if (!enabled) return task(); 82 | return strictdom.phase('measure', task); 83 | }, ctx); 84 | }, 85 | 86 | mutate: function(fn, ctx) { 87 | debug('mutate'); 88 | var task = !ctx ? fn : fn.bind(ctx); 89 | return this.fastdom.mutate(function() { 90 | if (!enabled) return task(); 91 | return strictdom.phase('mutate', task); 92 | }, ctx); 93 | }, 94 | 95 | strict: function(value) { 96 | if (value) { 97 | enabled = true; 98 | strictdom.enable(); 99 | } else { 100 | enabled = false; 101 | strictdom.disable(); 102 | } 103 | } 104 | }); 105 | 106 | // turn on strict-mode 107 | window.fastdom.strict(true); 108 | 109 | 110 | /***/ }), 111 | /* 1 */ 112 | /***/ (function(module, exports, __webpack_require__) { 113 | 114 | var __WEBPACK_AMD_DEFINE_RESULT__;!(function() { 115 | 'use strict'; 116 | 117 | var debug = 0 ? console.log.bind(console, '[strictdom]') : function() {}; 118 | 119 | /** 120 | * Crude webkit test. 121 | * 122 | * @type {Boolean} 123 | */ 124 | // var isWebkit = !!window.webkitURL; 125 | 126 | /** 127 | * List of properties observed. 128 | * 129 | * @type {Object} 130 | */ 131 | var properties = { 132 | prototype: { 133 | Document: { 134 | execCommand: Mutate, 135 | elementFromPoint: Measure, 136 | elementsFromPoint: Measure, 137 | scrollingElement: Measure 138 | }, 139 | 140 | Node: { 141 | appendChild: { 142 | type: Mutate, 143 | test: function(dom, parent, args) { 144 | var attached = isAttached(parent) || isAttached(args[0]); 145 | if (attached && dom.not('mutate')) throw error(3, this.name); 146 | } 147 | }, 148 | 149 | insertBefore: { 150 | type: Mutate, 151 | test: function(dom, parent, args) { 152 | var attached = isAttached(parent) || isAttached(args[0]); 153 | if (attached && dom.not('mutate')) throw error(3, this.name); 154 | } 155 | }, 156 | 157 | removeChild: { 158 | type: Mutate, 159 | test: function(dom, parent, args) { 160 | var attached = isAttached(parent) || isAttached(args[0]); 161 | if (attached && dom.not('mutate')) throw error(3, this.name); 162 | } 163 | }, 164 | 165 | textContent: Mutate 166 | }, 167 | 168 | Element: { 169 | scrollIntoView: Mutate, 170 | scrollBy: Mutate, 171 | scrollTo: Mutate, 172 | getClientRects: Measure, 173 | getBoundingClientRect: Measure, 174 | clientLeft: Measure, 175 | clientWidth: Measure, 176 | clientHeight: Measure, 177 | scrollLeft: Accessor, 178 | scrollTop: Accessor, 179 | scrollWidth: Measure, 180 | scrollHeight: Measure, 181 | innerHTML: Mutate, 182 | outerHTML: Mutate, 183 | insertAdjacentHTML: Mutate, 184 | remove: Mutate, 185 | setAttribute: Mutate, 186 | removeAttribute: Mutate, 187 | className: Mutate, 188 | classList: ClassList 189 | }, 190 | 191 | HTMLElement: { 192 | offsetLeft: Measure, 193 | offsetTop: Measure, 194 | offsetWidth: Measure, 195 | offsetHeight: Measure, 196 | offsetParent: Measure, 197 | innerText: Accessor, 198 | outerText: Accessor, 199 | focus: Measure, 200 | blur: Measure, 201 | style: Style, 202 | 203 | // `element.dataset` is hard to wrap. 204 | // We could use `Proxy` but it's not 205 | // supported in Chrome yet. Not too 206 | // concerned as `data-` attributes are 207 | // not often associated with render. 208 | // dataset: DATASET 209 | }, 210 | 211 | CharacterData: { 212 | remove: Mutate, 213 | data: Mutate 214 | }, 215 | 216 | Range: { 217 | getClientRects: Measure, 218 | getBoundingClientRect: Measure 219 | }, 220 | 221 | MouseEvent: { 222 | layerX: Measure, 223 | layerY: Measure, 224 | offsetX: Measure, 225 | offsetY: Measure 226 | }, 227 | 228 | HTMLButtonElement: { 229 | reportValidity: Measure 230 | }, 231 | 232 | HTMLDialogElement: { 233 | showModal: Mutate 234 | }, 235 | 236 | HTMLFieldSetElement: { 237 | reportValidity: Measure 238 | }, 239 | 240 | HTMLImageElement: { 241 | width: Accessor, 242 | height: Accessor, 243 | x: Measure, 244 | y: Measure 245 | }, 246 | 247 | HTMLInputElement: { 248 | reportValidity: Measure 249 | }, 250 | 251 | HTMLKeygenElement: { 252 | reportValidity: Measure 253 | }, 254 | 255 | SVGSVGElement: { 256 | currentScale: Accessor 257 | } 258 | }, 259 | 260 | instance: { 261 | window: { 262 | getComputedStyle: { 263 | type: Measure, 264 | 265 | /** 266 | * Throws when the Element is in attached 267 | * and strictdom is not in the 'measure' phase. 268 | * 269 | * @param {StrictDom} strictdom 270 | * @param {Window} win 271 | * @param {Object} args 272 | */ 273 | test: function(strictdom, win, args) { 274 | if (isAttached(args[0]) && strictdom.not('measure')) { 275 | throw error(2, 'getComputedStyle'); 276 | } 277 | } 278 | }, 279 | 280 | // innerWidth: { 281 | // type: isWebkit ? Value : Measure, 282 | // 283 | // /** 284 | // * Throws when the window is nested (in