コード例 #1
0
 /**
  * Fixes any href's starting with # so that they work properly
  * Also makes sure that any relative URL include an index.php if .htaccess is not used.
  */
 protected function fixupAnchors()
 {
     $pageURL = '';
     if (isset($_SERVER['PATH_INFO'])) {
         $pageURL = $_SERVER['PATH_INFO'];
         if ($pageURL[0] == '/') {
             $pageURL = substr($pageURL, 1);
         }
     }
     if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) {
         $pageURL .= '?' . $_SERVER['QUERY_STRING'];
     }
     $anchors = $this->query("//*[starts-with(@href,'#')]");
     for ($i = 0; $i < $anchors->length; $i++) {
         $anchor = $anchors->item($i);
         $href = $pageURL . $anchor->getAttribute('href');
         $anchor->setAttribute('href', $href);
     }
     if (I2CE_Page::rewrittenURLs()) {
         //there is (presumably) a .htaccess that is rewrite SITE_UR/a/blah/c?darp to SITE_URL/index.php/a/blah/c?darp
         //so we don't need to do anything.
         return;
     }
     //        $urls = $this->query("//*[not(matches(@href,'^([a-zA-Z]+:\/\/)|\/'))]"); //get all non-absolute urls. -- this should work but I get a : xmlXPathCompOpEval: function matches not found
     //so iwill do it by hand below
     $links = array('src', 'href', 'action');
     foreach ($links as $link) {
         $urls = $this->query("//*[@{$link}]");
         //get all non-absolute urls.
         for ($i = 0; $i < $urls->length; $i++) {
             $url = $urls->item($i);
             $ref = self::ensureURL($url->getAttribute($link));
             $url->setAttribute($link, $ref);
         }
     }
     //now fix any @import url's for style sheets
     $styles = $this->query("//style");
     for ($i = 0; $i < $styles->length; $i++) {
         $style = $styles->item($i);
         $replace = self::ensureCSSURLs($style->textContent);
         while ($style->hasChildNodes()) {
             $style->removeChild($style->lastChild);
         }
         $style->appendChild($this->doc->createTextNode($replace));
     }
     //now fixed ie hack type comments e.g
     //<!--[if lt IE 7]><link href="file/iehacks.css?newman" rel="stylesheet" type="text/css" media="screen" /><![endif]-->
     $comments = $this->query('//comment()');
     foreach ($comments as $comment) {
         $data = $comment->data;
         $comment->deleteData(0, $comment->length);
         $data = preg_replace('/href=([\'"])([a-zA-Z0-9])/', 'href=$1index.php/$2', $data, -1);
         $comment->appendData($data);
     }
 }