tangram.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* CopyRight (c) 2010 Baidu */
  2. /*
  3. * Tangram
  4. * Copyright 2010 Baidu Inc. All rights reserved.
  5. *
  6. * @author: meizz
  7. * @namespace: baidu.page.createStyleSheet
  8. * @version: 2010-06-12
  9. */
  10. /*
  11. * Tangram
  12. * Copyright 2009 Baidu Inc. All rights reserved.
  13. *
  14. * path: baidu/browser/ie.js
  15. * author: allstar
  16. * version: 1.1.0
  17. * date: 2009/11/23
  18. */
  19. /*
  20. * Tangram
  21. * Copyright 2009 Baidu Inc. All rights reserved.
  22. *
  23. * path: baidu/browser.js
  24. * author: allstar, erik
  25. * version: 1.1.0
  26. * date: 2009/12/02
  27. */
  28. /*
  29. * Tangram
  30. * Copyright 2009 Baidu Inc. All rights reserved.
  31. *
  32. * path: baidu.js
  33. * author: allstar, erik
  34. * version: 1.1.0
  35. * date: 2009/12/2
  36. */
  37. /**
  38. * 声明baidu包
  39. */
  40. var baidu = baidu || {version: "1-2-2"}; // meizz 20100513 将 guid 升级成 \x06
  41. baidu.guid = "$BAIDU$";//提出guid,防止修改window[undefined] 20100504 berg
  42. /**
  43. * meizz 2010/02/04
  44. * 顶级域名 baidu 有可能被闭包劫持,而需要页面级唯一信息时需要用到下面这个对象
  45. */
  46. window[baidu.guid] = window[baidu.guid] || {};
  47. /**
  48. * 声明baidu.browser包
  49. */
  50. baidu.browser = baidu.browser || {};
  51. /**
  52. * 判断是否为ie浏览器
  53. */
  54. if (/msie (\d+\.\d)/i.test(navigator.userAgent)) {
  55. baidu.ie = baidu.browser.ie = document.documentMode || parseFloat(RegExp['\x241']);
  56. }
  57. /*
  58. * Tangram
  59. * Copyright 2009 Baidu Inc. All rights reserved.
  60. *
  61. * path: baidu/dom/insertHTML.js
  62. * author: allstar, erik
  63. * version: 1.1.0
  64. * date: 2009/12/04
  65. */
  66. /*
  67. * Tangram
  68. * Copyright 2009 Baidu Inc. All rights reserved.
  69. *
  70. * path: baidu/dom/g.js
  71. * author: allstar, erik
  72. * version: 1.1.0
  73. * date: 2009/11/17
  74. */
  75. /*
  76. * Tangram
  77. * Copyright 2009 Baidu Inc. All rights reserved.
  78. *
  79. * path: baidu/dom.js
  80. * author: allstar, erik
  81. * version: 1.1.0
  82. * date: 2009/12/02
  83. */
  84. /**
  85. * 声明baidu.dom包
  86. */
  87. baidu.dom = baidu.dom || {};
  88. /**
  89. * 从文档中获取指定的DOM元素
  90. *
  91. * @param {string|HTMLElement} id 元素的id或DOM元素
  92. * @return {HTMLElement} DOM元素,如果不存在,返回null,如果参数不合法,直接返回参数
  93. */
  94. baidu.dom.g = function (id) {
  95. if ('string' == typeof id || id instanceof String) {
  96. return document.getElementById(id);
  97. } else if (id && id.nodeName && (id.nodeType == 1 || id.nodeType == 9)) {
  98. //todo: 以后把这个判断去掉,不做验证。
  99. return id;
  100. }
  101. return null;
  102. };
  103. // 声明快捷方法
  104. baidu.g = baidu.G = baidu.dom.g;
  105. /**
  106. * 获取目标元素所属的window对象
  107. * 如果使用本函数插入带有script标签的HTML字符串,script标签对应的脚本将不会被执行。
  108. *
  109. * @param {HTMLElement|string} element 目标元素或目标元素的id
  110. * @param {string} position 插入html的位置信息,取值为beforeBegin,afterBegin,beforeEnd,afterEnd
  111. * @param {string} html 要插入的html
  112. */
  113. baidu.dom.insertHTML = function (element, position, html) {
  114. element = baidu.dom.g(element);
  115. if (element.insertAdjacentHTML) {
  116. element.insertAdjacentHTML(position, html);
  117. } else {
  118. // 这里不做"undefined" != typeof(HTMLElement) && !window.opera判断,其它浏览器将出错?!
  119. // 但是其实做了判断,其它浏览器下等于这个函数就不能执行了
  120. var range = element.ownerDocument.createRange();
  121. range.setStartBefore(element);
  122. var fragment = range.createContextualFragment(html),
  123. parent = element.parentNode, tmpEl;
  124. switch (position.toUpperCase()) {
  125. case 'BEFOREBEGIN':
  126. parent.insertBefore(fragment, element);
  127. break;
  128. case 'AFTERBEGIN':
  129. element.insertBefore(fragment, element.firstChild);
  130. break;
  131. case 'BEFOREEND':
  132. element.appendChild(fragment);
  133. break;
  134. case 'AFTEREND':
  135. (tmpEl = element.nextSibling) ? parent.insertBefore(fragment, tmpEl) : parent.appendChild(fragment);
  136. }
  137. }
  138. // 如果要代码最精简,还有一种写法
  139. // var fragment = range.createContextualFragment(html),
  140. // parent = element.parentNode, tmpEl = element;
  141. // switch (position.toUpperCase()) {
  142. // case 'AFTERBEGIN':
  143. // element = element.firstChild;
  144. // case 'BEFOREBEGIN':
  145. // parent.insertBefore(fragment, element);
  146. // break;
  147. // case 'BEFOREEND':
  148. // element.appendChild(fragment);
  149. // break;
  150. // case 'AFTEREND':
  151. // (element = element.nextSibling) ? parent.insertBefore(fragment, element) : parent.appendChild(fragment);
  152. // }
  153. // 增加一次判断,代码可以更少
  154. // var fragment = range.createContextualFragment(html),
  155. // parent = element.parentNode, tmpEl = element;
  156. // switch (position.toUpperCase()) {
  157. // case 'AFTERBEGIN':
  158. // element = element.firstChild;
  159. // break;
  160. // case 'BEFOREEND':
  161. // parent = element;
  162. // element = null;
  163. // break;
  164. // case 'AFTEREND':
  165. // element = element.nextSibling;
  166. // }
  167. // element ? parent.insertBefore(fragment, element) : parent.appendChild(fragment);
  168. };
  169. baidu.insertHTML = baidu.dom.insertHTML;
  170. /*
  171. * Tangram
  172. * Copyright 2009 Baidu Inc. All rights reserved.
  173. *
  174. * path: baidu/page.js
  175. * author: erik
  176. * version: 1.1.0
  177. * date: 2009/11/17
  178. */
  179. /**
  180. * 声明baidu.page包
  181. */
  182. baidu.page = baidu.page || {};
  183. /**
  184. * 创建CSS样式集对象 style sheet
  185. *
  186. * @param {JSON} options 配置信息
  187. * @config {Document} document 指定在哪个document下创建,默认是当前文档
  188. * @config {String} url css文件的URL
  189. * @config {Number} index 在文档里的排序索引
  190. * @return {Object} styleSheet对象
  191. */
  192. baidu.page.createStyleSheet = function(options){
  193. var op = options || {},
  194. doc = op.document || document,
  195. s;
  196. if (baidu.browser.ie) {
  197. return doc.createStyleSheet(op.url, op.index);
  198. } else {
  199. s = "<style type='text/css'></style>";
  200. op.url && (s="<link type='text/css' rel='stylesheet' href='"+op.url+"'/>");
  201. baidu.dom.insertHTML(doc.getElementsByTagName("HEAD")[0],"beforeEnd",s);
  202. //如果用户传入了url参数,下面访问sheet.rules的时候会报错
  203. if(op.url){
  204. return null;
  205. }
  206. var sheet = doc.styleSheets[doc.styleSheets.length - 1],
  207. rules = sheet.rules || sheet.cssRules;
  208. return {
  209. self : sheet
  210. ,rules : sheet.rules || sheet.cssRules
  211. ,addRule : function(selector, style, i) {
  212. if (sheet.addRule) {
  213. return sheet.addRule(selector, style, i);
  214. } else if (sheet.insertRule) {
  215. isNaN(i) && (i = rules.length);
  216. return sheet.insertRule(selector +"{"+ style +"}", i);
  217. }
  218. }
  219. ,removeRule : function(i) {
  220. if (sheet.removeRule) {
  221. sheet.removeRule(i);
  222. } else if (sheet.deleteRule) {
  223. isNaN(i) && (i = 0);
  224. sheet.deleteRule(i);
  225. }
  226. }
  227. }
  228. }
  229. };
  230. /*
  231. * styleSheet对象 有两个方法
  232. * addRule(selector, style, i)
  233. * removeRule(i)
  234. * 这两个方法已经做了浏览器兼容处理
  235. * 一个集合
  236. * rules
  237. */