/usr/share/php/texy/src/Texy/TexyHtml.php is in php-texy 2.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | <?php
/**
* This file is part of the Texy! (http://texy.info)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/
/**
* HTML helper.
*
* usage:
* $anchor = TexyHtml::el('a')->href($link)->setText('Texy');
* $el->class = 'myclass';
*
* echo $el->startTag(), $el->endTag();
*
* @property mixed element's attributes
* @author David Grudl
*/
class TexyHtml extends TexyObject implements ArrayAccess, /* Countable, */ IteratorAggregate
{
/** @var string element's name */
private $name;
/** @var bool is element empty? */
private $isEmpty;
/** @var array element's attributes */
public $attrs = array();
/** @var array of TexyHtml | string nodes */
protected $children = array();
/** @var bool use XHTML syntax? */
public static $xhtml = TRUE;
/** @var array empty elements */
public static $emptyElements = array('img'=>1,'hr'=>1,'br'=>1,'input'=>1,'meta'=>1,'area'=>1,
'base'=>1,'col'=>1,'link'=>1,'param'=>1,'basefont'=>1,'frame'=>1,'isindex'=>1,'wbr'=>1,'embed'=>1);
/** @var array %inline; elements; replaced elements + br have value '1' */
public static $inlineElements = array('ins'=>0,'del'=>0,'tt'=>0,'i'=>0,'b'=>0,'big'=>0,'small'=>0,'em'=>0,
'strong'=>0,'dfn'=>0,'code'=>0,'samp'=>0,'kbd'=>0,'var'=>0,'cite'=>0,'abbr'=>0,'acronym'=>0,
'sub'=>0,'sup'=>0,'q'=>0,'span'=>0,'bdo'=>0,'a'=>0,'object'=>1,'img'=>1,'br'=>1,'script'=>1,
'map'=>0,'input'=>1,'select'=>1,'textarea'=>1,'label'=>0,'button'=>1,
'u'=>0,'s'=>0,'strike'=>0,'font'=>0,'applet'=>1,'basefont'=>0, // transitional
'embed'=>1,'wbr'=>0,'nobr'=>0,'canvas'=>1, // proprietary
);
/** @var array elements with optional end tag in HTML */
public static $optionalEnds = array('body'=>1,'head'=>1,'html'=>1,'colgroup'=>1,'dd'=>1,
'dt'=>1,'li'=>1,'option'=>1,'p'=>1,'tbody'=>1,'td'=>1,'tfoot'=>1,'th'=>1,'thead'=>1,'tr'=>1);
/** @see http://www.w3.org/TR/xhtml1/prohibitions.html */
public static $prohibits = array(
'a' => array('a','button'),
'img' => array('pre'),
'object' => array('pre'),
'big' => array('pre'),
'small' => array('pre'),
'sub' => array('pre'),
'sup' => array('pre'),
'input' => array('button'),
'select' => array('button'),
'textarea' => array('button'),
'label' => array('button', 'label'),
'button' => array('button'),
'form' => array('button', 'form'),
'fieldset' => array('button'),
'iframe' => array('button'),
'isindex' => array('button'),
);
/**
* Static factory.
* @param string element name (or NULL)
* @param array|string element's attributes (or textual content)
* @return TexyHtml
*/
public static function el($name = NULL, $attrs = NULL)
{
$el = new self;
$el->setName($name);
if (is_array($attrs)) {
$el->attrs = $attrs;
} elseif ($attrs !== NULL) {
$el->setText($attrs);
}
return $el;
}
/**
* Changes element's name.
* @param string
* @param bool Is element empty?
* @return self
* @throws InvalidArgumentException
*/
final public function setName($name, $empty = NULL)
{
if ($name !== NULL && !is_string($name)) {
throw new InvalidArgumentException("Name must be string or NULL.");
}
$this->name = $name;
$this->isEmpty = $empty === NULL ? isset(self::$emptyElements[$name]) : (bool) $empty;
return $this;
}
/**
* Returns element's name.
* @return string
*/
final public function getName()
{
return $this->name;
}
/**
* Is element empty?
* @return bool
*/
final public function isEmpty()
{
return $this->isEmpty;
}
/**
* Overloaded setter for element's attribute.
* @param string property name
* @param mixed property value
* @return void
*/
final public function __set($name, $value)
{
$this->attrs[$name] = $value;
}
/**
* Overloaded getter for element's attribute.
* @param string property name
* @return mixed property value
*/
final public function & __get($name)
{
return $this->attrs[$name];
}
/**
* Overloaded setter for element's attribute.
* @param string attribute name
* @param array value
* @return self
*/
/*
final public function __call($m, $args)
{
if (count($args) !== 1) {
throw new InvalidArgumentException("Just one argument is required.");
}
$this->attrs[$m] = $args[0];
return $this;
}
*/
/**
* Special setter for element's attribute.
* @param string path
* @param array query
* @return self
*/
final public function href($path, $query = NULL)
{
if ($query) {
$query = http_build_query($query, NULL, '&');
if ($query !== '') {
$path .= '?' . $query;
}
}
$this->attrs['href'] = $path;
return $this;
}
/**
* Sets element's textual content.
* @param string
* @return self
*/
final public function setText($text)
{
if (is_scalar($text)) {
$this->removeChildren();
$this->children = array($text);
} elseif ($text !== NULL) {
throw new InvalidArgumentException('Content must be scalar.');
}
return $this;
}
/**
* Gets element's textual content.
* @return string
*/
final public function getText()
{
$s = '';
foreach ($this->children as $child) {
if (is_object($child)) {
return FALSE;
}
$s .= $child;
}
return $s;
}
/**
* Adds new element's child.
* @param TexyHtml|string child node
* @return self
*/
final public function add($child)
{
return $this->insert(NULL, $child);
}
/**
* Creates and adds a new TexyHtml child.
* @param string elements's name
* @param array|string element's attributes (or textual content)
* @return TexyHtml created element
*/
final public function create($name, $attrs = NULL)
{
$this->insert(NULL, $child = self::el($name, $attrs));
return $child;
}
/**
* Inserts child node.
* @param int
* @param TexyHtml node
* @param bool
* @return self
* @throws Exception
*/
public function insert($index, $child, $replace = FALSE)
{
if ($child instanceof TexyHtml || is_string($child)) {
if ($index === NULL) { // append
$this->children[] = $child;
} else { // insert or replace
array_splice($this->children, (int) $index, $replace ? 1 : 0, array($child));
}
} else {
throw new /*::*/InvalidArgumentException('Child node must be scalar or TexyHtml object.');
}
return $this;
}
/**
* Inserts (replaces) child node (ArrayAccess implementation).
* @param int
* @param TexyHtml node
* @return void
*/
final public function offsetSet($index, $child)
{
$this->insert($index, $child, TRUE);
}
/**
* Returns child node (ArrayAccess implementation).
* @param int index
* @return mixed
*/
final public function offsetGet($index)
{
return $this->children[$index];
}
/**
* Exists child node? (ArrayAccess implementation).
* @param int index
* @return bool
*/
final public function offsetExists($index)
{
return isset($this->children[$index]);
}
/**
* Removes child node (ArrayAccess implementation).
* @param int index
* @return void
*/
public function offsetUnset($index)
{
if (isset($this->children[$index])) {
array_splice($this->children, (int) $index, 1);
}
}
/**
* Required by the Countable interface.
* @return int
*/
final public function count()
{
return count($this->children);
}
/**
* Removed all children.
* @return void
*/
public function removeChildren()
{
$this->children = array();
}
/**
* Required by the IteratorAggregate interface.
* @return ArrayIterator
*/
final public function getIterator()
{
return new ArrayIterator($this->children);
}
/**
* Returns all of children.
* return array
*/
final public function getChildren()
{
return $this->children;
}
/**
* Renders element's start tag, content and end tag to internal string representation.
* @param Texy
* @return string
*/
final public function toString(Texy $texy)
{
$ct = $this->getContentType();
$s = $texy->protect($this->startTag(), $ct);
// empty elements are finished now
if ($this->isEmpty) {
return $s;
}
// add content
foreach ($this->children as $child) {
if (is_object($child)) {
$s .= $child->toString($texy);
} else {
$s .= $child;
}
}
// add end tag
return $s . $texy->protect($this->endTag(), $ct);
}
/**
* Renders to final HTML.
* @param Texy
* @return string
*/
final public function toHtml(Texy $texy)
{
return $texy->stringToHtml($this->toString($texy));
}
/**
* Renders to final text.
* @param Texy
* @return string
*/
final public function toText(Texy $texy)
{
return $texy->stringToText($this->toString($texy));
}
/**
* Returns element's start tag.
* @return string
*/
public function startTag()
{
if (!$this->name) {
return '';
}
$s = '<' . $this->name;
if (is_array($this->attrs)) {
foreach ($this->attrs as $key => $value) {
// skip NULLs and false boolean attributes
if ($value === NULL || $value === FALSE) {
continue;
}
// true boolean attribute
if ($value === TRUE) {
// in XHTML must use unminimized form
if (self::$xhtml) {
$s .= ' ' . $key . '="' . $key . '"';
} else {
$s .= ' ' . $key;
}
continue;
} elseif (is_array($value)) {
// prepare into temporary array
$tmp = NULL;
foreach ($value as $k => $v) {
// skip NULLs & empty string; composite 'style' vs. 'others'
if ($v == NULL) {
continue;
} elseif (is_string($k)) {
$tmp[] = $k . ':' . $v;
} else {
$tmp[] = $v;
}
}
if (!$tmp) {
continue;
}
$value = implode($key === 'style' ? ';' : ' ', $tmp);
} else {
$value = (string) $value;
}
// add new attribute
$value = str_replace(array('&', '"', '<', '>', '@'), array('&', '"', '<', '>', '@'), $value);
$s .= ' ' . $key . '="' . Texy::freezeSpaces($value) . '"';
}
}
// finish start tag
if (self::$xhtml && $this->isEmpty) {
return $s . ' />';
}
return $s . '>';
}
/**
* Returns element's end tag.
* @return string
*/
public function endTag()
{
if ($this->name && !$this->isEmpty) {
return '</' . $this->name . '>';
}
return '';
}
/**
* Clones all children too.
*/
public function __clone()
{
foreach ($this->children as $key => $value) {
if (is_object($value)) {
$this->children[$key] = clone $value;
}
}
}
/**
* @return int
*/
final public function getContentType()
{
if (!isset(self::$inlineElements[$this->name])) {
return Texy::CONTENT_BLOCK;
}
return self::$inlineElements[$this->name] ? Texy::CONTENT_REPLACED : Texy::CONTENT_MARKUP;
}
/**
* @param array
* @return void
*/
final public function validateAttrs($dtd)
{
if (isset($dtd[$this->name])) {
$allowed = $dtd[$this->name][0];
if (is_array($allowed)) {
foreach ($this->attrs as $attr => $foo) {
if (!isset($allowed[$attr])) {
unset($this->attrs[$attr]);
}
}
}
}
}
public function validateChild($child, $dtd)
{
if (isset($dtd[$this->name])) {
if ($child instanceof TexyHtml) {
$child = $child->name;
}
return isset($dtd[$this->name][1][$child]);
} else {
return TRUE; // unknown element
}
}
/**
* Parses text as single line.
* @param Texy
* @param string
* @return void
*/
final public function parseLine(Texy $texy, $s)
{
// TODO!
// special escape sequences
$s = str_replace(array('\)', '\*'), array(')', '*'), $s);
$parser = new TexyLineParser($texy, $this);
$parser->parse($s);
return $parser;
}
/**
* Parses text as block.
* @param Texy
* @param string
* @param bool
* @return void
*/
final public function parseBlock(Texy $texy, $s, $indented = FALSE)
{
$parser = new TexyBlockParser($texy, $this, $indented);
$parser->parse($s);
}
}
|