Jop de Klein avatar Jop de Klein committed 9506811

* Made settings private to snap
* Simplified snap public API

Comments (0)

Files changed (12)

src/js/snap.backend.js

         var that = ns.baseBackend(options);
         
         var settings = {};
-        snap.utils.extendSettings(settings, ns.settings, options);
+        snap.utils.extendSettings(settings, snap.getSnapshotSettings(), options);
         
         if (!window.localStorage) {
             throw 'SnapWebStorageNotSupported';

src/js/snap.iterator.js

         var elementsCache = [];
         
         if (options) {
-            // root was specified but not valid
+            // root was specified but not valid            
             if (options.root !== undefined && !options.root) {
                 throw 'SnapRootNotFound';
             }
         }
         
-        snap.utils.extendSettings(settings, ns.settings, options);
+        snap.utils.extendSettings(settings, snap.getSnapshotSettings(), options);
         
         if (!window.Sizzle) {
             throw 'SnapSizzleNotFound';
         'error': function () {}
     };
     
-    ns.settings = {
+    var settings = {
         'id': 'snap.latest',
+        'root': null,
         'styleExcludes': [
             /^\d+$/,
             /^-moz/i,
     };
     
     ns.boot = function () {
-        // ns.utils.extendSettings(ns.settings, options);
-        
-        if (ns.settings.debug) {
+        if (settings.debug) {
             ns.debugtools = ns.debugtools(); // singleton
             // ns.debugtools.show();
         }
         
+        settings.root = document.body;
+        
         return true;
     };
     
     ns.setSnapshotSettings = function (options) {
-        ns.utils.extendSettings(ns.settings, options);
-        return ns.settings;
+        ns.utils.extendSettings(settings, options);
     };
     
-    ns.getSnapshotSettings = function (options) {
-        return ns.utils.cloneObject(ns.settings, {})
+    ns.getSnapshotSettings = function () {
+        return ns.utils.cloneObject(settings);
     };
     
     ns.setSnapshotSetting = function (setting, value) {
-        ns.settings[setting] = value;
+        settings[setting] = value;
     };
     
     ns.getSnapshotSetting = function (setting) {
-        return ns.utils.cloneObject(ns.settings[setting]);
+        return ns.utils.cloneObject(settings[setting]);
     };
         
-    ns.save = function (root) {
+    ns.save = function () {
         var backend = ns.webStorageBackend();
-        var snapshot = ns.createSnapshot(root);
+        var snapshot = ns.createSnapshot();
         
         return backend.save(ns.getSnapshotSetting('id'), snapshot.serialize());
     };
     
-    ns.createSnapshot = function (root) {
+    ns.createSnapshot = function () {
         var iterator = snap.iterator({
-            'root': root
+            'root': ns.getSnapshotSetting('root')
         });
         var nodes = iterator.getNodes();
         var snapshot = snap.snapshot();
         return snapshot;
     };
     
-    ns.compare = function (root) {
+    ns.compare = function () {
         var latestSnapshot = ns.loadSnapshot();
-        var currentSnapshot = ns.createSnapshot(root);
+        var currentSnapshot = ns.createSnapshot();
         var comparator = ns.nodeComparator();
         
         var comparison = comparator.compare(
         );    
     };
     
-    ns.showDifferences = function (root) {
+    ns.showDifferences = function () {
         var iterator = snap.iterator({
-            'root': root
+            'root': ns.getSnapshotSetting('root')
         });
         
         // TODO: store elements in snapshot to improve performance
-        var comparison = ns.compare(root);
+        var comparison = ns.compare();
         
         if (!comparison.equals) {
             var elements = iterator.getElements();
     // dummy implementation
     ns.compareSnapshotFromCache = function (root, cache) {
         var oldSnapshot = cache;
-        var newSnapshot = ns.createSnapshot(root);
+        var newSnapshot = ns.createSnapshot();
         
         var comparator = ns.nodeComparator();
         var isSame = comparator.compare(
         return isSame;
     };
     
-    ns.getElements = function (root) {
+    ns.getElements = function () {
         var iterator = snap.iterator({
-            'root': root
+            'root': ns.getSnapshotSetting('root')
         });
         var elements = iterator.getElements();
         

src/js/snap.node.js

             'styleIncludes': []
         };
         
-        snap.utils.extendSettings(settings, ns.settings, options);
+        snap.utils.extendSettings(settings, snap.getSnapshotSettings(), options);
         
         if (settings.styleExcludes.length && settings.styleIncludes.length) {
             snap.console.warn('Both styleIncludes and styleExcludes are specified, ignoring styleExcludes');

src/js/snap.report.js

                 var element = elements[diff.elementIndex];
                 if (element) {
                     // FIXME: use addClass construction
-                    element.className += ' ' + snap.settings.errorClass;
+                    element.className += ' ' + snap.getSnapshotSetting('errorClass');
                     
                     // FIXME: Allow reporting more than one difference per element
                     for (key in diff.difference) {

src/js/snap.snapshot.js

             'lifetime': 0
         };
 
-        snap.utils.extendSettings(settings, ns.settings, options);
+        snap.utils.extendSettings(settings, snap.getSnapshotSettings(), options);
         
         that.create = function (objects) {
             // TODO: Store settings used for comparison later

src/js/snap.utils.js

             }
             
             for (prop in target) {
-                if (prop in settings && settings[prop] !== null && settings[prop] !== undefined) {
-                    target[prop] = settings[prop];
+                newSetting = settings[prop];
+                if (prop in settings && newSetting !== null && newSetting !== undefined) {
+                    target[prop] = newSetting;
                 }
             }
         }
     };
     
     ns.cloneObject = function (orig, copy) {
-        if (/number|string|boolean|undefined/.test(typeof orig) || null === orig) {
+        if (/number|string|boolean|undefined/.test(typeof orig) || null === orig || orig.nodeType ) {
             return orig;
         }
 

test/unit/test.snap.backend.js

     equals(str, 'abc');
 });
 
+// TODO: clear test data
+

test/unit/test.snap.iterator.js

 
 
 test('root', function () {
+    
     var root = document.getElementById('test_root');
     var iterator = snap.iterator({'root': root});
     
     equals(iterator.getRoot(), root);
     equals(typeof iterator.getNodes, 'function');
+    
     equals(iterator.getElements().length, 4);
     equals(iterator.getNodes().length, 4);
 });
 test('no root', function () {
     var iterator = snap.iterator();
     
+    console.dir(snap.getSnapshotSetting('root'));
+    
     equals(iterator.getRoot(), document.body);
     equals(typeof iterator.getNodes, 'function');
 });

test/unit/test.snap.js

     var root = document.getElementById('test_root');
     
     var oldSettings = snap.getSnapshotSettings();
-    
     var newSettings = {
         'id': 'myApp',
+        'root': root,
         'elements': [
             {
                 'selector': 'div span'
     
     equals(newSettings.id, snap.getSnapshotSettings().id);
     
-    var snapshot = snap.createSnapshot(root);
-
+    ok(snap.createSnapshot());
     // restore settings
     snap.setSnapshotSettings(oldSettings);
 });
 });
 
 test('save snapshot', function () {
+    var oldRoot = snap.getSnapshotSetting('root');
+    var root = document.getElementById('test_root');
+    snap.setSnapshotSetting('root', root);
     snap.setSnapshotSetting('id', 'snap.test');
-    var root = document.getElementById('test_root');
-    // var snapshot = snap.createSnapshot(root);
-    ok('Saving snapshot', snap.save(root));
+    // var snapshot = snap.createSnapshot();
+    ok('Saving snapshot', snap.save());
+    snap.setSnapshotSetting('root', oldRoot);
+    
     // FIXME: Delete test snapshot
-    // ok(__cache = snap.createSnapshot(root).serialize());
 });
 
 test('compare with latest snapshot', function () {
+    var oldRoot = snap.getSnapshotSetting('root');
+    var root = document.getElementById('test_root');
+    
     snap.setSnapshotSetting('id', 'snap.test');
-    var root = document.getElementById('test_root');
-    ok(snap.compare(root), 'Comparing against same latest');
+    snap.setSnapshotSetting('root', root);
+    
+    // console.log(snap.getSnapshotSetting('root'), oldRoot);
+    
+    ok(snap.compare(), 'Comparing against same latest');
 
     root.style.backgroundColor = 'blue';
-    var comparison = snap.compare(root);
+    var comparison = snap.compare();
     equals(comparison.equals, false, 'Comparing against different latest');
     
-    var elements = snap.getElements(root);
+    var elements = snap.getElements();
     equals(elements.length, 4);
-    // same(elements[0], root.getElementsByTagName('*')[0]);
+    
+    snap.setSnapshotSetting('root', oldRoot);
 });
 

test/unit/test.snap.node.js

 });
 
 test('styles exclude setting (global)', function () {
-    snap.settings.styleExcludes = ['height', 'font-family'];
+    snap.setSnapshotSetting('styleExcludes', ['height', 'font-family']);
     
     var root = document.getElementById('test_root');
     var representation = snap.node(root);
 });
 
 test('styles exclude setting (global regexp)', function () {
-    snap.settings.styleExcludes = [/color/];
+    snap.setSnapshotSetting('styleExcludes', [/color/]);
     
     var root = document.getElementById('test_root');
     var representation = snap.node(root);
 });
 
 test('styles include setting (global regexp)', function () {
-    snap.settings.styleExcludes = [/color/];
+    snap.setSnapshotSetting('styleExcludes', [/color/]);
     
     var root = document.getElementById('test_root');
     var representation = snap.node(root);
     snap.node.__filteredStyles = {}; // clear filteredStyle cache
     
     // global styleIncludes should update cache
-    snap.settings.styleIncludes = ['color'];
+    snap.setSnapshotSetting('styleIncludes', ['color']);
     var root = document.getElementById('test_root');
     var representation = snap.node(root);
 

test/unit/test.snap.report.js

     
     report.report(differences, elements);
     
-    var errorClass = snap.settings.errorClass;
+    var errorClass = snap.getSnapshotSetting('errorClass');
     
     ok(!$(elements[0]).hasClass(errorClass), 'Verify that first element is not in error state');
     ok($(elements[1]).hasClass(errorClass), 'Verify that second element is in error state');
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.