Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Janky module system works in require() and injectJs()

Separated generic object functions into own file
  • Loading branch information...
commit 4bed366cb67076ff3243d11c3b98e8a26fd69d6f 1 parent f1a5b26
@begriffs authored
View
76 lib/css.js
@@ -1,10 +1,10 @@
/*jslint browser: true, indent: 2, nomen: true */
-/*global _, jQuery, console, CSSFontFaceRule */
+/*global exports, _, jQuery, OBJ, console, CSSFontFaceRule */
-(function () {
+var CSS = (function () {
"use strict";
-
- var $ = jQuery.noConflict();
+ var my = {},
+ $ = jQuery.noConflict();
function computedCssProperties(elt, pseudoclass) {
var result = {}, i, j, prop, val,
@@ -13,7 +13,7 @@
return s.replace(
/-([a-z])/g,
function (g) { return g[1].toUpperCase(); }
- )
+ );
};
// dealing with "array-like" objects so cannot use underscore
@@ -31,49 +31,8 @@
return result;
}
- function objectIntersection(array) {
- function simpleIntersection(a, b) {
- if (typeof a !== 'object' || typeof b !== 'object') {
- return {};
- }
- return _.pick(a, _.intersection(_.keys(a), _.keys(b)).filter(function (key) {
- return _.isEqual(a[key], b[key]);
- }));
- }
- function repeatedly(f) {
- return function (args) {
- if (args.length === 0) { return {}; }
- var seed = args.pop();
- return _.reduce(args, function (memo, obj) { return f(memo, obj); }, seed);
- };
- }
-
- return repeatedly(simpleIntersection)(array);
- }
-
- function objectDifference(a, b) {
- if (typeof a !== 'object' || typeof b !== 'object') {
- return a;
- }
- return _.pick(a, _.difference(_.keys(a), _.keys(b)));
- }
-
- function cartesianProduct() {
- return _.reduce(arguments, function (mtrx, vals) {
- return _.reduce(vals, function (array, val) {
- return array.concat(
- _.map(mtrx, function (row) { return row.concat(val); })
- );
- }, []);
- }, [[]]);
- }
-
- function toRealArray(array_like) {
- return Array.prototype.slice.call(array_like);
- }
-
function commonStyle(nodes) {
- return objectIntersection(_.map(nodes, function (k) { return $(k).data('style') || {}; }));
+ return OBJ.intersection(_.map(nodes, function (k) { return $(k).data('style') || {}; }));
}
function liftHeritable(node) {
@@ -97,7 +56,7 @@
node.children().each(function (i, kid) {
var common = _.pick(commonStyle([node, $(kid)]), heritable);
- $(kid).data('style', objectDifference($(kid).data('style'), common));
+ $(kid).data('style', OBJ.difference($(kid).data('style'), common));
});
}
@@ -402,7 +361,7 @@
// from the root node only.
function stripRootDefaultStyles(root) {
var defaults = {speak: 'normal', visibility: 'visible', orphans: 2, widows: 2};
- root.data('style', objectDifference(root.data('style'), defaults));
+ root.data('style', OBJ.difference(root.data('style'), defaults));
}
function selectorsUsed(node, so_far) {
@@ -460,7 +419,7 @@
while (max_depth > 1) {
_.each(
_.map(
- cartesianProduct(selectors, sub_selectors),
+ OBJ.cartesianProduct(selectors, sub_selectors),
build_selector
),
add_if_matches
@@ -496,7 +455,7 @@
return template.replace('TRBL', direction);
});
} else if (template.match(/TB-LR/)) {
- group = _.map(cartesianProduct(['top', 'bottom'], ['left', 'right']), function (pair) {
+ group = _.map(OBJ.cartesianProduct(['top', 'bottom'], ['left', 'right']), function (pair) {
return template.replace('TB', pair[0]).replace('LR', pair[1]);
});
} else {
@@ -528,17 +487,17 @@
return style;
}
- window.fontDeclarations = function () {
+ my.fontDeclarations = function () {
$('html').find('*').andSelf().each(function (i, elt) {
$(elt).data('style', abbreviate(computedCssProperties(elt)));
});
- var cssRules = _.flatten(_.map(_.pluck(document.styleSheets, 'cssRules'), toRealArray)),
+ var cssRules = _.flatten(_.map(_.pluck(document.styleSheets, 'cssRules'), _.toArray)),
fontRules = _.filter(cssRules, function (r) { return r.constructor === CSSFontFaceRule; });
return _.pluck(fontRules, 'cssText'); // TODO: filter by fontsUsed()
};
- window.simplerStyle = function (root) {
+ my.simplerStyle = function (root) {
root = root || $('html');
var selectors = {}, common, best, result = {},
@@ -557,7 +516,7 @@
},
removeStyle = function (style) {
return function (i, elt) {
- $(elt).data('style', objectDifference($(elt).data('style'), style));
+ $(elt).data('style', OBJ.difference($(elt).data('style'), style));
};
};
@@ -590,7 +549,7 @@
return result;
};
- window.renderStyle = function (selector, properties) {
+ my.renderStyle = function (selector, properties) {
var css = '';
if (!_.isEmpty(properties)) {
css += (selector + " {\n");
@@ -601,4 +560,9 @@
}
return css;
};
+
+ if (typeof exports !== 'undefined') {
+ _.extend(exports, my);
+ }
+ return my;
}());
View
53 lib/obj.js
@@ -0,0 +1,53 @@
+/*jslint indent: 2, nomen: true */
+/*global _, exports */
+
+var OBJ = (function () {
+ "use strict";
+ var my = {};
+
+ my.intersection = function (array) {
+ function simpleIntersection(a, b) {
+ if (typeof a !== 'object' || typeof b !== 'object') {
+ return {};
+ }
+ return _.pick(a, _.intersection(_.keys(a), _.keys(b)).filter(function (key) {
+ return _.isEqual(a[key], b[key]);
+ }));
+ }
+ function repeatedly(f) {
+ return function (args) {
+ if (args.length === 0) { return {}; }
+ var seed = args.pop();
+ return _.reduce(args, function (memo, obj) { return f(memo, obj); }, seed);
+ };
+ }
+
+ return repeatedly(simpleIntersection)(array);
+ };
+
+ my.difference = function (a, b) {
+ if (typeof a !== 'object' || typeof b !== 'object') {
+ return a;
+ }
+ return _.pick(a, _.difference(_.keys(a), _.keys(b)));
+ };
+
+ my.cartesianProduct = function () {
+ return _.reduce(arguments, function (mtrx, vals) {
+ return _.reduce(vals, function (array, val) {
+ return array.concat(
+ _.map(mtrx, function (row) { return row.concat(val); })
+ );
+ }, []);
+ }, [[]]);
+ };
+
+ my.toRealArray = function (array_like) {
+ return Array.prototype.slice.call(array_like);
+ };
+
+ if (typeof exports !== 'undefined') {
+ _.extend(exports, my);
+ }
+ return my;
+}());
View
1  lib/resource.js
@@ -32,6 +32,7 @@
} else {
page.injectJs("vendor/jquery-1.8.2.js");
page.injectJs("vendor/underscore-1.4.2.js");
+ page.injectJs('lib/obj.js');
page.injectJs('lib/css.js');
page.onConsoleMessage = function (msg) {
View
9 ratiocinate.js
@@ -1,5 +1,6 @@
/*jslint browser: true, indent: 2, nomen: true */
-/*global phantom, require, console, $, simplerStyle, _ */
+/*global phantom, CSS, require, console, $, simplerStyle, _ */
+
(function () {
"use strict";
@@ -28,15 +29,15 @@
resource.loadWithLibs(url, verbose, function (page) {
page.evaluate(function () {
- var styles = window.simplerStyle();
+ var styles = CSS.simplerStyle();
console.log("/* Begin computed CSS */");
- fonts = window.fontDeclarations().join("\n\n");
+ fonts = CSS.fontDeclarations().join("\n\n");
if (fonts) { console.log(fonts + "\n"); }
_.each(_.pairs(styles), function (pair) {
- console.log(window.renderStyle(pair[0], pair[1]));
+ console.log(CSS.renderStyle(pair[0], pair[1]));
});
});
phantom.exit();
View
4 test.js
@@ -1,5 +1,5 @@
/*jslint browser: true, indent: 2, nomen: true */
-/*global phantom, require, console, $, _, simplerStyle, jasmine, describe, it, expect */
+/*global phantom, require, console, $, _, CSS, jasmine, describe, it, expect */
(function () {
"use strict";
@@ -38,7 +38,7 @@
return window.expectedStyle;
}),
calculated = page.evaluate(function () {
- return window.simplerStyle();
+ return CSS.simplerStyle();
});
describe(scenario, function () {
Please sign in to comment.
Something went wrong with that request. Please try again.