Skip to content

Commit

Permalink
- use $texy->setOutputMode(...) to switch between HTML/XHTML, strict/…
Browse files Browse the repository at this point in the history
…loose modes. Parameter is one of these constants: Texy::HTML4_TRANSITIONAL, Texy::HTML4_STRICT, Texy::XHTML1_TRANSITIONAL, Texy::XHTML1_STRICT

- Texy::$strictDTD & $texy->htmlOutputModule->xhtml are deprecated
  • Loading branch information
dg committed Apr 15, 2008
1 parent ee396c3 commit 8aaf615
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 72 deletions.
4 changes: 4 additions & 0 deletions changelog.cs.texy
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Changelog
Verze 2.0 Beta 2
=========

rev. 208
- use `$texy->setOutputMode(...)` to switch between HTML/XHTML, strict/loose modes. Parameter is one of these constants: `Texy::HTML4_TRANSITIONAL`, `Texy::HTML4_STRICT`, `Texy::XHTML1_TRANSITIONAL`, `Texy::XHTML1_STRICT`
- `Texy::$strictDTD` & `$texy->htmlOutputModule->xhtml` are deprecated

rev. 206
- new constants `Texy::FILTER_ANCHOR` & `Texy::FILTER_IMAGE`

Expand Down
79 changes: 67 additions & 12 deletions texy/libs/Texy.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ class Texy extends Nette_Object
const FILTER_ANCHOR = 'anchor';
const FILTER_IMAGE = 'image';

// HTML minor-modes
const STRICT = 1;
const XML = 2;

// HTML modes
const HTML4_TRANSITIONAL = 0;
const HTML4_STRICT = Texy::STRICT;
const HTML5 = 4;
const XHTML1_TRANSITIONAL = Texy::XML;
const XHTML1_STRICT = 3; // Texy::STRICT | Texy::XML;
const XHTML5 = 6; // Texy::HTML5 | Texy::XML;

/** @var string input & output text encoding */
public $encoding = 'utf-8';

Expand Down Expand Up @@ -112,9 +124,6 @@ class Texy extends Nette_Object
'bottom' => NULL,
);

/** @var bool use Strict of Transitional DTD? */
public static $strictDTD = FALSE;

/** @var mixed */
public static $advertisingNotice = 'once';

Expand Down Expand Up @@ -207,28 +216,34 @@ class Texy extends Nette_Object
/** @var array of events and registered handlers */
private $handlers = array();

/** @var array */
public $dtd;

/** @var int HTML mode */
private $mode;


/** DEPRECATED */
public static $strictDTD;
public $cleaner;
public $xhtml;



public function __construct()
{
TexyHtml::initDTD(self::$strictDTD);

// accepts all valid HTML tags and attributes by default
foreach (TexyHtml::$dtd as $tag => $dtd) {
$this->allowedTags[$tag] = Texy::ALL;
}

// load all modules
$this->loadModules();

// DEPRECATED
if (self::$strictDTD !== NULL) {
$this->setOutputMode(self::$strictDTD ? self::XHTML1_STRICT : self::XHTML1_TRANSITIONAL);
} else {
$this->setOutputMode(self::XHTML1_TRANSITIONAL);
}

// DEPRECATED
$this->cleaner = & $this->htmlOutputModule;
$this->xhtml = & $this->htmlOutputModule->xhtml;

// examples of link references ;-)
$link = new TexyLink('http://texy.info/');
Expand All @@ -245,6 +260,46 @@ public function __construct()



/**
* Set HTML/XHTML output mode (overwrites self::$allowedTags)
* @param int
* @return void
*/
public function setOutputMode($mode)
{
if (!in_array($mode, array(self::HTML4_TRANSITIONAL, self::HTML4_STRICT,
self::HTML5, self::XHTML1_TRANSITIONAL, self::XHTML1_STRICT, self::XHTML5), TRUE)) {
throw new InvalidArgumentException("Invalid mode.");
}

if (!isset(TexyHtml::$dtd[$mode])) {
require dirname(__FILE__) . '/TexyHtml.DTD.php';
}

$this->mode = $mode;
$this->dtd = TexyHtml::$dtd[$mode];
TexyHtml::$xhtml = (bool) ($mode & self::XML); // TODO: remove?
// accept all valid HTML tags and attributes by default
$this->allowedTags = array();
foreach ($this->dtd as $tag => $dtd) {
$this->allowedTags[$tag] = self::ALL;
}
}



/**
* Get HTML/XHTML output mode
* @return int
*/
public function getOutputMode()
{
return $this->mode;
}



/**
* Create array of all used modules ($this->modules).
* This array can be changed by overriding this method (by subclasses)
Expand Down Expand Up @@ -481,7 +536,7 @@ final public function stringToText($s)
$s = preg_replace('#\n\s*\n\s*\n[\n\s]*\n#', "\n\n", $s);

// entities -> chars
$s = Texy::unescapeHtml($s);
$s = self::unescapeHtml($s);

// convert nbsp to normal space and remove shy
$s = strtr($s, array(
Expand Down
36 changes: 19 additions & 17 deletions texy/libs/TexyHtml.DTD.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
/** @version $Revision$ $Date$ */

// included by TexyHtml::initDTD
// @param $strict
// @param $mode

$strict = (bool) ($mode & Texy::STRICT);


// attributes
Expand Down Expand Up @@ -58,7 +60,7 @@
$bi = $b + $i;

// build DTD
self::$dtd = array(
TexyHtml::$dtd[$mode] = array(
'html' => array(
$strict ? $i18n + array('xmlns'=>1) : $i18n + array('version'=>1,'xmlns'=>1), // extra: xmlns
array('head'=>1,'body'=>1),
Expand Down Expand Up @@ -313,11 +315,11 @@
),
'label' => array(
$attrs + array('for'=>1,'accesskey'=>1,'onfocus'=>1,'onblur'=>1),
$i, // - label by self::$prohibits
$i, // - label by TexyHtml::$prohibits
),
'button' => array(
$attrs + array('name'=>1,'value'=>1,'type'=>1,'disabled'=>1,'tabindex'=>1,'accesskey'=>1,'onfocus'=>1,'onblur'=>1),
$bi, // - a input select textarea label button form fieldset, by self::$prohibits
$bi, // - a input select textarea label button form fieldset, by TexyHtml::$prohibits
),
'ins' => array(
$attrs + array('cite'=>1,'datetime'=>1),
Expand Down Expand Up @@ -383,7 +385,7 @@


// LOOSE DTD
self::$dtd += array(
TexyHtml::$dtd[$mode] += array(
// transitional
'dir' => array(
$attrs + array('compact'=>1),
Expand Down Expand Up @@ -458,17 +460,17 @@
);

// transitional modified
self::$dtd['a'][0] += array('target'=>1);
self::$dtd['area'][0] += array('target'=>1);
self::$dtd['body'][0] += array('background'=>1,'bgcolor'=>1,'text'=>1,'link'=>1,'vlink'=>1,'alink'=>1);
self::$dtd['form'][0] += array('target'=>1);
self::$dtd['img'][0] += array('align'=>1,'border'=>1,'hspace'=>1,'vspace'=>1);
self::$dtd['input'][0] += array('align'=>1);
self::$dtd['link'][0] += array('target'=>1);
self::$dtd['object'][0] += array('align'=>1,'border'=>1,'hspace'=>1,'vspace'=>1);
self::$dtd['script'][0] += array('language'=>1);
self::$dtd['table'][0] += array('align'=>1,'bgcolor'=>1);
self::$dtd['td'][0] += array('nowrap'=>1,'bgcolor'=>1,'width'=>1,'height'=>1);
self::$dtd['th'][0] += array('nowrap'=>1,'bgcolor'=>1,'width'=>1,'height'=>1);
TexyHtml::$dtd[$mode]['a'][0] += array('target'=>1);
TexyHtml::$dtd[$mode]['area'][0] += array('target'=>1);
TexyHtml::$dtd[$mode]['body'][0] += array('background'=>1,'bgcolor'=>1,'text'=>1,'link'=>1,'vlink'=>1,'alink'=>1);
TexyHtml::$dtd[$mode]['form'][0] += array('target'=>1);
TexyHtml::$dtd[$mode]['img'][0] += array('align'=>1,'border'=>1,'hspace'=>1,'vspace'=>1);
TexyHtml::$dtd[$mode]['input'][0] += array('align'=>1);
TexyHtml::$dtd[$mode]['link'][0] += array('target'=>1);
TexyHtml::$dtd[$mode]['object'][0] += array('align'=>1,'border'=>1,'hspace'=>1,'vspace'=>1);
TexyHtml::$dtd[$mode]['script'][0] += array('language'=>1);
TexyHtml::$dtd[$mode]['table'][0] += array('align'=>1,'bgcolor'=>1);
TexyHtml::$dtd[$mode]['td'][0] += array('nowrap'=>1,'bgcolor'=>1,'width'=>1,'height'=>1);
TexyHtml::$dtd[$mode]['th'][0] += array('nowrap'=>1,'bgcolor'=>1,'width'=>1,'height'=>1);

// missing: FRAMESET, FRAME, BGSOUND, XMP, ...
44 changes: 14 additions & 30 deletions texy/libs/TexyHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ class TexyHtml extends Nette_Object implements ArrayAccess, /* Countable, */ Ite

/**
* DTD descriptor.
* $dtd[element][0] - allowed attributes (as array keys)
* $dtd[element][1] - allowed content for an element (content model) (as array keys)
* - array of allowed elements (as keys)
* - FALSE - empty element
* - 0 - special case for ins & del
* $dtd[$mode][element][0] - allowed attributes (as array keys)
* $dtd[$mode][element][1] - allowed content for an element (content model) (as array keys)
* - array of allowed elements (as keys)
* - FALSE - empty element
* - 0 - special case for ins & del
* @var array
* @see TexyHtmlOutputModule::initDTD()
*/
public static $dtd;

Expand Down Expand Up @@ -566,27 +565,28 @@ final public function getContentType()


/**
* @param array
* @return void
*/
final public function validateAttrs()
final public function validateAttrs($dtd)
{
if (isset(self::$dtd[$this->name])) {
$dtd = self::$dtd[$this->name][0];
if (is_array($dtd)) {
if (isset($dtd[$this->name])) {
$allowed = $dtd[$this->name][0];
if (is_array($allowed)) {
foreach ($this->attrs as $attr => $foo) {
if (!isset($dtd[$attr])) unset($this->attrs[$attr]);
if (!isset($allowed[$attr])) unset($this->attrs[$attr]);
}
}
}
}



public function validateChild($child)
public function validateChild($child, $dtd)
{
if (isset(self::$dtd[$this->name])) {
if (isset($dtd[$this->name])) {
if ($child instanceof TexyHtml) $child = $child->name;
return isset(self::$dtd[$this->name][1][$child]);
return isset($dtd[$this->name][1][$child]);
} else {
return TRUE; // unknown element
}
Expand Down Expand Up @@ -626,20 +626,4 @@ final public function parseBlock(Texy $texy, $s, $indented = FALSE)
$parser->parse($s);
}



/**
* Initializes self::$dtd array.
* @param bool
* @return void
*/
public static function initDTD($strict)
{
static $last;
if ($last === $strict) return;
$last = $strict;

require __FILE__ . '/../TexyHtml.DTD.php';
}

}
4 changes: 2 additions & 2 deletions texy/libs/TexyModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function decorate($texy, $el)

} elseif ($tmp === Texy::ALL) {
$elAttrs = $this->attrs;
$el->validateAttrs();
$el->validateAttrs($texy->dtd);

} elseif (is_array($tmp) && isset($tmp[$el->getName()])) {
$tmp = $tmp[$el->getName()];
Expand All @@ -173,7 +173,7 @@ public function decorate($texy, $el)
foreach ($this->attrs as $key => $value)
if (isset($tmp[$key])) $el->attrs[$key] = $value;
}
$el->validateAttrs();
$el->validateAttrs($texy->dtd);
}

// title
Expand Down
6 changes: 3 additions & 3 deletions texy/modules/TexyHtmlModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function solveTag($invocation, TexyHtml $el, $isStart, $forceEmpty = NULL
// convert case
$name = $el->getName();
$lower = strtolower($name);
if (isset(TexyHtml::$dtd[$lower]) || $name === strtoupper($name)) {
if (isset($tx->dtd[$lower]) || $name === strtoupper($name)) {
// complete UPPER convert to lower
$name = $lower;
$el->setName($name);
Expand Down Expand Up @@ -202,7 +202,7 @@ public function solveTag($invocation, TexyHtml $el, $isStart, $forceEmpty = NULL
if (is_array($tmp)) {
$elAttrs['class'] = explode(' ', $elAttrs['class']);
foreach ($elAttrs['class'] as $key => $value)
if (!isset($tmp[$value])) unset($elAttrs['class'][$key]); // id & class are case-sensitive in XHTML
if (!isset($tmp[$value])) unset($elAttrs['class'][$key]); // id & class are case-sensitive

} elseif ($tmp !== Texy::ALL) {
$elAttrs['class'] = NULL;
Expand Down Expand Up @@ -256,7 +256,7 @@ public function solveTag($invocation, TexyHtml $el, $isStart, $forceEmpty = NULL
}
}

$el->validateAttrs();
$el->validateAttrs($tx->dtd);

return $el;
}
Expand Down
Loading

0 comments on commit 8aaf615

Please sign in to comment.