Skip to content

Commit

Permalink
Switched args for YUI methods
Browse files Browse the repository at this point in the history
  • Loading branch information
soljin committed May 23, 2011
1 parent 0ab7d73 commit a8c8a36
Showing 1 changed file with 70 additions and 46 deletions.
116 changes: 70 additions & 46 deletions libs/pure.js
Expand Up @@ -11,7 +11,7 @@
*/

var $p, pure = $p = function(){
var sel = arguments[0],
var sel = arguments[0],
ctxt = false;

if(typeof sel === 'string'){
Expand Down Expand Up @@ -41,7 +41,7 @@ $p.core = function(sel, ctxt, plugins){
default:
templates = sel;
}

for(var i = 0, ii = templates.length; i < ii; i++){
plugins[i] = templates[i];
}
Expand All @@ -66,7 +66,7 @@ $p.core = function(sel, ctxt, plugins){
function(o) {
return Object.prototype.toString.call(o) === "[object Array]";
};

/* * * * * * * * * * * * * * * * * * * * * * * * * *
core functions
* * * * * * * * * * * * * * * * * * * * * * * * * */
Expand All @@ -80,7 +80,7 @@ $p.core = function(sel, ctxt, plugins){
}
throw('pure error: ' + e);
}

//return a new instance of plugins
function getPlugins(){
var plugins = $p.plugins,
Expand All @@ -92,27 +92,27 @@ $p.core = function(sel, ctxt, plugins){
f.prototype.render = plugins.render || render;
f.prototype.autoRender = plugins.autoRender || autoRender;
f.prototype.find = plugins.find || find;

// give the compiler and the error handling to the plugin context
f.prototype._compiler = compiler;
f.prototype._error = error;

return new f();
}

// returns the outer HTML of a node
function outerHTML(node){
// if IE, Chrome take the internal method otherwise build one
return node.outerHTML || (
function(n){
var div = document.createElement('div'), h;
div.appendChild( n.cloneNode(true) );
var div = document.createElement('div'), h;
div.appendChild( n.cloneNode(true) );
h = div.innerHTML;
div = null;
return h;
})(node);
}

// returns the string generator function
function wrapquote(qfn, f){
return function(ctxt){
Expand All @@ -132,7 +132,7 @@ $p.core = function(sel, ctxt, plugins){
return error('You can test PURE standalone with: iPhone, FF3.5+, Safari4+ and IE8+\n\nTo run PURE on your browser, you need a JS library/framework with a CSS selector engine');
}
}

// create a function that concatenates constant string
// sections (given in parts) and the results of called
// functions to fill in the gaps between parts (fns).
Expand All @@ -148,7 +148,7 @@ $p.core = function(sel, ctxt, plugins){
for(var i = 1; i < n; i++){
fnVal = fns[i].call( this, ctxt );
pVal = parts[i];

// if the value is empty and attribute, remove it
if(fnVal === ''){
attLine = strs[ strs.length - 1 ];
Expand All @@ -157,7 +157,7 @@ $p.core = function(sel, ctxt, plugins){
pVal = pVal.substr( 1 );
}
}

strs[ strs.length ] = fnVal;
strs[ strs.length ] = pVal;
}
Expand All @@ -174,7 +174,7 @@ $p.core = function(sel, ctxt, plugins){
if(m[1] === 'item'){
error('"item<-..." is a reserved word for the current running iteration.\n\nPlease choose another name for your loop.');
}
if( !m[2] || (m[2] && (/context/i).test(m[2]))){ //undefined or space(IE)
if( !m[2] || (m[2] && (/context/i).test(m[2]))){ //undefined or space(IE)
m[2] = function(ctxt){return ctxt.context;};
}
return {name: m[1], sel: m[2]};
Expand Down Expand Up @@ -243,12 +243,12 @@ $p.core = function(sel, ctxt, plugins){
if( !m ){
error( 'bad selector syntax: ' + sel );
}

prepend = m[1];
selector = m[2];
attr = m[3];
append = m[4];

if(selector === '.' || ( !selector && attr ) ){
target[0] = dom;
}else{
Expand All @@ -264,7 +264,7 @@ $p.core = function(sel, ctxt, plugins){
append = sel.append;
target = [dom];
}

if( prepend || append ){
if( prepend && append ){
error('append/prepend cannot take place at the same time');
Expand All @@ -283,7 +283,7 @@ $p.core = function(sel, ctxt, plugins){
node.setAttribute(attPfx + attr, s);
if (attName in node && !isStyle) {
try{node[attName] = '';}catch(e){} //FF4 gives an error sometimes
}
}
if (node.nodeType === 1) {
node.removeAttribute(attr);
isClass && node.removeAttribute(attName);
Expand Down Expand Up @@ -429,7 +429,7 @@ $p.core = function(sel, ctxt, plugins){
itersel = dataselectfn(spec.sel),
target = gettarget(dom, sel, true),
nodes = target.nodes;

for(i = 0; i < nodes.length; i++){
var node = nodes[i],
inner = compiler(node, dsel);
Expand All @@ -439,7 +439,7 @@ $p.core = function(sel, ctxt, plugins){
}
return target;
}

function getAutoNodes(n, data){
var ns = n.getElementsByTagName('*'),
an = [],
Expand All @@ -453,7 +453,7 @@ $p.core = function(sel, ctxt, plugins){
if(ni.nodeType === 1 && ni.className !== ''){
//when a className is found
cs = ni.className.split(' ');
// for each className
// for each className
for(j = 0, jj=cs.length;j<jj;j++){
cj = cs[j];
// check if it is related to a context property
Expand All @@ -465,14 +465,14 @@ $p.core = function(sel, ctxt, plugins){
ni.className = ni.className.replace('@'+cspec.attr, '');
if(isNodeValue){
cspec.attr = false;
}
}
}
an.push({n:ni, cspec:cspec});
}
}
}
}

function checkClass(c, tagName){
// read the class
var ca = c.match(selRx),
Expand Down Expand Up @@ -568,18 +568,18 @@ $p.core = function(sel, ctxt, plugins){
}while(++i < sl);
}
}
// convert node to a string
h = outerHTML(dom);
// convert node to a string
h = outerHTML(dom);
// IE adds an unremovable "selected, value" attribute
// hard replace while waiting for a better solution
h = h.replace(/<([^>]+)\s(value\=""|selected)\s?([^>]*)>/ig, "<$1 $3>");
// remove attribute prefix
h = h.split(attPfx).join('');
h = h.replace(/<([^>]+)\s(value\=""|selected)\s?([^>]*)>/ig, "<$1 $3>");

// remove attribute prefix
h = h.split(attPfx).join('');

// slice the html string at "Sig"
parts = h.split( Sig );
// for each slice add the return string of
// for each slice add the return string of
for(i = 1; i < parts.length; i++){
p = parts[i];
// part is of the form "fn-number:..." as placed there by setsig.
Expand All @@ -599,7 +599,7 @@ $p.core = function(sel, ctxt, plugins){
}
//compile with the directive as argument
// run the template function on the context argument
// return an HTML string
// return an HTML string
// should replace the template and return this
function render(ctxt, directive){
var fn = typeof directive === 'function' && directive, i = 0, ii = this.length;
Expand All @@ -612,7 +612,7 @@ $p.core = function(sel, ctxt, plugins){

// compile the template with autoRender
// run the template function on the context argument
// return an HTML string
// return an HTML string
function autoRender(ctxt, directive){
var fn = plugins.compile( directive, ctxt, this[0] );
for(var i = 0, ii = this.length; i < ii; i++){
Expand All @@ -621,7 +621,7 @@ $p.core = function(sel, ctxt, plugins){
context = null;
return this;
}

function replaceWith(elm, html) {
var ne,
ep = elm.parentNode,
Expand Down Expand Up @@ -680,7 +680,7 @@ $p.libs = {
return $(n).cssSelect(sel);
};
}
DOMAssistant.attach({
DOMAssistant.attach({
publicMethods : [ 'compile', 'render', 'autoRender'],
compile:function(directive, ctxt){
return $p([this]).compile(directive, ctxt);
Expand Down Expand Up @@ -721,7 +721,7 @@ $p.libs = {
};
}
Element.implement({
compile:function(directive, ctxt){
compile:function(directive, ctxt){
return $p(this).compile(directive, ctxt);
},
render:function(ctxt, directive){
Expand All @@ -740,12 +740,12 @@ $p.libs = {
};
}
Element.addMethods({
compile:function(element, directive, ctxt){
compile:function(element, directive, ctxt){
return $p([element]).compile(directive, ctxt);
},
},
render:function(element, ctxt, directive){
return $p([element]).render(ctxt, directive);
},
},
autoRender:function(element, ctxt, directive){
return $p([element]).autoRender(ctxt, directive);
}
Expand All @@ -759,27 +759,51 @@ $p.libs = {
}
},
sly:function(){
if(typeof document.querySelector === 'undefined'){
if(typeof document.querySelector === 'undefined'){
$p.plugins.find = function(n, sel){
return Sly(sel, n);
};
}
},
yui:function(){
if(typeof document.querySelector === 'undefined'){
YUI().use("node",function(Y){
$p.plugins.find = function(n, sel){
return Y.NodeList.getDOMNodes(Y.one(n).all(sel));
};
});
}
YUI.add("pure-yui",function(Y){
Y.Node.prototype.directives = function(directive){
this._pure_d = directive; return this;
}
Y.Node.prototype.compile = function(directive, ctxt){
return $p([this._node]).compile(this._pure_d || directive, ctxt);
}
Y.Node.prototype.render = function(ctxt, directive){
return Y.one($p([this._node]).render(ctxt, this._pure_d || directive));
}
Y.Node.prototype.autoRender = function(ctxt, directive){
return Y.one($p([this._node]).autoRender(ctxt, this._pure_d || directive));
}
},"0.1",{requires:["node"]});
}
};

// get lib specifics if available
(function(){
var libkey =
typeof dojo !== 'undefined' && 'dojo' ||
var libkey =
typeof dojo !== 'undefined' && 'dojo' ||
typeof DOMAssistant !== 'undefined' && 'domassistant' ||
typeof jQuery !== 'undefined' && 'jquery' ||
typeof jQuery !== 'undefined' && 'jquery' ||
typeof MooTools !== 'undefined' && 'mootools' ||
typeof Prototype !== 'undefined' && 'prototype' ||
typeof Prototype !== 'undefined' && 'prototype' ||
typeof Sizzle !== 'undefined' && 'sizzle' ||
typeof Sly !== 'undefined' && 'sly';

typeof Sly !== 'undefined' && 'sly' ||
typeof YUI !== 'undefined' && 'yui';

libkey && $p.libs[libkey]();

//for node.js
if(typeof exports !== 'undefined'){
exports.$p = $p;
Expand Down

0 comments on commit a8c8a36

Please sign in to comment.