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 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. public CSSRule(String selector) {
  13. this.selector = selector;
  14. fetchRule(selector);
  15. }
  16. // TODO how to find the right LINK-element? We should probably give the
  17. // stylesheet a name.
  18. private native void fetchRule(final String selector)
  19. /*-{
  20. this.@com.vaadin.terminal.gwt.client.CSSRule::rules = @com.vaadin.terminal.gwt.client.CSSRule::searchForRule(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;)($doc.styleSheets[1], selector);
  21. }-*/;
  22. /*
  23. * Loops through all current style rules and collects all matching to
  24. * 'rules' array. The array is reverse ordered (last one found is first).
  25. */
  26. private static native JavaScriptObject searchForRule(
  27. JavaScriptObject sheet, final String selector)
  28. /*-{
  29. if(!$doc.styleSheets)
  30. return null;
  31. selector = selector.toLowerCase();
  32. var allMatches = [];
  33. var theRules = new Array();
  34. if (sheet.cssRules)
  35. theRules = sheet.cssRules
  36. else if (sheet.rules)
  37. theRules = sheet.rules
  38. var j = theRules.length;
  39. for(var i=0; i<j; i++) {
  40. var r = theRules[i];
  41. if(r.type == 3) {
  42. allMatches.unshift(@com.vaadin.terminal.gwt.client.CSSRule::searchForRule(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;)(r.styleSheet, selector));
  43. } else if(r.type == 1) {
  44. var selectors = r.selectorText.toLowerCase().split(",");
  45. var n = selectors.length;
  46. for(var m=0; m<n; m++) {
  47. if(selectors[m].replace(/^\s+|\s+$/g, "") == selector) {
  48. allMatches.unshift(r);
  49. break; // No need to loop other selectors for this rule
  50. }
  51. }
  52. }
  53. }
  54. return allMatches;
  55. }-*/;
  56. /**
  57. * Returns a specific property value from this CSS rule.
  58. *
  59. * @param propertyName
  60. * @return
  61. */
  62. public native String getPropertyValue(final String propertyName)
  63. /*-{
  64. for(var i=0; i<this.@com.vaadin.terminal.gwt.client.CSSRule::rules.length; i++){
  65. var value = this.@com.vaadin.terminal.gwt.client.CSSRule::rules[i].style[propertyName];
  66. if(value)
  67. return value;
  68. }
  69. return null;
  70. }-*/;
  71. public String getSelector() {
  72. return selector;
  73. }
  74. }