| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of EC-CUBE |
|---|
| 4 | * |
|---|
| 5 | * Copyright(c) 2000-2012 LOCKON CO.,LTD. All Rights Reserved. |
|---|
| 6 | * |
|---|
| 7 | * http://www.lockon.co.jp/ |
|---|
| 8 | * |
|---|
| 9 | * This program is free software; you can redistribute it and/or |
|---|
| 10 | * modify it under the terms of the GNU General Public License |
|---|
| 11 | * as published by the Free Software Foundation; either version 2 |
|---|
| 12 | * of the License, or (at your option) any later version. |
|---|
| 13 | * |
|---|
| 14 | * This program is distributed in the hope that it will be useful, |
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | * GNU General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU General Public License |
|---|
| 20 | * along with this program; if not, write to the Free Software |
|---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * テンプレートをDOM変形するためのヘルパークラス |
|---|
| 26 | * |
|---|
| 27 | * @package Helper |
|---|
| 28 | * @version $Id$ |
|---|
| 29 | */ |
|---|
| 30 | class SC_Helper_Transform { |
|---|
| 31 | protected $objDOM; |
|---|
| 32 | protected $arrSmartyTagsOrg; |
|---|
| 33 | protected $arrSmartyTagsSub; |
|---|
| 34 | protected $smarty_tags_idx; |
|---|
| 35 | protected $arrErr; |
|---|
| 36 | protected $arrElementTree; |
|---|
| 37 | protected $arrSelectElements; |
|---|
| 38 | protected $html_source; |
|---|
| 39 | protected $header_source; |
|---|
| 40 | protected $footer_source; |
|---|
| 41 | protected $search_depth; |
|---|
| 42 | |
|---|
| 43 | const ERR_TARGET_ELEMENT_NOT_FOUND = 1; |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * SmartyのHTMLソースをDOMに変換しておく |
|---|
| 47 | * |
|---|
| 48 | * @param string $source 変形対象のテンプレート |
|---|
| 49 | * @return void |
|---|
| 50 | */ |
|---|
| 51 | public function __construct($source) { |
|---|
| 52 | $this->objDOM = new DOMDocument(); |
|---|
| 53 | $this->objDOM->strictErrorChecking = false; |
|---|
| 54 | $this->snip_count = 0; |
|---|
| 55 | $this->smarty_tags_idx = 0; |
|---|
| 56 | $this->arrErr = array(); |
|---|
| 57 | $this->arrElementTree = array(); |
|---|
| 58 | $this->arrSelectElements = array(); |
|---|
| 59 | $this->html_source = $source; |
|---|
| 60 | $this->header_source = NULL; |
|---|
| 61 | $this->footer_source = NULL; |
|---|
| 62 | $this->search_depth = 0; |
|---|
| 63 | |
|---|
| 64 | $encoding = mb_detect_encoding($source); |
|---|
| 65 | if (!in_array($encoding, array('ASCII', 'UTF-8'))) { |
|---|
| 66 | if ($encoding === false) { |
|---|
| 67 | $encoding = t("c_Not detectable_01"); |
|---|
| 68 | } |
|---|
| 69 | $msg = t('c_The templates character code is T_ARG1. UTF 8 can only be used._01', array('T_ARG1' => $encoding)); |
|---|
| 70 | SC_Utils_Ex::sfDispSiteError(FREE_ERROR_MSG, '', true, $msg); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | // Smartyのコメントを削除 |
|---|
| 74 | $source = preg_replace( |
|---|
| 75 | '/<\!--{\*.+?\*\}-->/s', |
|---|
| 76 | '', |
|---|
| 77 | $source |
|---|
| 78 | ); |
|---|
| 79 | |
|---|
| 80 | // headタグの内側を退避 |
|---|
| 81 | $source = preg_replace_callback( |
|---|
| 82 | '/(<head[^>]*>)(.+)(<\/head>)/is', |
|---|
| 83 | array($this, 'lfCaptureHeadTags2Comment'), |
|---|
| 84 | $source |
|---|
| 85 | ); |
|---|
| 86 | |
|---|
| 87 | // JavaScript内にSmartyのタグが存在するものを、コメント形式に置換 |
|---|
| 88 | $source = preg_replace_callback( |
|---|
| 89 | '/<script.+?\/script>/s', |
|---|
| 90 | array($this, 'lfCaptureSmartyTags2Comment'), |
|---|
| 91 | $source |
|---|
| 92 | ); |
|---|
| 93 | |
|---|
| 94 | // HTMLタグ内にSmartyのタグが存在するものを、まず置換する |
|---|
| 95 | $source = preg_replace_callback( |
|---|
| 96 | '/<(?:[^<>]*?(?:(<\!--\{.+?\}-->)|(?R))[^<>]*?)*?>/s', |
|---|
| 97 | array($this, 'lfCaptureSmartyTagsInTag'), |
|---|
| 98 | $source |
|---|
| 99 | ); |
|---|
| 100 | |
|---|
| 101 | // 通常のノードに属する部分を、コメント形式に置換 |
|---|
| 102 | $source = preg_replace_callback( |
|---|
| 103 | '/<\!--{.+?\}-->/s', |
|---|
| 104 | array($this, 'lfCaptureSmartyTags2Comment'), |
|---|
| 105 | $source |
|---|
| 106 | ); |
|---|
| 107 | |
|---|
| 108 | // HTMLタグの有無、BODYタグの有無で動作を切り替える |
|---|
| 109 | if (preg_match('/^(.*?)(<html[^>]*>.+<\/html>)(.*?)$/is', $source, $arrMatches)) { |
|---|
| 110 | $this->header_source = $arrMatches[1]; |
|---|
| 111 | $source = $arrMatches[2]; |
|---|
| 112 | $this->footer_source = $arrMatches[3]; |
|---|
| 113 | } elseif (preg_match('/^.*?<body[^>]*>.+<\/body>.*$/is', $source)) { |
|---|
| 114 | $source = '<meta http-equiv="content-type" content="text/html; charset=UTF-8" /><html><!--TemplateTransformer start-->'.$source.'<!--TemplateTransformer end--></html>'; |
|---|
| 115 | } |
|---|
| 116 | else { |
|---|
| 117 | $source = '<meta http-equiv="content-type" content="text/html; charset=UTF-8" /><html><body><!--TemplateTransformer start-->'.$source.'<!--TemplateTransformer end--></body></html>'; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | @$this->objDOM->loadHTML($source); |
|---|
| 121 | $this->lfScanChild($this->objDOM); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | * jQueryライクなセレクタを用いてエレメントを選択する |
|---|
| 127 | * |
|---|
| 128 | * @param string $selector セレクタ |
|---|
| 129 | * @param integer $index インデックス(指定がある場合) |
|---|
| 130 | * @param boolean $require エレメントが見つからなかった場合、エラーとするか |
|---|
| 131 | * @param string $err_msg エラーメッセージ |
|---|
| 132 | * @return SC_Helper_Transformオブジェクト |
|---|
| 133 | */ |
|---|
| 134 | public function select($selector, $index = NULL, $require = true, $err_msg = NULL) { |
|---|
| 135 | $this->arrSelectElements = array(); |
|---|
| 136 | $this->search_depth = 0; |
|---|
| 137 | |
|---|
| 138 | $regex = $this->lfSelector2Regex($selector); // セレクタをツリー検索用正規表現に変換 |
|---|
| 139 | |
|---|
| 140 | $cur_idx = 0; |
|---|
| 141 | // ツリーを初めから全検索する |
|---|
| 142 | for ($iLoop=0; $iLoop < count($this->arrElementTree); $iLoop++) { |
|---|
| 143 | if (preg_match($regex, $this->arrElementTree[$iLoop][0])) { |
|---|
| 144 | // インデックスが指定されていない(見つけたエレメント全て)、もしくは指定されたインデックスなら選択する |
|---|
| 145 | if (is_null($index) || $cur_idx == $index) { |
|---|
| 146 | $this->lfAddElement($iLoop, $this->arrElementTree[$iLoop]); |
|---|
| 147 | } |
|---|
| 148 | $cur_idx++; |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | // 見つからなかった場合エラーとするならエラーを記録する |
|---|
| 153 | if ($require && $cur_idx == 0) { |
|---|
| 154 | $this->lfSetError( |
|---|
| 155 | $selector, |
|---|
| 156 | self::ERR_TARGET_ELEMENT_NOT_FOUND, |
|---|
| 157 | $err_msg |
|---|
| 158 | ); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | return $this; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | /** |
|---|
| 166 | * jQueryライクなセレクタを用いて、選択したエレメント内をさらに絞り込む |
|---|
| 167 | * |
|---|
| 168 | * @param string $selector セレクタ |
|---|
| 169 | * @param integer $index インデックス(指定がある場合) |
|---|
| 170 | * @param boolean $require エレメントが見つからなかった場合、エラーとするか |
|---|
| 171 | * @param string $err_msg エラーメッセージ |
|---|
| 172 | * @return SC_Helper_Transformオブジェクト |
|---|
| 173 | */ |
|---|
| 174 | public function find($selector, $index = NULL, $require = true, $err_msg = NULL) { |
|---|
| 175 | $arrParentElements = $this->arrSelectElements[$this->search_depth]; |
|---|
| 176 | $this->search_depth++; |
|---|
| 177 | $this->arrSelectElements[$this->search_depth] = array(); |
|---|
| 178 | |
|---|
| 179 | foreach ($arrParentElements as $key => &$objElement) { |
|---|
| 180 | $regex = $this->lfSelector2Regex($selector, $objElement[0]); // セレクタをツリー検索用正規表現に変換(親要素のセレクタを頭に付ける) |
|---|
| 181 | |
|---|
| 182 | $cur_idx = 0; |
|---|
| 183 | // 親エレメント位置からツリーを検索する |
|---|
| 184 | for ($iLoop=$objElement[0]; $iLoop < count($this->arrElementTree); $iLoop++) { |
|---|
| 185 | if (preg_match($regex, $this->arrElementTree[$iLoop][0])) { |
|---|
| 186 | // インデックスが指定されていない(見つけたエレメント全て)、もしくは指定されたインデックスなら選択する |
|---|
| 187 | if (is_null($index) || $cur_idx == $index) { |
|---|
| 188 | $this->lfAddElement($iLoop, $this->arrElementTree[$iLoop]); |
|---|
| 189 | } |
|---|
| 190 | $cur_idx++; |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | // 見つからなかった場合エラーとするならエラーを記録する |
|---|
| 196 | if ($require && count($this->arrSelectElements[$this->search_depth]) == 0) { |
|---|
| 197 | $this->lfSetError( |
|---|
| 198 | $selector, |
|---|
| 199 | self::ERR_TARGET_ELEMENT_NOT_FOUND, |
|---|
| 200 | $err_msg |
|---|
| 201 | ); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | return $this; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | /** |
|---|
| 209 | * 選択状態を指定数戻す |
|---|
| 210 | * |
|---|
| 211 | * @param int $back_num 選択状態を戻す数 |
|---|
| 212 | * @return SC_Helper_Transformオブジェクト |
|---|
| 213 | */ |
|---|
| 214 | public function end($back_num = 1) { |
|---|
| 215 | if ($this->search_depth >= $back_num) { |
|---|
| 216 | $this->search_depth -= $back_num; |
|---|
| 217 | } else { |
|---|
| 218 | $this->search_depth = 0; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | return $this; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | /** |
|---|
| 226 | * 要素の前にHTMLを挿入 |
|---|
| 227 | * |
|---|
| 228 | * @param string $html_snip 挿入するHTMLの断片 |
|---|
| 229 | * @return SC_Helper_Transformオブジェクト |
|---|
| 230 | */ |
|---|
| 231 | public function insertBefore($html_snip) { |
|---|
| 232 | foreach ($this->arrSelectElements[$this->search_depth] as $key => $objElement) { |
|---|
| 233 | $this->lfSetTransform('insertBefore', $objElement[0], $html_snip); |
|---|
| 234 | } |
|---|
| 235 | return $this; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | /** |
|---|
| 240 | * 要素の後にHTMLを挿入 |
|---|
| 241 | * |
|---|
| 242 | * @param string $html_snip 挿入するHTMLの断片 |
|---|
| 243 | * @return SC_Helper_Transformオブジェクト |
|---|
| 244 | */ |
|---|
| 245 | public function insertAfter($html_snip) { |
|---|
| 246 | foreach ($this->arrSelectElements[$this->search_depth] as $key => $objElement) { |
|---|
| 247 | $this->lfSetTransform('insertAfter', $objElement[0], $html_snip); |
|---|
| 248 | } |
|---|
| 249 | return $this; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | /** |
|---|
| 254 | * 要素の先頭にHTMLを挿入 |
|---|
| 255 | * |
|---|
| 256 | * @param string $html_snip 挿入するHTMLの断片 |
|---|
| 257 | * @return SC_Helper_Transformオブジェクト |
|---|
| 258 | */ |
|---|
| 259 | public function appendFirst($html_snip) { |
|---|
| 260 | foreach ($this->arrSelectElements[$this->search_depth] as $key => $objElement) { |
|---|
| 261 | $this->lfSetTransform('appendFirst', $objElement[0], $html_snip); |
|---|
| 262 | } |
|---|
| 263 | return $this; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | /** |
|---|
| 268 | * 要素の末尾にHTMLを挿入 |
|---|
| 269 | * |
|---|
| 270 | * @param string $html_snip 挿入するHTMLの断片 |
|---|
| 271 | * @return SC_Helper_Transformオブジェクト |
|---|
| 272 | */ |
|---|
| 273 | public function appendChild($html_snip) { |
|---|
| 274 | foreach ($this->arrSelectElements[$this->search_depth] as $key => $objElement) { |
|---|
| 275 | $this->lfSetTransform('appendChild', $objElement[0], $html_snip); |
|---|
| 276 | } |
|---|
| 277 | return $this; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | /** |
|---|
| 282 | * 要素を指定したHTMLに置換 |
|---|
| 283 | * |
|---|
| 284 | * @param string $html_snip 置換後のHTMLの断片 |
|---|
| 285 | * @return SC_Helper_Transformオブジェクト |
|---|
| 286 | */ |
|---|
| 287 | public function replaceElement($html_snip) { |
|---|
| 288 | foreach ($this->arrSelectElements[$this->search_depth] as $key => &$objElement) { |
|---|
| 289 | $this->lfSetTransform('replaceElement', $objElement[0], $html_snip); |
|---|
| 290 | } |
|---|
| 291 | return $this; |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | /** |
|---|
| 296 | * 要素を削除する |
|---|
| 297 | * |
|---|
| 298 | * @return SC_Helper_Transformオブジェクト |
|---|
| 299 | */ |
|---|
| 300 | public function removeElement() { |
|---|
| 301 | foreach ($this->arrSelectElements[$this->search_depth] as $key => &$objElement) { |
|---|
| 302 | $this->lfSetTransform('replaceElement', $objElement[0], ''); |
|---|
| 303 | } |
|---|
| 304 | return $this; |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | /** |
|---|
| 309 | * HTMLに戻して、Transform用に付けたマーカーを削除し、Smartyのタグを復元する |
|---|
| 310 | * |
|---|
| 311 | * @return string トランスフォーム済みHTML。まったくトランスフォームが行われなかった場合は元のHTMLを返す。。 |
|---|
| 312 | */ |
|---|
| 313 | public function getHTML() { |
|---|
| 314 | if (count($this->arrErr)) { |
|---|
| 315 | // エラーメッセージ組み立て |
|---|
| 316 | $err_msg = ''; |
|---|
| 317 | foreach ($this->arrErr as $arrErr) { |
|---|
| 318 | if ($arrErr['err_msg']) { |
|---|
| 319 | $err_msg .= '<br />'.$arrErr['err_msg']; |
|---|
| 320 | } else { |
|---|
| 321 | if ($arrErr['type'] == self::ERR_TARGET_ELEMENT_NOT_FOUND) { |
|---|
| 322 | $err_msg .= "<br />${arrErr['selector']} が存在しません"; |
|---|
| 323 | } else { |
|---|
| 324 | $err_msg .= '<br />'.print_r($arrErr, true); |
|---|
| 325 | } |
|---|
| 326 | } |
|---|
| 327 | } |
|---|
| 328 | // エラー画面表示 |
|---|
| 329 | SC_Utils_Ex::sfDispSiteError(FREE_ERROR_MSG, '', true, t('c_Template operation failed. T_ARG1_01', array('T_ARG1' => $err_msg))); |
|---|
| 330 | } elseif ($this->snip_count) { |
|---|
| 331 | $html = $this->objDOM->saveHTML(); |
|---|
| 332 | $html = preg_replace('/^.*(<html[^>]*>)/s', '$1', $html); |
|---|
| 333 | $html = preg_replace('/(<\/html>).*$/s', '$1', $html); |
|---|
| 334 | $html = preg_replace('/^.*<\!--TemplateTransformer start-->/s', '', $html); |
|---|
| 335 | $html = preg_replace('/<\!--TemplateTransformer end-->.*$/s', '', $html); |
|---|
| 336 | $html = preg_replace( |
|---|
| 337 | '/<\!--TemplateTransformerSnip start-->.*?<\!--TemplateTransformerSnip end-->/s', |
|---|
| 338 | '', |
|---|
| 339 | $html |
|---|
| 340 | ); |
|---|
| 341 | $html = $this->header_source.$html.$this->footer_source; |
|---|
| 342 | $html = str_replace($this->arrSmartyTagsSub, $this->arrSmartyTagsOrg, $html); |
|---|
| 343 | return $html; |
|---|
| 344 | } else { |
|---|
| 345 | return $this->html_source; |
|---|
| 346 | } |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | /** |
|---|
| 353 | * DOMの処理の邪魔になるSmartyのタグを代理文字に置換する preg_replace_callback のコールバック関数 |
|---|
| 354 | * |
|---|
| 355 | * コメント形式への置換 |
|---|
| 356 | * |
|---|
| 357 | * @param array $arrMatches マッチしたタグの情報 |
|---|
| 358 | * @return string 代わりの文字列 |
|---|
| 359 | */ |
|---|
| 360 | protected function lfCaptureSmartyTags2Comment(array $arrMatches) { |
|---|
| 361 | $substitute_tag = sprintf('<!--###%08d###-->', $this->smarty_tags_idx); |
|---|
| 362 | $this->arrSmartyTagsOrg[$this->smarty_tags_idx] = $arrMatches[0]; |
|---|
| 363 | $this->arrSmartyTagsSub[$this->smarty_tags_idx] = $substitute_tag; |
|---|
| 364 | $this->smarty_tags_idx++; |
|---|
| 365 | return $substitute_tag; |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | /** |
|---|
| 370 | * DOMの処理の邪魔になるSmartyのタグを代理文字に置換する preg_replace_callback のコールバック関数 |
|---|
| 371 | * |
|---|
| 372 | * コメント形式への置換 |
|---|
| 373 | * |
|---|
| 374 | * @param array $arrMatches マッチしたタグの情報 |
|---|
| 375 | * @return string 代わりの文字列 |
|---|
| 376 | */ |
|---|
| 377 | protected function lfCaptureHeadTags2Comment(array $arrMatches) { |
|---|
| 378 | $substitute_tag = sprintf('<!--###%08d###-->', $this->smarty_tags_idx); |
|---|
| 379 | $this->arrSmartyTagsOrg[$this->smarty_tags_idx] = $arrMatches[2]; |
|---|
| 380 | $this->arrSmartyTagsSub[$this->smarty_tags_idx] = $substitute_tag; |
|---|
| 381 | $this->smarty_tags_idx++; |
|---|
| 382 | |
|---|
| 383 | // 文字化け防止用のMETAを入れておく |
|---|
| 384 | $content_type_tag = '<!--TemplateTransformerSnip start-->'; |
|---|
| 385 | $content_type_tag .= '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />'; |
|---|
| 386 | $content_type_tag .= '<!--TemplateTransformerSnip end-->'; |
|---|
| 387 | |
|---|
| 388 | return $arrMatches[1].$content_type_tag.$substitute_tag.$arrMatches[3]; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | /** |
|---|
| 393 | * DOMの処理の邪魔になるSmartyのタグを代理文字に置換する preg_replace_callback のコールバック関数 |
|---|
| 394 | * |
|---|
| 395 | * HTMLエレメント内部の処理 |
|---|
| 396 | * |
|---|
| 397 | * @param array $arrMatches マッチしたタグの情報 |
|---|
| 398 | * @return string 代わりの文字列 |
|---|
| 399 | */ |
|---|
| 400 | protected function lfCaptureSmartyTagsInTag(array $arrMatches) { |
|---|
| 401 | // Smartyタグ内のクォートを処理しやすいよう、いったんダミーのタグに |
|---|
| 402 | $html = preg_replace_callback('/<\!--{.+?\}-->/s', array($this, 'lfCaptureSmartyTags2Temptag'), $arrMatches[0]); |
|---|
| 403 | $html = preg_replace_callback('/\"[^"]*?\"/s', array($this, 'lfCaptureSmartyTagsInQuote'), $html); |
|---|
| 404 | $html = preg_replace_callback('/###TEMP(\d{8})###/s', array($this, 'lfCaptureSmartyTags2Attr'), $html); |
|---|
| 405 | return $html; |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | /** |
|---|
| 410 | * DOMの処理の邪魔になるSmartyのタグを代理文字に置換する preg_replace_callback のコールバック関数 |
|---|
| 411 | * |
|---|
| 412 | * ダミーへの置換実行 |
|---|
| 413 | * |
|---|
| 414 | * @param array $arrMatches マッチしたタグの情報 |
|---|
| 415 | * @return string 代わりの文字列 |
|---|
| 416 | */ |
|---|
| 417 | protected function lfCaptureSmartyTags2Temptag(array $arrMatches) { |
|---|
| 418 | $substitute_tag = sprintf('###TEMP%08d###', $this->smarty_tags_idx); |
|---|
| 419 | $this->arrSmartyTagsOrg[$this->smarty_tags_idx] = $arrMatches[0]; |
|---|
| 420 | $this->arrSmartyTagsSub[$this->smarty_tags_idx] = $substitute_tag; |
|---|
| 421 | $this->smarty_tags_idx++; |
|---|
| 422 | return $substitute_tag; |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | /** |
|---|
| 427 | * DOMの処理の邪魔になるSmartyのタグを代理文字に置換する preg_replace_callback のコールバック関数 |
|---|
| 428 | * |
|---|
| 429 | * クォート内(=属性値)内にあるSmartyタグ(ダミーに置換済み)を、テキストに置換 |
|---|
| 430 | * |
|---|
| 431 | * @param array $arrMatches マッチしたタグの情報 |
|---|
| 432 | * @return string 代わりの文字列 |
|---|
| 433 | */ |
|---|
| 434 | protected function lfCaptureSmartyTagsInQuote(array $arrMatches) { |
|---|
| 435 | $html = preg_replace_callback( |
|---|
| 436 | '/###TEMP(\d{8})###/s', |
|---|
| 437 | array($this, 'lfCaptureSmartyTags2Value'), |
|---|
| 438 | $arrMatches[0] |
|---|
| 439 | ); |
|---|
| 440 | return $html; |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | |
|---|
| 444 | /** |
|---|
| 445 | * DOMの処理の邪魔になるSmartyのタグを代理文字に置換する preg_replace_callback のコールバック関数 |
|---|
| 446 | * |
|---|
| 447 | * テキストへの置換実行 |
|---|
| 448 | * |
|---|
| 449 | * @param array $arrMatches マッチしたタグの情報 |
|---|
| 450 | * @return string 代わりの文字列 |
|---|
| 451 | */ |
|---|
| 452 | protected function lfCaptureSmartyTags2Value(array $arrMatches) { |
|---|
| 453 | $tag_idx = (int)$arrMatches[1]; |
|---|
| 454 | $substitute_tag = sprintf('###%08d###', $tag_idx); |
|---|
| 455 | $this->arrSmartyTagsSub[$tag_idx] = $substitute_tag; |
|---|
| 456 | return $substitute_tag; |
|---|
| 457 | } |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | /** |
|---|
| 461 | * DOMの処理の邪魔になるSmartyのタグを代理文字に置換する preg_replace_callback のコールバック関数 |
|---|
| 462 | * |
|---|
| 463 | * エレメント内部にあって、属性値ではないものを、ダミーの属性として置換 |
|---|
| 464 | * |
|---|
| 465 | * @param array $arrMatches マッチしたタグの情報 |
|---|
| 466 | * @return string 代わりの文字列 |
|---|
| 467 | */ |
|---|
| 468 | protected function lfCaptureSmartyTags2Attr(array $arrMatches) { |
|---|
| 469 | $tag_idx = (int)$arrMatches[1]; |
|---|
| 470 | $substitute_tag = sprintf('rel%08d="######"', $tag_idx); |
|---|
| 471 | $this->arrSmartyTagsSub[$tag_idx] = $substitute_tag; |
|---|
| 472 | return ' '.$substitute_tag.' '; // 属性はパース時にスペースが詰まるので、こちらにはスペースを入れておく |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | |
|---|
| 476 | /** |
|---|
| 477 | * DOM Element / Document を走査し、name、class別に分類する |
|---|
| 478 | * |
|---|
| 479 | * @param DOMNode $objDOMElement DOMNodeオブジェクト |
|---|
| 480 | * @return void |
|---|
| 481 | */ |
|---|
| 482 | protected function lfScanChild(DOMNode $objDOMElement, $parent_selector = '') { |
|---|
| 483 | $objNodeList = $objDOMElement->childNodes; |
|---|
| 484 | if (is_null($objNodeList)) return; |
|---|
| 485 | |
|---|
| 486 | foreach ($objNodeList as $element) { |
|---|
| 487 | // DOMElementのみ取り出す |
|---|
| 488 | if ($element instanceof DOMElement) { |
|---|
| 489 | $arrAttr = array(); |
|---|
| 490 | $arrAttr[] = $element->tagName; |
|---|
| 491 | if (method_exists($element, 'getAttribute')) { |
|---|
| 492 | // idを持っていればidを付加する |
|---|
| 493 | if ($element->hasAttribute('id')) |
|---|
| 494 | $arrAttr[] = '#'.$element->getAttribute('id'); |
|---|
| 495 | // classを持っていればclassを付加する(複数の場合は複数付加する) |
|---|
| 496 | if ($element->hasAttribute('class')) { |
|---|
| 497 | $arrClasses = preg_split('/\s+/', $element->getAttribute('class')); |
|---|
| 498 | foreach ($arrClasses as $classname) $arrAttr[] = '.'.$classname; |
|---|
| 499 | } |
|---|
| 500 | } |
|---|
| 501 | // 親要素のセレクタを付けてツリーへ登録する |
|---|
| 502 | $this_selector = $parent_selector.' '.implode('', $arrAttr); |
|---|
| 503 | $this->arrElementTree[] = array($this_selector, $element); |
|---|
| 504 | // エレメントが子孫要素を持っていればさらに調べる |
|---|
| 505 | if ($element->hasChildNodes()) $this->lfScanChild($element, $this_selector); |
|---|
| 506 | } |
|---|
| 507 | } |
|---|
| 508 | } |
|---|
| 509 | |
|---|
| 510 | |
|---|
| 511 | /** |
|---|
| 512 | * セレクタ文字列をツリー検索用の正規表現に変換する |
|---|
| 513 | * |
|---|
| 514 | * @param string $selector セレクタ |
|---|
| 515 | * @param string $parent_index セレクタ検索時の親要素の位置(子孫要素検索のため) |
|---|
| 516 | * @return string 正規表現文字列 |
|---|
| 517 | */ |
|---|
| 518 | protected function lfSelector2Regex($selector, $parent_index = NULL){ |
|---|
| 519 | // jQueryライクなセレクタを正規表現に |
|---|
| 520 | $selector = preg_replace('/ *> */', ' >', $selector); // 子セレクタをツリー検索用に 「A >B」の記法にする |
|---|
| 521 | $regex = '/'; |
|---|
| 522 | if (!is_null($parent_index)) $regex .= preg_quote($this->arrElementTree[$parent_index][0], '/'); // (親要素の指定(絞り込み時)があれば頭に付加する(特殊文字はエスケープ) |
|---|
| 523 | $arrSelectors = explode(' ', $selector); |
|---|
| 524 | foreach ($arrSelectors as $sub_selector) { |
|---|
| 525 | if (preg_match('/^(>?)([\w\-]+)?(#[\w\-]+)?(\.[\w\-]+)*$/', $sub_selector, $arrMatch)) { |
|---|
| 526 | // 子セレクタ |
|---|
| 527 | if (isset($arrMatch[1]) && $arrMatch[1]) $regex .= ' '; |
|---|
| 528 | else $regex .= '.* '; |
|---|
| 529 | // タグ名 |
|---|
| 530 | if (isset($arrMatch[2]) && $arrMatch[2]) $regex .= preg_quote($arrMatch[2], '/'); |
|---|
| 531 | else $regex .= '([\w\-]+)?'; |
|---|
| 532 | // id |
|---|
| 533 | if (isset($arrMatch[3]) && $arrMatch[3]) $regex .= preg_quote($arrMatch[3], '/'); |
|---|
| 534 | else $regex .= '(#(\w|\-|#{3}[0-9]{8}#{3})+)?'; |
|---|
| 535 | // class |
|---|
| 536 | if (isset($arrMatch[4]) && $arrMatch[4]) $regex .= '(\.(\w|\-|#{3}[0-9]{8}#{3})+)*'.preg_quote($arrMatch[4], '/').'(\.(\w|\-|#{3}[0-9]{8}#{3})+)*'; // class指定の時は前後にもclassが付いているかもしれない |
|---|
| 537 | else $regex .= '(\.(\w|\-|#{3}[0-9]{8}#{3})+)*'; |
|---|
| 538 | } |
|---|
| 539 | } |
|---|
| 540 | $regex .= '$/i'; |
|---|
| 541 | |
|---|
| 542 | return $regex; |
|---|
| 543 | } |
|---|
| 544 | |
|---|
| 545 | |
|---|
| 546 | /** |
|---|
| 547 | * 見つかった要素をプロパティに登録 |
|---|
| 548 | * |
|---|
| 549 | * @param integer $elementNo エレメントのインデックス |
|---|
| 550 | * @param array $arrElement インデックスとDOMオブジェクトをペアとした配列 |
|---|
| 551 | * @return void |
|---|
| 552 | */ |
|---|
| 553 | protected function lfAddElement($elementNo, array &$arrElement) { |
|---|
| 554 | if (is_array($this->arrSelectElements[$this->search_depth]) && array_key_exists($arrElement[0], $this->arrSelectElements[$this->search_depth])) { |
|---|
| 555 | //nop |
|---|
| 556 | } else { |
|---|
| 557 | $this->arrSelectElements[$this->search_depth][$arrElement[0]] = array($elementNo, &$arrElement[1]); |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | } |
|---|
| 561 | |
|---|
| 562 | |
|---|
| 563 | /** |
|---|
| 564 | * DOMを用いた変形を実行する |
|---|
| 565 | * |
|---|
| 566 | * @param string $mode 実行するメソッドの種類 |
|---|
| 567 | * @param string $target_key 対象のエレメントの完全なセレクタ |
|---|
| 568 | * @param string $html_snip HTMLコード |
|---|
| 569 | * @return boolean |
|---|
| 570 | */ |
|---|
| 571 | protected function lfSetTransform($mode, $target_key, $html_snip) { |
|---|
| 572 | |
|---|
| 573 | $substitute_tag = sprintf('<!--###%08d###-->', $this->smarty_tags_idx); |
|---|
| 574 | $this->arrSmartyTagsOrg[$this->smarty_tags_idx] = $html_snip; |
|---|
| 575 | $this->arrSmartyTagsSub[$this->smarty_tags_idx] = $substitute_tag; |
|---|
| 576 | $this->smarty_tags_idx++; |
|---|
| 577 | |
|---|
| 578 | $this->objDOM->createDocumentFragment(); |
|---|
| 579 | $objSnip = $this->objDOM->createDocumentFragment(); |
|---|
| 580 | $objSnip->appendXML($substitute_tag); |
|---|
| 581 | |
|---|
| 582 | $objElement = false; |
|---|
| 583 | if (isset($this->arrElementTree[$target_key]) && $this->arrElementTree[$target_key][0]) { |
|---|
| 584 | $objElement = &$this->arrElementTree[$target_key][1]; |
|---|
| 585 | } |
|---|
| 586 | |
|---|
| 587 | if (!$objElement) return false; |
|---|
| 588 | |
|---|
| 589 | try { |
|---|
| 590 | switch ($mode) { |
|---|
| 591 | case 'appendFirst': |
|---|
| 592 | if ($objElement->hasChildNodes()) { |
|---|
| 593 | $objElement->insertBefore($objSnip, $objElement->firstChild); |
|---|
| 594 | } else { |
|---|
| 595 | $objElement->appendChild($objSnip); |
|---|
| 596 | } |
|---|
| 597 | break; |
|---|
| 598 | case 'appendChild': |
|---|
| 599 | $objElement->appendChild($objSnip); |
|---|
| 600 | break; |
|---|
| 601 | case 'insertBefore': |
|---|
| 602 | if (!is_object($objElement->parentNode)) return false; |
|---|
| 603 | $objElement->parentNode->insertBefore($objSnip, $objElement); |
|---|
| 604 | break; |
|---|
| 605 | case 'insertAfter': |
|---|
| 606 | if ($objElement->nextSibling) { |
|---|
| 607 | $objElement->parentNode->insertBefore($objSnip, $objElement->nextSibling); |
|---|
| 608 | } else { |
|---|
| 609 | $objElement->parentNode->appendChild($objSnip); |
|---|
| 610 | } |
|---|
| 611 | break; |
|---|
| 612 | case 'replaceElement': |
|---|
| 613 | if (!is_object($objElement->parentNode)) return false; |
|---|
| 614 | $objElement->parentNode->replaceChild($objSnip, $objElement); |
|---|
| 615 | break; |
|---|
| 616 | default: |
|---|
| 617 | break; |
|---|
| 618 | } |
|---|
| 619 | $this->snip_count++; |
|---|
| 620 | } |
|---|
| 621 | catch (Exception $e) { |
|---|
| 622 | SC_Utils_Ex::sfDispSiteError(FREE_ERROR_MSG, '', true, t("c_Template operation failed._01")); |
|---|
| 623 | } |
|---|
| 624 | |
|---|
| 625 | return true; |
|---|
| 626 | } |
|---|
| 627 | |
|---|
| 628 | |
|---|
| 629 | /** |
|---|
| 630 | * セレクタエラーを記録する |
|---|
| 631 | * |
|---|
| 632 | * @param string $selector セレクタ |
|---|
| 633 | * @param integer $type エラーの種類 |
|---|
| 634 | * @param string $err_msg エラーメッセージ |
|---|
| 635 | * @return void |
|---|
| 636 | */ |
|---|
| 637 | protected function lfSetError($selector, $type, $err_msg = NULL) { |
|---|
| 638 | $this->arrErr[] = array( |
|---|
| 639 | 'selector' => $selector, |
|---|
| 640 | 'type' => $type, |
|---|
| 641 | 'err_msg' => $err_msg |
|---|
| 642 | ); |
|---|
| 643 | } |
|---|
| 644 | } |
|---|