Exemplo n.º 1
0
 /**
  * @param string $jsCode
  * @param array $expectedStrings
  * @dataProvider translateExamples
  */
 function testParseStrings($jsCode, $expectedStrings)
 {
     $actualStrings = CRM_Utils_JS::parseStrings($jsCode);
     sort($expectedStrings);
     sort($actualStrings);
     $this->assertEquals($expectedStrings, $actualStrings);
 }
Exemplo n.º 2
0
 /**
  * @param string $html
  *   Example HTML input.
  * @param array $expectedStrings
  *   List of expected strings.
  * @dataProvider translateExamples
  */
 public function testParseStrings($html, $expectedStrings)
 {
     // Magic! The JS parser works with HTML!
     $actualStrings = CRM_Utils_JS::parseStrings($html);
     sort($expectedStrings);
     sort($actualStrings);
     $this->assertEquals($expectedStrings, $actualStrings);
 }
Exemplo n.º 3
0
 /**
  * @param array $files
  *   File paths.
  * @return string
  */
 public function digestJs($files)
 {
     $scripts = array();
     foreach ($files as $file) {
         $scripts[] = file_get_contents($file);
     }
     $scripts = \CRM_Utils_JS::dedupeClosures($scripts, array('angular', '$', '_'), array('angular', 'CRM.$', 'CRM._'));
     // This impl of stripComments currently adds 10-20ms and cuts ~7%
     return \CRM_Utils_JS::stripComments(implode("\n", $scripts));
 }
Exemplo n.º 4
0
 /**
  * Extract a list of strings from a file.
  *
  * @param string $file
  *   File path.
  * @param string $format
  *   Type of file (e.g. 'text/javascript', 'text/html').
  * @return array
  *   List of translatable strings.
  * @throws Exception
  */
 public function extract($file, $format)
 {
     switch ($format) {
         case 'text/javascript':
             return CRM_Utils_JS::parseStrings(file_get_contents($file));
         case 'text/html':
             // Magic! The JS parser works with HTML! See CRM_Utils_HTMLTest.
             return CRM_Utils_JS::parseStrings(file_get_contents($file));
         default:
             throw new Exception("Cannot extract strings: Unrecognized file type.");
     }
 }
Exemplo n.º 5
0
 /**
  * Translate strings in a javascript file
  *
  * @param $ext string, extension name
  * @param $file string, file path
  * @return void
  */
 private function translateScript($ext, $file)
 {
     // For each extension, maintain one cache record which
     // includes parsed (translatable) strings for all its JS files.
     $stringsByFile = $this->cache->get($ext);
     // array($file => array(...strings...))
     if (!$stringsByFile) {
         $stringsByFile = array();
     }
     if (!isset($stringsByFile[$file])) {
         $filePath = $this->getPath($ext, $file);
         if ($filePath && is_readable($filePath)) {
             $stringsByFile[$file] = CRM_Utils_JS::parseStrings(file_get_contents($filePath));
         } else {
             $stringsByFile[$file] = array();
         }
         $this->cache->set($ext, $stringsByFile);
     }
     $this->addString($stringsByFile[$file]);
 }
Exemplo n.º 6
0
 /**
  * @param string $input
  * @param string $expectedOutput
  * @dataProvider stripCommentsExamples
  */
 public function testStripComments($input, $expectedOutput)
 {
     $this->assertEquals($expectedOutput, CRM_Utils_JS::stripComments($input));
 }