Guest User

Untitled

a guest
Apr 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. var querySelectorAll;
  2. // multiple scoped selector not supported yet. e.g. ">embed,>object"
  3.  
  4. (function () {
  5. var toArray = function (nodes) {
  6. return [].slice.call(nodes, 0);
  7. };
  8. var squeezers = {
  9. ">": function (node) {
  10. return toArray(node.children);
  11. },
  12. "+": function (node) {
  13. var next = node.nextElementSibling;
  14. return next ? [next] : [];
  15. },
  16. "~": function (node) {
  17. var nodes = [], n;
  18. for (n = node.nextElementSibling; n; n = n.nextElementSibling)
  19. nodes.push(n);
  20. return nodes;
  21. }
  22. };
  23.  
  24. querySelectorAll = function (selector, context) {
  25. var scoped = /^\s*([>+~])(.*)/;
  26. var match = selector.match(scoped); // [all, type, selector] or null
  27. if (match) {
  28. return squeezers[match[1]](context).filter(function (node) {
  29. return node.webkitMatchesSelector(match[2]);
  30. });
  31. }
  32. return toArray(context.querySelectorAll(selector));
  33. }
  34. })();
  35.  
  36. /*
  37.  
  38. function like(a, b) {
  39. var p = a.every(function (x, i) { return x == b[i] });
  40. console.assert(p, "test failed: %s != %s", a, b);
  41. }
  42.  
  43. like(
  44. querySelectorAll("a", document.body),
  45. document.body.querySelectorAll("a")
  46. );
  47.  
  48. like(
  49. querySelectorAll("+ a", document.body),
  50. document.querySelectorAll("body > a")
  51. );
  52.  
  53. like(
  54. querySelectorAll("+ *", document.head),
  55. document.querySelectorAll("head + *")
  56. );
  57.  
  58. like(
  59. querySelectorAll("~ *", document.head),
  60. document.querySelectorAll("head ~ *")
  61. );
  62.  
  63. */
Add Comment
Please, Sign In to add comment