Skip to content

Commit

Permalink
Merge branch 'standard-contains' fixes #2402
Browse files Browse the repository at this point in the history
  • Loading branch information
arian committed Dec 1, 2012
2 parents d54312c + ce25f3c commit c596b60
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
23 changes: 18 additions & 5 deletions Docs/Types/String.md
Expand Up @@ -87,16 +87,16 @@ String method: contains {#String:contains}
-----------------------------------

Checks to see if the string passed in is contained in this string.
If the separator parameter is passed, will check to see if the string is contained in the list of values separated by that parameter.
If the position parameter is passed, it will only check for the string from that point.

### Syntax:

myString.contains(string[, separator]);
myString.contains(string[, position]);

### Arguments:

1. string - (*string*) The string to search for.
2. separator - (*string*, optional) The string that separates the values in this string (e.g. Element classNames are separated by a ' ').
2. position - (*number*, optional) Position in the string to begin searching for `string`, defaults to `0`.

### Returns:

Expand All @@ -106,10 +106,18 @@ If the separator parameter is passed, will check to see if the string is contain
### Examples:

'a bc'.contains('bc'); // returns true
'a b c'.contains('c', ' '); // returns true
'a bc'.contains('b', ' '); // returns false
'abc'.contains('b', 1); // returns true
'abc'.contains('b', 2); // returns false

### See Also:

- [MDC String:indexOf][]
- [MDC String:contains][]

### Note:

Since MooTools 1.5 the second parameter changed from `separator` to `position` so it conforms the ES6 specification.
If using the 1.4 compatibility layer, this method will be overwritten to have the old behavior.

String method: trim {#String:trim}
---------------------------
Expand All @@ -128,7 +136,9 @@ Trims the leading and trailing spaces off a string.

' i like cookies '.trim(); // returns 'i like cookies'

### See Also:

- [MDC String:trim][]

String method: clean {#String:clean}
-----------------------------
Expand Down Expand Up @@ -390,6 +400,9 @@ Strips the String of its *<script>* tags and anything in between them.


[MDC String]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String
[MDC String:contains]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/contains
[MDC String:indexOf]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/indexOf
[MDC String:trim]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/trim
[MDC Regexp:test]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/RegExp/test
[MDC Regular Expressions]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions
[MDC parseInt]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/parseInt
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core.js
Expand Up @@ -264,7 +264,7 @@ var force = function(name, object, methods){
};

force('String', String, [
'charAt', 'charCodeAt', 'concat', 'indexOf', 'lastIndexOf', 'match', 'quote', 'replace', 'search',
'charAt', 'charCodeAt', 'concat', 'contains', 'indexOf', 'lastIndexOf', 'match', 'quote', 'replace', 'search',
'slice', 'split', 'substr', 'substring', 'trim', 'toLowerCase', 'toUpperCase'
])('Array', Array, [
'pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift', 'concat', 'join', 'slice',
Expand Down
2 changes: 1 addition & 1 deletion Source/Element/Element.js
Expand Up @@ -699,7 +699,7 @@ Element.implement({
hasClass: hasClassList ? function(className) {
return this.classList.contains(className);
} : function(className){
return this.className.clean().contains(className, ' ');
return (' ' + this.className.clean() + ' ').indexOf(' ' + className + ' ') > -1;
},

addClass: hasClassList ? function(className) {
Expand Down
2 changes: 1 addition & 1 deletion Source/Fx/Fx.CSS.js
Expand Up @@ -122,7 +122,7 @@ Fx.CSS = new Class({

Array.each(document.styleSheets, function(sheet, j){
var href = sheet.href;
if (href && href.contains('://') && !href.contains(document.domain)) return;
if (href && href.indexOf('://') > -1 && href.indexOf(document.domain) == -1) return;
var rules = sheet.rules || sheet.cssRules;
searchStyles(rules);
});
Expand Down
4 changes: 2 additions & 2 deletions Source/Request/Request.js
Expand Up @@ -183,10 +183,10 @@ var Request = this.Request = new Class({
if (trimPosition > -1 && (trimPosition = url.indexOf('#')) > -1) url = url.substr(0, trimPosition);

if (this.options.noCache)
url += (url.contains('?') ? '&' : '?') + String.uniqueID();
url += (url.indexOf('?') > -1 ? '&' : '?') + String.uniqueID();

if (data && (method == 'get' || method == 'delete')){
url += (url.contains('?') ? '&' : '?') + data;
url += (url.indexOf('?') > -1 ? '&' : '?') + data;
data = null;
}

Expand Down
16 changes: 12 additions & 4 deletions Source/Types/String.js
Expand Up @@ -16,12 +16,14 @@ provides: String

String.implement({

test: function(regex, params){
return ((typeOf(regex) == 'regexp') ? regex : new RegExp('' + regex, params)).test(this);
//<!ES6>
contains: function(string, index){
return (index ? String(this).slice(index) : String(this)).indexOf(string) > -1;
},
//</!ES6>

contains: function(string, separator){
return (separator) ? (separator + this + separator).indexOf(separator + string + separator) > -1 : String(this).indexOf(string) > -1;
test: function(regex, params){
return ((typeOf(regex) == 'regexp') ? regex : new RegExp('' + regex, params)).test(this);
},

trim: function(){
Expand Down Expand Up @@ -80,3 +82,9 @@ String.implement({
}

});

//<1.4compat>
String.prototype.contains = function(string, separator){
return (separator) ? (separator + this + separator).indexOf(separator + string + separator) > -1 : String(this).indexOf(string) > -1;
};
//</1.4compat>
4 changes: 2 additions & 2 deletions Specs/1.2/Native/String.js
Expand Up @@ -44,7 +44,7 @@ describe("String Methods", {
},

// String.contains

//<1.4compat>
'should return true if the string contains a string otherwise false': function(){
expect('i like cookies'.contains('cookies')).toBeTruthy();
expect('i,like,cookies'.contains('cookies')).toBeTruthy();
Expand All @@ -58,7 +58,7 @@ describe("String Methods", {
expect('i,like,cookies'.contains('cookies', ' ')).toBeFalsy();
expect('i,like,cookies'.contains('cookies', ',')).toBeTruthy();
},

//</1.4compat>
// String.test

'should return true if the test matches the string otherwise false': function(){
Expand Down

0 comments on commit c596b60

Please sign in to comment.