Skip to content

Commit

Permalink
making stripTags a little more configurable; adding String.getTags; n…
Browse files Browse the repository at this point in the history
…ote that this could (and perhaps should?) replace String.stripScripts in -core.
  • Loading branch information
anutron committed Oct 15, 2009
1 parent c15a284 commit 56e8cf8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
43 changes: 41 additions & 2 deletions Docs/Native/String.Extras.md
Expand Up @@ -81,7 +81,37 @@ Removes non-ascii characters and converts them to their most appropriate ascii c

* (*string*) a string without any non-ascii characters.

String Method: getTags {#String:getTags}
-------------------------------------

Get all the HTML tags from a given string.

### Syntax

myString.getTags([tag, contents]);

### Arguments

1. tag - (*string*; optional) if defined, returns the tags of the specified type in an array. If not defined, returns all tags in an array.
2. contents - (*boolean*; optional) if *true*, you are returned an array of tag matches and their contents.

### Example

var html = "<b>This is a string with <i>html</i> in it.</b>"
var tags = html.getTags();
//returns ["<b>", "<i>", "</i>", "</b>"]
var italics = html.getTags('i');
//returns ["<i>", "</i>"]
var italicsWithContent = html.getTags('i', true);
//returns ["<i>html</i>"]

### Returns

* (*array*) An array of strings for each matched tag (important note: NOT *elements*, *strings*)

### Note

Currently, you cannot ask for all tags with their content. If you want the inner content of tags, you must specify the tag type.


String Method: stripTags {#String:stripTags}
Expand All @@ -91,17 +121,26 @@ Remove all html tags from a string.

### Syntax

myString.stripTags();
myString.stripTags([tag, contents]);

### Arguments

1. tag - (*string*; optional) if defined, returns the tags of the specified type in an array. If not defined, returns all tags in an array.
2. contents - (*boolean*; optional) if *true*, you are returned an array of tag matches and their contents.

### Example

var html = "<b>This is a string with <i>html</i> in it.</b>"
var noHtml = html.stripTags();
//returns "This is a string with html in it."
var noItalics = html.stripTags('i');
//returns "<b>This is a string with html in it.</b>"
var noItalicsContent = html.stripTags('i', true);
returns "<b>This is a string with in it.</b>"

### Returns

* (*string*) a string without any HTML tags
* (*string*) a string with the appropriate HTML stripped

String Method: tidy {#String:tidy}
----------------------------------
Expand Down
20 changes: 13 additions & 7 deletions Source/Native/String.Extras.js
Expand Up @@ -9,11 +9,6 @@ Script: String.Extras.js
Aaron Newton
Guillermo Rauch
@requires core/1.2.4: String Array
@requires Hash.Extras
@provides Date
*/

(function(){
Expand All @@ -33,6 +28,13 @@ var tidymap = {
"\uFFFD": "&raquo;"
};

var getRegForTag = function(tag, contents) {
tag = tag || '';
var regstr = contents ? "<" + tag + "[^>]*>([\\s\\S]*?)<\/" + tag + ">" : "<\/?" + tag + "([^>]+)?>";
reg = new RegExp(regstr, "gi");
return reg;
};

String.implement({

standardize: function(){
Expand All @@ -55,8 +57,12 @@ String.implement({
return pad.substr(0, (pad.length / 2).floor()) + this + pad.substr(0, (pad.length / 2).ceil());
},

stripTags: function(){
return this.replace(/<\/?[^>]+>/gi, '');
getTags: function(tag, contents){
return this.match(getRegForTag(tag, contents)) || [];
},

stripTags: function(tag, contents){
return this.replace(getRegForTag(tag, contents), '');
},

tidy: function(){
Expand Down

0 comments on commit 56e8cf8

Please sign in to comment.