source: branches/version-2_13-dev/data/class/helper/SC_Helper_Transform.php @ 22856

Revision 22856, 25.6 KB checked in by Seasoft, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.13.0)

  • 主に空白・空白行の調整。もう少し整えたいが、一旦現状コミット。
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2013 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 */
30class 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        }
118        else {
119            $source = '<meta http-equiv="content-type" content="text/html; charset=UTF-8" /><html><body><!--TemplateTransformer start-->'.$source.'<!--TemplateTransformer end--></body></html>';
120        }
121
122        @$this->objDOM->loadHTML($source);
123        $this->lfScanChild($this->objDOM);
124    }
125
126    /**
127     * jQueryライクなセレクタを用いてエレメントを選択する
128     *
129     * @param string  $selector      セレクタ
130     * @param integer $index         インデックス(指定がある場合)
131     * @param boolean $require       エレメントが見つからなかった場合、エラーとするか
132     * @param string  $err_msg       エラーメッセージ
133     * @return SC_Helper_Transformオブジェクト
134     */
135    public function select($selector, $index = NULL, $require = true, $err_msg = NULL)
136    {
137        $this->arrSelectElements = array();
138        $this->search_depth = 0;
139
140        $regex = $this->lfSelector2Regex($selector);    // セレクタをツリー検索用正規表現に変換
141
142        $cur_idx = 0;
143        // ツリーを初めから全検索する
144        for ($iLoop=0; $iLoop < count($this->arrElementTree); $iLoop++) {
145            if (preg_match($regex, $this->arrElementTree[$iLoop][0])) {
146                // インデックスが指定されていない(見つけたエレメント全て)、もしくは指定されたインデックスなら選択する
147                if (is_null($index) || $cur_idx == $index) {
148                    $this->lfAddElement($iLoop, $this->arrElementTree[$iLoop]);
149                }
150                $cur_idx++;
151            }
152        }
153
154        // 見つからなかった場合エラーとするならエラーを記録する
155        if ($require && $cur_idx == 0) {
156            $this->lfSetError(
157                $selector,
158                self::ERR_TARGET_ELEMENT_NOT_FOUND,
159                $err_msg
160            );
161        }
162
163        return $this;
164    }
165
166    /**
167     * jQueryライクなセレクタを用いて、選択したエレメント内をさらに絞り込む
168     *
169     * @param string  $selector      セレクタ
170     * @param integer $index         インデックス(指定がある場合)
171     * @param boolean $require       エレメントが見つからなかった場合、エラーとするか
172     * @param string  $err_msg       エラーメッセージ
173     * @return SC_Helper_Transformオブジェクト
174     */
175    public function find($selector, $index = NULL, $require = true, $err_msg = NULL)
176    {
177        $arrParentElements = $this->arrSelectElements[$this->search_depth];
178        $this->search_depth++;
179        $this->arrSelectElements[$this->search_depth] = array();
180
181        foreach ($arrParentElements as $key => &$objElement) {
182            $regex = $this->lfSelector2Regex($selector, $objElement[0]);    // セレクタをツリー検索用正規表現に変換(親要素のセレクタを頭に付ける)
183
184            $cur_idx = 0;
185            // 親エレメント位置からツリーを検索する
186            for ($iLoop=$objElement[0]; $iLoop < count($this->arrElementTree); $iLoop++) {
187                if (preg_match($regex, $this->arrElementTree[$iLoop][0])) {
188                    // インデックスが指定されていない(見つけたエレメント全て)、もしくは指定されたインデックスなら選択する
189                    if (is_null($index) || $cur_idx == $index) {
190                        $this->lfAddElement($iLoop, $this->arrElementTree[$iLoop]);
191                    }
192                    $cur_idx++;
193                }
194            }
195        }
196
197        // 見つからなかった場合エラーとするならエラーを記録する
198        if ($require && count($this->arrSelectElements[$this->search_depth]) == 0) {
199            $this->lfSetError(
200                $selector,
201                self::ERR_TARGET_ELEMENT_NOT_FOUND,
202                $err_msg
203            );
204        }
205
206        return $this;
207    }
208
209    /**
210     * 選択状態を指定数戻す
211     *
212     * @param int $back_num 選択状態を戻す数
213     * @return SC_Helper_Transformオブジェクト
214     */
215    public function end($back_num = 1)
216    {
217        if ($this->search_depth >= $back_num) {
218            $this->search_depth -= $back_num;
219        } else {
220            $this->search_depth = 0;
221        }
222
223        return $this;
224    }
225
226    /**
227     * 要素の前にHTMLを挿入
228     *
229     * @param string $html_snip 挿入するHTMLの断片
230     * @return SC_Helper_Transformオブジェクト
231     */
232    public function insertBefore($html_snip)
233    {
234        if (isset($this->arrSelectElements[$this->search_depth])) {
235            foreach ($this->arrSelectElements[$this->search_depth] as $key => $objElement) {
236                $this->lfSetTransform('insertBefore', $objElement[0], $html_snip);
237            }
238        }
239
240        return $this;
241    }
242
243    /**
244     * 要素の後にHTMLを挿入
245     *
246     * @param string $html_snip 挿入するHTMLの断片
247     * @return SC_Helper_Transformオブジェクト
248     */
249    public function insertAfter($html_snip)
250    {
251        if (isset($this->arrSelectElements[$this->search_depth])) {
252            foreach ($this->arrSelectElements[$this->search_depth] as $key => $objElement) {
253                $this->lfSetTransform('insertAfter', $objElement[0], $html_snip);
254            }
255        }
256
257        return $this;
258    }
259
260    /**
261     * 要素の先頭にHTMLを挿入
262     *
263     * @param string $html_snip 挿入するHTMLの断片
264     * @return SC_Helper_Transformオブジェクト
265     */
266    public function appendFirst($html_snip)
267    {
268        if (isset($this->arrSelectElements[$this->search_depth])) {
269            foreach ($this->arrSelectElements[$this->search_depth] as $key => $objElement) {
270                $this->lfSetTransform('appendFirst', $objElement[0], $html_snip);
271            }
272        }
273
274        return $this;
275    }
276
277    /**
278     * 要素の末尾にHTMLを挿入
279     *
280     * @param string $html_snip 挿入するHTMLの断片
281     * @return SC_Helper_Transformオブジェクト
282     */
283    public function appendChild($html_snip)
284    {
285        if (isset($this->arrSelectElements[$this->search_depth])) {
286            foreach ($this->arrSelectElements[$this->search_depth] as $key => $objElement) {
287                $this->lfSetTransform('appendChild', $objElement[0], $html_snip);
288            }
289        }
290
291        return $this;
292    }
293
294    /**
295     * 要素を指定したHTMLに置換
296     *
297     * @param string $html_snip 置換後のHTMLの断片
298     * @return SC_Helper_Transformオブジェクト
299     */
300    public function replaceElement($html_snip)
301    {
302        if (isset($this->arrSelectElements[$this->search_depth])) {
303            foreach ($this->arrSelectElements[$this->search_depth] as $key => &$objElement) {
304                $this->lfSetTransform('replaceElement', $objElement[0], $html_snip);
305            }
306        }
307
308        return $this;
309    }
310
311    /**
312     * 要素を削除する
313     *
314     * @return SC_Helper_Transformオブジェクト
315     */
316    public function removeElement()
317    {
318        if (isset($this->arrSelectElements[$this->search_depth])) {
319            foreach ($this->arrSelectElements[$this->search_depth] as $key => &$objElement) {
320                $this->lfSetTransform('replaceElement', $objElement[0], '');
321            }
322        }
323
324        return $this;
325    }
326
327    /**
328     * HTMLに戻して、Transform用に付けたマーカーを削除し、Smartyのタグを復元する
329     *
330     * @return string トランスフォーム済みHTML。まったくトランスフォームが行われなかった場合は元のHTMLを返す。。
331     */
332    public function getHTML()
333    {
334        if (count($this->arrErr)) {
335            // エラーメッセージ組み立て
336            $err_msg = '';
337            foreach ($this->arrErr as $arrErr) {
338                if ($arrErr['err_msg']) {
339                    $err_msg .= '<br />'.$arrErr['err_msg'];
340                } else {
341                    if ($arrErr['type'] == self::ERR_TARGET_ELEMENT_NOT_FOUND) {
342                        $err_msg .= "<br />${arrErr['selector']} が存在しません";
343                    } else {
344                        $err_msg .= '<br />'.print_r($arrErr, true);
345                    }
346                }
347            }
348            // エラー画面表示
349            SC_Utils_Ex::sfDispSiteError(FREE_ERROR_MSG, '', true, 'テンプレートの操作に失敗しました。' . $err_msg);
350        } elseif ($this->snip_count) {
351            $html = $this->objDOM->saveHTML();
352            $html = preg_replace('/^.*(<html[^>]*>)/s', '$1', $html);
353            $html = preg_replace('/(<\/html>).*$/s', '$1', $html);
354            $html = preg_replace('/^.*<\!--TemplateTransformer start-->/s', '', $html);
355            $html = preg_replace('/<\!--TemplateTransformer end-->.*$/s', '', $html);
356            $html = preg_replace(
357                '/<\!--TemplateTransformerSnip start-->.*?<\!--TemplateTransformerSnip end-->/s',
358                '',
359                $html
360            );
361            $html = $this->header_source.$html.$this->footer_source;
362            $html = str_replace($this->arrSmartyTagsSub, $this->arrSmartyTagsOrg, $html);
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        }
643        catch (Exception $e) {
644            SC_Utils_Ex::sfDispSiteError(FREE_ERROR_MSG, '', true, 'テンプレートの操作に失敗しました。');
645        }
646
647        return true;
648    }
649
650    /**
651     * セレクタエラーを記録する
652     *
653     * @param string  $selector    セレクタ
654     * @param integer $type        エラーの種類
655     * @param string  $err_msg     エラーメッセージ
656     * @return void
657     */
658    protected function lfSetError($selector, $type, $err_msg = NULL)
659    {
660        $this->arrErr[] = array(
661            'selector'    => $selector,
662            'type'        => $type,
663            'err_msg'     => $err_msg
664        );
665    }
666}
Note: See TracBrowser for help on using the repository browser.