clone.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //.CommonJS
  2. var CSSOM = {
  3. CSSStyleSheet: require("./CSSStyleSheet").CSSStyleSheet,
  4. CSSStyleRule: require("./CSSStyleRule").CSSStyleRule,
  5. CSSMediaRule: require("./CSSMediaRule").CSSMediaRule,
  6. CSSSupportsRule: require("./CSSSupportsRule").CSSSupportsRule,
  7. CSSStyleDeclaration: require("./CSSStyleDeclaration").CSSStyleDeclaration,
  8. CSSKeyframeRule: require('./CSSKeyframeRule').CSSKeyframeRule,
  9. CSSKeyframesRule: require('./CSSKeyframesRule').CSSKeyframesRule
  10. };
  11. ///CommonJS
  12. /**
  13. * Produces a deep copy of stylesheet — the instance variables of stylesheet are copied recursively.
  14. * @param {CSSStyleSheet|CSSOM.CSSStyleSheet} stylesheet
  15. * @nosideeffects
  16. * @return {CSSOM.CSSStyleSheet}
  17. */
  18. CSSOM.clone = function clone(stylesheet) {
  19. var cloned = new CSSOM.CSSStyleSheet();
  20. var rules = stylesheet.cssRules;
  21. if (!rules) {
  22. return cloned;
  23. }
  24. var RULE_TYPES = {
  25. 1: CSSOM.CSSStyleRule,
  26. 4: CSSOM.CSSMediaRule,
  27. //3: CSSOM.CSSImportRule,
  28. //5: CSSOM.CSSFontFaceRule,
  29. //6: CSSOM.CSSPageRule,
  30. 8: CSSOM.CSSKeyframesRule,
  31. 9: CSSOM.CSSKeyframeRule,
  32. 12: CSSOM.CSSSupportsRule
  33. };
  34. for (var i=0, rulesLength=rules.length; i < rulesLength; i++) {
  35. var rule = rules[i];
  36. var ruleClone = cloned.cssRules[i] = new RULE_TYPES[rule.type]();
  37. var style = rule.style;
  38. if (style) {
  39. var styleClone = ruleClone.style = new CSSOM.CSSStyleDeclaration();
  40. for (var j=0, styleLength=style.length; j < styleLength; j++) {
  41. var name = styleClone[j] = style[j];
  42. styleClone[name] = style[name];
  43. styleClone._importants[name] = style.getPropertyPriority(name);
  44. }
  45. styleClone.length = style.length;
  46. }
  47. if (rule.hasOwnProperty('keyText')) {
  48. ruleClone.keyText = rule.keyText;
  49. }
  50. if (rule.hasOwnProperty('selectorText')) {
  51. ruleClone.selectorText = rule.selectorText;
  52. }
  53. if (rule.hasOwnProperty('mediaText')) {
  54. ruleClone.mediaText = rule.mediaText;
  55. }
  56. if (rule.hasOwnProperty('conditionText')) {
  57. ruleClone.conditionText = rule.conditionText;
  58. }
  59. if (rule.hasOwnProperty('cssRules')) {
  60. ruleClone.cssRules = clone(rule).cssRules;
  61. }
  62. }
  63. return cloned;
  64. };
  65. //.CommonJS
  66. exports.clone = CSSOM.clone;
  67. ///CommonJS