diff --git a/libraries.api.php b/libraries.api.php index 66fa772..a30f44c 100644 --- a/libraries.api.php +++ b/libraries.api.php @@ -118,10 +118,14 @@ * - $variant: If the $library array belongs to a certain variant (see * above), a string containing the variant name. NULL, otherwise. * Valid callback groups are: - * - prepare: Callbacks registered in this group are applied as soon as the + * - info: Callbacks registered in this group are applied as soon as the * library information has been retrieved via hook_libraries_info() or * info files. - * - detect: Callbacks registered in this group are applied as soon as + * - pre detect: Callbacks registered in this group are applied when a + * library is about to be detected, before the version callback is + * invoked. At this point the following additional information is available: + * - $library['library path']: The path on the file system to the library. + * - post detect: Callbacks registered in this group are applied as soon as * library detection has been completed. At this point the library * contains the version-specific information, if specified, and following * additional information is available: diff --git a/libraries.module b/libraries.module index 2c864d9..4c44a18 100644 --- a/libraries.module +++ b/libraries.module @@ -155,7 +155,7 @@ function libraries_scan_info_files() { * * @param $group * A string containing the group of callbacks that is to be applied. Should be - * either 'prepare', 'detect', or 'load'. + * either 'info', 'pre detect', 'post detect', or 'load'. * @param $library * An array of library information, passed by reference. */ @@ -250,9 +250,9 @@ function libraries_info($name = NULL) { // Allow modules to alter the registered libraries. drupal_alter('libraries_info', $libraries); - // Invoke callbacks in the 'prepare' group. + // Invoke callbacks in the 'info' group. foreach ($libraries as &$properties) { - libraries_invoke('prepare', $properties); + libraries_invoke('info', $properties); } } @@ -287,8 +287,9 @@ function libraries_info_defaults(&$library, $name) { 'callbacks' => array(), ); $library['callbacks'] += array( - 'prepare' => array(), - 'detect' => array(), + 'info' => array(), + 'pre detect' => array(), + 'post detect' => array(), 'load' => array(), ); return $library; @@ -342,6 +343,9 @@ function libraries_detect($name) { return $library; } + // Invoke callbacks in the 'pre detect' group. + libraries_invoke('pre detect', $library); + // Detect library version, if not hardcoded. if (!isset($library['version'])) { // We support both a single parameter, which is an associative array, and an @@ -417,8 +421,8 @@ function libraries_detect($name) { // If we end up here, the library should be usable. $library['installed'] = TRUE; - // Invoke callbacks in the 'detect' group. - libraries_invoke('detect', $library); + // Invoke callbacks in the 'post detect' group. + libraries_invoke('post detect', $library); return $library; } diff --git a/tests/libraries.test b/tests/libraries.test index 785b8ae..a0d67d0 100644 --- a/tests/libraries.test +++ b/tests/libraries.test @@ -148,57 +148,66 @@ class LibrariesTestCase extends DrupalWebTestCase { '1' => array( 'variants' => array( 'example_variant' => array( - 'prepare callback' => 'not applied', - 'detect callback' => 'not applied', + 'info callback' => 'not applied', + 'pre detect callback' => 'not applied', + 'post detect callback' => 'not applied', 'load callback' => 'not applied', ), ), - 'prepare callback' => 'not applied', - 'detect callback' => 'not applied', + 'info callback' => 'not applied', + 'pre detect callback' => 'not applied', + 'post detect callback' => 'not applied', 'load callback' => 'not applied', ), ), 'variants' => array( 'example_variant' => array( - 'prepare callback' => 'not applied', - 'detect callback' => 'not applied', + 'info callback' => 'not applied', + 'pre detect callback' => 'not applied', + 'post detect callback' => 'not applied', 'load callback' => 'not applied', ), ), 'callbacks' => array( - 'prepare' => array('_libraries_test_prepare_callback'), - 'detect' => array('_libraries_test_detect_callback'), + 'info' => array('_libraries_test_info_callback'), + 'pre detect' => array('_libraries_test_pre_detect_callback'), + 'post detect' => array('_libraries_test_post_detect_callback'), 'load' => array('_libraries_test_load_callback'), ), - 'prepare callback' => 'not applied', - 'detect callback' => 'not applied', + 'info callback' => 'not applied', + 'pre detect callback' => 'not applied', + 'post detect callback' => 'not applied', 'load callback' => 'not applied', 'module' => 'libraries_test', ); libraries_info_defaults($expected, 'example_callback'); - // Test a callback in the 'prepare' phase. - $expected['prepare callback'] = 'applied (top-level)'; - $expected['versions']['1']['prepare callback'] = 'applied (version 1)'; - $expected['versions']['1']['variants']['example_variant']['prepare callback'] = 'applied (version 1, variant example_variant)'; - $expected['variants']['example_variant']['prepare callback'] = 'applied (variant example_variant)'; + // Test a callback in the 'info' group. + $expected['info callback'] = 'applied (top-level)'; + $expected['versions']['1']['info callback'] = 'applied (version 1)'; + $expected['versions']['1']['variants']['example_variant']['info callback'] = 'applied (version 1, variant example_variant)'; + $expected['variants']['example_variant']['info callback'] = 'applied (variant example_variant)'; $library = libraries_info('example_callback'); $this->verbose('Expected:
' . var_export($expected, TRUE) . '
'); $this->verbose('Actual:
' . var_export($library, TRUE) . '
'); $this->assertEqual($library, $expected, 'Prepare callback was applied correctly.'); - // Test a callback in the 'detect' phase. + // Test a callback in the 'pre detect' and 'post detect' phase. // Successfully detected libraries should only contain version information // for the detected version and thus, be marked as installed. unset($expected['versions']); $expected['installed'] = TRUE; // Additionally, version-specific properties of the detected version are // supposed to override the corresponding top-level properties. - $expected['prepare callback'] = 'applied (version 1)'; + $expected['info callback'] = 'applied (version 1)'; $expected['variants']['example_variant']['installed'] = TRUE; - $expected['variants']['example_variant']['prepare callback'] = 'applied (version 1, variant example_variant)'; - $expected['detect callback'] = 'applied (top-level)'; - $expected['variants']['example_variant']['detect callback'] = 'applied (variant example_variant)'; + $expected['variants']['example_variant']['info callback'] = 'applied (version 1, variant example_variant)'; + // Version-overloading takes place after the 'pre detect' callbacks have + // been applied. + $expected['pre detect callback'] = 'applied (version 1)'; + $expected['post detect callback'] = 'applied (top-level)'; + $expected['variants']['example_variant']['pre detect callback'] = 'applied (version 1, variant example_variant)'; + $expected['variants']['example_variant']['post detect callback'] = 'applied (variant example_variant)'; $library = libraries_detect('example_callback'); $this->verbose('Expected:
' . var_export($expected, TRUE) . '
'); $this->verbose('Actual:
' . var_export($library, TRUE) . '
'); @@ -218,8 +227,9 @@ class LibrariesTestCase extends DrupalWebTestCase { drupal_static_reset('libraries_load'); // Successfully loaded library variants are supposed to contain the specific // variant information only. - $expected['prepare callback'] = 'applied (version 1, variant example_variant)'; - $expected['detect callback'] = 'applied (variant example_variant)'; + $expected['info callback'] = 'applied (version 1, variant example_variant)'; + $expected['pre detect callback'] = 'applied (version 1, variant example_variant)'; + $expected['post detect callback'] = 'applied (variant example_variant)'; $library = libraries_load('example_callback', 'example_variant'); $this->verbose('Expected:
' . var_export($expected, TRUE) . '
'); $this->verbose('Actual:
' . var_export($library, TRUE) . '
'); diff --git a/tests/libraries_test.module b/tests/libraries_test.module index c1c454c..0f4999b 100644 --- a/tests/libraries_test.module +++ b/tests/libraries_test.module @@ -73,6 +73,45 @@ function libraries_test_libraries_info() { ), ); + // Test all types of JavaScript and CSS supported by drupal_add_js() and + // drupal_add_css(). + $libraries['example_js_css'] = array( + 'name' => 'Example JavaScript and CSS', + 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', + 'version' => 1, + 'variants' => array( + 'file' => array( + 'files' => array( + 'js' => array('example_1.js'), + 'css' => array('example_1.css'), + ), + ), + 'external' => array( + 'files' => array( + 'js' => array(url(NULL, array('absolute' => TRUE)) . '/' . drupal_get_path('module', 'libraries') . '/tests/example/example_1.js'), + 'css' => array(url(NULL, array('absolute' => TRUE)) . '/' . drupal_get_path('module', 'libraries') . '/tests/example/example_1.css'), + ), + ), + 'inline' => array( + 'files' => array( + // This is the content of example_1.js. + 'js' => array('$(".libraries-test-javascript").text("If this text shows up, example_1.js was loaded successfully.")'), + // This is the content of example_1.css. + 'css' => array('.libraries-test-css{color:red;}'), + ), + ), + // There is no CSS equivalent of JavaScript settings. + 'setting' => array( + 'files' => array( + 'js' => array( + array('librariesTest' => array('testSetting' => TRUE)), + 'if(Drupal.settings.librariesTest.testSetting){$(".libraries-test-javascript").text("If this text shows up, example_1.js was loaded successfully.")}', + ), + ), + ) + ), + ); + // Test loading of integration files. // Normally added by the corresponding module via hook_libraries_info_alter(), // these files should be automatically loaded when the library is loaded. @@ -210,33 +249,38 @@ function libraries_test_libraries_info() { 'variants' => array( 'example_variant' => array( // These keys are for testing purposes only. - 'prepare callback' => 'not applied', - 'detect callback' => 'not applied', + 'info callback' => 'not applied', + 'pre detect callback' => 'not applied', + 'post detect callback' => 'not applied', 'load callback' => 'not applied', ), ), // These keys are for testing purposes only. - 'prepare callback' => 'not applied', - 'detect callback' => 'not applied', + 'info callback' => 'not applied', + 'pre detect callback' => 'not applied', + 'post detect callback' => 'not applied', 'load callback' => 'not applied', ), ), 'variants' => array( 'example_variant' => array( // These keys are for testing purposes only. - 'prepare callback' => 'not applied', - 'detect callback' => 'not applied', + 'info callback' => 'not applied', + 'pre detect callback' => 'not applied', + 'post detect callback' => 'not applied', 'load callback' => 'not applied', ), ), 'callbacks' => array( - 'prepare' => array('_libraries_test_prepare_callback'), - 'detect' => array('_libraries_test_detect_callback'), + 'info' => array('_libraries_test_info_callback'), + 'pre detect' => array('_libraries_test_pre_detect_callback'), + 'post detect' => array('_libraries_test_post_detect_callback'), 'load' => array('_libraries_test_load_callback'), ), // These keys are for testing purposes only. - 'prepare callback' => 'not applied', - 'detect callback' => 'not applied', + 'info callback' => 'not applied', + 'pre detect callback' => 'not applied', + 'post detect callback' => 'not applied', 'load callback' => 'not applied', ); @@ -322,45 +366,37 @@ function _libraries_test_return_installed($library, $name, $installed) { } /** - * Sets the 'prepare callback' key. - * - * This function is used as a test callback for the 'prepare' callback group. + * Sets the 'info callback' key. * - * @param $library - * An array of library information, which may be version- or variant-specific. - * Passed by reference. - * @param $version - * The version the library information passed in $library belongs to, or NULL - * if the passed library information is not version-specific. - * @param $variant - * The variant the library information passed in $library belongs to, or NULL - * if the passed library information is not variant-specific. + * This function is used as a test callback for the 'info' callback group. * * @see _libraries_test_callback() */ -function _libraries_test_prepare_callback(&$library, $version, $variant) { - _libraries_test_callback($library, $version, $variant, 'prepare'); +function _libraries_test_info_callback(&$library, +$version, $variant) { + _libraries_test_callback($library, $version, $variant, 'info'); } /** - * Sets the 'detect callback' key. + * Sets the 'pre detect callback' key. * - * This function is used as a test callback for the 'detect' callback group. + * This function is used as a test callback for the 'pre detect' callback group. * - * @param $library - * An array of library information, which may be version- or variant-specific. - * Passed by reference. - * @param $version - * The version the library information passed in $library belongs to, or NULL - * if the passed library information is not version-specific. - * @param $variant - * The variant the library information passed in $library belongs to, or NULL - * if the passed library information is not variant-specific. + * @see _libraries_test_callback() + */ +function _libraries_test_pre_detect_callback(&$library, $version, $variant) { + _libraries_test_callback($library, $version, $variant, 'pre detect'); +} + +/** + * Sets the 'post detect callback' key. + * + * This function is used as a test callback for the 'post detect callback group. * * @see _libraries_test_callback() */ -function _libraries_test_detect_callback(&$library, $version, $variant) { - _libraries_test_callback($library, $version, $variant, 'detect'); +function _libraries_test_post_detect_callback(&$library, $version, $variant) { + _libraries_test_callback($library, $version, $variant, 'post detect'); } /** @@ -456,6 +492,30 @@ function libraries_test_menu() { 'page arguments' => array('example_versions_and_variants', 'example_variant_2'), 'access callback' => TRUE, ); + $items['libraries_test/js_css/file'] = array( + 'title' => 'Test JavaScript and CSS: Local files', + 'page callback' => '_libraries_test_load', + 'page arguments' => array('example_js_css', 'file'), + 'access callback' => TRUE, + ); + $items['libraries_test/js_css/external'] = array( + 'title' => 'Test JavaScript and CSS: External files', + 'page callback' => '_libraries_test_load', + 'page arguments' => array('example_js_css', 'external'), + 'access callback' => TRUE, + ); + $items['libraries_test/js_css/inline'] = array( + 'title' => 'Test JavaScript and CSS: Inline code', + 'page callback' => '_libraries_test_load', + 'page arguments' => array('example_js_css', 'inline'), + 'access callback' => TRUE, + ); + $items['libraries_test/js_css/setting'] = array( + 'title' => 'Test JavaScript and CSS: JavaScript settings', + 'page callback' => '_libraries_test_load', + 'page arguments' => array('example_js_css', 'setting'), + 'access callback' => TRUE, + ); return $items; }