You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CSSRule.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client;
  5. import com.google.gwt.core.client.JavaScriptObject;
  6. /**
  7. * Utility class for fetching CSS properties from DOM StyleSheets JS object.
  8. */
  9. public class CSSRule {
  10. private final String selector;
  11. private JavaScriptObject rules = null;
  12. /**
  13. *
  14. * @param selector
  15. * the CSS selector to search for in the stylesheets
  16. * @param deep
  17. * should the search follow any @import statements?
  18. */
  19. public CSSRule(final String selector, final boolean deep) {
  20. this.selector = selector;
  21. fetchRule(selector, deep);
  22. }
  23. // TODO how to find the right LINK-element? We should probably give the
  24. // stylesheet a name.
  25. private native void fetchRule(final String selector, final boolean deep) /*-{
  26. var sheets = $doc.styleSheets;
  27. for(var i = 0; i < sheets.length; i++) {
  28. var sheet = sheets[i];
  29. if(sheet.href && sheet.href.indexOf("VAADIN/themes")>-1) {
  30. this.@com.vaadin.terminal.gwt.client.CSSRule::rules = @com.vaadin.terminal.gwt.client.CSSRule::searchForRule(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;Z)(sheet, selector, deep);
  31. return;
  32. }
  33. }
  34. this.@com.vaadin.terminal.gwt.client.CSSRule::rules = [];
  35. }-*/;
  36. /*
  37. * Loops through all current style rules and collects all matching to
  38. * 'rules' array. The array is reverse ordered (last one found is first).
  39. */
  40. private static native JavaScriptObject searchForRule(
  41. final JavaScriptObject sheet, final String selector,
  42. final boolean deep) /*-{
  43. if(!$doc.styleSheets)
  44. return null;
  45. selector = selector.toLowerCase();
  46. var allMatches = [];
  47. // IE handles imported sheet differently
  48. if(deep && sheet.imports && sheet.imports.length > 0) {
  49. for(var i=0; i < sheet.imports.length; i++) {
  50. var imports = @com.vaadin.terminal.gwt.client.CSSRule::searchForRule(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;Z)(sheet.imports[i], selector, deep);
  51. allMatches.concat(imports);
  52. }
  53. }
  54. var theRules = new Array();
  55. if (sheet.cssRules)
  56. theRules = sheet.cssRules
  57. else if (sheet.rules)
  58. theRules = sheet.rules
  59. var j = theRules.length;
  60. for(var i=0; i<j; i++) {
  61. var r = theRules[i];
  62. if(r.type == 1 || sheet.imports) {
  63. var selectors = r.selectorText.toLowerCase().split(",");
  64. var n = selectors.length;
  65. for(var m=0; m<n; m++) {
  66. if(selectors[m].replace(/^\s+|\s+$/g, "") == selector) {
  67. allMatches.unshift(r);
  68. break; // No need to loop other selectors for this rule
  69. }
  70. }
  71. } else if(deep && r.type == 3) {
  72. // Search @import stylesheet
  73. var imports = @com.vaadin.terminal.gwt.client.CSSRule::searchForRule(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;Z)(r.styleSheet, selector, deep);
  74. allMatches = allMatches.concat(imports);
  75. }
  76. }
  77. return allMatches;
  78. }-*/;
  79. /**
  80. * Returns a specific property value from this CSS rule.
  81. *
  82. * @param propertyName
  83. * camelCase CSS property name
  84. * @return the value of the property as a String
  85. */
  86. public native String getPropertyValue(final String propertyName) /*-{
  87. var j = this.@com.vaadin.terminal.gwt.client.CSSRule::rules.length;
  88. for(var i=0; i<j; i++) {
  89. var value = this.@com.vaadin.terminal.gwt.client.CSSRule::rules[i].style[propertyName];
  90. if(value)
  91. return value;
  92. }
  93. return null;
  94. }-*/;
  95. public String getSelector() {
  96. return selector;
  97. }
  98. }