Re: HTMLBBCodeParser patch
| From: | Stijn de Reede | Date: | Wed, 20 Aug 2003 07:12:28 +0000 |
| Subject: | Re: HTMLBBCodeParser patch | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-20119@lists.php.net to get a copy of this message | ||
Wohaa, dude, you're almost putting in more work into the class than I
ever did! :) Thanks alot!
And yes, it's true, I've been busy working and studying for exams
lately, and I don't read pear dev/general daily anymore (more or less
twice a week nowadays *shame*).
But, on the bright side, I'm still willing to put some work into the
class, since I notice people are really using it (who'd ever thought!).
So, Stan, we'll talk soon (aol: StijnDeReede, msn:
stijndereede@hotmail.com, icq: 3386484, email: sjr@gmx.co.uk) on
extending the class, making documentation and new features (strip_tags
and nl2br are great, why didn't I think of such simple things!).
Greets,
Stijn de Reede
Stan Lemon wrote:
Hey everyone, I've attached a patch for HTML_BBCodeParser. I've contacted Stij (the author) in regards to the patch, however, he seems to be rather busy so I figured I'd just send the code out. If the higher ups give a go I'd be more then happy to commit this code. None of the changes code in the patch is a major change, everything is very minor. Here's a brief summary of what I did: - In the current release you are not allowed to use numbers in tags, and this is both undocumented, and I believe unintended. Essentially if I created a tag called [h1] I would receive bad formatting problems because the current release does not parse the tag correctly at all. The output is rather ugly. I fixed the portion of the code which didn't account for numbers being included. Now it parses BBCode tags with numbers with no problems. - Currently the tag [b] and [B] are different, even though the average developer will want both to output <strong> Now I realized this may be by design so what I did was first created an option called "strict" and if toggled true it [b] and [B] are different, if toggled false [b] and [B} are the same. - I added two very small features, one called strip_tags and the other called nl2br (very original, I know) these features allow for the getText() method to return text which has had <br /> added, and/or remaining HTML tags stripped. This is beneficial for various security reasons and if you don't want output to run together the nl2br item is nice. (Note: I need to alter the methods in the list filter to clean up <br /> tags that have been added. This is not essential, but what it is, is a friendly "cleaner" utility. I had code started, just not finished.) They are not ridiculously major changes, but they are definitely beneficial. I will in the upcoming days also be releasing a patch to update and elaborate on the current phpdoc that exists in HTML_BBCodeParser. I would also like to receive some type of a go to develop a series of loadTag(), loadTags(), removeTag(), and removeTags() methods. Currently you have to develop a filter in order to add tags. This is nice for large sets of tags for a predefined filter, however, at the same time if I want to load a single tag I have to create a whole new filter in order to do so. There is a much easier way of handling this, and there is also an easy way to remove tags from the current set tags (e.g. Say I want to load the list filter but only allow for unordered lists instead of ordered list, I'd have to redo the entire class, rather then just running a removeTags("list"); type function. A patch for the above methods will be out shortly before my documentation patch. Anyhow, please take a look at my patch and try it out. Any input would be appreciated. Thanks. - Stan Lemon ------------------------------------------------------------------------ Index: HTML_BBCodeParser/BBCodeParser.php =================================================================== RCS file: /repository/pear/HTML_BBCodeParser/BBCodeParser.php,v retrieving revision 1.3 diff -u -r1.3 BBCodeParser.php --- HTML_BBCodeParser/BBCodeParser.php 24 Jul 2003 21:04:28 -0000 1.3 +++ HTML_BBCodeParser/BBCodeParser.php 6 Aug 2003 22:43:56 -0000 @@ -16,7 +16,7 @@// | Author: Stijn de Reede <sjr@gmx.co.uk> |// +----------------------------------------------------------------------+ // -// $Id: BBCodeParser.php,v 1.3 2003/07/24 21:04:28 sjr Exp $ +// $Id$ // /** @@ -141,7 +141,10 @@'open' => '[', 'close' => ']', 'xmlclose' => true, - 'filters' => 'Basic' + 'filters' => 'Basic', + 'nl2br' => true, + 'strip_tags' => false, + 'strict' => false, ); /**@@ -220,6 +223,16 @@$this->_filters[$filter] = new $class; $this->_definedTags = array_merge($this->_definedTags, $this->_filters[$filter]->_definedTags); }+ + /* make sure all of the keys in definedTags are lowered for comparing */ + $lowerTags = array(); ++ if ($this->_options['strict'] == false) { + foreach ($this->_definedTags as $key => $value) { + $lowerTags[strtolower($key)] = $value; + } + $this->_definedTags = $lowerTags;+ }} @@ -313,7 +326,9 @@ } else { /* possible valid tag */ $newPos = $closePos + 1;+$newTag = $this->_buildTag(substr($str, $strPos, $closePos - $strPos + 1));+if ( ($newTag !== false) ) { $tag = $newTag; } else {@@ -362,9 +377,14 @@{ $tag = array('text' => $str, 'attributes' => array()); - if (substr($str, 1, 1) == '/') { /* closing tag */ + /* Lower the current tag for comparison against the loaded tags */ + if ($this->_options['strict'] == false) { + $str = strtolower($str); + } + if (substr($str, 1, 1) == '/') { /* closing tag */ $tag['tag'] = substr($str, 2, strlen($str) - 3);+if ( (in_array($tag['tag'], array_keys($this->_definedTags)) == false) ) { return false; /* nope, it's not valid */ } else {@@ -372,7 +392,6 @@return $tag; } } else { /* opening tag */-$tag['type'] = 1; if ( (strpos($str, ' ') == true) && (strpos($str, '=') == false) ) { return false; /* nope, it's not valid */@@ -382,9 +401,12 @@split the tag with arguments and all */ $oe = $this->_options['open_esc']; $ce = $this->_options['close_esc']; - preg_match("!$oe([a-z]+)[^$ce]*$ce!i", $str, $tagArray);++ /* Retrieve the tag name */ + preg_match("!$oe([a-z0-9]+)[^$ce]*$ce!i", $str, $tagArray); $tag['tag'] = $tagArray[1]; - if ( (in_array($tag['tag'], array_keys($this->_definedTags)) == false) ) {++ if ((in_array($tag['tag'], array_keys($this->_definedTags)) == false) ) { return false; /* nope, it's not valid */ } @@ -583,6 +605,15 @@ */ function setText($str) { + /* If the user wants all html cleaned out, do so */ + if ($this->_options['strip_tags'] == true) { + $str = strip_tags($str); + } + /* If the user wants \n replaced with <br />, do so */ + if ($this->_options['nl2br'] != true) { + $str = nl2br($str); + }+$this->_text = $str; }