summaryrefslogtreecommitdiff
path: root/it_tag.class
blob: f2b0fdc199fdc7878d000bf13c05942cdc72c6d0 (plain)
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
<?php
/*
**	$Id$
**
**	Copyright (C) 1995-2008 by the ITools Authors.
**	This file is part of ITools - the Internet Tools Library
**
**	ITools is free software; you can redistribute it and/or modify
**	it under the terms of the GNU General Public License as published by
**	the Free Software Foundation; either version 3 of the License, or
**	(at your option) any later version.
**
**	ITools is distributed in the hope that it will be useful,
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**	GNU General Public License for more details.
**
**	You should have received a copy of the GNU General Public License
**	along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
**	UltraHTML 3000 tool layer. Tags are now objects, used by it_html
**
**/

class it_tag extends ArrayObject
{
	var $tag;
	var $attributes = array();
	var $prefix = "";
	var $suffix = "";

function __construct($tag, $values = array())
{
	$this->tag = $tag;
	$this->offsetSet(0, $values);
}

function offsetSet($index, $value)
{
	if (is_string($index))
	{
		$this->attributes[$index] = $value;
	}
	else if (is_array($value))
	{
		foreach ($value as $key => $val)
			$this->offsetSet($key, $val);
	}
	else
	{
		if (is_string($value))
		{
			if ($errortype = $GLOBALS['it_html']->p['reportquote'])
				trigger_error("Unquoted '$value' at " . it_debug::backtrace(), $errortype);
			if ($GLOBALS['it_html']->p['autoquote'])
				$value = new it_q($value);
		}

		parent::offsetSet($index, $value);
	}
}

function __toString()
{
	$result = array($this->prefix, "<$this->tag");

	foreach ($this->attributes as $key => $value)
	{
		if (($value === null) || ($value === false))	# null or false: omit whole tag
			;
		else if (isset($value) && $value !== true)	# normal case: value
			$result[] = " $key=\"" . (preg_match("/[<>&\"'\n\x80-\x9f]/", $value) ? str_replace("\n", "&#10;", new it_q($value)) : $value) . '"';
		else						# true: tag without value
			$result[] = ($GLOBALS['it_html']->p['htmltype'] == 'html') ? " $key" : " $key=\"$key\"";
	}

	$newline = isset($GLOBALS['it_html']->hasnonewline[$this->tag]) ? "" : "\n";

	if (count($this) || preg_match('/^(a|div|iframe|script|span|td|textarea)$/i', $this->tag))
	{
		$result[] = ">";

		$data = join("", $this->getArrayCopy());

		# Ultra XML PrettyPrinter 3000 [\] by SCA
		if ($GLOBALS['it_html']->p['prettyprint'] && $newline && (substr($data, -1, 1) == "\n") && (strpos($data, '<textarea') === false) && ($data != strip_tags($data)))
			$data = str_replace("\n", "\n  ", "\n" . trim($data)) . "\n";

		$result[] = $data;

		$result[] = "</$this->tag>$newline";
	}
	else if ($GLOBALS['it_html']->p['htmltype'] == 'html')
		$result[] = ">$newline";
	else
		$result[] = " />$newline";

	if ($GLOBALS['debug_srclines'])
		array_unshift($result, "<!-- " . trim(it::replace(array('^(it_\w+.class:\d+\s+)+' => "", '^(([\w.]+:\d+\s+){2}).*' => '$1'), it_debug::backtrace())) . " -->");

	$result[] = $this->suffix;

	return join("", $result);
}

function clear()
{
	$this->exchangeArray(array());
}

}

?>