anima-yocto-core
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -5,4 +5,8 @@ # History | ||
## 0.0.0 | ||
## 1.1.0 | ||
整体优化,具体见[这里]http://gitlab.alibaba-inc.com/animajs/yocto-core/issues/2 | ||
## 1.0.0 | ||
`new` It is the first version of anima-yocto-core. |
{ | ||
"name": "anima-yocto-core", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "the core from yocto", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
731
src/core.js
@@ -1,27 +0,10 @@ | ||
var Yocto = (function() { | ||
var Yocto = (function () { | ||
var undefined, key, $, classList, emptyArray = [], slice = emptyArray.slice, filter = emptyArray.filter, | ||
document = window.document, | ||
// [Opt:C] 增加win变量缓存window | ||
win = window, | ||
document = win.document, | ||
elementDisplay = {}, classCache = {}, | ||
cssNumber = { 'column-count': 1, 'columns': 1, 'font-weight': 1, 'line-height': 1,'opacity': 1, 'z-index': 1, 'zoom': 1 }, | ||
fragmentRE = /^\s*<(\w+|!)[^>]*>/, | ||
singleTagRE = /^<(\w+)\s*\/?>(?:<\/\1>|)$/, | ||
tagExpanderRE = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig, | ||
rootNodeRE = /^(?:body|html)$/i, | ||
capitalRE = /([A-Z])/g, | ||
// special attributes that should be get/set via method calls | ||
methodAttributes = ['val', 'css', 'html', 'text', 'data', 'width', 'height', 'offset'], | ||
adjacencyOperators = [ 'after', 'prepend', 'before', 'append' ], | ||
table = document.createElement('table'), | ||
tableRow = document.createElement('tr'), | ||
containers = { | ||
'tr': document.createElement('tbody'), | ||
'tbody': table, 'thead': table, 'tfoot': table, | ||
'td': tableRow, 'th': tableRow, | ||
'*': document.createElement('div') | ||
}, | ||
readyRE = /complete|loaded|interactive/, | ||
simpleSelectorRE = /^[\w-]*$/, | ||
class2type = {}, | ||
@@ -31,25 +14,15 @@ toString = class2type.toString, | ||
camelize, uniq, | ||
tempParent = document.createElement('div'), | ||
propMap = { | ||
'tabindex': 'tabIndex', | ||
'readonly': 'readOnly', | ||
'for': 'htmlFor', | ||
'class': 'className', | ||
'maxlength': 'maxLength', | ||
'cellspacing': 'cellSpacing', | ||
'cellpadding': 'cellPadding', | ||
'rowspan': 'rowSpan', | ||
'colspan': 'colSpan', | ||
'usemap': 'useMap', | ||
'frameborder': 'frameBorder', | ||
'contenteditable': 'contentEditable' | ||
}, | ||
isArray = Array.isArray || | ||
function(object){ return object instanceof Array } | ||
//[Opt:B][V1.0+] 由于已经删除prop方法,因此原propMap变量一并删除 | ||
//[Opt:C] 去掉isArray旧方法的兼容 | ||
isArray = Array.isArray | ||
zepto.matches = function(element, selector) { | ||
zepto.matches = function (element, selector) { | ||
// [Opt:C] 将原本在父级作用域的变量转移至局部变量 | ||
var tempParent = document.createElement('div') | ||
if (!selector || !element || element.nodeType !== 1) return false | ||
var matchesSelector = element.webkitMatchesSelector || element.mozMatchesSelector || | ||
element.oMatchesSelector || element.matchesSelector | ||
if (matchesSelector) return matchesSelector.call(element, selector) | ||
// [Opt:C] 去除对moz o 的支持,一般情况下,是不会遇到以上的浏览器,不针对moz和o做专门的优化 | ||
var matchesSelector = element.webkitMatchesSelector || element.matchesSelector; | ||
if (matchesSelector) return matchesSelector.call(element, selector); | ||
// fall back to performing a selector: | ||
@@ -63,2 +36,6 @@ var match, parent = element.parentNode, temp = !parent | ||
//opt by 完颜 | ||
//Get string type of an object. | ||
//Possible types are: | ||
//null undefined boolean number string function array date regexp object error. | ||
function type(obj) { | ||
@@ -69,49 +46,60 @@ return obj == null ? String(obj) : | ||
function isFunction(value) { return type(value) == "function" } | ||
function isWindow(obj) { return obj != null && obj == obj.window } | ||
function isDocument(obj) { return obj != null && obj.nodeType == obj.DOCUMENT_NODE } | ||
function isObject(obj) { return type(obj) == "object" } | ||
function isFunction(value) { | ||
return type(value) == "function" | ||
} | ||
function isWindow(obj) { | ||
return obj != null && obj == obj.window | ||
} | ||
function isDocument(obj) { | ||
return obj != null && obj.nodeType == obj.DOCUMENT_NODE | ||
} | ||
function isObject(obj) { | ||
return type(obj) == "object" | ||
} | ||
function isPlainObject(obj) { | ||
return isObject(obj) && !isWindow(obj) && Object.getPrototypeOf(obj) == Object.prototype | ||
} | ||
function likeArray(obj) { return typeof obj.length == 'number' } | ||
function compact(array) { return filter.call(array, function(item){ return item != null }) } | ||
function flatten(array) { return array.length > 0 ? $.fn.concat.apply([], array) : array } | ||
camelize = function(str){ return str.replace(/-+(.)?/g, function(match, chr){ return chr ? chr.toUpperCase() : '' }) } | ||
function dasherize(str) { | ||
return str.replace(/::/g, '/') | ||
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2') | ||
.replace(/([a-z\d])([A-Z])/g, '$1_$2') | ||
.replace(/_/g, '-') | ||
.toLowerCase() | ||
function likeArray(obj) { | ||
return typeof obj.length == 'number' | ||
} | ||
uniq = function(array){ return filter.call(array, function(item, idx){ return array.indexOf(item) == idx }) } | ||
function classRE(name) { | ||
return name in classCache ? | ||
classCache[name] : (classCache[name] = new RegExp('(^|\\s)' + name + '(\\s|$)')) | ||
function compact(array) { | ||
return filter.call(array, function (item) { | ||
return item != null | ||
}) | ||
} | ||
function maybeAddPx(name, value) { | ||
return (typeof value == "number" && !cssNumber[dasherize(name)]) ? value + "px" : value | ||
function flatten(array) { | ||
return array.length > 0 ? $.fn.concat.apply([], array) : array | ||
} | ||
function defaultDisplay(nodeName) { | ||
var element, display | ||
if (!elementDisplay[nodeName]) { | ||
element = document.createElement(nodeName) | ||
document.body.appendChild(element) | ||
display = getComputedStyle(element, '').getPropertyValue("display") | ||
element.parentNode.removeChild(element) | ||
display == "none" && (display = "block") | ||
elementDisplay[nodeName] = display | ||
} | ||
return elementDisplay[nodeName] | ||
//将中划线连接符转化为驼峰字符串 | ||
camelize = function (str) { | ||
return str.replace(/-+(.)?/g, function (match, chr) { | ||
return chr ? chr.toUpperCase() : '' | ||
}) | ||
} | ||
uniq = function (array) { | ||
return filter.call(array, function (item, idx) { | ||
return array.indexOf(item) == idx | ||
}) | ||
} | ||
function classRE(name) { | ||
return name in classCache ? | ||
classCache[name] : (classCache[name] = new RegExp('(^|\\s)' + name + '(\\s|$)')) | ||
} | ||
function children(element) { | ||
return 'children' in element ? | ||
slice.call(element.children) : | ||
$.map(element.childNodes, function(node){ if (node.nodeType == 1) return node }) | ||
$.map(element.childNodes, function (node) { | ||
if (node.nodeType == 1) return node | ||
}) | ||
} | ||
@@ -124,5 +112,19 @@ | ||
// it compatible with browsers that don't support the DOM fully. | ||
zepto.fragment = function(html, name, properties) { | ||
var dom, nodes, container | ||
zepto.fragment = function (html, name) { | ||
// [Opt:C] 将原本在父级作用域的变量转移至局部变量 | ||
var table = document.createElement('table'), | ||
tableRow = document.createElement('tr'), | ||
containers = { | ||
'tr': document.createElement('tbody'), | ||
'tbody': table, 'thead': table, 'tfoot': table, | ||
'td': tableRow, 'th': tableRow, | ||
'*': document.createElement('div') | ||
}, | ||
tagExpanderRE = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig, | ||
singleTagRE = /^<(\w+)\s*\/?>(?:<\/\1>|)$/, | ||
// special attributes that should be get/set via method calls | ||
methodAttributes = ['val', 'css', 'html', 'text', 'data', 'width', 'height', 'offset'] | ||
var dom, nodes, container; | ||
// A special case optimization for a single tag | ||
@@ -138,15 +140,9 @@ if (singleTagRE.test(html)) dom = $(document.createElement(RegExp.$1)) | ||
container.innerHTML = '' + html | ||
dom = $.each(slice.call(container.childNodes), function(){ | ||
dom = $.each(slice.call(container.childNodes), function () { | ||
container.removeChild(this) | ||
}) | ||
} | ||
//[Opt:B-1][V1.0+] 移除 $(htmlString, attributes) 的api方法支持 | ||
if (isPlainObject(properties)) { | ||
nodes = $(dom) | ||
$.each(properties, function(key, value) { | ||
if (methodAttributes.indexOf(key) > -1) nodes[key](value) | ||
else nodes.attr(key, value) | ||
}) | ||
} | ||
return dom | ||
@@ -159,3 +155,3 @@ } | ||
// Explorer. This method can be overriden in plugins. | ||
zepto.Z = function(dom, selector) { | ||
zepto.Z = function (dom, selector) { | ||
dom = dom || [] | ||
@@ -169,3 +165,3 @@ dom.__proto__ = $.fn | ||
// collection. This method can be overriden in plugins. | ||
zepto.isZ = function(object) { | ||
zepto.isZ = function (object) { | ||
return object instanceof zepto.Z | ||
@@ -178,3 +174,3 @@ } | ||
// This method can be overriden in plugins. | ||
zepto.init = function(selector, context) { | ||
zepto.init = function (selector, context) { | ||
var dom | ||
@@ -224,21 +220,22 @@ // If nothing given, return an empty Zepto collection | ||
// patchable in plugins. | ||
$ = function(selector, context){ | ||
$ = function (selector, context) { | ||
return zepto.init(selector, context) | ||
} | ||
function extend(target, source, deep) { | ||
for (key in source) | ||
if (deep && (isPlainObject(source[key]) || isArray(source[key]))) { | ||
if (isPlainObject(source[key]) && !isPlainObject(target[key])) | ||
target[key] = {} | ||
if (isArray(source[key]) && !isArray(target[key])) | ||
target[key] = [] | ||
extend(target[key], source[key], deep) | ||
} | ||
else if (source[key] !== undefined) target[key] = source[key] | ||
} | ||
// Copy all but undefined properties from one or more | ||
// objects to the `target` object. | ||
$.extend = function(target){ | ||
$.extend = function (target) { | ||
//[Opt:C] 将全局函数编程内部函数 | ||
var extend = function (target, source, deep) { | ||
for (key in source) | ||
if (deep && (isPlainObject(source[key]) || isArray(source[key]))) { | ||
if (isPlainObject(source[key]) && !isPlainObject(target[key])) | ||
target[key] = {} | ||
if (isArray(source[key]) && !isArray(target[key])) | ||
target[key] = [] | ||
extend(target[key], source[key], deep) | ||
} | ||
else if (source[key] !== undefined) target[key] = source[key] | ||
} | ||
var deep, args = slice.call(arguments, 1) | ||
@@ -249,3 +246,5 @@ if (typeof target == 'boolean') { | ||
} | ||
args.forEach(function(arg){ extend(target, arg, deep) }) | ||
args.forEach(function (arg) { | ||
extend(target, arg, deep) | ||
}) | ||
return target | ||
@@ -257,17 +256,19 @@ } | ||
// This method can be overriden in plugins. | ||
zepto.qsa = function(element, selector){ | ||
var found, | ||
maybeID = selector[0] == '#', | ||
maybeClass = !maybeID && selector[0] == '.', | ||
nameOnly = maybeID || maybeClass ? selector.slice(1) : selector, // Ensure that a 1 char tag name still gets checked | ||
isSimple = simpleSelectorRE.test(nameOnly) | ||
// opt by 轩与 | ||
zepto.qsa = function (element, selector) { | ||
// [Opt:C] 将全局simpleSelectorRE转到局部 | ||
var found, simpleSelectorRE = /^[\w-]*$/, | ||
maybeID = selector[0] == '#', | ||
maybeClass = !maybeID && selector[0] == '.', | ||
nameOnly = maybeID || maybeClass ? selector.slice(1) : selector, // Ensure that a 1 char tag name still gets checked | ||
isSimple = /^[\w-]*$/.test(nameOnly) | ||
return (isDocument(element) && isSimple && maybeID) ? | ||
( (found = element.getElementById(nameOnly)) ? [found] : [] ) : | ||
(element.nodeType !== 1 && element.nodeType !== 9) ? [] : | ||
slice.call( | ||
isSimple && !maybeID ? | ||
maybeClass ? element.getElementsByClassName(nameOnly) : // If it's simple, it could be a class | ||
element.getElementsByTagName(selector) : // Or a tag | ||
element.querySelectorAll(selector) // Or it's not simple, and we need to query all | ||
) | ||
slice.call( | ||
isSimple && !maybeID ? | ||
maybeClass ? element.getElementsByClassName(nameOnly) : // If it's simple, it could be a class | ||
element.getElementsByTagName(selector) : // Or a tag | ||
element.querySelectorAll(selector) // Or it's not simple, and we need to query all | ||
) | ||
} | ||
@@ -279,3 +280,3 @@ | ||
$.contains = function(parent, node) { | ||
$.contains = function (parent, node) { | ||
return parent !== node && parent.contains(node) | ||
@@ -293,5 +294,5 @@ } | ||
// access className property while respecting SVGAnimatedString | ||
function className(node, value){ | ||
function className(node, value) { | ||
var klass = node.className, | ||
svg = klass && klass.baseVal !== undefined | ||
svg = klass && klass.baseVal !== undefined | ||
@@ -316,8 +317,8 @@ if (value === undefined) return svg ? klass.baseVal : klass | ||
( value == "false" ? false : | ||
value == "null" ? null : | ||
!/^0/.test(value) && !isNaN(num = Number(value)) ? num : | ||
value == "null" ? null : | ||
!/^0/.test(value) && !isNaN(num = Number(value)) ? num : | ||
/^[\[\{]/.test(value) ? $.parseJSON(value) : | ||
value ) | ||
value ) | ||
: value | ||
} catch(e) { | ||
} catch (e) { | ||
return value | ||
@@ -333,10 +334,7 @@ } | ||
$.isEmptyObject = function(obj) { | ||
var name | ||
for (name in obj) return false | ||
return true | ||
} | ||
//[Opt:A] 移除$.isEmptyObject方法,官网无公开,core内无引用 | ||
//$.isEmptyObject | ||
$.camelCase = camelize | ||
$.trim = function(str) { | ||
$.trim = function (str) { | ||
return str == null ? "" : String.prototype.trim.call(str) | ||
@@ -350,3 +348,3 @@ } | ||
$.map = function(elements, callback){ | ||
$.map = function (elements, callback) { | ||
var value, values = [], i, key | ||
@@ -366,3 +364,3 @@ if (likeArray(elements)) | ||
$.each = function(elements, callback){ | ||
$.each = function (elements, callback) { | ||
var i, key | ||
@@ -380,6 +378,7 @@ if (likeArray(elements)) { | ||
if (window.JSON) $.parseJSON = JSON.parse | ||
// [Opt:C] 删除不必要的if (win.JSON) | ||
$.parseJSON = JSON.parse | ||
// Populate the class2type map | ||
$.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function(i, name) { | ||
$.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function (i, name) { | ||
class2type[ "[object " + name + "]" ] = name.toLowerCase() | ||
@@ -402,25 +401,34 @@ }) | ||
// from their array counterparts | ||
map: function(fn){ | ||
return $($.map(this, function(el, i){ return fn.call(el, i, el) })) | ||
map: function (fn) { | ||
return $($.map(this, function (el, i) { | ||
return fn.call(el, i, el) | ||
})) | ||
}, | ||
slice: function(){ | ||
slice: function () { | ||
return $(slice.apply(this, arguments)) | ||
}, | ||
ready: function (callback) { | ||
//[Opt:C]将原本在父级作用域的变量转移至局部变量 | ||
var readyRE = /complete|loaded|interactive/ | ||
ready: function(callback){ | ||
// need to check if document.body exists for IE as that browser reports | ||
// document ready when it hasn't yet created the body element | ||
if (readyRE.test(document.readyState) && document.body) callback($) | ||
else document.addEventListener('DOMContentLoaded', function(){ callback($) }, false) | ||
// [Opt:C] 不做ie的兼容 | ||
if (readyRE.test(document.readyState)) callback($) | ||
else document.addEventListener('DOMContentLoaded', function () { | ||
callback($) | ||
}, false) | ||
return this | ||
}, | ||
get: function(idx){ | ||
get: function (idx) { | ||
return idx === undefined ? slice.call(this) : this[idx >= 0 ? idx : idx + this.length] | ||
}, | ||
toArray: function(){ return this.get() }, | ||
size: function(){ | ||
toArray: function () { | ||
return this.get() | ||
}, | ||
size: function () { | ||
return this.length | ||
}, | ||
remove: function(){ | ||
return this.each(function(){ | ||
remove: function () { | ||
return this.each(function () { | ||
if (this.parentNode != null) | ||
@@ -430,4 +438,4 @@ this.parentNode.removeChild(this) | ||
}, | ||
each: function(callback){ | ||
emptyArray.every.call(this, function(el, idx){ | ||
each: function (callback) { | ||
emptyArray.every.call(this, function (el, idx) { | ||
return callback.call(el, idx, el) !== false | ||
@@ -437,19 +445,19 @@ }) | ||
}, | ||
filter: function(selector){ | ||
filter: function (selector) { | ||
if (isFunction(selector)) return this.not(this.not(selector)) | ||
return $(filter.call(this, function(element){ | ||
return $(filter.call(this, function (element) { | ||
return zepto.matches(element, selector) | ||
})) | ||
}, | ||
add: function(selector,context){ | ||
return $(uniq(this.concat($(selector,context)))) | ||
add: function (selector, context) { | ||
return $(uniq(this.concat($(selector, context)))) | ||
}, | ||
is: function(selector){ | ||
is: function (selector) { | ||
return this.length > 0 && zepto.matches(this[0], selector) | ||
}, | ||
not: function(selector){ | ||
var nodes=[] | ||
not: function (selector) { | ||
var nodes = [] | ||
if (isFunction(selector) && selector.call !== undefined) | ||
this.each(function(idx){ | ||
if (!selector.call(this,idx)) nodes.push(this) | ||
this.each(function (idx) { | ||
if (!selector.call(this, idx)) nodes.push(this) | ||
}) | ||
@@ -459,3 +467,3 @@ else { | ||
(likeArray(selector) && isFunction(selector.item)) ? slice.call(selector) : $(selector) | ||
this.forEach(function(el){ | ||
this.forEach(function (el) { | ||
if (excludes.indexOf(el) < 0) nodes.push(el) | ||
@@ -466,4 +474,4 @@ }) | ||
}, | ||
has: function(selector){ | ||
return this.filter(function(){ | ||
has: function (selector) { | ||
return this.filter(function () { | ||
return isObject(selector) ? | ||
@@ -474,19 +482,19 @@ $.contains(this, selector) : | ||
}, | ||
eq: function(idx){ | ||
return idx === -1 ? this.slice(idx) : this.slice(idx, + idx + 1) | ||
eq: function (idx) { | ||
return idx === -1 ? this.slice(idx) : this.slice(idx, +idx + 1) | ||
}, | ||
first: function(){ | ||
first: function () { | ||
var el = this[0] | ||
return el && !isObject(el) ? el : $(el) | ||
}, | ||
last: function(){ | ||
last: function () { | ||
var el = this[this.length - 1] | ||
return el && !isObject(el) ? el : $(el) | ||
}, | ||
find: function(selector){ | ||
find: function (selector) { | ||
var result, $this = this | ||
if (typeof selector == 'object') | ||
result = $(selector).filter(function(){ | ||
result = $(selector).filter(function () { | ||
var node = this | ||
return emptyArray.some.call($this, function(parent){ | ||
return emptyArray.some.call($this, function (parent) { | ||
return $.contains(parent, node) | ||
@@ -496,16 +504,16 @@ }) | ||
else if (this.length == 1) result = $(zepto.qsa(this[0], selector)) | ||
else result = this.map(function(){ return zepto.qsa(this, selector) }) | ||
else result = this.map(function () { | ||
return zepto.qsa(this, selector) | ||
}) | ||
return result | ||
}, | ||
closest: function(selector, context){ | ||
var node = this[0], collection = false | ||
if (typeof selector == 'object') collection = $(selector) | ||
while (node && !(collection ? collection.indexOf(node) >= 0 : zepto.matches(node, selector))) | ||
node = node !== context && !isDocument(node) && node.parentNode | ||
return $(node) | ||
//[Opt:B][V1.0+] : closest的父级选择,代理parents,去除原有的第二个参数支持 | ||
closest: function (selector) { | ||
if(zepto.matches(this[0], selector)) return $(this[0]) | ||
else return $(this.parents(selector).get(0)) | ||
}, | ||
parents: function(selector){ | ||
parents: function (selector) { | ||
var ancestors = [], nodes = this | ||
while (nodes.length > 0) | ||
nodes = $.map(nodes, function(node){ | ||
nodes = $.map(nodes, function (node) { | ||
if ((node = node.parentNode) && !isDocument(node) && ancestors.indexOf(node) < 0) { | ||
@@ -518,98 +526,92 @@ ancestors.push(node) | ||
}, | ||
parent: function(selector){ | ||
parent: function (selector) { | ||
return filtered(uniq(this.pluck('parentNode')), selector) | ||
}, | ||
children: function(selector){ | ||
return filtered(this.map(function(){ return children(this) }), selector) | ||
children: function (selector) { | ||
return filtered(this.map(function () { | ||
return children(this) | ||
}), selector) | ||
}, | ||
contents: function() { | ||
return this.map(function() { return slice.call(this.childNodes) }) | ||
}, | ||
siblings: function(selector){ | ||
return filtered(this.map(function(i, el){ | ||
return filter.call(children(el.parentNode), function(child){ return child!==el }) | ||
//[Opt:B][V1.0+] : contents已经转移至plugin | ||
siblings: function (selector) { | ||
return filtered(this.map(function (i, el) { | ||
return filter.call(children(el.parentNode), function (child) { | ||
return child !== el | ||
}) | ||
}), selector) | ||
}, | ||
empty: function(){ | ||
return this.each(function(){ this.innerHTML = '' }) | ||
}, | ||
//[Opt:A] : empty已经转移至plugin | ||
// `pluck` is borrowed from Prototype.js | ||
pluck: function(property){ | ||
return $.map(this, function(el){ return el[property] }) | ||
pluck: function (property) { | ||
return $.map(this, function (el) { | ||
return el[property] | ||
}) | ||
}, | ||
show: function(){ | ||
return this.each(function(){ | ||
show: function () { | ||
// [Opt:C] 提取函数 | ||
var getDisplay = function (DOM) { | ||
return getComputedStyle(DOM, '').getPropertyValue("display") | ||
}; | ||
return this.each(function () { | ||
this.style.display == "none" && (this.style.display = '') | ||
if (getComputedStyle(this, '').getPropertyValue("display") == "none") | ||
if (getDisplay(this) == "none") { | ||
// [Opt:C] 将defaultDisplay方法局部化 | ||
var defaultDisplay = function (nodeName) { | ||
var element, display | ||
if (!elementDisplay[nodeName]) { | ||
element = document.createElement(nodeName) | ||
document.body.appendChild(element) | ||
display = getDisplay(element) | ||
element.parentNode.removeChild(element) | ||
display == "none" && (display = "block") | ||
elementDisplay[nodeName] = display | ||
} | ||
return elementDisplay[nodeName] | ||
} | ||
this.style.display = defaultDisplay(this.nodeName) | ||
} | ||
}) | ||
}, | ||
replaceWith: function(newContent){ | ||
replaceWith: function (newContent) { | ||
return this.before(newContent).remove() | ||
}, | ||
wrap: function(structure){ | ||
var func = isFunction(structure) | ||
if (this[0] && !func) | ||
var dom = $(structure).get(0), | ||
clone = dom.parentNode || this.length > 1 | ||
return this.each(function(index){ | ||
$(this).wrapAll( | ||
func ? structure.call(this, index) : | ||
clone ? dom.cloneNode(true) : dom | ||
) | ||
//[Opt:A] : wrap系列方法,已经转移至plugin | ||
clone: function () { | ||
return this.map(function () { | ||
return this.cloneNode(true) | ||
}) | ||
}, | ||
wrapAll: function(structure){ | ||
if (this[0]) { | ||
$(this[0]).before(structure = $(structure)) | ||
var children | ||
// drill down to the inmost element | ||
while ((children = structure.children()).length) structure = children.first() | ||
$(structure).append(this) | ||
} | ||
return this | ||
hide: function () { | ||
return this.css("display", "none") | ||
}, | ||
wrapInner: function(structure){ | ||
var func = isFunction(structure) | ||
return this.each(function(index){ | ||
var self = $(this), contents = self.contents(), | ||
dom = func ? structure.call(this, index) : structure | ||
contents.length ? contents.wrapAll(dom) : self.append(dom) | ||
toggle: function (setting) { | ||
return this.each(function () { | ||
var el = $(this) | ||
; | ||
(setting === undefined ? el.css("display") == "none" : setting) ? el.show() : el.hide() | ||
}) | ||
}, | ||
unwrap: function(){ | ||
this.parent().each(function(){ | ||
$(this).replaceWith($(this).children()) | ||
}) | ||
return this | ||
prev: function (selector) { | ||
return $(this.pluck('previousElementSibling')).filter(selector || '*') | ||
}, | ||
clone: function(){ | ||
return this.map(function(){ return this.cloneNode(true) }) | ||
next: function (selector) { | ||
return $(this.pluck('nextElementSibling')).filter(selector || '*') | ||
}, | ||
hide: function(){ | ||
return this.css("display", "none") | ||
}, | ||
toggle: function(setting){ | ||
return this.each(function(){ | ||
var el = $(this) | ||
;(setting === undefined ? el.css("display") == "none" : setting) ? el.show() : el.hide() | ||
}) | ||
}, | ||
prev: function(selector){ return $(this.pluck('previousElementSibling')).filter(selector || '*') }, | ||
next: function(selector){ return $(this.pluck('nextElementSibling')).filter(selector || '*') }, | ||
html: function(html){ | ||
html: function (html) { | ||
return arguments.length === 0 ? | ||
(this.length > 0 ? this[0].innerHTML : null) : | ||
this.each(function(idx){ | ||
this.each(function (idx) { | ||
var originHtml = this.innerHTML | ||
$(this).empty().append( funcArg(this, html, idx, originHtml) ) | ||
this.innerHTML = '' | ||
$(this).append(funcArg(this, html, idx, originHtml)) | ||
}) | ||
}, | ||
text: function(text){ | ||
text: function (text) { | ||
return arguments.length === 0 ? | ||
(this.length > 0 ? this[0].textContent : null) : | ||
this.each(function(){ this.textContent = (text === undefined) ? '' : ''+text }) | ||
this.each(function () { | ||
this.textContent = (text === undefined) ? '' : '' + text | ||
}) | ||
}, | ||
attr: function(name, value){ | ||
attr: function (name, value) { | ||
var result | ||
@@ -619,5 +621,5 @@ return (typeof name == 'string' && value === undefined) ? | ||
(name == 'value' && this[0].nodeName == 'INPUT') ? this.val() : | ||
(!(result = this[0].getAttribute(name)) && name in this[0]) ? this[0][name] : result | ||
) : | ||
this.each(function(idx){ | ||
(!(result = this[0].getAttribute(name)) && name in this[0]) ? this[0][name] : result | ||
) : | ||
this.each(function (idx) { | ||
if (this.nodeType !== 1) return | ||
@@ -628,37 +630,32 @@ if (isObject(name)) for (key in name) setAttribute(this, key, name[key]) | ||
}, | ||
removeAttr: function(name){ | ||
return this.each(function(){ this.nodeType === 1 && setAttribute(this, name) }) | ||
removeAttr: function (name) { | ||
return this.each(function () { | ||
this.nodeType === 1 && setAttribute(this, name) | ||
}) | ||
}, | ||
data: function(name, value){ | ||
var data = this.attr('data-' + name.replace(capitalRE, '-$1').toLowerCase(), value) | ||
data: function (name, value) { | ||
//[Opt:C]将原本在父级作用域的变量转移至局部变量 | ||
var capitalRE = /([A-Z])/g, | ||
data = this.attr('data-' + name.replace(capitalRE, '-$1').toLowerCase(), value) | ||
return data !== null ? deserializeValue(data) : undefined | ||
}, | ||
val: function(value){ | ||
val: function (value) { | ||
return arguments.length === 0 ? | ||
(this[0] && (this[0].multiple ? | ||
$(this[0]).find('option').filter(function(){ return this.selected }).pluck('value') : | ||
this[0].value) | ||
) : | ||
this.each(function(idx){ | ||
$(this[0]).find('option').filter(function () { | ||
return this.selected | ||
}).pluck('value') : | ||
this[0].value) | ||
) : | ||
this.each(function (idx) { | ||
this.value = funcArg(this, value, idx, this.value) | ||
}) | ||
}, | ||
offset: function(coordinates){ | ||
if (coordinates) return this.each(function(index){ | ||
var $this = $(this), | ||
coords = funcArg(this, coordinates, index, $this.offset()), | ||
parentOffset = $this.offsetParent().offset(), | ||
props = { | ||
top: coords.top - parentOffset.top, | ||
left: coords.left - parentOffset.left | ||
} | ||
if ($this.css('position') == 'static') props['position'] = 'relative' | ||
$this.css(props) | ||
}) | ||
if (this.length==0) return null | ||
//[Opt:B][V1.0+] 去除offset的coordinates参数 | ||
offset: function () { | ||
if (this.length == 0) return null | ||
var obj = this[0].getBoundingClientRect() | ||
return { | ||
left: obj.left + window.pageXOffset, | ||
top: obj.top + window.pageYOffset, | ||
left: obj.left + win.pageXOffset, | ||
top: obj.top + win.pageYOffset, | ||
width: Math.round(obj.width), | ||
@@ -668,6 +665,24 @@ height: Math.round(obj.height) | ||
}, | ||
css: function(property, value){ | ||
css: function (property, value) { | ||
//智能补足:分析css方法中传入的value,如果name是在cssNumber清单外的纯数字,则增加px单位 | ||
//[Opt:C] 将全局函数装到局部函数 | ||
var maybeAddPx = function (name, value) { | ||
//[Opt:C]将原本在父级作用域的变量转移至局部变量 | ||
var cssNumber = { 'column-count': 1, 'columns': 1, 'font-weight': 1, 'line-height': 1, 'opacity': 1, 'z-index': 1, 'zoom': 1 } | ||
return (typeof value == "number" && !cssNumber[dasherize(name)]) ? value + "px" : value | ||
} | ||
//将字符串(驼峰)转换为dasherized(中划线连接符形式命名)字符 | ||
//[Opt:C] 将全局函数装到局部函数 | ||
var dasherize = function (str) { | ||
return str.replace(/::/g, '/') | ||
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2') | ||
.replace(/([a-z\d])([A-Z])/g, '$1_$2') | ||
.replace(/_/g, '-') | ||
.toLowerCase() | ||
} | ||
if (arguments.length < 2) { | ||
var element = this[0], computedStyle = getComputedStyle(element, '') | ||
if(!element) return | ||
if (!element) return | ||
if (typeof property == 'string') | ||
@@ -677,3 +692,3 @@ return element.style[camelize(property)] || computedStyle.getPropertyValue(property) | ||
var props = {} | ||
$.each(isArray(property) ? property: [property], function(_, prop){ | ||
$.each(isArray(property) ? property : [property], function (_, prop) { | ||
props[prop] = (element.style[camelize(prop)] || computedStyle.getPropertyValue(prop)) | ||
@@ -688,3 +703,5 @@ }) | ||
if (!value && value !== 0) | ||
this.each(function(){ this.style.removeProperty(dasherize(property)) }) | ||
this.each(function () { | ||
this.style.removeProperty(dasherize(property)) | ||
}) | ||
else | ||
@@ -695,3 +712,5 @@ css = dasherize(property) + ":" + maybeAddPx(property, value) | ||
if (!property[key] && property[key] !== 0) | ||
this.each(function(){ this.style.removeProperty(dasherize(key)) }) | ||
this.each(function () { | ||
this.style.removeProperty(dasherize(key)) | ||
}) | ||
else | ||
@@ -701,19 +720,21 @@ css += dasherize(key) + ':' + maybeAddPx(key, property[key]) + ';' | ||
return this.each(function(){ this.style.cssText += ';' + css }) | ||
return this.each(function () { | ||
this.style.cssText += ';' + css | ||
}) | ||
}, | ||
index: function(element){ | ||
index: function (element) { | ||
return element ? this.indexOf($(element)[0]) : this.parent().children().indexOf(this[0]) | ||
}, | ||
hasClass: function(name){ | ||
hasClass: function (name) { | ||
if (!name) return false | ||
return emptyArray.some.call(this, function(el){ | ||
return emptyArray.some.call(this, function (el) { | ||
return this.test(className(el)) | ||
}, classRE(name)) | ||
}, | ||
addClass: function(name){ | ||
addClass: function (name) { | ||
if (!name) return this | ||
return this.each(function(idx){ | ||
return this.each(function (idx) { | ||
classList = [] | ||
var cls = className(this), newName = funcArg(this, name, idx, cls) | ||
newName.split(/\s+/g).forEach(function(klass){ | ||
newName.split(/\s+/g).forEach(function (klass) { | ||
if (!$(this).hasClass(klass)) classList.push(klass) | ||
@@ -724,7 +745,7 @@ }, this) | ||
}, | ||
removeClass: function(name){ | ||
return this.each(function(idx){ | ||
removeClass: function (name) { | ||
return this.each(function (idx) { | ||
if (name === undefined) return className(this, '') | ||
classList = className(this) | ||
funcArg(this, name, idx, classList).split(/\s+/g).forEach(function(klass){ | ||
funcArg(this, name, idx, classList).split(/\s+/g).forEach(function (klass) { | ||
classList = classList.replace(classRE(klass), " ") | ||
@@ -735,7 +756,7 @@ }) | ||
}, | ||
toggleClass: function(name, when){ | ||
toggleClass: function (name, when) { | ||
if (!name) return this | ||
return this.each(function(idx){ | ||
return this.each(function (idx) { | ||
var $this = $(this), names = funcArg(this, name, idx, className(this)) | ||
names.split(/\s+/g).forEach(function(klass){ | ||
names.split(/\s+/g).forEach(function (klass) { | ||
(when === undefined ? !$this.hasClass(klass) : when) ? | ||
@@ -746,3 +767,3 @@ $this.addClass(klass) : $this.removeClass(klass) | ||
}, | ||
scrollTop: function(value){ | ||
scrollTop: function (value) { | ||
if (!this.length) return | ||
@@ -752,47 +773,12 @@ var hasScrollTop = 'scrollTop' in this[0] | ||
return this.each(hasScrollTop ? | ||
function(){ this.scrollTop = value } : | ||
function(){ this.scrollTo(this.scrollX, value) }) | ||
}, | ||
scrollLeft: function(value){ | ||
if (!this.length) return | ||
var hasScrollLeft = 'scrollLeft' in this[0] | ||
if (value === undefined) return hasScrollLeft ? this[0].scrollLeft : this[0].pageXOffset | ||
return this.each(hasScrollLeft ? | ||
function(){ this.scrollLeft = value } : | ||
function(){ this.scrollTo(value, this.scrollY) }) | ||
}, | ||
position: function() { | ||
if (!this.length) return | ||
var elem = this[0], | ||
// Get *real* offsetParent | ||
offsetParent = this.offsetParent(), | ||
// Get correct offsets | ||
offset = this.offset(), | ||
parentOffset = rootNodeRE.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset() | ||
// Subtract element margins | ||
// note: when an element has margin: auto the offsetLeft and marginLeft | ||
// are the same in Safari causing offset.left to incorrectly be 0 | ||
offset.top -= parseFloat( $(elem).css('margin-top') ) || 0 | ||
offset.left -= parseFloat( $(elem).css('margin-left') ) || 0 | ||
// Add offsetParent borders | ||
parentOffset.top += parseFloat( $(offsetParent[0]).css('border-top-width') ) || 0 | ||
parentOffset.left += parseFloat( $(offsetParent[0]).css('border-left-width') ) || 0 | ||
// Subtract the two offsets | ||
return { | ||
top: offset.top - parentOffset.top, | ||
left: offset.left - parentOffset.left | ||
} | ||
}, | ||
offsetParent: function() { | ||
return this.map(function(){ | ||
var parent = this.offsetParent || document.body | ||
while (parent && !rootNodeRE.test(parent.nodeName) && $(parent).css("position") == "static") | ||
parent = parent.offsetParent | ||
return parent | ||
}) | ||
function () { | ||
this.scrollTop = value | ||
} : | ||
function () { | ||
this.scrollTo(this.scrollX, value) | ||
}) | ||
} | ||
//[Opt:A] : scrollLeft已经转移至plugin | ||
//[Opt:A] : position已经转移至plugin | ||
//[Opt:A] : offsetParent已经转移至plugin | ||
} | ||
@@ -804,12 +790,16 @@ | ||
// Generate the `width` and `height` functions | ||
;['width', 'height'].forEach(function(dimension){ | ||
// todo:高度值bug待修复 | ||
; | ||
['width', 'height'].forEach(function (dimension) { | ||
var dimensionProperty = | ||
dimension.replace(/./, function(m){ return m[0].toUpperCase() }) | ||
dimension.replace(/./, function (m) { | ||
return m[0].toUpperCase() | ||
}) | ||
$.fn[dimension] = function(value){ | ||
$.fn[dimension] = function (value) { | ||
var offset, el = this[0] | ||
if (value === undefined) return isWindow(el) ? el['inner' + dimensionProperty] : | ||
isDocument(el) ? el.documentElement['scroll' + dimensionProperty] : | ||
(offset = this.offset()) && offset[dimension] | ||
else return this.each(function(idx){ | ||
(offset = this.offset()) && offset[dimension] | ||
else return this.each(function (idx) { | ||
el = $(this) | ||
@@ -821,42 +811,20 @@ el.css(dimension, funcArg(this, value, idx, el[dimension]())) | ||
// add `outerWidth`、`outerHeight` apis | ||
// 增加 outerWidth / outerHeight API,含义:元素的实际宽高+外边距。如一个元素的宽度是100,margin-left是50,则outerWidth为150。 | ||
;['width', 'height'].forEach(function(dimension) { | ||
var offset, Dimension = dimension.replace(/./, function(m) { return m[0].toUpperCase() }); | ||
$.fn['outer' + Dimension] = function(margin) { | ||
var elem = this; | ||
if (elem) { | ||
var size = elem[dimension](); | ||
var sides = {'width': ['left', 'right'], 'height': ['top', 'bottom']}; | ||
sides[dimension].forEach(function(side) { | ||
if (margin) size += parseInt(elem.css('margin-' + side), 10); | ||
}); | ||
return size; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
}) | ||
function traverseNode(node, fun) { | ||
fun(node) | ||
for (var key in node.childNodes) traverseNode(node.childNodes[key], fun) | ||
} | ||
// Generate the `after`, `prepend`, `before`, `append`, | ||
// `insertAfter`, `insertBefore`, `appendTo`, and `prependTo` methods. | ||
adjacencyOperators.forEach(function(operator, operatorIndex) { | ||
// [Opt:C] 将全部变量,改为局部变量 | ||
; | ||
[ 'after', 'prepend', 'before', 'append' ].forEach(function (operator, operatorIndex) { | ||
var inside = operatorIndex % 2 //=> prepend, append | ||
$.fn[operator] = function(){ | ||
$.fn[operator] = function () { | ||
// arguments can be nodes, arrays of nodes, Zepto objects and HTML strings | ||
var argType, nodes = $.map(arguments, function(arg) { | ||
argType = type(arg) | ||
return argType == "object" || argType == "array" || arg == null ? | ||
arg : zepto.fragment(arg) | ||
}), | ||
parent, copyByClone = this.length > 1 | ||
var argType, nodes = $.map(arguments, function (arg) { | ||
argType = type(arg) | ||
return argType == "object" || argType == "array" || arg == null ? | ||
arg : zepto.fragment(arg) | ||
}), | ||
parent, copyByClone = this.length > 1 | ||
if (nodes.length < 1) return this | ||
return this.each(function(_, target){ | ||
return this.each(function (_, target) { | ||
parent = inside ? target : target.parentNode | ||
@@ -866,14 +834,21 @@ | ||
target = operatorIndex == 0 ? target.nextSibling : | ||
operatorIndex == 1 ? target.firstChild : | ||
operatorIndex == 2 ? target : | ||
null | ||
operatorIndex == 1 ? target.firstChild : | ||
operatorIndex == 2 ? target : | ||
null | ||
nodes.forEach(function(node){ | ||
// [Opt:C] 全部变量局部化 | ||
var traverseNode = function (node, fun) { | ||
fun(node) | ||
for (var key in node.childNodes) traverseNode(node.childNodes[key], fun) | ||
} | ||
nodes.forEach(function (node) { | ||
if (copyByClone) node = node.cloneNode(true) | ||
else if (!parent) return $(node).remove() | ||
traverseNode(parent.insertBefore(node, target), function(el){ | ||
traverseNode(parent.insertBefore(node, target), function (el) { | ||
if (el.nodeName != null && el.nodeName.toUpperCase() === 'SCRIPT' && | ||
(!el.type || el.type === 'text/javascript') && !el.src) | ||
window['eval'].call(window, el.innerHTML) | ||
(!el.type || el.type === 'text/javascript') && !el.src) | ||
win['eval'].call(win, el.innerHTML) | ||
}) | ||
@@ -888,3 +863,3 @@ }) | ||
// append => appendTo | ||
$.fn[inside ? operator+'To' : 'insert'+(operatorIndex ? 'Before' : 'After')] = function(html){ | ||
$.fn[inside ? operator + 'To' : 'insert' + (operatorIndex ? 'Before' : 'After')] = function (html) { | ||
$(html)[operator](this) | ||
@@ -891,0 +866,0 @@ return this |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33937
882
6