cache.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojo.cache"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojo.cache"] = true;
  8. dojo.provide("dojo.cache");
  9. /*=====
  10. dojo.cache = {
  11. // summary:
  12. // A way to cache string content that is fetchable via `dojo.moduleUrl`.
  13. };
  14. =====*/
  15. var cache = {};
  16. dojo.cache = function(/*String||Object*/module, /*String*/url, /*String||Object?*/value){
  17. // summary:
  18. // A getter and setter for storing the string content associated with the
  19. // module and url arguments.
  20. // description:
  21. // module and url are used to call `dojo.moduleUrl()` to generate a module URL.
  22. // If value is specified, the cache value for the moduleUrl will be set to
  23. // that value. Otherwise, dojo.cache will fetch the moduleUrl and store it
  24. // in its internal cache and return that cached value for the URL. To clear
  25. // a cache value pass null for value. Since XMLHttpRequest (XHR) is used to fetch the
  26. // the URL contents, only modules on the same domain of the page can use this capability.
  27. // The build system can inline the cache values though, to allow for xdomain hosting.
  28. // module: String||Object
  29. // If a String, the module name to use for the base part of the URL, similar to module argument
  30. // to `dojo.moduleUrl`. If an Object, something that has a .toString() method that
  31. // generates a valid path for the cache item. For example, a dojo._Url object.
  32. // url: String
  33. // The rest of the path to append to the path derived from the module argument. If
  34. // module is an object, then this second argument should be the "value" argument instead.
  35. // value: String||Object?
  36. // If a String, the value to use in the cache for the module/url combination.
  37. // If an Object, it can have two properties: value and sanitize. The value property
  38. // should be the value to use in the cache, and sanitize can be set to true or false,
  39. // to indicate if XML declarations should be removed from the value and if the HTML
  40. // inside a body tag in the value should be extracted as the real value. The value argument
  41. // or the value property on the value argument are usually only used by the build system
  42. // as it inlines cache content.
  43. // example:
  44. // To ask dojo.cache to fetch content and store it in the cache (the dojo["cache"] style
  45. // of call is used to avoid an issue with the build system erroneously trying to intern
  46. // this example. To get the build system to intern your dojo.cache calls, use the
  47. // "dojo.cache" style of call):
  48. // | //If template.html contains "<h1>Hello</h1>" that will be
  49. // | //the value for the text variable.
  50. // | var text = dojo["cache"]("my.module", "template.html");
  51. // example:
  52. // To ask dojo.cache to fetch content and store it in the cache, and sanitize the input
  53. // (the dojo["cache"] style of call is used to avoid an issue with the build system
  54. // erroneously trying to intern this example. To get the build system to intern your
  55. // dojo.cache calls, use the "dojo.cache" style of call):
  56. // | //If template.html contains "<html><body><h1>Hello</h1></body></html>", the
  57. // | //text variable will contain just "<h1>Hello</h1>".
  58. // | var text = dojo["cache"]("my.module", "template.html", {sanitize: true});
  59. // example:
  60. // Same example as previous, but demostrates how an object can be passed in as
  61. // the first argument, then the value argument can then be the second argument.
  62. // | //If template.html contains "<html><body><h1>Hello</h1></body></html>", the
  63. // | //text variable will contain just "<h1>Hello</h1>".
  64. // | var text = dojo["cache"](new dojo._Url("my/module/template.html"), {sanitize: true});
  65. //Module could be a string, or an object that has a toString() method
  66. //that will return a useful path. If it is an object, then the "url" argument
  67. //will actually be the value argument.
  68. if(typeof module == "string"){
  69. var pathObj = dojo.moduleUrl(module, url);
  70. }else{
  71. pathObj = module;
  72. value = url;
  73. }
  74. var key = pathObj.toString();
  75. var val = value;
  76. if(value != undefined && !dojo.isString(value)){
  77. val = ("value" in value ? value.value : undefined);
  78. }
  79. var sanitize = value && value.sanitize ? true : false;
  80. if(typeof val == "string"){
  81. //We have a string, set cache value
  82. val = cache[key] = sanitize ? dojo.cache._sanitize(val) : val;
  83. }else if(val === null){
  84. //Remove cached value
  85. delete cache[key];
  86. }else{
  87. //Allow cache values to be empty strings. If key property does
  88. //not exist, fetch it.
  89. if(!(key in cache)){
  90. val = dojo._getText(key);
  91. cache[key] = sanitize ? dojo.cache._sanitize(val) : val;
  92. }
  93. val = cache[key];
  94. }
  95. return val; //String
  96. };
  97. dojo.cache._sanitize = function(/*String*/val){
  98. // summary:
  99. // Strips <?xml ...?> declarations so that external SVG and XML
  100. // documents can be added to a document without worry. Also, if the string
  101. // is an HTML document, only the part inside the body tag is returned.
  102. // description:
  103. // Copied from dijit._Templated._sanitizeTemplateString.
  104. if(val){
  105. val = val.replace(/^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im, "");
  106. var matches = val.match(/<body[^>]*>\s*([\s\S]+)\s*<\/body>/im);
  107. if(matches){
  108. val = matches[1];
  109. }
  110. }else{
  111. val = "";
  112. }
  113. return val; //String
  114. };
  115. }