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.

SelectorEngine.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package gquery.client;
  2. import com.google.gwt.core.client.GWT;
  3. import com.google.gwt.core.client.JavaScriptObject;
  4. import com.google.gwt.dom.client.Document;
  5. import com.google.gwt.dom.client.Element;
  6. import com.google.gwt.dom.client.Node;
  7. import com.google.gwt.dom.client.NodeList;
  8. import gquery.client.impl.SelectorEngineImpl;
  9. /**
  10. *
  11. */
  12. public class SelectorEngine {
  13. private SelectorEngineImpl impl;
  14. public SelectorEngine() {
  15. impl = (SelectorEngineImpl) GWT
  16. .create(SelectorEngineImpl.class);
  17. }
  18. public static native boolean eq(String s1, String s2) /*-{
  19. return s1 == s2;
  20. }-*/;
  21. public static native NodeList<Element> getElementsByClassName(String clazz,
  22. Node ctx) /*-{
  23. return ctx.getElementsByClassName(clazz);
  24. }-*/;
  25. public static native String or(String s1, String s2) /*-{
  26. return s1 || s2;
  27. }-*/;
  28. public static native NodeList<Element> querySelectorAll(String selector) /*-{
  29. return $doc.querySelectorAll(selector);
  30. }-*/;
  31. public static native NodeList<Element> querySelectorAll(String selector,
  32. Node ctx) /*-{
  33. return ctx.querySelectorAll(selector);
  34. }-*/;
  35. public NodeList<Element> select(String selector, Node ctx) {
  36. return impl.select(selector, ctx);
  37. }
  38. public static boolean truth(String a) {
  39. return GWT.isScript() ? truth0(a) : a != null && !"".equals(a);
  40. }
  41. public static boolean truth(JavaScriptObject a) {
  42. return GWT.isScript() ? truth0(a) : a != null;
  43. }
  44. public static NodeList<Element> xpathEvaluate(String selector, Node ctx) {
  45. return xpathEvaluate(selector, ctx, JSArray.create());
  46. }
  47. public static native NodeList<Element> xpathEvaluate(String selector,
  48. Node ctx, JSArray r) /*-{
  49. var node;
  50. var result = $doc.evaluate(selector, ctx, null, 0, null);
  51. while ((node = result.iterateNext())) {
  52. r.push(node);
  53. }
  54. return r;
  55. }-*/;
  56. private static native boolean truth0(String a) /*-{
  57. return a;
  58. }-*/;
  59. private static native boolean truth0(JavaScriptObject a) /*-{
  60. return a;
  61. }-*/;
  62. protected JSArray veryQuickId(Node context, String id) {
  63. JSArray r = JSArray.create();
  64. if (context.getNodeType() == Node.DOCUMENT_NODE) {
  65. r.addNode(((Document) context).getElementById(id));
  66. return r;
  67. } else {
  68. r.addNode(context.getOwnerDocument().getElementById(id));
  69. return r;
  70. }
  71. }
  72. public static native Node getNextSibling(Node n) /*-{
  73. return n.nextSibling || null;
  74. }-*/;
  75. public static native Node getPreviousSibling(Node n) /*-{
  76. return n.previousSibling || null;
  77. }-*/;
  78. }