Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced named entity with character number for XHTML comatibility. #18

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/prototype/dom/dom.js
Expand Up @@ -3006,7 +3006,7 @@ Element._getContentFromAnonymousElement = function(tagName, html, force) {
if (workaround) {
// Adding a text node to the beginning of the string (then removing it)
// fixes an issue in Internet Explorer. See Element#update above.
div.innerHTML = ' ' + t[0] + html + t[1];
div.innerHTML = ' ' + t[0] + html + t[1];
div.removeChild(div.firstChild);
for (var i = t[2]; i--; ) {
div = div.firstChild;
Expand Down
90 changes: 89 additions & 1 deletion src/prototype/lang/function.js
Expand Up @@ -377,6 +377,92 @@ Object.extend(Function.prototype, (function() {
};
}

/**
* Function#fluent() -> Function
*
* Makes function acting fluent-way (allows chaining). It makes method of an
* object always returning that object (no matter what oriignal method returns).
*
* ##### Example
*
* // Original Hash.set returns value which was passed to it
* var hash = new Hash();
* // Now we make it chainable
* hash.set = hash.set.fluent();
*
* // Use it
* hash.set("a", "foo")
* .set("b", "bar");
* hash.get("b");
* // -> "bar"
**/
function fluent()
{
var method = this;

return function() {
// note that "this" here is relative context bound at call-time
method.apply(this, arguments);
return this;
};
}

/**
* Function#fluent() -> Function
*
* Allows only single execution of subject function. It can be usefull when
* attaching functions as callbacks for multiple events when only first execution
* should be cought.
*
* ##### Example
*
* var i = 0;
* var f = function() {
* ++i;
* }.once();
*
* // Now call it as many times as you want
* f();
* f();
* f();
* f();
*
* i;
* // -> 1
*
* At first time, function will return value returned by original function. After
* that result of function will be undefined.
*
* ##### Example
*
* var i = 0;
* var f = function() {
* return ++i;
* }.once();
*
* f();
* // -> 0
* f();
* // -> undefined
**/
function once()
{
var method = this, names = this.argumentNames();

var wrapper = function() {
// remembers returned value
var result = method();
method = function() {};
return result;
};
// this will overwrite wrapper argument names to manifest oryginal arugment names
wrapper.argumentNames = function() {
return names;
};

return wrapper
}

return {
argumentNames: argumentNames,
bind: bind,
Expand All @@ -385,7 +471,9 @@ Object.extend(Function.prototype, (function() {
delay: delay,
defer: defer,
wrap: wrap,
methodize: methodize
methodize: methodize,
fluent: fluent,
once: once
}
})());

16 changes: 16 additions & 0 deletions test/unit/function_test.js
Expand Up @@ -129,5 +129,21 @@ new Test.Unit.Runner({
this.assertEqual.bind(this, arg3), arg1, arg2, arg3 );
call(eventTest);
}
},

testFluent: function() {
var hash = new Hash();
hash.set = hash.set.fluent();
this.assertIdentical(hash, hash.set('a', 'foo') );
},

testOnce: function() {
var i = 0;
var f = function() {
return ++i;
}.once();
this.assertIdentical(0, f() );
this.assertIdentical(undefined, f() );
this.assertEqual(i, 1);
}
});