snapshot.js 1.9 KB

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