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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  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. /*-{
  27. var sheets = $doc.styleSheets;
  28. for(var i = 0; i < sheets.length; i++) {
  29. var sheet = sheets[i];
  30. if(sheet.href && sheet.href.indexOf("VAADIN/themes")>-1) {
  31. // $entry not needed as function is not exported
  32. 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);
  33. return;
  34. }
  35. }
  36. this.@com.vaadin.terminal.gwt.client.CSSRule::rules = [];
  37. }-*/;
  38. /*
  39. * Loops through all current style rules and collects all matching to
  40. * 'rules' array. The array is reverse ordered (last one found is first).
  41. */
  42. private static native JavaScriptObject searchForRule(
  43. final JavaScriptObject sheet, final String selector,
  44. final boolean deep)
  45. /*-{
  46. if(!$doc.styleSheets)
  47. return null;
  48. selector = selector.toLowerCase();
  49. var allMatches = [];
  50. // IE handles imported sheet differently
  51. if(deep && sheet.imports && sheet.imports.length > 0) {
  52. for(var i=0; i < sheet.imports.length; i++) {
  53. // $entry not needed as function is not exported
  54. var imports = @com.vaadin.terminal.gwt.client.CSSRule::searchForRule(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;Z)(sheet.imports[i], selector, deep);
  55. allMatches.concat(imports);
  56. }
  57. }
  58. var theRules = new Array();
  59. if (sheet.cssRules)
  60. theRules = sheet.cssRules
  61. else if (sheet.rules)
  62. theRules = sheet.rules
  63. var j = theRules.length;
  64. for(var i=0; i<j; i++) {
  65. var r = theRules[i];
  66. if(r.type == 1 || sheet.imports) {
  67. var selectors = r.selectorText.toLowerCase().split(",");
  68. var n = selectors.length;
  69. for(var m=0; m<n; m++) {
  70. if(selectors[m].replace(/^\s+|\s+$/g, "") == selector) {
  71. allMatches.unshift(r);
  72. break; // No need to loop other selectors for this rule
  73. }
  74. }
  75. } else if(deep && r.type == 3) {
  76. // Search @import stylesheet
  77. // $entry not needed as function is not exported
  78. var imports = @com.vaadin.terminal.gwt.client.CSSRule::searchForRule(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;Z)(r.styleSheet, selector, deep);
  79. allMatches = allMatches.concat(imports);
  80. }
  81. }
  82. return allMatches;
  83. }-*/;
  84. /**
  85. * Returns a specific property value from this CSS rule.
  86. *
  87. * @param propertyName
  88. * camelCase CSS property name
  89. * @return the value of the property as a String
  90. */
  91. public native String getPropertyValue(final String propertyName)
  92. /*-{
  93. var j = this.@com.vaadin.terminal.gwt.client.CSSRule::rules.length;
  94. for(var i=0; i<j; i++) {
  95. // $entry not needed as function is not exported
  96. var value = this.@com.vaadin.terminal.gwt.client.CSSRule::rules[i].style[propertyName];
  97. if(value)
  98. return value;
  99. }
  100. return null;
  101. }-*/;
  102. public String getSelector() {
  103. return selector;
  104. }
  105. }