Repository: go-gitea/gitea
File: web_src/js/features/common-page.ts (TypeScript)

Code before:
  if (el.type !== 'hidden' &&
    el.type !== 'checkbox' &&
    el.type !== 'radio' &&
    el.type !== 'range' &&
    el.type !== 'color') {
    el.dir = 'auto';
  }
}

function setSelectionEnd(el: HTMLInputElement | HTMLTextAreaElement) {
  el.setSelectionRange(el.value.length, el.value.length);
}

export function initGlobalInput() {
  registerGlobalSelectorFunc('input, textarea', attachInputDirAuto);

  // handles [data-autofocus-end], moves the cursor to end after native autofocus
  registerGlobalSelectorFunc('[data-autofocus-end]', (el) => {
    if (document.activeElement === el) {
      setSelectionEnd(el as HTMLInputElement | HTMLTextAreaElement);
    }
  });

  // handles [data-autofocus-on-show-panel], when a show-panel button is clicked, focus the element
  // inside the shown panel. This handler runs after initGlobalButtons has already made the panel visible.
  addDelegatedEventListener(document, 'click', '.show-panel', (el) => {
    const panelSel = el.getAttribute('data-panel');
    if (!panelSel) return;
    for (const panel of document.querySelectorAll<HTMLElement>(panelSel)) {
      if (!isElemVisible(panel)) continue;
      const focusEl = panel.querySelector<HTMLInputElement>('[data-autofocus-on-show-panel]');
      if (focusEl) {
        focusEl.focus();
        if (focusEl.hasAttribute('data-autofocus-end')) {
          setSelectionEnd(focusEl);
        }
      }
    }
  });
}

/**
 * Too many users set their ROOT_URL to wrong value, and it causes a lot of problems:
 *   * Cross-origin API request without correct cookie
 *   * Incorrect href in <a>
 *   * ...
 * So we check whether current URL starts with AppUrl(ROOT_URL).
 * If they don't match, show a warning to users.
 */
export function checkAppUrl() {
  const curUrl = window.location.href;

Review comment (suggestion):
Completely a mess and it can't be right.

I have shown you it should be done in the existing `.show-panel` handler.

Code after:
  if (el.type !== 'hidden' &&
    el.type !== 'checkbox' &&
    el.type !== 'radio' &&
    el.type !== 'range' &&
    el.type !== 'color') {
    el.dir = 'auto';
  }
}

function autoFocusEnd(el: HTMLInputElement | HTMLTextAreaElement) {
  el.focus();
  el.setSelectionRange(el.value.length, el.value.length);
}

export function applyAutoFocus(container: Element) {
  // https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/autofocus
  // "autofocus" behavior is defined by the standard: when a container (e.g.: dialog) becomes visible, focus the element with "autofocus" attribute
  // Fomantic UI already supports it for its modal dialog, we need to cover more cases (e.g.: ".show-panel" button)
  // Here is just a simple support, we don't expect more than one element that need "autofocus" appearing in the same container
  container.querySelector<HTMLElement>('[autofocus]')?.focus();
  // Also, apply our autoFocusEnd behavior
  // TODO: GLOBAL-INIT-MULTIPLE-FUNCTIONS: use "~=" operator in case we would extend the "data-global-init" to support more functions in the future.
  const el = container.querySelector<HTMLInputElement>('[data-global-init~="autoFocusEnd"]');
  if (el) autoFocusEnd(el);
}

export function initGlobalInput() {
  registerGlobalSelectorFunc('input, textarea', attachInputDirAuto);

  // autoFocusEnd is used for autofocus an input/textarea and move the cursor to the end of the text.
  // It is useful for "New Issue"/"New PR" pages when the title is pre-filled with prefix text (e.g.: from template or commit message)
  // The native "autofocus" isn't used because there is a delay between "focused (DOM rendering)" and "move cursor to end (our JS)", it causes flickers.
  registerGlobalInitFunc('autoFocusEnd', autoFocusEnd);
}

/**
 * Too many users set their ROOT_URL to wrong value, and it causes a lot of problems:
 *   * Cross-origin API request without correct cookie
 *   * Incorrect href in <a>
 *   * ...
 * So we check whether current URL starts with AppUrl(ROOT_URL).
 * If they don't match, show a warning to users.
 */
export function checkAppUrl() {
  const curUrl = window.location.href;
  // some users visit "https://domain/gitea" while appUrl is "https://domain/gitea/", there should be no warning
  if (curUrl.startsWith(appUrl) || `${curUrl}/` === appUrl) {
    return;
  }
  showGlobalErrorMessage(`Your ROOT_URL in app.ini is "${appUrl}", it's unlikely matching the site you are visiting.
Mismatched ROOT_URL config causes wrong URL links for web UI/mail content/webhook notification/OAuth2 sign-in.`, 'warning');