Skip to content

Commit

Permalink
Total conversion back to Javascript from Typescript.
Browse files Browse the repository at this point in the history
No need for Typescript for such a small package
  • Loading branch information
evert committed Jan 26, 2024
1 parent 217ea58 commit fa38bfb
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Changelog
=========

0.2.0 (????-??-??)
1.0.0 (????-??-??)
------------------

* Updated to Typescript 5.
* Conversion from Typescript to Javascript.


0.1.12 (2022-10-22)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "html-form-enhancer",
"version": "0.1.12",
"description": "Adds support for enctype=\"application/json\", more HTTP methods and HTTP status codes to HTML forms",
"export": "html-form-enhancer.mjs",
"main": "dist/html-form-enhancer.js",
"export": "src/html-form-enhancer.mjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint"
},
"type": "module",
"keywords": [
"html",
"json",
Expand Down
27 changes: 20 additions & 7 deletions src/html-form-enhancer.ts → src/html-form-enhancer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint no-console: 0 */
import { serializeJsonForm } from './serialize-json-form.js';

export function enhanceForm(elem: HTMLFormElement) {
/**
* @param {HTMLFormElement} elem
*/
export function enhanceForm(elem) {

elem.addEventListener('submit', (ev) => {

Expand All @@ -12,7 +14,10 @@ export function enhanceForm(elem: HTMLFormElement) {

}

export function autoEnhanceForms(doc: Document) {
/**
* @param {Document} doc
*/
export function autoEnhanceForms(doc) {
const forms = doc.getElementsByTagName('form');
for(const form of forms) {

Expand All @@ -30,7 +35,10 @@ export function autoEnhanceForms(doc: Document) {

autoEnhanceForms(document);

async function processSubmit(elem: HTMLFormElement) {
/**
* @param {HTMLFormElement} elem
*/
async function processSubmit(elem) {

const method = elem.getAttribute('method')?.toUpperCase() || 'GET';
const encType = elem.getAttribute('enctype')?.toLowerCase() || 'application/x-www-form-urlencoded';
Expand All @@ -51,7 +59,9 @@ async function processSubmit(elem: HTMLFormElement) {
} else {

if (!encType || encType === 'application/x-www-form-urlencoded') {
body = new URLSearchParams(Object.fromEntries(new FormData(elem).entries()) as Record<string, string>);
body = new URLSearchParams(
/** @type {any} */(new FormData(elem))
);
} else if (encType === 'application/json' || encType.match(/^application\/(.*)\+json$/)) {
body = JSON.stringify(serializeJsonForm(elem));
}
Expand All @@ -63,7 +73,7 @@ async function processSubmit(elem: HTMLFormElement) {
response = await fetch(action, {
method,
headers: {
'Content-Type': encType!,
'Content-Type': encType,
'Accept': 'text/html',
},
body,
Expand Down Expand Up @@ -123,7 +133,10 @@ async function processSubmit(elem: HTMLFormElement) {

}

async function replaceBody(response: Response) {
/**
* @param {Response} response
*/
async function replaceBody(response) {

const responseBody = await response.text();
const domParser = new DOMParser();
Expand Down
43 changes: 30 additions & 13 deletions src/serialize-json-form.ts → src/serialize-json-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* https://github.com/defunctzombie/form-serialize/blob/master/LICENSE
*
* It's cleaned up, modernized and converted to Typescript, but the original
* It's cleaned up and modernized, but the original
* is Copyright (c) 2013 Roman Shtylman and licensed under the MIT license.
*/

Expand All @@ -17,9 +17,10 @@ const brackets = /(\[[^[\]]*\])/g;
/**
* Serializes a form into html-json-forms format.
*
* @param {HTMLFormElement} form
* https://www.w3.org/TR/html-json-forms/
*/
export function serializeJsonForm(form: HTMLFormElement) {
export function serializeJsonForm(form) {

let result = {};

Expand Down Expand Up @@ -112,7 +113,11 @@ export function serializeJsonForm(form: HTMLFormElement) {
return result;
}

function parse_keys(str: string): string[] {
/**
* @param {string} str
* @returns {string[]}
*/
function parse_keys(str) {
const keys = [];
const prefix = /^([^[\]]*)/;
const children = new RegExp(brackets);
Expand All @@ -130,18 +135,18 @@ function parse_keys(str: string): string[] {
}

/**
* Too hard to type right now
* @param {any} result
* @param {string[]} keys
* @param {any} value
*/
type Result = any;
type Value = string;

function hash_assign(result: Result, keys: string[], value: Value): Result {
function hash_assign(result, keys, value) {
if (keys.length === 0) {
result = value;
return result;
}

const key = keys.shift()!;
const key = keys.shift();
if (!key) return result;
const between = key.match(/^\[(.+?)\]$/);

if (key === '[]') {
Expand Down Expand Up @@ -189,8 +194,12 @@ function hash_assign(result: Result, keys: string[], value: Value): Result {
return result;
}

// Object/hash encoding serializer.
function hash_serializer(result: Result, key: string, value: Value) {
/**
* @param {any} result
* @param {string} key
* @param {any} value
*/
function hash_serializer(result, key, value) {
const matches = key.match(brackets);

// Has brackets? Use the recursive assignment function to walk the keys,
Expand Down Expand Up @@ -225,7 +234,11 @@ function hash_serializer(result: Result, key: string, value: Value) {
return result;
}

function isValidInputField(elem: Element): elem is HTMLFormElement {
/**
* @param {Element} elem
* @returns {elem is HTMLFormElement}
*/
function isValidInputField(elem) {

const supportedElements = ['input', 'select', 'textarea'];
const unsupportedInputType = ['submit' , 'button' , 'image' , 'reset' , 'file'];
Expand All @@ -234,7 +247,11 @@ function isValidInputField(elem: Element): elem is HTMLFormElement {
return false;
}

if (unsupportedInputType.includes((elem as any).type)) {
if (!(elem instanceof HTMLInputElement)) {
return false;
}

if (unsupportedInputType.includes(elem.type)) {
return false;
}
return true;
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"noEmit": true,
"baseUrl": ".",
"paths": {
"*": [
"src/types/*"
]
},
"checkJs": true,
"lib": [
"DOM",
"DOM.Iterable",
Expand Down

0 comments on commit fa38bfb

Please sign in to comment.