Skip to content
Snippets Groups Projects
Select Git revision
  • 1.9.7
  • develop default protected
  • dependabot/npm_and_yarn/dev-dependencies-fc5944c4b5
  • dependabot/npm_and_yarn/dev-dependencies-fdd5346215
  • dependabot/npm_and_yarn/openapi-backend-5.13.0
  • dependabot/npm_and_yarn/dev-dependencies-a66720c4a6
  • use_lts_node
  • dependabot/npm_and_yarn/dev-dependencies-31c91a86cf
  • dependabot/npm_and_yarn/dev-dependencies-8d73062cf7
  • master
  • dependabot/npm_and_yarn/multi-e6409b9447
  • dependabot/npm_and_yarn/express-rate-limit-7.5.1
  • dependabot/npm_and_yarn/dev-dependencies-79c3cc1047
  • dependabot/npm_and_yarn/dev-dependencies-759dda3d50
  • dependabot/npm_and_yarn/dev-dependencies-9e91d7a84c
  • dependabot/npm_and_yarn/dev-dependencies-df497be196
  • dependabot/npm_and_yarn/axios-1.10.0
  • dependabot/npm_and_yarn/dev-dependencies-b84056033a
  • dependabot/npm_and_yarn/esbuild-0.25.5
  • dependabot/npm_and_yarn/tsx-4.20.3
  • dependabot/npm_and_yarn/live-plugin-manager-1.1.0
  • v2.3.2
  • 2.3.2
  • v2.3.1
  • 2.3.1
  • v2.3.0
  • 2.3.0
  • v2.2.7
  • 2.2.7
  • v2.2.6
  • 2.2.6
  • v2.2.5
  • 2.2.5
  • 2.2.4
  • v2.2.4
  • 2.2.3
  • v2.2.3
  • v2.2.2
  • 2.2.2
  • v2.2.1
  • 2.2.1
41 results

ace2_common.js

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ace2_common.js 2.17 KiB
    'use strict';
    
    /**
     * This code is mostly from the old Etherpad. Please help us to comment this code.
     * This helps other people to understand this code better and helps them to improve it.
     * TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
     */
    
    /**
     * Copyright 2009 Google Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS-IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    const isNodeText = (node) => (node.nodeType === 3);
    
    const getAssoc = (obj, name) => obj[`_magicdom_${name}`];
    
    const setAssoc = (obj, name, value) => {
      // note that in IE designMode, properties of a node can get
      // copied to new nodes that are spawned during editing; also,
      // properties representable in HTML text can survive copy-and-paste
      obj[`_magicdom_${name}`] = value;
    };
    
    // "func" is a function over 0..(numItems-1) that is monotonically
    // "increasing" with index (false, then true).  Finds the boundary
    // between false and true, a number between 0 and numItems inclusive.
    
    
    const binarySearch = (numItems, func) => {
      if (numItems < 1) return 0;
      if (func(0)) return 0;
      if (!func(numItems - 1)) return numItems;
      let low = 0; // func(low) is always false
      let high = numItems - 1; // func(high) is always true
      while ((high - low) > 1) {
        const x = Math.floor((low + high) / 2); // x != low, x != high
        if (func(x)) high = x;
        else low = x;
      }
      return high;
    };
    
    const binarySearchInfinite = (expectedLength, func) => {
      let i = 0;
      while (!func(i)) i += expectedLength;
      return binarySearch(i, func);
    };
    
    const noop = () => {};
    
    exports.isNodeText = isNodeText;
    exports.getAssoc = getAssoc;
    exports.setAssoc = setAssoc;
    exports.binarySearch = binarySearch;
    exports.binarySearchInfinite = binarySearchInfinite;
    exports.noop = noop;