Advertisement
Guest User

JSDomHighlight

a guest
Oct 25th, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(selector) {
  2.     let targetLimit = 0;
  3.     function scan(target) {
  4.         let children = $(target).children();
  5.         if (children.length == 0)
  6.             return target;
  7.         let childWithMostChildren, maxChildrenCount = Number.NEGATIVE_INFINITY;
  8.         for (let i = 0; i < children.length; i++) {
  9.             if ($(children[i]).children().length > maxChildrenCount) {
  10.                 maxChildrenCount = $(children[i]).children().length;
  11.                 childWithMostChildren = i;
  12.             }
  13.         }
  14.         targetLimit++;
  15.         return scan(children[childWithMostChildren]);
  16.     }
  17.     let deepestNode = scan(selector);
  18.     $(deepestNode).addClass('highlight');
  19.     let targets = $(deepestNode).parents();
  20.     for (let i = 0; i < targets.length && i < targetLimit; i++)
  21.         $(targets[i]).addClass('highlight');
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement