Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support detect selector when there are animation-name defined already #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions src/sentinel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ var isArray = Array.isArray,
animationCallbacks = {},
styleEl,
styleSheet,
cssRules;
cssRules,
ANIM = 'sentinel-animation-name';

function matches (elem, selector) {
try {
return elem.matches(selector);
} catch (e) {}
return false;
}

return {
/**
Expand All @@ -22,16 +29,25 @@ return {

// add animationstart event listener
doc.addEventListener('animationstart', function(ev, callbacks, l, i) {
callbacks = animationCallbacks[ev.animationName];
callbacks = animationCallbacks[ev.animationName] || [];

Object.keys(selectorToAnimationMap).forEach(function (selector) {
if (matches(ev.target, selector)) {
callbacks = callbacks.concat(
animationCallbacks[selectorToAnimationMap[selector]] || []
);
}
});

// iterate through callbacks
l = callbacks.length;

// exit if callbacks haven't been registered
if (!callbacks) return;
if (!l) return;

// stop other callbacks from firing
ev.stopImmediatePropagation();

// iterate through callbacks
l = callbacks.length;
for (i=0; i < l; i++) callbacks[i](ev.target);
}, true);

Expand All @@ -40,6 +56,13 @@ return {
head.insertBefore(styleEl, head.firstChild);
styleSheet = styleEl.sheet;
cssRules = styleSheet.cssRules;

styleSheet.insertRule('* {animation-name:' + ANIM +';}', cssRules.length);

styleSheet.insertRule(
'@keyframes ' + ANIM + '{from{transform:none;}to{transform:none;}}',
cssRules.length
);
}

// listify argument and add css rules/ cache callbacks
Expand All @@ -54,19 +77,12 @@ return {
selectorToAnimationMap[selector] = animId =
isCustomName ? selector.slice(1) : 'sentinel-' +
Math.random().toString(16).slice(2);

// add keyframe rule
cssRules[styleSheet.insertRule(
'@keyframes ' + animId +
'{from{transform:none;}to{transform:none;}}',
cssRules.length)]
._id = selector;

// add selector animation rule
if (!isCustomName) {

if (isCustomName) {
// add keyframe rule
cssRules[styleSheet.insertRule(
selector + '{animation-duration:0.0001s;animation-name:' +
animId + ';}',
'@keyframes ' + animId +
'{from{transform:none;}to{transform:none;}}',
cssRules.length)]
._id = selector;
}
Expand Down
40 changes: 35 additions & 5 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('SentinelJS tests', function() {

// check CSS rules
var cssRules = getSentinelStyleEl().sheet.cssRules;
assert.equal(cssRules.length, 2);
assert.equal(cssRules.length, 4);

done();
});
Expand All @@ -126,6 +126,36 @@ describe('SentinelJS tests', function() {
testEl.className = 'my-div';
document.body.appendChild(testEl);
});

it('detect new nodes with custom animation names', function(done) {

// add css rule
var styleEl = document.createElement('style');
document.head.appendChild(styleEl);
var sheet = styleEl.sheet;
sheet.insertRule(
'.my-div2{animation-duration:0.0001s;animation-name:node-inserted2;}',
sheet.cssRules.length)

sheet.insertRule(
'@keyframes node-inserted2 {from{transform:none;}to{transform:none;}}',
sheet.cssRules.length)

// add watch
sentinel.on('.my-div2', function(el) {
assert.equal(el.className, 'my-div2');

assert.ok(getComputedStyle(el).animationName.indexOf('node-inserted2') >= 0);

document.head.removeChild(styleEl);

done();
});

// add element to dom
testEl.className = 'my-div2';
document.body.appendChild(testEl);
});
});


Expand All @@ -145,7 +175,7 @@ describe('SentinelJS tests', function() {

// when queue is empty css rules will be removed
sentinel.off('.test-div', fn2);
assert.equal(cssRules.length, 0);
assert.equal(cssRules.length, 2);
});


Expand All @@ -157,7 +187,7 @@ describe('SentinelJS tests', function() {

assert.equal(cssRules.length, 2);
sentinel.off('.test-div');
assert.equal(cssRules.length, 0);
assert.equal(cssRules.length, 2);
});


Expand All @@ -173,11 +203,11 @@ describe('SentinelJS tests', function() {

// removing first one will keep css rules
sentinel.off('!node-inserted', fn1);
assert.equal(cssRules.length, 1);
assert.equal(cssRules.length, 3);

// when queue is empty css rules will be removed
sentinel.off('!node-inserted', fn2);
assert.equal(cssRules.length, 0);
assert.equal(cssRules.length, 2);
});
});
});