/**
  * CSS の構文を解析します。
  *
  * @param string $data CSS データ。
  * @author Naomichi Yamakita <*****@*****.**>
  */
 private function parseCSS($data)
 {
     $encoding = mb_detect_encoding($data, 'UTF-8, eucjp-win, sjis-win, iso-2022-jp');
     if ($encoding !== 'UTF-8') {
         $data = mb_convert_encoding($data, 'UTF-8', $encoding);
     }
     $this->_htmlCSS->parseString($data);
 }
Beispiel #2
0
 /**
  * Import cascading style sheet (CSS) elements
  *
  * Set the CSS required to display the progress meter in a HTML document.
  *
  * @param      mixed     $styles        CSS elements reference to import
  *
  * @return     void|PEAR_Error
  * @since      2.2.0
  * @access     public
  * @throws     HTML_PROGRESS2_ERROR_INVALID_INPUT
  */
 function importStyle($styles)
 {
     if (is_string($styles)) {
         $styles = (array) $styles;
     }
     if (!is_array($styles)) {
         return $this->raiseError(HTML_PROGRESS2_ERROR_INVALID_INPUT, 'exception', array('var' => '$styles', 'was' => gettype($styles), 'expected' => 'array | string', 'paramnum' => 1));
     }
     $css = new HTML_CSS();
     $res = $css->parseData($styles);
     if ($css->isError()) {
         return $this->raiseError(HTML_PROGRESS2_ERROR_INVALID_INPUT, 'error', array('var' => '$styles', 'was' => 'unknown data source', 'expected' => 'valid CSS', 'paramnum' => 1));
     }
     if (strpos($this->border['class'], '%s') === false) {
         $pattern = $this->ident . '\\.' . $this->border['class'];
     } else {
         $pattern = '\\.' . sprintf($this->border['class'], $this->ident);
     }
     $border = $css->grepStyle("/{$pattern}/");
     foreach ($border as $b) {
         foreach ($b as $p => $v) {
             if (substr($p, 0, 6) == 'border') {
                 $n = str_replace('border-', '', $p);
                 if (isset($this->border[$n])) {
                     if (substr($v, -2) == 'px') {
                         $this->border[$n] = intval($v);
                     } else {
                         $this->border[$n] = $v;
                     }
                 }
             } else {
                 if ($p == 'background-color' && $this->cellCount == 0) {
                     $this->cell['inactive-color'] = $v;
                 } elseif (isset($this->_progress[$p])) {
                     if (substr($v, -2) == 'px') {
                         $this->_progress[$p] = intval($v);
                     } else {
                         $this->_progress[$p] = $v;
                     }
                 }
             }
         }
         if ($this->border['width'] > 0) {
             $this->_paintBorder = true;
         }
     }
     foreach ($this->label as $name => $data) {
         if (strpos($data['class'], '%s') === false) {
             $pattern = $name . $this->ident . '\\.' . $data['class'];
         } else {
             $pattern = '\\.' . sprintf($data['class'], $name . $this->ident);
         }
         $label = $css->grepStyle("/{$pattern}/");
         foreach ($label as $l) {
             foreach ($l as $p => $v) {
                 if (substr($p, 0, 4) == 'text') {
                     $n = str_replace('text-', '', $p);
                     if (isset($this->label[$name][$n])) {
                         $this->label[$name][$n] = $v;
                     }
                 } elseif (isset($this->label[$name][$p])) {
                     if (substr($v, -2) == 'px') {
                         $this->label[$name][$p] = intval($v);
                     } else {
                         $this->label[$name][$p] = $v;
                     }
                 }
             }
         }
     }
     $pcell = '.' . sprintf($this->cell['class'], $this->ident);
     if (strpos($this->cell['class'], '%s') === false) {
         $pattern = $this->ident . '\\s*\\.' . $this->cell['class'];
     } else {
         $pattern = '\\.' . sprintf($this->cell['class'], $this->ident);
     }
     $cell = $css->grepStyle("/{$pattern}/");
     foreach ($cell as $c => $data) {
         foreach ($data as $p => $v) {
             if ($p == 'background-color') {
                 if (strpos($c, $pcell . 'A') !== false) {
                     $this->cell['active-color'] = $v;
                 } else {
                     $this->cell['inactive-color'] = $v;
                 }
             } elseif ($p == 'background-image') {
                 $pattern = "\\s*url\\s*\\([\\s\"'`]*([\\w:?=@&\\/#._;-]+)[\\s\"'`]*\\)\\s*";
                 $found = preg_match("/{$pattern}/", $v, $matches);
                 if ($found) {
                     $this->cell[$p] = $matches[1];
                 }
             } else {
                 if (substr($v, -2) == 'px') {
                     $this->cell[$p] = intval($v);
                 } else {
                     $this->cell[$p] = $v;
                 }
             }
         }
     }
 }
 public function apply($document, $base_dir = '')
 {
     if (!$base_dir) {
         $base_dir = $this->base_dir;
     }
     // XHTMLをパース
     $dom = new DOMDocument();
     $dom->loadHTML($document);
     $dom_xpath = new DOMXPath($dom);
     // 外部参照のCSSファイルを抽出する
     $nodes = $dom_xpath->query('//link[@rel="stylesheet" or @type="text/css"] | //style[@type="text/css"]');
     $add_style = array();
     $psudo_classes = array('a:hover', 'a:link', 'a:focus', 'a:visited');
     foreach ($nodes as $key => $node) {
         // CSSをパース
         $html_css = new HTML_CSS();
         if ($node->tagName == 'link' && ($href = $node->attributes->getNamedItem('href'))) {
             // linkタグの場合
             if (!file_exists($base_dir . $href->nodeValue)) {
                 throw new UnexpectedValueException('ERROR: ' . $base_dir . $href->nodeValue . ' file does not exist');
             }
             #TODO: @importのサポート
             $css_string = file_get_contents($base_dir . $href->nodeValue);
         } else {
             if ($node->tagName == 'style') {
                 // styleタグの場合
                 $css_string = $node->nodeValue;
             }
         }
         $css_error = $html_css->parseString($css_string);
         if ($css_error) {
             throw new RuntimeException('ERROR: css parse error');
         }
         // a:hover, a:link, a:focus a:visited を退避
         foreach ($psudo_classes as $psude_class) {
             $block = $html_css->toInline($psude_class);
             if (!$block) {
                 continue;
             }
             $add_style[] = $psude_class . '{' . $block . '}';
         }
         // CSSをインライン化
         $css = $html_css->toArray();
         foreach ($css as $selector => $style) {
             #TODO: 疑似要素のサポート
             // 疑似要素と@ルールはスルー(selectorToXPath的にバグでやすい)
             if (strpos($selector, '@') !== false) {
                 continue;
             }
             if (strpos($selector, ':') !== false) {
                 continue;
             }
             $xpath = selectorToXPath::toXPath($selector);
             $elements = $dom_xpath->query($xpath);
             if ($elements->length == 0) {
                 continue;
             }
             // inlineにするCSS文を構成(toInline($selector)だとh2, h3 などでうまくいかないバグ?があったため)
             $inline_style = '';
             foreach ($style as $k => $v) {
                 $inline_style .= $k . ':' . $v . ';';
             }
             foreach ($elements as $element) {
                 if ($attr_style = $element->attributes->getNamedItem('style')) {
                     // style要素が存在する場合は追記
                     if (substr($attr_style->nodeValue, -1) != ';') {
                         $inline_style = ';' . $inline_style;
                     }
                     $attr_style->nodeValue .= $inline_style;
                 } else {
                     // style要素が存在しない場合は追加
                     $element->setAttribute('style', $inline_style);
                 }
             }
         }
         // 読み込み終わったノードを削除
         $parent = $node->parentNode;
         $parent->removeChild($node);
     }
     // 疑似クラスを<style>タグとして追加
     if (!empty($add_style)) {
         $new_style = implode(PHP_EOL, $add_style);
         $new_style = implode(PHP_EOL, array('<![CDATA[', $new_style, ']]>'));
         $head = $dom_xpath->query('//head');
         $new_style_node = new DOMElement('style', $new_style);
         $head->item(0)->appendChild($new_style_node)->setAttribute('type', 'text/css');
     }
     return $dom->saveHTML();
 }
Beispiel #4
0
    function _renderForm(&$page)
    {
        $pageName = $page->getAttribute('name');
        $tabPreview = array_slice($page->controller->_tabs, -2, 1);
        $css = new HTML_CSS();
        $css->setStyle('body', 'background-color', '#7B7B88');
        $css->setStyle('body', 'font-family', 'Verdana, Arial, helvetica');
        $css->setStyle('body', 'font-size', '10pt');
        $css->setStyle('h1', 'color', '#FFC');
        $css->setStyle('h1', 'text-align', 'center');
        $css->setStyle('.maintable', 'width', '100%');
        $css->setStyle('.maintable', 'border-width', '0');
        $css->setStyle('.maintable', 'border-style', 'thin dashed');
        $css->setStyle('.maintable', 'border-color', '#D0D0D0');
        $css->setStyle('.maintable', 'background-color', '#EEE');
        $css->setStyle('.maintable', 'cellspacing', '2');
        $css->setStyle('.maintable', 'cellspadding', '3');
        $css->setStyle('th', 'text-align', 'center');
        $css->setStyle('th', 'color', '#FFC');
        $css->setStyle('th', 'background-color', '#AAA');
        $css->setStyle('th', 'white-space', 'nowrap');
        $css->setStyle('input', 'font-family', 'Verdana, Arial, helvetica');
        $css->setStyle('input.flat', 'border-style', 'solid');
        $css->setStyle('input.flat', 'border-width', '2px 2px 0px 2px');
        $css->setStyle('input.flat', 'border-color', '#996');
        $header = '
<style type="text/css">
<!--
{%style%}
// -->
</style>
';
        // on preview tab, add progress bar javascript and stylesheet
        if ($pageName == $tabPreview[0][0]) {
            $bar = $page->controller->createProgressBar();
            $header .= '
<script type="text/javascript">
<!--
{%javascript%}
//-->
</script>
';
            $header = str_replace('{%style%}', $css->toString() . $bar->getStyle(), $header);
            $header = str_replace('{%javascript%}', $bar->getScript(), $header);
            $barElement =& $page->getElement('progressBar');
            $barElement->setText($bar->toHtml());
        } else {
            $header = str_replace('{%style%}', $css->toString(), $header);
        }
        $renderer =& $page->defaultRenderer();
        $renderer->setFormTemplate($header . '<table class="maintable"><form{attributes}>{content}</form></table>');
        $renderer->setHeaderTemplate('<tr><th colspan="2">{header}</th></tr>');
        $renderer->setGroupTemplate('<table><tr>{content}</tr></table>', 'name');
        $renderer->setGroupElementTemplate('<td>{element}<br /><span class="qfLabel">{label}</span></td>', 'name');
        $page->accept($renderer);
        echo $renderer->toHtml();
    }
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997 - 2003 The PHP Group                              |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Author:  Klaus Guenther <*****@*****.**>                     |
// +----------------------------------------------------------------------+
//
// $Id: CSS_Stylesheet.php,v 1.1 2003/07/31 14:58:19 thesaur Exp $
require_once 'HTML/CSS.php';
$css = new HTML_CSS();
// define styles
$css->setStyle('body', 'background-color', '#0c0c0c');
$css->setStyle('body', 'color', '#ffffff');
$css->setStyle('h1', 'text-align', 'center');
$css->setStyle('h1', 'font', '16pt helvetica, arial, sans-serif');
$css->setStyle('p', 'font', '12pt helvetica, arial, sans-serif');
// output the stylesheet directly to browser
$css->display();
 /**
  * Returns the custom style sheet
  *
  * Returns the custom style sheet to use for layout
  *
  * @param int   $destination (optional) Destination of css content
  * @param mixed $extra       (optional) Additional data depending of destination
  *
  * @return mixed
  * @access public
  * @since  version 1.8.0b4 (2008-06-18)
  */
 function getStyleSheet($destination = 1, $extra = null)
 {
     $css = new HTML_CSS();
     $css->parseFile($this->css);
     $tdw = $this->conf['tdwidth'];
     $em = array_sum($tdw);
     $td = 'td';
     $o = $this->args['output-level'];
     $css->setStyle('.outer td.dirname', 'width', $em . 'em');
     if ($o & 16) {
         $td .= '+td';
         $css->setStyle('.outer ' . $td, 'width', $tdw[1] . 'em');
         $em = $em - $tdw[1];
     }
     if ($o & 1) {
         $td .= '+td';
         $css->setStyle('.outer ' . $td, 'width', $tdw[2] . 'em');
         $em = $em - $tdw[2];
     }
     if ($o & 2) {
         $td .= '+td';
         $css->setStyle('.outer ' . $td, 'width', $tdw[3] . 'em');
         $em = $em - $tdw[3];
     }
     if ($o & 12) {
         $td .= '+td';
         $css->setStyle('.outer ' . $td, 'width', $tdw[4] . 'em');
         $em = $em - $tdw[4];
     }
     $css->setStyle('.outer td', 'width', $em . 'em');
     $styles = '';
     switch ($destination) {
         case 1:
             // embedded styles
             $styles = $css->toString();
             break;
         case 2:
             // save only to file
             $css->toFile($extra);
             $styles = $extra;
             break;
         case 3:
             // apply a user function
             if (is_callable($extra)) {
                 $styles = call_user_func_array($extra, array($css));
             }
             break;
         default:
             break;
     }
     return $styles;
 }
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Author:  Klaus Guenther <*****@*****.**>                     |
// +----------------------------------------------------------------------+
//
// $Id: CSS_Inline.php,v 1.1 2003/07/31 14:58:19 thesaur Exp $
require_once 'HTML/CSS.php';
// generate a new class
$css = new HTML_CSS();
// let's set some styles for <body>
$css->setStyle('body', 'background-color', '#0c0c0c');
$css->setStyle('body', 'color', '#ffffff');
// now for <h1>
$css->setStyle('h1', 'text-align', 'center');
$css->setStyle('h1', 'font', '16pt helvetica, arial, sans-serif');
// and finally for <p>
$css->setStyle('p', 'font', '12pt helvetica, arial, sans-serif');
// let's make <body> inherit from <p>
$css->setSameStyle('body', 'p');
// and let's put this into a tag:
echo '<body style="' . $css->toInline('body') . '">';
// will output:
// <body style="font:12pt helvetica, arial, sans-serif;background-color:#0c0c0c;color:#ffffff;">
// ideas for inline use:
Beispiel #8
0
 /**
  * Get the cascading style sheet to put inline on HTML document
  *
  * @return     object                   HTML_CSS instance
  * @since      0.2
  * @access     public
  * @tutorial   ui.getstyle.pkg
  * @author     Stefan Neufeind <*****@*****.**> Contributor.
  *             See details on thanks section of README file.
  */
 function &getStyle()
 {
     include_once 'HTML/CSS.php';
     $progressAttr = $this->getProgressAttributes();
     $borderAttr = $this->getBorderAttributes();
     $stringAttr = $this->getStringAttributes();
     $cellAttr = $this->getCellAttributes();
     $orient = $this->getOrientation();
     $css = new HTML_CSS();
     $css->setStyle('.' . $progressAttr['class'], 'background-color', $progressAttr['background-color']);
     $css->setStyle('.' . $progressAttr['class'], 'width', $progressAttr['width'] . 'px');
     $css->setStyle('.' . $progressAttr['class'], 'height', $progressAttr['height'] . 'px');
     $css->setStyle('.' . $progressAttr['class'], 'position', 'relative');
     $css->setStyle('.' . $progressAttr['class'], 'left', '0px');
     $css->setStyle('.' . $progressAttr['class'], 'top', '0px');
     $css->setSameStyle('.' . $borderAttr['class'], '.' . $progressAttr['class']);
     $css->setStyle('.' . $borderAttr['class'], 'border-width', $borderAttr['width'] . 'px');
     $css->setStyle('.' . $borderAttr['class'], 'border-style', $borderAttr['style']);
     $css->setStyle('.' . $borderAttr['class'], 'border-color', $borderAttr['color']);
     $css->setStyle('.' . $stringAttr['id'], 'width', $stringAttr['width'] . 'px');
     if (isset($stringAttr['height'])) {
         $css->setStyle('.' . $stringAttr['id'], 'height', $stringAttr['height'] . 'px');
     }
     $css->setStyle('.' . $stringAttr['id'], 'text-align', $stringAttr['align']);
     $css->setStyle('.' . $stringAttr['id'], 'font-family', $stringAttr['font-family']);
     $css->setStyle('.' . $stringAttr['id'], 'font-size', $stringAttr['font-size'] . 'px');
     $css->setStyle('.' . $stringAttr['id'], 'color', $stringAttr['color']);
     $css->setStyle('.' . $stringAttr['id'], 'background-color', $stringAttr['background-color']);
     $css->setStyle('.' . $cellAttr['class'] . 'I', 'width', $cellAttr['width'] . 'px');
     $css->setStyle('.' . $cellAttr['class'] . 'I', 'height', $cellAttr['height'] . 'px');
     $css->setStyle('.' . $cellAttr['class'] . 'I', 'font-family', $cellAttr['font-family']);
     $css->setStyle('.' . $cellAttr['class'] . 'I', 'font-size', $cellAttr['font-size'] . 'px');
     if ($orient == HTML_PROGRESS_BAR_HORIZONTAL) {
         $css->setStyle('.' . $cellAttr['class'] . 'I', 'float', 'left');
     }
     if ($orient == HTML_PROGRESS_BAR_VERTICAL) {
         $css->setStyle('.' . $cellAttr['class'] . 'I', 'float', 'none');
     }
     $css->setSameStyle('.' . $cellAttr['class'] . 'A', '.' . $cellAttr['class'] . 'I');
     if ($orient !== HTML_PROGRESS_CIRCLE) {
         $css->setStyle('.' . $cellAttr['class'] . 'I', 'background-color', $cellAttr['inactive-color']);
         $css->setStyle('.' . $cellAttr['class'] . 'A', 'background-color', $cellAttr['active-color']);
     }
     $css->setStyle('.' . $cellAttr['class'] . 'A', 'visibility', 'hidden');
     if (isset($cellAttr['background-image'])) {
         $css->setStyle('.' . $cellAttr['class'] . 'A', 'background-image', 'url("' . $cellAttr['background-image'] . '")');
         $css->setStyle('.' . $cellAttr['class'] . 'A', 'background-repeat', 'no-repeat');
     }
     if ($orient == HTML_PROGRESS_CIRCLE) {
         $css->setStyle('.' . $cellAttr['class'] . 'I', 'background-image', 'url("' . $cellAttr[0]['background-image'] . '")');
         $css->setStyle('.' . $cellAttr['class'] . 'I', 'background-repeat', 'no-repeat');
     }
     return $css;
 }
// | This source file is subject to version 2.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Author:  Klaus Guenther <*****@*****.**>                     |
// +----------------------------------------------------------------------+
//
// $Id: CSS_InHeader.php,v 1.1 2003/07/31 14:58:19 thesaur Exp $
require_once 'HTML/Page.php';
require_once 'HTML/CSS.php';
// generate an instance
$css = new HTML_CSS();
// define the various settings for <body>
$css->setStyle('body', 'background-color', '#0c0c0c');
$css->setStyle('body', 'color', '#ffffff');
// define <h1> element
$css->setStyle('h1', 'text-align', 'center');
$css->setStyle('h1', 'font', '16pt helvetica, arial, sans-serif');
// define <p> element
$css->setStyle('p', 'font', '12pt helvetica, arial, sans-serif');
// create a new HTML page
$p = new HTML_Page();
$p->setTitle("My page");
// it can be added as an object
$p->addStyleDeclaration(&$css, 'text/css');
$p->addMetaData("author", "My Name");
$p->addBodyContent = "<h1>headline</h1>";
Beispiel #10
0
 function _renderForm(&$page)
 {
     $pageName = $page->getAttribute('name');
     $tabPreview = array_slice($page->controller->_tabs, -2, 1);
     $p = new HTML_Page(array('lineend' => OS_WINDOWS ? 'win' : 'unix', 'doctype' => "XHTML 1.0 Strict", 'language' => 'en', 'cache' => 'false'));
     $p->disableXmlProlog();
     $p->setTitle("PEAR::HTML_Progress - Generator");
     $p->setMetaData("author", "Laurent Laville");
     $css = new HTML_CSS();
     $css->setStyle('body', 'background-color', '#7B7B88');
     $css->setStyle('body', 'font-family', 'Verdana, Arial, helvetica');
     $css->setStyle('body', 'font-size', '10pt');
     $css->setStyle('h1', 'color', '#FFC');
     $css->setStyle('h1', 'text-align', 'center');
     $css->setStyle('.maintable', 'width', '100%');
     $css->setStyle('.maintable', 'border-width', '0');
     $css->setStyle('.maintable', 'border-style', 'thin dashed');
     $css->setStyle('.maintable', 'border-color', '#D0D0D0');
     $css->setStyle('.maintable', 'background-color', '#EEE');
     $css->setStyle('.maintable', 'cellspacing', '2');
     $css->setStyle('.maintable', 'cellspadding', '3');
     $css->setStyle('th', 'text-align', 'center');
     $css->setStyle('th', 'color', '#FFC');
     $css->setStyle('th', 'background-color', '#AAA');
     $css->setStyle('th', 'white-space', 'nowrap');
     $css->setStyle('input', 'font-family', 'Verdana, Arial, helvetica');
     $css->setStyle('input.flat', 'border-style', 'solid');
     $css->setStyle('input.flat', 'border-width', '2px 2px 0px 2px');
     $css->setStyle('input.flat', 'border-color', '#996');
     // on preview tab, add progress bar javascript and stylesheet
     if ($pageName == $tabPreview[0][0]) {
         $bar = $page->controller->createProgressBar();
         $p->addStyleDeclaration($css->toString() . $bar->getStyle());
         $p->addScriptDeclaration($bar->getScript());
         $barElement =& $page->getElement('progressBar');
         $barElement->setText($bar->toHtml());
     } else {
         $p->addStyleDeclaration($css->toString());
     }
     $renderer =& $page->defaultRenderer();
     $renderer->setFormTemplate('<table class="maintable"><form{attributes}>{content}</form></table>');
     $renderer->setHeaderTemplate('<tr><th colspan="2">{header}</th></tr>');
     $renderer->setGroupTemplate('<table><tr>{content}</tr></table>', 'name');
     $renderer->setGroupElementTemplate('<td>{element}<br /><span style="font-size:10px;"><span class="label">{label}</span></span></td>', 'name');
     $page->accept($renderer);
     $p->addBodyContent($renderer->toHtml());
     $p->display();
 }