HOME>WEBプログラム覚書>PEAR::HTML_CSSを使ってみる
PEAR::HTML_CSSを使ってみる
CSSを処理したり生成したりするためのPEAR::HTML_CSSを使ってみる。使う機会はあまりなさそうですが。
CSSファイルを変更
style.css
内容の取得変更するCSS
PHP
<?php
@CHARSET "utf-8";
h1 {
font-size: 160%;
}
h2 {
font-size: 130%;
}
h3 {
font-size: 130%;
}
#html_css {
width: 300px;
height: 200px;
padding: 1em;
background: #CCCCCC;
}
?>
テストコード
PHP
<?php
require_once 'HTML/CSS.php';
define('DIR_CSS', '/css/');
$css_filename = DIR_CSS . 'style.css';
// コンストラクタ
$css = new HTML_CSS();
// test.cssの内容をパース
$result = $css->parseFile($css_filename);
if (is_null($result)) {
// 横幅変更
$css->setStyle('#html_css', 'width', '500px');
//新しいセレクタの追加
$css->setStyle('#html_css_add', 'width', '200px');
// 見出しをまとめて設定する
// 既にCSS記述してあるhxを取得
$hxs = array_keys($css->grepStyle('/^h[1-6]$/'));
$group_selector = implode(', ', $hxs);
if (!array_key_exists($group_selector, $css->toArray())) {
// グループ化
$group_id = $css->createGroup(implode(',', $hxs));
// グループに共通のスタイルを設定
$css->setGroupStyle($group_id, 'padding', '0px');
$css->setGroupStyle($group_id, 'font-weight', 'bigger');
// PHP5系なら妥当性チェック可能
echo '<h4>Validate結果</h4>';
$message = array();
var_dump('結果は? ', $css->validate(array($css->toString()), $message));
var_dump('<pre>', $message, '</pre>');
echo '<hr />';
// inVaildなので修正
$css->setGroupStyle($group_id, 'font-weight', 'normal');
// 再度確認
echo '<h4>2回目:Validate結果</h4>';
var_dump('結果は? ', $css->validate(array($css->toString()), $message));
// OKなので書き込むデータの確認
echo '<h4>書き込みデータ</h4>';
echo '<pre>';
echo $css->display();
echo '</pre>';
echo '<hr />';
// 上書き保存
$css->toFile($css_filename);
// 1行にまとめて新規保存
$css->setSingleLineOutput(true);
$css->setLineEnd('');
$css->toFile(DIR_CSS . 'compress.css');
}
} else {
throw new Exception($result->getType() . '::' . $result->getMessage());
}
?>
実行結果
Validate結果
string(13) "結果は? " bool(false) string(5) "
"
array(2) {
["errors"]=>
array(1) {
[0]=>
array(6) {
["errortype"]=>
string(11) "parse-error"
["context"]=>
string(12) " h1, h2, h3 "
["property"]=>
NULL
["uri"]=>
NULL
["line"]=>
string(1) "5"
["message"]=>
string(235) "
Value Error : font-weight (http://www.w3.org/TR/CSS21/fonts.html#propdef-font-weight)
bigger is not a font-weight value :
"
}
}
["warnings"]=>
array(0) {
}
}
string(6) "
"
2回目:Validate結果
string(13) "結果は? " bool(true)
書き込みデータ
@charset "utf-8";
h1, h2, h3 {
padding: 0px;
font-weight: normal;
}
h1 {
font-size: 160%;
}
h2 {
font-size: 130%;
}
h3 {
font-size: 130%;
}
#html_css {
width: 500px;
height: 200px;
padding: 1em;
background: #CCCCCC;
}
#html_css_add {
width: 200px;
}
一行にまとめたcompress.cssは下記のようになります。
PHP
<?php
@charset "utf-8";h1, h2, h3 { padding: 0px; font-weight: normal;}h1 { font-size: 160%;}h2 { font-size: 130%;}h3 { font-size: 130%;}#html_css { width: 500px; height: 200px; padding: 1em; background: #CCCCCC;}#html_css_add { width: 200px;}
?>
※Windows環境でグループとか使ってtoFile()で上書きすると結果がおかしくなる場合があるっぽい。
CMSとかジェネレータ作る時とかつかえそうです。あとは他のサイトの調査に使えます。
W3CのCSS確認
PHP
<?php
require_once 'HTML/CSS.php';
$css = new HTML_CSS();
$result = $css->parseFile(file_get_contents('http://www.w3.org/2008/site/css/minimum'));
$message = array();
var_dump('結果は? ', $css->validate(array($css->toString()), $message));
?>
おっ!?vaildだw
投稿日 |
2009年11月22日 16:01 |
カテゴリ |
PHP |
タグ |
PEAR | サンプルコード | テストコード |
トラックバック URL |
http://www.kantenna.com/cgi-bin/mt504/mt-tb.cgi/1195 |
コメント