| version 1.2, 2002/10/08 17:58:06 |
version 1.3, 2002/10/08 22:37:41 |
| Line 89 class HTML_Content |
|
| Line 89 class HTML_Content |
|
| * @see parseString() |
* @see parseString() |
| * @access public |
* @access public |
| */ |
*/ |
| function parse() |
function parse($all_data = '') |
| { |
{ |
| if (!is_resource($this->_fp)) { |
if ($all_data == '') { |
| //return $this->raiseError('no input'); |
if (!is_resource($this->_fp)) { |
| return false; |
//return $this->raiseError('no input'); |
| } |
return false; |
| |
} |
| |
|
| |
$all_data = ''; |
| |
while ($data = fread($this->_fp, 2048)) { |
| |
$all_data .= $data; |
| |
} |
| |
fclose($this->_fp); |
| |
} |
| |
|
| $this->_data['doctype'] = ''; |
$this->_data['doctype'] = ''; |
| $this->_data['head'] = ''; |
$this->_data['head'] = ''; |
| $this->_data['body'] = ''; |
$this->_data['body'] = ''; |
| $this->_data['head_attr'] = ''; |
$this->_data['head_attr'] = ''; |
| $this->_data['body_attr'] = ''; |
$this->_data['body_attr'] = ''; |
| |
|
| $all_data = ''; |
|
| while ($data = fread($this->_fp, 2048)) { |
|
| $all_data .= $data; |
|
| } |
|
| fclose($this->_fp); |
|
| |
|
| if (preg_match('|^\s*<!DOCTYPE ([^>]*)>|', $all_data, $matches)) { |
if (preg_match('|^\s*<!DOCTYPE ([^>]*)>|', $all_data, $matches)) { |
| $this->_data['doctype'] = trim($matches[1]); |
$this->_data['doctype'] = trim($matches[1]); |
| } |
} |
| Line 115 class HTML_Content |
|
| Line 117 class HTML_Content |
|
| $this->_data['head_attr'] = trim($matches[1]); |
$this->_data['head_attr'] = trim($matches[1]); |
| $this->_data['head'] = trim($matches[2]); |
$this->_data['head'] = trim($matches[2]); |
| } |
} |
| if (preg_match('|<body([^>]*)>(.*)</body>|si', $all_data, $matches)) { |
if (preg_match('{<body([^>]*)>(.*)(</body>|</html>|$)}si', $all_data, $matches)) { |
| $this->_data['body_attr'] = trim($matches[1]); |
$this->_data['body_attr'] = trim($matches[1]); |
| $this->_data['body'] = trim($matches[2]); |
$this->_data['body'] = trim($matches[2]); |
| } |
} |
| |
if ($this->_data['head'] == '' && $this->_data['body'] == '') { |
| |
$this->_data['body'] = $all_data; |
| |
} |
| return true; |
return true; |
| } |
} |
| |
|
| function _getTag($where, $name, $idx = 0) |
function _getTag($where, $name, $idx = -1) |
| { |
{ |
| $ar =& $this->_data[$where.'_tags'][$name]; |
$ar =& $this->_data[$where.'_tags'][$name]; |
| if (! isset($ar)) { |
if (! isset($ar)) { |
| Line 196 class HTML_Content |
|
| Line 201 class HTML_Content |
|
| |
|
| function getTitle() |
function getTitle() |
| { |
{ |
| //$this->_data['title'] == null && $this->_parseTitle(); |
|
| //return $this->_data['title']; |
|
| |
|
| $title_tag = $this->_getTag('head', 'title', 0); |
$title_tag = $this->_getTag('head', 'title', 0); |
| return $title_tag['__content']; |
return $title_tag['__content']; |
| } |
} |
| |
|
| function getTag($name, $attributes) |
function getTag($where, $name, $attr = '', $value = '') |
| |
{ |
| |
$attr = strtolower($attr); |
| |
$where = strtolower($where); |
| |
if ($where != 'body') { |
| |
$where = 'head'; |
| |
} |
| |
$tag = $this->_getTag($where, $name); |
| |
if ($attr == '') { |
| |
return $tag; |
| |
} |
| |
|
| |
if ($value == '') { |
| |
for ($i = 0; $i < count($tag); $i++) { |
| |
if (isset($tag[$i][$attr])) { |
| |
return $tag[$i]; |
| |
} |
| |
} |
| |
} else { |
| |
for ($i = 0; $i < count($tag); $i++) { |
| |
if (isset($tag[$i][$attr]) |
| |
&& stristr($tag[$i][$attr], $value)) { |
| |
return $tag[$i]; |
| |
} |
| |
} |
| |
} |
| |
return false; |
| |
} |
| |
|
| |
function getComposedTag($where, $name, $attr = '', $value = '') |
| |
{ |
| |
$single = $attr == '' ? false : true; |
| |
$ar = $this->getTag($where, $name, $attr, $value); |
| |
if ($ar === false) { |
| |
return false; |
| |
} |
| |
if ($single) { |
| |
return HTML_Content::composeTag($name, $ar); |
| |
} |
| |
foreach ($ar as $tag) { |
| |
$out_ar[] = HTML_Content::composeTag($name, $tag); |
| |
} |
| |
return $out_ar; |
| |
} |
| |
|
| |
function composeTag($name, $attributes) |
| { |
{ |
| $ret = "$name"; |
$ret = htmlspecialchars($name); |
| foreach ($attributes as $attr => $value) { |
foreach ($attributes as $attr => $value) { |
| if (! strncmp('__', $value, 2)) { |
if (! strncmp('__', $attr, 2)) { |
| continue; |
continue; |
| } |
} |
| |
|
| $ret .= " $attr="; |
$ret .= ' '.htmlspecialchars($attr).'='; |
| if (strchr($value, '"') && strchr($value, "'")) { |
if (strchr($value, '"') && strchr($value, "'")) { |
| $value = str_replace('"', '', $value); |
$value = str_replace('"', '', $value); |
| } |
} |
| if (strchr($value, '"')) { |
if (strchr($value, '"')) { |
| $ret .= "'$value'"; |
$ret .= "'".htmlspecialchars($value)."'"; |
| } else { |
} else { |
| $ret .= '"'.$value.'"'; |
$ret .= '"'.htmlspecialchars($value).'"'; |
| } |
} |
| } |
} |
| $ret = '<'.htmlspecialchars($ret).'>'; |
$ret = '<'.$ret.'>'; |
| if (isset($attributes['__content'])) { |
if (isset($attributes['__content'])) { |
| $ret .= $attributes['__content']; |
$ret .= $attributes['__content']; |
| $ret .= '</'.htmlspecialchars($name).'>'; |
$ret .= '</'.htmlspecialchars($name).'>'; |