From ddfe76026e0fe1b93c20143c4d2da6473d283d18 Mon Sep 17 00:00:00 2001 From: Onno van der Zee Date: Fri, 14 Jun 2019 23:51:38 +0200 Subject: [PATCH 1/5] add data attribute to joblist creation --- CHANGELOG.md | 5 ++++- src/onScreenness.js | 12 ++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f222f87..1134629 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ ## Version History +1.3.0 + +* Automatic collection of elements with data-onscreenness attribute + 1.2.4 * Integration tests with Puppeteer. @@ -49,4 +53,3 @@ With support of Babel and Rollup it was possible to assemble it to a NPM-module. ## toDo, toWant * Callbacks * Refine the DOM observer -* automatically collect elements with data-onscreenness attribute \ No newline at end of file diff --git a/src/onScreenness.js b/src/onScreenness.js index 996bd09..2add259 100644 --- a/src/onScreenness.js +++ b/src/onScreenness.js @@ -5,6 +5,7 @@ import { } from './utilities.js'; let onScreennessModule = (function () { + var baseQuery = '[data-onscreenness]'; var queryList = []; var blackList = []; @@ -191,18 +192,13 @@ let onScreennessModule = (function () { * @private */ var composeJobList = function () { - var elementList = queryList.length - ? queryToArray ( queryList.join(',') ) - : []; + var fullList = [baseQuery].concat(queryList); + var elementList = queryToArray ( fullList.join(',') ); var ignoreList = blackList.length ? queryToArray ( blackList.join(',') ) : []; - if ( elementList.length ) { - return elementList.filter ( elm => !ignoreList.includes ( elm ) ); - } else { - return []; - } + return elementList.filter ( elm => !ignoreList.includes ( elm ) ); }; /** From 69990a454e74078f02f3f09ba057b14699c7dc94 Mon Sep 17 00:00:00 2001 From: Onno van der Zee Date: Mon, 17 Jun 2019 22:11:12 +0200 Subject: [PATCH 2/5] auto-asses unit test --- test/unit/auto-asses.test.js | 101 +++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 test/unit/auto-asses.test.js diff --git a/test/unit/auto-asses.test.js b/test/unit/auto-asses.test.js new file mode 100644 index 0000000..6e5ec00 --- /dev/null +++ b/test/unit/auto-asses.test.js @@ -0,0 +1,101 @@ +/** + * @jest-environment jsdom + */ + +import { + onScreenness, + onScreenTest +} from '../../src/onScreenness.js'; + + +describe('An element with an onscreenness data attribute in the html will be in the assesment', function () { + + let trigger; + let liveList; + + beforeAll(() => { + trigger = onScreenTest.triggerEvent; + liveList = onScreenTest.makeNodeList; + }); + + beforeEach(() => { + onScreenness.reset(); + }); + + + /* Test creation of live nodeLists */ + + test('A reset situation without onscreenness data attributes', () => { + document.body.innerHTML = ` +

Basic Example

+
+
Basic Example Element
+
Basic Example Element
+
Basic Example Element
+
Basic Example Element
+
Basic Example Element
+
Basic Example Element
+
Basic Example Element
+
Basic Example Element
+
+ `; + let nodes = liveList(); + expect(nodes.length).toBe(0); + }); + + test('A reset situation with one onscreenness data attributes', () => { + document.body.innerHTML = ` +

Basic Example

+
+
Basic Example Element
+
Basic Example Element
+
Basic Example Element
+
Example Element with data-onscreenness attribute
+
Basic Example Element
+
Basic Example Element
+
Basic Example Element
+
Basic Example Element
+
+ `; + let nodes = liveList(); + expect(nodes.length).toBe(1); + }); + + test('A reset situation with three onscreenness data attributes', () => { + document.body.innerHTML = ` +

Basic Example

+
+
Example Element with data-onscreenness attribute
+
Basic Example Element
+
Basic Example Element
+
Example Element with data-onscreenness attribute
+
Basic Example Element
+
Basic Example Element
+
Example Element with data-onscreenness attribute
+
Basic Example Element
+
+ `; + let nodes = liveList(); + expect(nodes.length).toBe(3); + }); + + test('A reset situation with three onscreenness data attributes, of which one excluded', () => { + document.body.innerHTML = ` +

Basic Example

+
+
Example Element with data-onscreenness attribute
+
Basic Example Element
+
Basic Example Element
+
Example Element with data-onscreenness attribute
+
Basic Example Element
+
Basic Example Element
+
Example Element with data-onscreenness attribute
+
Basic Example Element
+
+ `; + onScreenness.exclude('#fourth') + let nodes = liveList(); + expect(nodes.length).toBe(2); + }); + +}); From 718d0ba3474205c18f962a3e6195f996798e1565 Mon Sep 17 00:00:00 2001 From: Onno van der Zee Date: Wed, 19 Jun 2019 21:47:25 +0200 Subject: [PATCH 3/5] Working build, can't run sources directly --- dist/index.js | 2 +- dist/index.js.map | 2 +- package-lock.json | 5 +++++ package.json | 6 ++++-- rollup.config.js | 4 +++- src/onScreenness.js | 17 ++++++++++------- 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/dist/index.js b/dist/index.js index 40fa892..d443b0d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,2 +1,2 @@ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).onScreenness=e()}(this,function(){"use strict";var t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function e(t,e){return t(e={exports:{}},e.exports),e.exports}var n,r,o,i="object",c=function(t){return t&&t.Math==Math&&t},u=c(typeof globalThis==i&&globalThis)||c(typeof window==i&&window)||c(typeof self==i&&self)||c(typeof t==i&&t)||Function("return this")(),a=function(t){try{return!!t()}catch(t){return!0}},l=!a(function(){return 7!=Object.defineProperty({},"a",{get:function(){return 7}}).a}),s={}.propertyIsEnumerable,f=Object.getOwnPropertyDescriptor,p={f:f&&!s.call({1:2},1)?function(t){var e=f(this,t);return!!e&&e.enumerable}:s},h=function(t,e){return{enumerable:!(1&t),configurable:!(2&t),writable:!(4&t),value:e}},d={}.toString,v=function(t){return d.call(t).slice(8,-1)},g="".split,y=a(function(){return!Object("z").propertyIsEnumerable(0)})?function(t){return"String"==v(t)?g.call(t,""):Object(t)}:Object,m=function(t){if(null==t)throw TypeError("Can't call method on "+t);return t},b=function(t){return y(m(t))},S=function(t){return"object"==typeof t?null!==t:"function"==typeof t},x=function(t,e){if(!S(t))return t;var n,r;if(e&&"function"==typeof(n=t.toString)&&!S(r=n.call(t)))return r;if("function"==typeof(n=t.valueOf)&&!S(r=n.call(t)))return r;if(!e&&"function"==typeof(n=t.toString)&&!S(r=n.call(t)))return r;throw TypeError("Can't convert object to primitive value")},E={}.hasOwnProperty,L=function(t,e){return E.call(t,e)},w=u.document,O=S(w)&&S(w.createElement),I=function(t){return O?w.createElement(t):{}},M=!l&&!a(function(){return 7!=Object.defineProperty(I("div"),"a",{get:function(){return 7}}).a}),A=Object.getOwnPropertyDescriptor,T={f:l?A:function(t,e){if(t=b(t),e=x(e,!0),M)try{return A(t,e)}catch(t){}if(L(t,e))return h(!p.f.call(t,e),t[e])}},j=function(t){if(!S(t))throw TypeError(String(t)+" is not an object");return t},N=Object.defineProperty,P={f:l?N:function(t,e,n){if(j(t),e=x(e,!0),j(n),M)try{return N(t,e,n)}catch(t){}if("get"in n||"set"in n)throw TypeError("Accessors not supported");return"value"in n&&(t[e]=n.value),t}},_=l?function(t,e,n){return P.f(t,e,h(1,n))}:function(t,e,n){return t[e]=n,t},R=function(t,e){try{_(u,t,e)}catch(n){u[t]=e}return e},C=e(function(t){var e=u["__core-js_shared__"]||R("__core-js_shared__",{});(t.exports=function(t,n){return e[t]||(e[t]=void 0!==n?n:{})})("versions",[]).push({version:"3.1.3",mode:"global",copyright:"© 2019 Denis Pushkarev (zloirock.ru)"})}),k=C("native-function-to-string",Function.toString),F=u.WeakMap,V="function"==typeof F&&/native code/.test(k.call(F)),G=0,z=Math.random(),D=function(t){return"Symbol(".concat(void 0===t?"":t,")_",(++G+z).toString(36))},$=C("keys"),H=function(t){return $[t]||($[t]=D(t))},W={},q=u.WeakMap;if(V){var B=new q,Y=B.get,U=B.has,X=B.set;n=function(t,e){return X.call(B,t,e),e},r=function(t){return Y.call(B,t)||{}},o=function(t){return U.call(B,t)}}else{var K=H("state");W[K]=!0,n=function(t,e){return _(t,K,e),e},r=function(t){return L(t,K)?t[K]:{}},o=function(t){return L(t,K)}}var J={set:n,get:r,has:o,enforce:function(t){return o(t)?r(t):n(t,{})},getterFor:function(t){return function(e){var n;if(!S(e)||(n=r(e)).type!==t)throw TypeError("Incompatible receiver, "+t+" required");return n}}},Q=e(function(t){var e=J.get,n=J.enforce,r=String(k).split("toString");C("inspectSource",function(t){return k.call(t)}),(t.exports=function(t,e,o,i){var c=!!i&&!!i.unsafe,a=!!i&&!!i.enumerable,l=!!i&&!!i.noTargetGet;"function"==typeof o&&("string"!=typeof e||L(o,"name")||_(o,"name",e),n(o).source=r.join("string"==typeof e?e:"")),t!==u?(c?!l&&t[e]&&(a=!0):delete t[e],a?t[e]=o:_(t,e,o)):a?t[e]=o:R(e,o)})(Function.prototype,"toString",function(){return"function"==typeof this&&e(this).source||k.call(this)})}),Z=Math.ceil,tt=Math.floor,et=function(t){return isNaN(t=+t)?0:(t>0?tt:Z)(t)},nt=Math.min,rt=function(t){return t>0?nt(et(t),9007199254740991):0},ot=Math.max,it=Math.min,ct=function(t,e){var n=et(t);return n<0?ot(n+e,0):it(n,e)},ut=function(t){return function(e,n,r){var o,i=b(e),c=rt(i.length),u=ct(r,c);if(t&&n!=n){for(;c>u;)if((o=i[u++])!=o)return!0}else for(;c>u;u++)if((t||u in i)&&i[u]===n)return t||u||0;return!t&&-1}},at=ut(!1),lt=function(t,e){var n,r=b(t),o=0,i=[];for(n in r)!L(W,n)&&L(r,n)&&i.push(n);for(;e.length>o;)L(r,n=e[o++])&&(~at(i,n)||i.push(n));return i},st=["constructor","hasOwnProperty","isPrototypeOf","propertyIsEnumerable","toLocaleString","toString","valueOf"],ft=st.concat("length","prototype"),pt={f:Object.getOwnPropertyNames||function(t){return lt(t,ft)}},ht={f:Object.getOwnPropertySymbols},dt=u.Reflect,vt=dt&&dt.ownKeys||function(t){var e=pt.f(j(t)),n=ht.f;return n?e.concat(n(t)):e},gt=function(t,e){for(var n=vt(e),r=P.f,o=T.f,i=0;im;m++)if((u||m in d)&&(p=v(f=d[m],m,h),t))if(n)b[m]=p;else if(p)switch(t){case 3:return!0;case 5:return f;case 6:return m;case 2:b.push(f)}else if(i)return!1;return c?-1:o||i?i:b}},kt=Pt("species"),Ft=function(t){return!a(function(){var e=[];return(e.constructor={})[kt]=function(){return{foo:1}},1!==e[t](Boolean).foo})},Vt=Ct(2);Ot({target:"Array",proto:!0,forced:!Ft("filter")},{filter:function(t){return Vt(this,t,arguments[1])}});var Gt=function(t,e){var n=[][t];return!n||!a(function(){n.call(null,e||function(){throw 1},1)})},zt=Ct(0),Dt=Gt("forEach")?function(t){return zt(this,t,arguments[1])}:[].forEach;Ot({target:"Array",proto:!0,forced:[].forEach!=Dt},{forEach:Dt});var $t=Object.keys||function(t){return lt(t,st)},Ht=l?Object.defineProperties:function(t,e){j(t);for(var n,r=$t(e),o=r.length,i=0;o>i;)P.f(t,n=r[i++],e[n]);return t},Wt=u.document,qt=Wt&&Wt.documentElement,Bt=H("IE_PROTO"),Yt=function(){},Ut=function(){var t,e=I("iframe"),n=st.length;for(e.style.display="none",qt.appendChild(e),e.src=String("javascript:"),(t=e.contentWindow.document).open(),t.write("