/**
  * @group ZF-12287
  */
 public function testNoEscapeTrue()
 {
     $this->helper->appendScript('// some script' . PHP_EOL, 'text/javascript', array('noescape' => true));
     $test = $this->helper->toString();
     $this->assertNotContains('//<!--', $test);
     $this->assertNotContains('//-->', $test);
 }
 /**
  * @group GH-515
  */
 public function testConditionalScriptNoIE()
 {
     $this->helper->setAllowArbitraryAttributes(true);
     $this->helper->appendFile('/js/foo.js', 'text/javascript', array('conditional' => '!IE'));
     $test = $this->helper->toString();
     $this->assertContains('<!--[if !IE]><!--><', $test);
     $this->assertContains('<!--<![endif]-->', $test);
 }
Example #3
0
 /**
  * @issue ZF-5435
  */
 public function testContainerMaintainsCorrectOrderOfItems()
 {
     $this->helper->offsetSetFile(1, 'test1.js');
     $this->helper->offsetSetFile(20, 'test2.js');
     $this->helper->offsetSetFile(10, 'test3.js');
     $this->helper->offsetSetFile(5, 'test4.js');
     $test = $this->helper->toString();
     $expected = '<script type="text/javascript" src="test1.js"></script>' . PHP_EOL . '<script type="text/javascript" src="test4.js"></script>' . PHP_EOL . '<script type="text/javascript" src="test3.js"></script>' . PHP_EOL . '<script type="text/javascript" src="test2.js"></script>';
     $this->assertEquals($expected, $test);
 }
Example #4
0
    public function testIndentationIsHonored()
    {
        $this->helper->setIndent(4);
        $this->helper->appendScript('
var foo = "bar";
document.write(foo.strlen());');
        $this->helper->appendScript('
var bar = "baz";
document.write(bar.strlen());');
        $string = $this->helper->toString();
        $scripts = substr_count($string, '    <script');
        $this->assertEquals(2, $scripts);
        $this->assertContains('    //', $string);
        $this->assertContains('    var', $string);
        $this->assertContains('    document', $string);
    }
Example #5
0
 /**
  * Retrieve string representation
  *
  * @param  string|int $indent
  * @return string
  */
 public function toString($indent = null)
 {
     // adds the automatic cache buster functionality
     foreach ($this as &$item) {
         if (!$this->_isValid($item)) {
             continue;
         }
         if (is_array($item->attributes)) {
             if (isset($item->attributes["src"])) {
                 $realFile = PIMCORE_DOCUMENT_ROOT . $item->attributes["src"];
                 if (file_exists($realFile)) {
                     $item->attributes["src"] = $item->attributes["src"] . "?_dc=" . filemtime($realFile);
                 }
             }
         }
     }
     return parent::toString($indent);
 }
Example #6
0
 /**
  * Retrieve string representation
  *
  * @param  string|int $indent
  * @return string
  */
 public function toString($indent = null)
 {
     foreach ($this as &$item) {
         if (!$this->_isValid($item)) {
             continue;
         }
         if ($this->isCacheBuster()) {
             // adds the automatic cache buster functionality
             if (is_array($item->attributes)) {
                 if (isset($item->attributes["src"])) {
                     $realFile = PIMCORE_DOCUMENT_ROOT . $item->attributes["src"];
                     if (file_exists($realFile)) {
                         $item->attributes["src"] = "/cache-buster-" . filemtime($realFile) . $item->attributes["src"];
                     }
                 }
             }
         }
         \Pimcore::getEventManager()->trigger("frontend.view.helper.head-script", $this, ["item" => $item]);
     }
     return parent::toString($indent);
 }
Example #7
0
 public function toString($indent = null)
 {
     $this->_files = array();
     // turn off errors for this code block, we are mucking with the loop structing in real time
     $errdis = ini_get('display_errors');
     $errlog = ini_get('error_reporting');
     ini_set('display_errors', 0);
     ini_set('error_reporting', 0);
     foreach ($this as $key => $item) {
         if (!empty($this[$key]->attributes['src'])) {
             $this->_files[] = array('modified' => $this->getFiletime($this[$key]->attributes['src']), 'fullpath' => Zend_Uri::check($this[$key]->attributes['src']) ? $this[$key]->attributes['src'] : $this->_path . '/' . basename($this[$key]->attributes['src']), 'filename' => basename($this[$key]->attributes['src']));
             $this->offsetUnset($key);
         }
     }
     // turn errors back on
     ini_set('display_errors', $errdis);
     ini_set('error_reporting', $errlog);
     if (!defined('USE_COMPILED_LIBRARIES') || USE_COMPILED_LIBRARIES !== 'true') {
         if (!file_exists($this->_path . '/' . $this->_state . '.combined.js') || $this->_checkModifiedDate($this->_path . '/' . $this->_state . '.combined.js')) {
             $this->_createCombined();
         }
         $this->appendScript(file_get_contents($this->_path . '/' . $this->_state . '.combined.js'));
     } else {
         if (!file_exists($this->_path . '/' . $this->_state . '.compiled.js') || $this->_checkModifiedDate($this->_path . '/' . $this->_state . '.compiled.js')) {
             if (!file_exists($this->_path . '/' . $this->_state . '.combined.js') || $this->_checkModifiedDate($this->_path . '/' . $this->_state . '.combined.js')) {
                 $this->_createCombined();
             }
             $this->_createCompiled();
         }
         if (file_exists($this->_path . '/' . $this->_state . '.compiled.js')) {
             $this->appendScript(file_get_contents($this->_path . '/' . $this->_state . '.compiled.js'));
         } else {
             $this->appendScript(file_get_contents($this->_path . '/' . $this->_state . '.combined.js'));
         }
     }
     return parent::toString();
 }
Example #8
0
 /**
  * Retrieve string representation
  *
  * @param  string|int $indent
  * @return string
  */
 public function toString($indent = null)
 {
     // if static pack enable use toStringPacked instead of toString
     if (self::$_packsEnable) {
         return $this->toStringPacked($indent);
     } else {
         return parent::toString($indent);
     }
 }
Example #9
0
 /**
  * Iterates over scripts, concatenating, optionally minifying, 
  * optionally compressiong, and caching them.
  * 
  * This detects updates to the source javascripts using filemtime.
  * A file with an mtime more recent than the mtime of the cached bundle will
  * invalidate the cached bundle.
  * 
  * Modifications of captured scripts cannot be detected by this.
  * DONT USE DYNAMICALLY GENERATED CAPTURED SCRIPTS.
  * 
  * 
  *
  * @param string $indent 
  * @return void
  * @throws UnexpectedValueException if item has no src attribute or contains no captured source
  */
 public function toString($indent = null)
 {
     if (isset($_REQUEST['bundle_off'])) {
         return parent::toString($indent);
     }
     $this->getContainer()->ksort();
     $filelist = '';
     $mostrecent = 0;
     foreach ($this as $item) {
         if (!$this->_isValid($item)) {
             continue;
         }
         if (isset($item->attributes['src'])) {
             $src = $item->attributes['src'];
             if ($this->_baseUrl && strpos($src, $this->_baseUrl) !== false) {
                 $src = substr($src, strlen($this->_baseUrl));
             }
             $mtime = filemtime($this->_docRoot . $src);
             if ($mtime > $mostrecent) {
                 $mostrecent = $mtime;
             }
         } else {
             if (!empty($item->source)) {
                 // BEWARE: Cannot detect modification of captured scripts!
                 $src = $item->source;
             } else {
                 throw new UnexpectedValueException("Item has no src attribute nor captured source.");
             }
         }
         $filelist .= $src;
     }
     $hash = md5($filelist);
     $cacheFile = "{$this->_cacheDir}/bundle_{$hash}.js";
     // suppress warning for file DNE
     if (file_exists($cacheFile)) {
         $cacheTime = filemtime($cacheFile);
     } else {
         $cacheTime = false;
     }
     if (false === $cacheTime || $cacheTime < $mostrecent) {
         if (!($data = $this->_getJsData())) {
             return "";
         }
         $this->_writeUncompressed($cacheFile, $data);
         if ($this->_doGzip) {
             $this->_writeCompressed($cacheFile, $data);
         }
         $cacheTime = filemtime($cacheFile);
     }
     $urlPath = "{$this->_baseUrl}/{$this->_urlPrefix}/bundle_{$hash}.js?{$cacheTime}";
     $ret = '<script type="text/javascript" src="' . $urlPath . '"></script>';
     return $ret;
 }
Example #10
0
 /**
  * Retrieve string representation
  *
  * @param  string|int $indent
  * @return string
  */
 public function toString($indent = null)
 {
     // if no static pack
     if (self::$_packsEnable == false) {
         return parent::toString($indent);
     }
     // looking for packs
     $container = array();
     foreach ($this as $item) {
         $itemPack = $this->_getItemPack($item);
         if (is_object($itemPack)) {
             $container[] = $itemPack;
         }
     }
     // display items
     $indent = null !== $indent ? $this->getWhitespace($indent) : $this->getIndent();
     if ($this->view) {
         $useCdata = $this->view->doctype()->isXhtml() ? true : false;
     } else {
         $useCdata = $this->useCdata ? true : false;
     }
     $escapeStart = $useCdata ? '//<![CDATA[' : '//<!--';
     $escapeEnd = $useCdata ? '//]]>' : '//-->';
     $items = array();
     foreach ($container as $item) {
         if (!$this->_isValid($item)) {
             continue;
         }
         // add prefix only if not contain "http://"
         self::_addItemSrcPrefix($item);
         $items[] = $this->itemToString($item, $indent, $escapeStart, $escapeEnd);
     }
     $return = implode($this->getSeparator(), $items);
     return $return;
 }
Example #11
0
 public function toString($indent = null)
 {
     // Return early if no minifier has been set up
     if (false === $this->_minifier || !$this->_minifier->doBundle()) {
         return parent::toString($indent);
     }
     $container = $this->getContainer();
     $container->ksort();
     $items = $container->getArrayCopy();
     // Collect JS files
     $compressable = array();
     foreach ($items as $key => $item) {
         if (isset($item->attributes['src'])) {
             $src = $item->attributes['src'];
             $file = basename($src);
             if (in_array($file, $this->doNotBundle)) {
                 continue;
             }
             // If the source refers to a local file, process it
             if (preg_match('~^(\\w+:)?//~', $src) === 0) {
                 $compressable[] = $item;
                 unset($items[$key]);
             }
         }
     }
     $container->exchangeArray($items);
     // Collect current data of the JS files
     $hash = '';
     $mtimes = array();
     foreach ($compressable as $item) {
         $file = $_SERVER['DOCUMENT_ROOT'] . $item->attributes['src'];
         if (Zend_Loader::isReadable($file)) {
             $hash .= $item->attributes['src'];
             $mtimes[] = filemtime($file);
         }
     }
     // Check if the original JS files have been updated since the
     // last minification
     $regenerate = true;
     $filename = sha1($hash) . ".js";
     $cachedir = $this->_minifier->getCacheDir();
     $path = $_SERVER['DOCUMENT_ROOT'] . $cachedir;
     // check directory exists. if not, create it
     if (!file_exists($path)) {
         mkdir($path, 0775, true);
     }
     if (file_exists($path . "/" . $filename)) {
         $mtime = filemtime($path . "/" . $filename);
         $regenerate = array_reduce($mtimes, function ($u, $v) use($mtime) {
             return $u || $v > $mtime;
         }, false);
     }
     // If any JS file has been updated since the last minification
     // collect the content again, and store it in the cached version
     if ($regenerate) {
         $jsContent = '';
         foreach ($compressable as $item) {
             $file = $_SERVER['DOCUMENT_ROOT'] . $item->attributes['src'];
             if (Zend_Loader::isReadable($file)) {
                 $jsContent .= file_get_contents($file) . PHP_EOL;
             }
         }
         $jsContent = $this->_minifier->minify($jsContent);
         file_put_contents($path . "/" . $filename, $jsContent);
     }
     //Some scripts should be before the bundle, some should be after... Some should even be inbetween... Need something smarter.
     $this->prependFile($cachedir . "/" . $filename);
     return parent::toString($indent);
 }
Example #12
0
 /**
  * 
  * Gets a string representation of the headscripts suitable for inserting
  * in the html head section. All included javascript files will be minified
  * and any script sections will remain as is. 
  * 
  * It is important to note that the minified javascript files will be minified
  * in reverse order of being added to this object, and ALL files will be rendered
  * prior to inline scripts being rendered.
  *
  * @see Zend_View_Helper_HeadScript->toString()
  * @param  string|int $indent
  * @return string
  */
 public function toString($indent = null)
 {
     if (!$this->_doMinify) {
         return parent::toString($indent);
         // Do not minify output
     }
     // An array of Script Items to be rendered
     $items = array();
     // An array of Javascript Items
     $scripts = array();
     // Any indentation we should use.
     $indent = null !== $indent ? $this->getWhitespace($indent) : $this->getIndent();
     // Determining the appropriate way to handle inline scripts
     if ($this->view) {
         $useCdata = $this->view->doctype()->isXhtml() ? true : false;
     } else {
         $useCdata = $this->useCdata ? true : false;
     }
     $escapeStart = $useCdata ? '//<![CDATA[' : '//<!--';
     $escapeEnd = $useCdata ? '//]]>' : '//-->';
     $this->getContainer()->ksort();
     $groupIndex = 0;
     foreach ($this as $i => $item) {
         if ($this->_isNeedToMinify($item)) {
             if (!empty($item->attributes['minify_split_before']) || !empty($item->attributes['minify_split'])) {
                 $items[] = $this->_generateMinifyItem($scripts);
                 $scripts = array();
             }
             $scripts[] = $item->attributes['src'];
             if (!empty($item->attributes['minify_split_after']) || !empty($item->attributes['minify_split'])) {
                 $items[] = $this->_generateMinifyItem($scripts);
                 $scripts = array();
             }
         } else {
             if ($scripts) {
                 $items[] = $this->_generateMinifyItem($scripts);
                 $scripts = array();
             }
             $items[] = $this->itemToString($item, $indent, $escapeStart, $escapeEnd);
         }
     }
     if ($scripts) {
         $items[] = $this->_generateMinifyItem($scripts);
     }
     return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
 }
Example #13
0
 public function toString($indent = null)
 {
     $strings = parent::toString($indent);
     $headbase = new Evil_Cdn_HeadBase('js');
     return $headbase->toString($strings);
 }