<commit_before>(function () {

  // Main code
  function jqueryLoaded() {
    $(function() {



      /**
       * Send data to main Pontoon code
       */
      function sendData() {
        // Deep copy: http://api.jquery.com/jQuery.extend
        var entities = $.extend(true, [], Pontoon.entities);
        $(entities).each(function () {
          delete this.node;
        });

        postMessage("DATA", {
          entities: entities
        });
      }



      /**
       * Render main UI and handle events
       */
      function renderHandle() {
        sendData();
        postMessage("RENDER", {
          url: Pontoon.project.url,
          title: Pontoon.project.title
        });

        // Update UI and progress when saved
        $(".pontoon-editable-toolbar > .save").click(function () {
          var element = $(this).parent()[0].target,
              entity = element.entity,
              content = $(element).html();

          if (Pontoon.user.localizer || !entity.translation[0].approved) {
            entity.translation[0].string = content;
            $(entity.node).each(function() {
              this.html(content);
            });
            sendData();
          } else {
            $(element).html(entity.translation[0].string);
          }

          postMessage("UPDATE", {
            id: entity.id,
            content: content
          });
        });

        // Do not change anything when cancelled
        $(".pontoon-editable-toolbar > .cancel").click(function () {
          var element = $(this).parent()[0].target,
              entity = element.entity;

          $(element).html(entity.translation[0].string || entity.original);
          postMessage("INACTIVE", entity.id);
        });

        // Prevent button click with space
        $("button").unbind("keyup.pontoon").bind("keyup.pontoon", function (e) {
          var key = e.which;
          if ($(".pontoon-editable-toolbar .save").is(":visible")) {
            if (key === 32) {
              e.preventDefault();
            }
          }
        });

        // In-place keyboard shortcuts
        $("html").unbind("keydown.pontoon").bind("keydown.pontoon", function (e) {
          var key = e.which,
              toolbar = $(".pontoon-editable-toolbar"),
              save = toolbar.find(".save"),
              cancel = toolbar.find(".cancel");

          if (save.is(":visible")) {
            var target = toolbar[0].target,
                entity = target.entity,
                id = entity.id,
                next = id + 1,
                entities = Pontoon.entities;

            if (key === 13 && !e.shiftKey) { // Enter: confirm translation
              save.click();
              hideToolbar(target);
              return false;
            }

            if (key === 27) { // Esc: status quo
              cancel.click();
              hideToolbar(target);
              return false;
            }
          }
        });
      }



      /**
       * Makes DOM nodes editable using contentEditable property
       *
       * entity Entity object
       */ 
      function makeEditable(entity) {
        entity.body = true;
        $(entity.node).each(function() {
          this[0].entity = entity; // Store entity reference to the node

          // Show/hide toolbar on node hover
          if (!this.handlersAttached) {
            this.hover(function () {
              showToolbar(this);
            }, function() {
              hideToolbar(this);
            });
            this.handlersAttached = true;
          }
        });

        // Show/hide toolbar on entity hover
        entity.hover = function () {
          showToolbar(this.node[0][0]);
        };
        entity.unhover = function () {
          hideToolbar(this.node[0][0]);
        };
      }



      /**
       * Extract entities from the document, not prepared for working with Pontoon
       *
       * Create entity object from every non-empty text node
       * Exclude nodes from special tags (e.g. <script>) and with translate=no attribute
       * Skip nodes already included in parent nodes
       * Add temporary pontoon-entity class to prevent duplicate entities when guessing
       */
      function loadEntitiesGuess() {
        var counter = 0;

        // <noscript> contents are not in the DOM
        $('noscript').each(function() {
          $("<div/>", {
            class: "pontoon-noscript",
            innerHTML: $(this).text()
          }).appendTo("body");
        });

        $(':not("script, style, iframe, noscript, [translate=\'no\']")').contents().each(function () {
          if (this.nodeType === Node.TEXT_NODE && $.trim(this.nodeValue).length > 0 && $(this).parents(".pontoon-entity").length === 0) {
            var entity = {},
                parent = $(this).parent();

            // If project uses hooks, but not available in the DB, remove <!--l10n--> comment nodes
            parent.contents().each(function () {
              if (this.nodeType === Node.COMMENT_NODE && this.nodeValue.indexOf('l10n') === 0) {
                $(this).remove();
              }
            });

            entity.id = counter;
            counter++;
            entity.original = parent.html();

            // Head entities cannot be edited in place
            if ($(this).parents('head').length === 0) {
              entity.node = [parent];
              makeEditable(entity);
            }

            // Remove entities from child nodes if parent node is entity
            parent.find(".pontoon-entity").each(function() {
              delete this.entity;
              Pontoon.entities.pop();
              entity.id--;
              counter--;
            });

            Pontoon.entities.push(entity);
            parent.addClass("pontoon-entity");
          }
        });

        $(".pontoon-entity").removeClass("pontoon-entity");
        $(".pontoon-noscript").remove();
        renderHandle();
      }



      /*
       * Match entities and elements with exact same contents
       */
      function loadEntitiesMatch() {
        var counter = 0,
            elements = {};

        $(':not("script, style, iframe, noscript, [translate=\'no\']")')
          .children().each(function() {
            elements[$.trim($(this).html())] = {
              node: $(this),
              body: !$(this).parents('head').length
            };
        });

        $(Pontoon.entities).each(function(i, entity) {
          var translation = entity.translation[0].string,
              original = entity.original,
              element = elements[original];

          entity.id = counter;

          if (element) {
            if (translation) {
              element.node.html(translation);
            }

            // Head entities cannot be edited in place
            if (element.body) {
              if (!entity.node) {
                entity.node = [element.node];
              } else {
                entity.node.push(element.node);
              }
              makeEditable(entity);
            }
          }

          counter++;
        });

        renderHandle();
      }



      /**
       * Match entities and strings prepended with l10n comment nodes
       * Example: <!--l10n-->Hello World
       */
      function loadEntitiesHooks() {
        var counter = 0,
            l10n = {};

        // Create object with l10n comment nodes
        $('*').contents().each(function () {<commit_msg>Match source strings with all occurences in the DOM<commit_after>(function () {

  // Main code
  function jqueryLoaded() {
    $(function() {



      /**
       * Send data to main Pontoon code
       */
      function sendData() {
        // Deep copy: http://api.jquery.com/jQuery.extend
        var entities = $.extend(true, [], Pontoon.entities);
        $(entities).each(function () {
          delete this.node;
        });

        postMessage("DATA", {
          entities: entities
        });
      }



      /**
       * Render main UI and handle events
       */
      function renderHandle() {
        sendData();
        postMessage("RENDER", {
          url: Pontoon.project.url,
          title: Pontoon.project.title
        });

        // Update UI and progress when saved
        $(".pontoon-editable-toolbar > .save").click(function () {
          var element = $(this).parent()[0].target,
              entity = element.entity,
              content = $(element).html();

          if (Pontoon.user.localizer || !entity.translation[0].approved) {
            entity.translation[0].string = content;
            $(entity.node).each(function() {
              this.html(content);
            });
            sendData();
          } else {
            $(element).html(entity.translation[0].string);
          }

          postMessage("UPDATE", {
            id: entity.id,
            content: content
          });
        });

        // Do not change anything when cancelled
        $(".pontoon-editable-toolbar > .cancel").click(function () {
          var element = $(this).parent()[0].target,
              entity = element.entity;

          $(element).html(entity.translation[0].string || entity.original);
          postMessage("INACTIVE", entity.id);
        });

        // Prevent button click with space
        $("button").unbind("keyup.pontoon").bind("keyup.pontoon", function (e) {
          var key = e.which;
          if ($(".pontoon-editable-toolbar .save").is(":visible")) {
            if (key === 32) {
              e.preventDefault();
            }
          }
        });

        // In-place keyboard shortcuts
        $("html").unbind("keydown.pontoon").bind("keydown.pontoon", function (e) {
          var key = e.which,
              toolbar = $(".pontoon-editable-toolbar"),
              save = toolbar.find(".save"),
              cancel = toolbar.find(".cancel");

          if (save.is(":visible")) {
            var target = toolbar[0].target,
                entity = target.entity,
                id = entity.id,
                next = id + 1,
                entities = Pontoon.entities;

            if (key === 13 && !e.shiftKey) { // Enter: confirm translation
              save.click();
              hideToolbar(target);
              return false;
            }

            if (key === 27) { // Esc: status quo
              cancel.click();
              hideToolbar(target);
              return false;
            }
          }
        });
      }



      /**
       * Makes DOM nodes editable using contentEditable property
       *
       * entity Entity object
       */ 
      function makeEditable(entity) {
        entity.body = true;
        $(entity.node).each(function() {
          this[0].entity = entity; // Store entity reference to the node

          // Show/hide toolbar on node hover
          if (!this.handlersAttached) {
            this.hover(function () {
              showToolbar(this);
            }, function() {
              hideToolbar(this);
            });
            this.handlersAttached = true;
          }
        });

        // Show/hide toolbar on entity hover
        entity.hover = function () {
          showToolbar(this.node[0][0]);
        };
        entity.unhover = function () {
          hideToolbar(this.node[0][0]);
        };
      }



      /**
       * Extract entities from the document, not prepared for working with Pontoon
       *
       * Create entity object from every non-empty text node
       * Exclude nodes from special tags (e.g. <script>) and with translate=no attribute
       * Skip nodes already included in parent nodes
       * Add temporary pontoon-entity class to prevent duplicate entities when guessing
       */
      function loadEntitiesGuess() {
        var counter = 0;

        // <noscript> contents are not in the DOM
        $('noscript').each(function() {
          $("<div/>", {
            class: "pontoon-noscript",
            innerHTML: $(this).text()
          }).appendTo("body");
        });

        $(':not("script, style, iframe, noscript, [translate=\'no\']")').contents().each(function () {
          if (this.nodeType === Node.TEXT_NODE && $.trim(this.nodeValue).length > 0 && $(this).parents(".pontoon-entity").length === 0) {
            var entity = {},
                parent = $(this).parent();

            // If project uses hooks, but not available in the DB, remove <!--l10n--> comment nodes
            parent.contents().each(function () {
              if (this.nodeType === Node.COMMENT_NODE && this.nodeValue.indexOf('l10n') === 0) {
                $(this).remove();
              }
            });

            entity.id = counter;
            counter++;
            entity.original = parent.html();

            // Head entities cannot be edited in place
            if ($(this).parents('head').length === 0) {
              entity.node = [parent];
              makeEditable(entity);
            }

            // Remove entities from child nodes if parent node is entity
            parent.find(".pontoon-entity").each(function() {
              delete this.entity;
              Pontoon.entities.pop();
              entity.id--;
              counter--;
            });

            Pontoon.entities.push(entity);
            parent.addClass("pontoon-entity");
          }
        });

        $(".pontoon-entity").removeClass("pontoon-entity");
        $(".pontoon-noscript").remove();
        renderHandle();
      }



      /*
       * Match entities and elements with exact same contents
       */
      function loadEntitiesMatch() {
        var counter = 0,
            elements = {};

        $(':not("script, style, iframe, noscript, [translate=\'no\']")')
          .children().each(function() {
            var text = $.trim($(this).html());
            if (!elements[text]) {
              elements[text] = {
                node: [$(this)],
                body: [!$(this).parents('head').length]
              };
            } else {
              elements[text].node.push($(this));
              elements[text].body.push(!$(this).parents('head').length);
            }
        });

        $(Pontoon.entities).each(function(i, entity) {
          var translation = entity.translation[0].string,
              original = entity.original,
              element = elements[original];

          entity.id = counter;

          if (element) {
            $(element.node).each(function(i) {
              if (translation) {
                this.html(translation);
              }

              // Head entities cannot be edited in place
              if (element.body[i]) {
                if (!entity.node) {
                  entity.node = [this];
                } else {
                  entity.node.push(this);
                }
                makeEditable(entity);
              }
            });
          }

          counter++;
        });

        renderHandle();
      }



      /**
       * Match entities and strings prepended with l10n comment nodes
       * Example: <!--l10n-->Hello World
       */
      function loadEntitiesHooks() {
        var counter = 0,
            l10n = {};

        // Create object with l10n comment nodes
        $('*').contents().each(function () {
{'fraction_non_alphanumeric': 0.13995677516536773, 'fraction_numerical': 0.00510838954744908, 'mean_word_length': 1.4732750242954324, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 19, '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': '4648714', 'n_tokens_mistral': 4034, 'n_tokens_neox': 3861, 'n_words': 879}