buildFormField function presentation exported

Last updated: 2026-03-04T23:21:38.397Z

Metrics

LOC: 38 Complexity: 8 Params: 2

Signature

buildFormField( el: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, ): : Promise<FormField>

Summary

Builds a FormField from a DOM element, classifying it via the detection pipeline.

Source Code

export async function buildFormField(
  el: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement,
): Promise<FormField> {
  const field: FormField = {
    element: el,
    selector: getUniqueSelector(el),
    category: "unknown",
    fieldType: "unknown",
    label: findLabel(el),
    name: el.name || undefined,
    id: el.id || undefined,
    placeholder:
      ("placeholder" in el ? el.placeholder : undefined) || undefined,
    autocomplete: el.autocomplete || undefined,
    required: el.required,
    detectionMethod: "html-type",
    detectionConfidence: 0.5,
  };

  field.contextSignals = buildSignals(field);

  // Custom-select ancestry overrides all keyword matching
  const isInsideCustomSelect = !!el.closest(CUSTOM_SELECT_ANCESTOR);
  const isCombobox = el.getAttribute("role") === "combobox";
  if (isInsideCustomSelect || isCombobox) {
    field.fieldType = "select";
    field.detectionMethod = "custom-select";
    field.detectionConfidence = 1.0;
    return field;
  }

  const pipelineResult = await DEFAULT_PIPELINE.runAsync(field);
  field.fieldType = pipelineResult.type;
  field.detectionMethod = pipelineResult.method;
  field.detectionConfidence = pipelineResult.confidence;

  return field;
}

No outgoing dependencies.

No incoming dependencies.