@@ -274,6 +274,17 @@ goog.constructNamespace_ = function(name, opt_obj) {
274
274
} ;
275
275
276
276
277
+ /**
278
+ * Module identifier validation regexp.
279
+ * Note: This is a conservative check, it is very possible to be more lienent,
280
+ * the primary exclusion here is "/" and "\" and a leading ".", these
281
+ * restrictions are intended to leave the door open for using goog.require
282
+ * with relative file paths rather than module identifiers.
283
+ * @private
284
+ */
285
+ goog . VALID_MODULE_RE_ = / ^ [ a - z A - Z _ $ ] [ a - z A - Z 0 - 9 . _ $ ] * $ / ;
286
+
287
+
277
288
/**
278
289
* goog.module serves two purposes:
279
290
* - marks a file that must be loaded as a module
@@ -296,7 +307,9 @@ goog.constructNamespace_ = function(name, opt_obj) {
296
307
* "goog.package.part", is expected but not required.
297
308
*/
298
309
goog . module = function ( name ) {
299
- if ( ! goog . isString ( name ) || ! name ) {
310
+ if ( ! goog . isString ( name ) ||
311
+ ! name ||
312
+ name . search ( goog . VALID_MODULE_RE_ ) == - 1 ) {
300
313
throw Error ( 'Invalid module identifier' ) ;
301
314
}
302
315
if ( ! goog . isInModuleLoader_ ( ) ) {
@@ -888,58 +901,6 @@ if (goog.DEPENDENCIES_ENABLED) {
888
901
goog . queuedModules_ = [ ] ;
889
902
890
903
891
- /**
892
- * Retrieve and execute a module.
893
- * @param {string } src Script source URL.
894
- * @private
895
- */
896
- goog . retrieveAndExecModule_ = function ( src ) {
897
- // The full but non-canonicalized URL for later use.
898
- var originalPath = src ;
899
-
900
- // Canonicalize the path, removing any /./ or /../ since Chrome's debugging
901
- // console doesn't auto-canonicalize XHR loads as it does <script> srcs.
902
- var separator ;
903
- while ( ( separator = src . indexOf ( '/./' ) ) != - 1 ) {
904
- src = src . substr ( 0 , separator ) + src . substr ( separator + '/.' . length ) ;
905
- }
906
- while ( ( separator = src . indexOf ( '/../' ) ) != - 1 ) {
907
- var previousComponent = src . lastIndexOf ( '/' , separator - 1 ) ;
908
- src = src . substr ( 0 , previousComponent ) +
909
- src . substr ( separator + '/..' . length ) ;
910
- }
911
-
912
- var importScript = goog . global . CLOSURE_IMPORT_SCRIPT ||
913
- goog . writeScriptTag_ ;
914
-
915
- var scriptText = null ;
916
-
917
- var xhr = new goog . global [ 'XMLHttpRequest' ] ( ) ;
918
-
919
- /** @this {Object} */
920
- xhr . onload = function ( ) {
921
- scriptText = this . responseText ;
922
- } ;
923
- xhr . open ( 'get' , src , false ) ;
924
- xhr . send ( ) ;
925
-
926
- scriptText = xhr . responseText ;
927
-
928
- if ( scriptText != null ) {
929
- var execModuleScript = goog . wrapModule_ ( src , scriptText ) ;
930
- var isOldIE = goog . IS_OLD_IE_ ;
931
- if ( isOldIE ) {
932
- goog . dependencies_ . deferred [ originalPath ] = execModuleScript ;
933
- goog . queuedModules_ . push ( originalPath ) ;
934
- } else {
935
- importScript ( src , execModuleScript ) ;
936
- }
937
- } else {
938
- throw new Error ( 'load of ' + src + 'failed' ) ;
939
- }
940
- } ;
941
-
942
-
943
904
/**
944
905
* Return an appropriate module text. Suitable to insert into
945
906
* a script tag (that is unescaped).
@@ -1324,6 +1285,74 @@ if (goog.DEPENDENCIES_ENABLED) {
1324
1285
}
1325
1286
1326
1287
1288
+ /**
1289
+ * Normalize a file path by removing redundant ".." and extraneous "." file
1290
+ * path components.
1291
+ * @param {string } path
1292
+ * @return {string }
1293
+ * @private
1294
+ */
1295
+ goog . normalizePath_ = function ( path ) {
1296
+ var components = path . split ( '/' ) ;
1297
+ var i = 0 ;
1298
+ while ( i < components . length ) {
1299
+ if ( components [ i ] == '.' ) {
1300
+ components . splice ( i , 1 ) ;
1301
+ } else if ( i && components [ i ] == '..' &&
1302
+ components [ i - 1 ] && components [ i - 1 ] != '..' ) {
1303
+ components . splice ( -- i , 2 ) ;
1304
+ } else {
1305
+ i ++ ;
1306
+ }
1307
+ }
1308
+ return components . join ( '/' ) ;
1309
+ } ;
1310
+
1311
+
1312
+ /**
1313
+ * Retrieve and execute a module.
1314
+ * @param {string } src Script source URL.
1315
+ * @private
1316
+ */
1317
+ goog . retrieveAndExecModule_ = function ( src ) {
1318
+ if ( ! COMPILED ) {
1319
+ // The full but non-canonicalized URL for later use.
1320
+ var originalPath = src ;
1321
+ // Canonicalize the path, removing any /./ or /../ since Chrome's debugging
1322
+ // console doesn't auto-canonicalize XHR loads as it does <script> srcs.
1323
+ src = goog . normalizePath_ ( src ) ;
1324
+
1325
+ var importScript = goog . global . CLOSURE_IMPORT_SCRIPT ||
1326
+ goog . writeScriptTag_ ;
1327
+
1328
+ var scriptText = null ;
1329
+
1330
+ var xhr = new goog . global [ 'XMLHttpRequest' ] ( ) ;
1331
+
1332
+ /** @this {Object} */
1333
+ xhr . onload = function ( ) {
1334
+ scriptText = this . responseText ;
1335
+ } ;
1336
+ xhr . open ( 'get' , src , false ) ;
1337
+ xhr . send ( ) ;
1338
+
1339
+ scriptText = xhr . responseText ;
1340
+
1341
+ if ( scriptText != null ) {
1342
+ var execModuleScript = goog . wrapModule_ ( src , scriptText ) ;
1343
+ var isOldIE = goog . IS_OLD_IE_ ;
1344
+ if ( isOldIE ) {
1345
+ goog . dependencies_ . deferred [ originalPath ] = execModuleScript ;
1346
+ goog . queuedModules_ . push ( originalPath ) ;
1347
+ } else {
1348
+ importScript ( src , execModuleScript ) ;
1349
+ }
1350
+ } else {
1351
+ throw new Error ( 'load of ' + src + 'failed' ) ;
1352
+ }
1353
+ }
1354
+ } ;
1355
+
1327
1356
1328
1357
//==============================================================================
1329
1358
// Language Enhancements
0 commit comments