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.

SelectorEngineNative.java 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright 2011, The gwtquery team.
  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.google.gwt.query.client.impl;
  17. import static com.google.gwt.query.client.GQuery.console;
  18. import com.google.gwt.core.client.GWT;
  19. import com.google.gwt.dom.client.Element;
  20. import com.google.gwt.dom.client.Node;
  21. import com.google.gwt.dom.client.NodeList;
  22. import com.google.gwt.query.client.js.JsNamedArray;
  23. /**
  24. * Runtime selector engine implementation for browsers with native
  25. * querySelectorAll support.
  26. */
  27. public class SelectorEngineNative extends SelectorEngineImpl {
  28. // querySelectorAll unsupported selectors
  29. public static String NATIVE_EXCEPTIONS_REGEXP =
  30. "(^[\\./]/.*)|(.*(:contains|:first([^-]|$)|:last([^-]|$)|:even|:odd)).*";
  31. private static HasSelector impl;
  32. static JsNamedArray<String> cache;
  33. public SelectorEngineNative() {
  34. if (impl == null) {
  35. impl = GWT.create(HasSelector.class);
  36. GWT.log("GQuery - Created HasSelector: " + impl.getClass().getName());
  37. }
  38. }
  39. public NodeList<Element> select(String selector, Node ctx) {
  40. // querySelectorAllImpl does not support ids starting with a digit.
  41. // if (selector.matches("#[\\w\\-]+")) {
  42. // return SelectorEngine.veryQuickId(selector.substring(1), ctx);
  43. // } else
  44. if (selector.contains("!=")) {
  45. if (cache == null) {
  46. cache = JsNamedArray.create();
  47. }
  48. String xsel = cache.get(selector);
  49. if (xsel == null) {
  50. xsel = selector.replaceAll("(\\[\\w+)!(=[^\\]]+\\])", ":not($1$2)");
  51. cache.put(selector, xsel);
  52. }
  53. selector = xsel;
  54. }
  55. if (!SelectorEngine.HAS_QUERY_SELECTOR || selector.matches(NATIVE_EXCEPTIONS_REGEXP)) {
  56. return impl.select(selector, ctx);
  57. } else {
  58. try {
  59. return SelectorEngine.querySelectorAllImpl(selector, ctx);
  60. } catch (Exception e) {
  61. console.info("ERROR SelectorEngineNative " + e.getMessage()
  62. + " " + selector + ", falling back to "
  63. + impl.getClass().getName().replaceAll(".*\\.", ""));
  64. return impl.select(selector, ctx);
  65. }
  66. }
  67. }
  68. }