File indexing completed on 2024-12-22 05:36:20

0001 <?php
0002 
0003 /**
0004  * Abstract class of a tag token (start, end or empty), and its behavior.
0005  */
0006 abstract class HTMLPurifier_Token_Tag extends HTMLPurifier_Token
0007 {
0008     /**
0009      * Static bool marker that indicates the class is a tag.
0010      *
0011      * This allows us to check objects with <tt>!empty($obj->is_tag)</tt>
0012      * without having to use a function call <tt>is_a()</tt>.
0013      * @type bool
0014      */
0015     public $is_tag = true;
0016 
0017     /**
0018      * The lower-case name of the tag, like 'a', 'b' or 'blockquote'.
0019      *
0020      * @note Strictly speaking, XML tags are case sensitive, so we shouldn't
0021      * be lower-casing them, but these tokens cater to HTML tags, which are
0022      * insensitive.
0023      * @type string
0024      */
0025     public $name;
0026 
0027     /**
0028      * Associative array of the tag's attributes.
0029      * @type array
0030      */
0031     public $attr = array();
0032 
0033     /**
0034      * Non-overloaded constructor, which lower-cases passed tag name.
0035      *
0036      * @param string $name String name.
0037      * @param array $attr Associative array of attributes.
0038      * @param int $line
0039      * @param int $col
0040      * @param array $armor
0041      */
0042     public function __construct($name, $attr = array(), $line = null, $col = null, $armor = array())
0043     {
0044         $this->name = ctype_lower($name) ? $name : strtolower($name);
0045         foreach ($attr as $key => $value) {
0046             // normalization only necessary when key is not lowercase
0047             if (!ctype_lower($key)) {
0048                 $new_key = strtolower($key);
0049                 if (!isset($attr[$new_key])) {
0050                     $attr[$new_key] = $attr[$key];
0051                 }
0052                 if ($new_key !== $key) {
0053                     unset($attr[$key]);
0054                 }
0055             }
0056         }
0057         $this->attr = $attr;
0058         $this->line = $line;
0059         $this->col = $col;
0060         $this->armor = $armor;
0061     }
0062 
0063     public function toNode() {
0064         return new HTMLPurifier_Node_Element($this->name, $this->attr, $this->line, $this->col, $this->armor);
0065     }
0066 }
0067 
0068 // vim: et sw=4 sts=4