<commit_before>define(
  'ephox.alloy.positioning.SelectionAnchor',

  [
    'ephox.alloy.alien.Boxes',
    'ephox.alloy.alien.CssPosition',
    'ephox.alloy.alien.Descend',
    'ephox.alloy.positioning.Anchoring',
    'ephox.alloy.positioning.ContainerOffsets',
    'ephox.boulder.api.FieldSchema',
    'ephox.fussy.api.SelectionRange',
    'ephox.fussy.api.WindowSelection',
    'ephox.peanut.Fun',
    'ephox.perhaps.Option',
    'ephox.repartee.api.Bubble',
    'ephox.repartee.api.Layout',
    'ephox.repartee.api.Origins',
    'ephox.scullion.Struct',
    'ephox.sugar.alien.Position',
    'ephox.sugar.api.Direction',
    'ephox.sugar.api.Node',
    'ephox.sugar.api.Traverse'
  ],

  function (Boxes, CssPosition, Descend, Anchoring, ContainerOffsets, FieldSchema, SelectionRange, WindowSelection, Fun, Option, Bubble, Layout, Origins, Struct, Position, Direction, Node, Traverse) {
    var point = Struct.immutable('element', 'offset');

    // A range from (a, 1) to (body, end) was giving the wrong bounds.
    var descendOnce = function (element, offset) {
      return Node.isText(element) ? point(element, offset) : Descend.descendOnce(element, offset);
    };

    var getAnchorSelection = function (win, anchorInfo) {
      var getSelection = anchorInfo.getSelection().getOrThunk(function () {
        return function () {
          return WindowSelection.get(win);
        };
      });

      return getSelection().map(function (sel) {
        var modStart = descendOnce(sel.start(), sel.soffset());
        var modFinish = descendOnce(sel.finish(), sel.foffset());
        return SelectionRange.general(modStart.element(), modStart.offset(), modFinish.element(), modFinish.offset());
      });
    };

    var placement = function (component, posInfo, anchorInfo, origin) {
      var win = Traverse.defaultView(anchorInfo.root()).dom();
      var rootPoint = ContainerOffsets.getRootPoint(component, origin, anchorInfo);

      var selectionBox = getAnchorSelection(win, anchorInfo).bind(function (sel) {
        // This represents the *visual* rectangle of the selection.
        var optRect = WindowSelection.rectangleAt(win, sel.start(), sel.soffset(), sel.finish(), sel.foffset());
        return optRect.map(function (rawRect) {
          // NOTE: We are going to have to do some interesting things to make inline toolbars not appear over the toolbar.
          var point = CssPosition.screen(
            Position(rawRect.left, rawRect.top)
          );

          console.log('rawRect', rawRect.height, rawRect.top);

          return Boxes.pointed(point, rawRect.width, rawRect.height);
        });
      });

      return selectionBox.map(function (box) {<commit_msg>VAN-44-wheel: various approaches to fix positioning<commit_after>define(
  'ephox.alloy.positioning.SelectionAnchor',

  [
    'ephox.alloy.alien.Boxes',
    'ephox.alloy.alien.CssPosition',
    'ephox.alloy.alien.Descend',
    'ephox.alloy.positioning.Anchoring',
    'ephox.alloy.positioning.ContainerOffsets',
    'ephox.boulder.api.FieldSchema',
    'ephox.fussy.api.SelectionRange',
    'ephox.fussy.api.WindowSelection',
    'ephox.oath.proximity.Awareness',
    'ephox.peanut.Fun',
    'ephox.perhaps.Option',
    'ephox.repartee.api.Bubble',
    'ephox.repartee.api.Layout',
    'ephox.repartee.api.Origins',
    'ephox.scullion.Struct',
    'ephox.sugar.alien.Position',
    'ephox.sugar.api.Direction',
    'ephox.sugar.api.Node',
    'ephox.sugar.api.Traverse'
  ],

  function (Boxes, CssPosition, Descend, Anchoring, ContainerOffsets, FieldSchema, SelectionRange, WindowSelection, Awareness, Fun, Option, Bubble, Layout, Origins, Struct, Position, Direction, Node, Traverse) {
    var point = Struct.immutable('element', 'offset');

    // A range from (a, 1) to (body, end) was giving the wrong bounds.
    var descendOnce = function (element, offset) {
      return Node.isText(element) ? point(element, offset) : Descend.descendOnce(element, offset);
    };

    var getAnchorSelection = function (win, anchorInfo) {
      var getSelection = anchorInfo.getSelection().getOrThunk(function () {
        return function () {
          return WindowSelection.get(win);
        };
      });

      return getSelection().map(function (sel) {
        var modStart = descendOnce(sel.start(), sel.soffset());
        var modFinish = descendOnce(sel.finish(), sel.foffset());
        var originalStart = point(sel.start(), sel.soffset());
        var originalFinish = point(sel.finish(), sel.foffset());
        var useStart = Node.name(modStart.element()) === 'br' ? originalStart : modStart;
        var useFinish = Node.name(modFinish.element()) === 'br' ? originalFinish : modFinish;
        return SelectionRange.general(useStart.element(), useStart.offset(), useFinish.element(), useFinish.offset());
      });
    };

    var placement = function (component, posInfo, anchorInfo, origin) {
      var win = Traverse.defaultView(anchorInfo.root()).dom();
      var rootPoint = ContainerOffsets.getRootPoint(component, origin, anchorInfo);

      console.log('selection.placement');

      var selectionBox = getAnchorSelection(win, anchorInfo).bind(function (sel) {
        // This represents the *visual* rectangle of the selection.
        var optRect = WindowSelection.rectangleAt(win, sel.start(), sel.soffset(), sel.finish(), sel.foffset()).orThunk(function () {
          // Certain things like <p><br/></p> with (p, 0) as collapsed selection do not return a client rectangle
          return WindowSelection.rectangleAt(win, sel.start(), sel.soffset(), anchorInfo.root(), Awareness.getEnd(anchorInfo.root())).map(function (rectToEnd) {
            return {
              left: rectToEnd.left,
              top: rectToEnd.top,
              width: 0,
              height: 0
            };
          });
        });
        return optRect.map(function (rawRect) {
          // NOTE: We are going to have to do some interesting things to make inline toolbars not appear over the toolbar.
          var point = CssPosition.screen(
            Position(rawRect.left, rawRect.top)
          );

          console.log('rawRect', rawRect.height, rawRect.top);

          return Boxes.pointed(point, rawRect.width, rawRect.height);
        });
      });

      return selectionBox.map(function (box) {
{'fraction_non_alphanumeric': 0.1359023152553124, 'fraction_numerical': 0.0011100539169045353, 'mean_word_length': 3.1357377049180326, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7316903', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1934, 'n_words': 396}