Changeset 21807


Ignore:
Timestamp:
2012/05/05 14:35:22 (12 years ago)
Author:
sunat134
Message:

#1751 Transform対象のテンプレートに<HTML>タグが含まれるて、<BODY>が含まれない場合に対応

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/version-2_12-dev/data/class/helper/SC_Helper_Transform.php

    r21775 r21807  
    7272            $source 
    7373        ); 
     74 
     75        // headタグの内側を退避 
     76        $source = preg_replace_callback( 
     77            '/(<head[^>]*>)(.+)(<\/head>)/is', 
     78            array($this, 'lfCaptureHeadTags2Comment'), 
     79            $source 
     80        ); 
     81 
    7482        // JavaScript内にSmartyのタグが存在するものを、コメント形式に置換 
    7583        $source = preg_replace_callback( 
     
    7886            $source 
    7987        ); 
     88 
    8089        // HTMLタグ内にSmartyのタグが存在するものを、まず置換する 
    8190        $source = preg_replace_callback( 
     
    8493            $source 
    8594        ); 
     95 
    8696        // 通常のノードに属する部分を、コメント形式に置換 
    8797        $source = preg_replace_callback( 
     
    91101        ); 
    92102 
    93         // BODYタグの外側は退避させ 
    94         if (preg_match('/^(.*?<body[^>]*>)(.+)(<\/body>.*)$/is', $source, $arrMatches)) { 
     103        // HTMLタグの有無、BODYタグの有無で動作を切り替え 
     104        if (preg_match('/^(.*?)(<html[^>]*>.+<\/html>)(.*?)$/is', $source, $arrMatches)) { 
    95105            $this->header_source = $arrMatches[1]; 
    96106            $source = $arrMatches[2]; 
    97107            $this->footer_source = $arrMatches[3]; 
    98108        } 
     109        elseif (preg_match('/^.*?<body[^>]*>.+<\/body>.*$/is', $source)) { 
     110            $source = '<meta http-equiv="content-type" content="text/html; charset=UTF-8" /><html><!--TemplateTransformer start-->'.$source.'<!--TemplateTransformer end--></html>'; 
     111        } 
     112        else { 
     113            $source = '<meta http-equiv="content-type" content="text/html; charset=UTF-8" /><html><body><!--TemplateTransformer start-->'.$source.'<!--TemplateTransformer end--></body></html>'; 
     114        } 
    99115         
    100         $source = '<meta http-equiv="content-type" content="text/html; charset=UTF-8" /><html><body><!--TemplateTransformer start-->'.$source.'<!--TemplateTransformer end--></body></html>'; 
    101116        @$this->objDOM->loadHTML($source); 
    102117        $this->lfScanChild($this->objDOM); 
     
    311326        } elseif ($this->snip_count) { 
    312327            $html = $this->objDOM->saveHTML(); 
     328            $html = preg_replace('/^.*(<html[^>]*>)/s', '$1', $html); 
     329            $html = preg_replace('/(<\/html>).*$/s', '$1', $html); 
    313330            $html = preg_replace('/^.*<\!--TemplateTransformer start-->/s', '', $html); 
    314331            $html = preg_replace('/<\!--TemplateTransformer end-->.*$/s', '', $html); 
     332            $html = preg_replace( 
     333                '/<\!--TemplateTransformerSnip start-->.*?<\!--TemplateTransformerSnip end-->/s', 
     334                '', 
     335                $html 
     336            ); 
    315337            $html = $this->header_source.$html.$this->footer_source; 
    316338            $html = str_replace($this->arrSmartyTagsSub, $this->arrSmartyTagsOrg, $html); 
     
    344366     * DOMの処理の邪魔になるSmartyのタグを代理文字に置換する preg_replace_callback のコールバック関数 
    345367     * 
     368     * コメント形式への置換 
     369     * 
     370     * @param array $arrMatches マッチしたタグの情報 
     371     * @return string 代わりの文字列 
     372     */ 
     373    protected function lfCaptureHeadTags2Comment(array $arrMatches) { 
     374        $substitute_tag = sprintf('<!--###%08d###-->', $this->smarty_tags_idx); 
     375        $this->arrSmartyTagsOrg[$this->smarty_tags_idx] = $arrMatches[2]; 
     376        $this->arrSmartyTagsSub[$this->smarty_tags_idx] = $substitute_tag; 
     377        $this->smarty_tags_idx++; 
     378 
     379        // 文字化け防止用のMETAを入れておく 
     380        $content_type_tag = '<!--TemplateTransformerSnip start-->'; 
     381        $content_type_tag .= '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />';  
     382        $content_type_tag .= '<!--TemplateTransformerSnip end-->'; 
     383 
     384        return $arrMatches[1].$content_type_tag.$substitute_tag.$arrMatches[3]; 
     385    } 
     386 
     387 
     388    /** 
     389     * DOMの処理の邪魔になるSmartyのタグを代理文字に置換する preg_replace_callback のコールバック関数 
     390     * 
    346391     * HTMLエレメント内部の処理 
    347392     * 
     
    384429     */ 
    385430    protected function lfCaptureSmartyTagsInQuote(array $arrMatches) { 
    386         $html = preg_replace_callback('/###TEMP(\d{8})###/s', array($this, 'lfCaptureSmartyTags2Value'), $arrMatches[0]); 
     431        $html = preg_replace_callback( 
     432            '/###TEMP(\d{8})###/s', 
     433            array($this, 'lfCaptureSmartyTags2Value'), 
     434            $arrMatches[0] 
     435        ); 
    387436        return $html; 
    388437    } 
Note: See TracChangeset for help on using the changeset viewer.