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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client;
  17. import com.google.gwt.core.client.JavaScriptObject;
  18. /**
  19. * Utility class for fetching CSS properties from DOM StyleSheets JS object.
  20. */
  21. public class CSSRule {
  22. private final String selector;
  23. private JavaScriptObject rules = null;
  24. /**
  25. *
  26. * @param selector
  27. * the CSS selector to search for in the stylesheets
  28. * @param deep
  29. * should the search follow any @import statements?
  30. */
  31. public CSSRule(final String selector, final boolean deep) {
  32. this.selector = selector;
  33. fetchRule(selector, deep);
  34. }
  35. // TODO how to find the right LINK-element? We should probably give the
  36. // stylesheet a name.
  37. private native void fetchRule(final String selector, final boolean deep)
  38. /*-{
  39. var sheets = $doc.styleSheets;
  40. for (var i = 0; i < sheets.length; i++) {
  41. var sheet = sheets[i];
  42. if (sheet.href && sheet.href.indexOf("VAADIN/themes")>-1) {
  43. // $entry not needed as function is not exported
  44. this.@com.vaadin.client.CSSRule::rules = @com.vaadin.client.CSSRule::searchForRule(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;Z)(sheet, selector, deep);
  45. return;
  46. }
  47. }
  48. this.@com.vaadin.client.CSSRule::rules = [];
  49. }-*/;
  50. /*
  51. * Loops through all current style rules and collects all matching to
  52. * 'rules' array. The array is reverse ordered (last one found is first).
  53. */
  54. private static native JavaScriptObject searchForRule(
  55. final JavaScriptObject sheet, final String selector,
  56. final boolean deep)
  57. /*-{
  58. if (!$doc.styleSheets)
  59. return null;
  60. selector = selector.toLowerCase('en');
  61. var allMatches = [];
  62. // IE handles imported sheet differently
  63. if (deep && sheet.imports && sheet.imports.length > 0) {
  64. for (var i=0; i < sheet.imports.length; i++) {
  65. // $entry not needed as function is not exported
  66. var imports = @com.vaadin.client.CSSRule::searchForRule(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;Z)(sheet.imports[i], selector, deep);
  67. allMatches.concat(imports);
  68. }
  69. }
  70. var theRules = new Array();
  71. if (sheet.cssRules)
  72. theRules = sheet.cssRules
  73. else if (sheet.rules)
  74. theRules = sheet.rules
  75. var j = theRules.length;
  76. for (var i=0; i<j; i++) {
  77. var r = theRules[i];
  78. if (r.type == 1 || sheet.imports) {
  79. var selectors = r.selectorText.toLowerCase('en').split(",");
  80. var n = selectors.length;
  81. for (var m=0; m<n; m++) {
  82. if (selectors[m].replace(/^\s+|\s+$/g, "") == selector) {
  83. allMatches.unshift(r);
  84. break; // No need to loop other selectors for this rule
  85. }
  86. }
  87. } else if (deep && r.type == 3) {
  88. // Search @import stylesheet
  89. // $entry not needed as function is not exported
  90. var imports = @com.vaadin.client.CSSRule::searchForRule(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;Z)(r.styleSheet, selector, deep);
  91. allMatches = allMatches.concat(imports);
  92. }
  93. }
  94. return allMatches;
  95. }-*/;
  96. /**
  97. * Returns a specific property value from this CSS rule.
  98. *
  99. * @param propertyName
  100. * camelCase CSS property name
  101. * @return the value of the property as a String
  102. */
  103. public native String getPropertyValue(final String propertyName)
  104. /*-{
  105. var j = this.@com.vaadin.client.CSSRule::rules.length;
  106. for (var i=0; i<j; i++) {
  107. // $entry not needed as function is not exported
  108. var value = this.@com.vaadin.client.CSSRule::rules[i].style[propertyName];
  109. if (value)
  110. return value;
  111. }
  112. return null;
  113. }-*/;
  114. public String getSelector() {
  115. return selector;
  116. }
  117. }