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