Skip to content

Commit

Permalink
Merge pull request canjs#964 from bitovi/domless-compilation
Browse files Browse the repository at this point in the history
Domless compilation
  • Loading branch information
Curtis Cummings committed May 5, 2014
2 parents 2a82b64 + 1366a6c commit d4e0931
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 21 deletions.
5 changes: 5 additions & 0 deletions builder.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@
"type": "core",
"isDefault": true,
"hidden": true
},
"can/util/domless": {
"name": "can.util.domless",
"type": "plugin",
"hidden": true
}
},
"types": {
Expand Down
2 changes: 1 addition & 1 deletion util/array/makeArray.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
steal('./each.js', function () {
steal('./each.js', function (can) {
can.makeArray = function (arr) {
var ret = [];
can.each(arr, function (a, i) {
Expand Down
24 changes: 14 additions & 10 deletions util/attr/attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ steal("can/util/can.js", function (can) {

// Acts as a polyfill for setImmediate which only works in IE 10+. Needed to make
// the triggering of `attributes` event async.
var setImmediate = window.setImmediate || function (cb) {
var setImmediate = (typeof window !== "undefined" && window.setImmediate) || function (cb) {
return setTimeout(cb, 0);
},
attr = {
// This property lets us know if the browser supports mutation observers.
// If they are supported then that will be setup in can/util/jquery and those native events will be used to inform observers of attribute changes.
// Otherwise this module handles triggering an `attributes` event on the element.
MutationObserver: window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver,
MutationObserver: typeof window !== "undefined" && (window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver),

/**
* @property {Object.<String,(String|Boolean|function)>} can.view.attr.map
Expand Down Expand Up @@ -161,15 +161,19 @@ steal("can/util/can.js", function (can) {
// Checks if an element contains an attribute.
// For browsers that support `hasAttribute`, creates a function that calls hasAttribute, otherwise creates a function that uses `getAttribute` to check that the attribute is not null.
has: (function () {
var el = document.createElement('div');
if (el.hasAttribute) {
return function (el, name) {
return el.hasAttribute(name);
};
if(typeof document !== "undefined") {
var el = document.createElement('div');
if (el.hasAttribute) {
return function (el, name) {
return el.hasAttribute(name);
};
} else {
return function (el, name) {
return el.getAttribute(name) !== null;
};
}
} else {
return function (el, name) {
return el.getAttribute(name) !== null;
};
return function() {};
}
})()
};
Expand Down
11 changes: 8 additions & 3 deletions util/can.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
steal(function () {
/* global GLOBALCAN */
var can = window.can || {};
if (typeof GLOBALCAN === 'undefined' || GLOBALCAN !== false) {
window.can = can;
var can;
if (typeof window !== 'undefined') {
can = window.can || {};
if(typeof GLOBALCAN === 'undefined' || GLOBALCAN !== false) {
window.can = can;
}
} else {
can = {};
}

// An empty function useful for where you need a dummy callback.
Expand Down
126 changes: 126 additions & 0 deletions util/domless/domless.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
steal('can/util/can.js', 'can/util/attr', 'can/util/array/each.js', 'can/util/array/makeArray.js', function(can, attr) {

var core_trim = String.prototype.trim;
var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;

function likeArray(obj) {
return typeof obj.length === 'number';
}

function flatten(array) {
return array.length > 0 ? Array.prototype.concat.apply([], array) : array;
}

can.isFunction = (function(){
if(typeof document !== 'undefined' && typeof document.getElementsByTagName('body') === 'function'){
return function (value) {
return Object.prototype.toString.call(value) === '[object Function]';
};
} else {
return function (value) {
return typeof value === 'function';
};
}
})();

can.trim = core_trim && !core_trim.call('\uFEFF\xA0') ?
function (text) {
return text == null ? '' : core_trim.call(text);
} :
// Otherwise use our own trimming functionality
function (text) {
return text == null ? '' : ( text + '' ).replace(rtrim, '');
};

// This extend() function is ruthlessly and shamelessly stolen from
// jQuery 1.8.2:, lines 291-353.
can.extend = function () {
/*jshint maxdepth:6 */
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;

// Handle a deep copy situation
if (typeof target === "boolean") {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}

// Handle case when target is a string or something (possible in deep copy)
if (typeof target !== "object" && !can.isFunction(target)) {
target = {};
}

// extend jQuery itself if only one argument is passed
if (length === i) {
target = this;
--i;
}

for (; i < length; i++) {
// Only deal with non-null/undefined values
if ((options = arguments[ i ]) != null) {
// Extend the base object
for (name in options) {
src = target[ name ];
copy = options[ name ];

// Prevent never-ending loop
if (target === copy) {
continue;
}

// Recurse if we're merging plain objects or arrays
if (deep && copy && ( can.isPlainObject(copy) || (copyIsArray = can.isArray(copy)) )) {
if (copyIsArray) {
copyIsArray = false;
clone = src && can.isArray(src) ? src : [];

} else {
clone = src && can.isPlainObject(src) ? src : {};
}

// Never move original objects, clone them
target[ name ] = can.extend(deep, clone, copy);

// Don't bring in undefined values
} else if (copy !== undefined) {
target[ name ] = copy;
}
}
}
}

// Return the modified object
return target;
};

can.map = function (elements, callback) {
var values = [],
putValue = function (val, index) {
var value = callback(val, index);
if (value != null) {
values.push(value);
}
};
if (likeArray(elements)) {
for (var i = 0, l = elements.length; i < l; i++) {
putValue(elements[i], i);
}
} else {
for (var key in elements) {
putValue(elements[key], key);
}
}
return flatten(values);
};

can.attr = attr;

return can;

});
2 changes: 1 addition & 1 deletion view/callbacks/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ steal("can/util", "can/view",function(can){
var tag = can.view.tag = function (tagName, tagHandler) {
if(tagHandler) {
// if we have html5shive ... re-generate
if (window.html5) {
if (typeof window !== "undefined" && window.html5) {
window.html5.elements += " " + tagName;
window.html5.shivDocument();
}
Expand Down
2 changes: 1 addition & 1 deletion view/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ steal('can/util', "can/view",function (can) {
*/
var elements = {
tagToContentPropMap: {
option: 'textContent' in document.createElement('option') ? 'textContent' : 'innerText',
option: ( typeof document !=="undefined" && "textContent" in document.createElement("option") ) ? "textContent" : "innerText",
textarea: 'value'
},
/**
Expand Down
5 changes: 4 additions & 1 deletion view/mustache/mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ steal('can/util',
* @add can.Mustache
*/
// Put Mustache on the `can` object.
can.Mustache = window.Mustache = Mustache;
can.Mustache = Mustache;
if(typeof window !== "undefined") {
window.Mustache = Mustache;
}

/**
* @prototype
Expand Down
2 changes: 1 addition & 1 deletion view/parser/parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* jshint maxdepth:7*/
steal("can/view", function(){
steal("can/view", function(can){


function makeMap(str){
Expand Down
6 changes: 3 additions & 3 deletions view/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ steal('can/util', function (can) {
// You should only be using `//` if you are using an AMD loader like `steal` or `require` (not almond).
if (url.match(/^\/\//)) {
url = url.substr(2);
url = !window.steal ?
url = ( typeof window === "undefined" || ! window.steal ) ?
url :
steal.config()
.root.mapJoin("" + steal.id(url));
Expand Down Expand Up @@ -382,7 +382,7 @@ steal('can/util', function (can) {
// _removed if not used as a steal module_

//!steal-remove-start
if (window.steal) {
if ( typeof window !== "undefined" && window.steal ) {
steal.type(info.suffix + " view js", function (options, success, error) {
var type = $view.types["." + options.type],
id = $view.toId(options.id + '');
Expand Down Expand Up @@ -711,7 +711,7 @@ steal('can/util', function (can) {
// _removed if not used as a steal module_

//!steal-remove-start
if (window.steal) {
if ( typeof window !== "undefined" && window.steal ) {
//when being used as a steal module, add a new type for 'view' that runs
// `can.view.preloadStringRenderer` with the loaded string/text for the dependency.
steal.type("view js", function (options, success, error) {
Expand Down

0 comments on commit d4e0931

Please sign in to comment.