mod2dom 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/Makefile +27 -0
- package/README.md +1 -0
- package/config/vitest.config.ts +7 -0
- package/dist/domparser.d.ts +7 -0
- package/dist/domparser.d.ts.map +1 -0
- package/dist/domparser.js +48 -0
- package/dist/domparser.js.map +1 -0
- package/dist/errors.d.ts +15 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +18 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/query.d.ts +16 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +28 -0
- package/dist/query.js.map +1 -0
- package/dist/types.d.ts +3 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/validate.d.ts +3 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.js +24 -0
- package/dist/validate.js.map +1 -0
- package/package.json +32 -0
- package/src/domparser.ts +48 -0
- package/src/errors.ts +11 -0
- package/src/index.ts +1 -0
- package/src/query.ts +40 -0
- package/src/types.ts +2 -0
- package/src/validate.ts +25 -0
- package/tests/dom/error.test.ts +79 -0
- package/tests/dom/simple.test.ts +29 -0
- package/tsconfig.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 General Software Development
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Makefile
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
.PHONY: all, test, prod, build, updateMinor, updateMajor, updatePatch, update
|
|
2
|
+
|
|
3
|
+
all: build
|
|
4
|
+
|
|
5
|
+
build: prod
|
|
6
|
+
|
|
7
|
+
prod:
|
|
8
|
+
@npm run buildprod
|
|
9
|
+
|
|
10
|
+
test:
|
|
11
|
+
@npm run test
|
|
12
|
+
|
|
13
|
+
updateMinor:
|
|
14
|
+
npm version minor
|
|
15
|
+
$(MAKE) update
|
|
16
|
+
|
|
17
|
+
updateMajor:
|
|
18
|
+
npm version major
|
|
19
|
+
$(MAKE) update
|
|
20
|
+
|
|
21
|
+
updatePatch:
|
|
22
|
+
npm version patch
|
|
23
|
+
$(MAKE) update
|
|
24
|
+
|
|
25
|
+
update:
|
|
26
|
+
git push
|
|
27
|
+
npm publish
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# mod2dom
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { QueryOption, QueryOptions } from "./query.js";
|
|
2
|
+
import type { DNode } from "./types.js";
|
|
3
|
+
export declare const DOMParser: {
|
|
4
|
+
findAll: (query: QueryOptions | QueryOption, parentNode?: ParentNode) => NodeListOf<Element>;
|
|
5
|
+
find: (query: QueryOptions | QueryOption, parentNode?: ParentNode) => DNode | undefined;
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=domparser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"domparser.d.ts","sourceRoot":"","sources":["../src/domparser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC5D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGxC,eAAO,MAAM,SAAS;qBACe,YAAY,GAAG,WAAW,eAAc,UAAU;kBAuCxD,YAAY,GAAG,WAAW,eAAc,UAAU,KAAc,KAAK,GAAG,SAAS;CAG/G,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { validateClassName, validateTagName } from "./validate.js";
|
|
2
|
+
export const DOMParser = {
|
|
3
|
+
findAll: function findAll(query, parentNode = document) {
|
|
4
|
+
if (!Array.isArray(query))
|
|
5
|
+
query = [query];
|
|
6
|
+
let queryString = "";
|
|
7
|
+
for (const q of query) {
|
|
8
|
+
if (q?.__queryType === "element") {
|
|
9
|
+
queryString += q.name;
|
|
10
|
+
if (q.options) {
|
|
11
|
+
for (const option of Object.keys(q.options)) {
|
|
12
|
+
queryString += "[";
|
|
13
|
+
const value = q.options[option];
|
|
14
|
+
queryString += `${option}="${CSS.escape(value)}"`;
|
|
15
|
+
queryString += "]";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
for (const q2 of q.other) {
|
|
19
|
+
if (q2?.__queryType === "tagname") {
|
|
20
|
+
validateTagName(q2.name);
|
|
21
|
+
queryString += `#${q2.name}`;
|
|
22
|
+
}
|
|
23
|
+
else if (q2?.__queryType === "classname") {
|
|
24
|
+
validateClassName(q2.name);
|
|
25
|
+
queryString += `.${q2.name}`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
else if (q?.__queryType === "tagname") {
|
|
30
|
+
validateTagName(q.name);
|
|
31
|
+
queryString += `#${q.name}`;
|
|
32
|
+
}
|
|
33
|
+
else if (q?.__queryType === "classname") {
|
|
34
|
+
validateClassName(q.name);
|
|
35
|
+
queryString += `.${q.name}`;
|
|
36
|
+
}
|
|
37
|
+
queryString += " ";
|
|
38
|
+
}
|
|
39
|
+
if (queryString[queryString.length - 1] === " ") {
|
|
40
|
+
queryString = queryString.slice(0, queryString.length - 1);
|
|
41
|
+
}
|
|
42
|
+
return parentNode.querySelectorAll(queryString);
|
|
43
|
+
},
|
|
44
|
+
find: function find(query, parentNode = document) {
|
|
45
|
+
return this.findAll(query, parentNode)[0];
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
//# sourceMappingURL=domparser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"domparser.js","sourceRoot":"","sources":["../src/domparser.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEnE,MAAM,CAAC,MAAM,SAAS,GAAG;IACrB,OAAO,EAAE,SAAS,OAAO,CAAC,KAAiC,EAAE,aAAyB,QAAQ;QAC1F,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;YACrB,KAAK,GAAG,CAAC,KAAK,CAAC,CAAC;QAEpB,IAAI,WAAW,GAAG,EAAE,CAAC;QACrB,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE,CAAC;YACpB,IAAI,CAAC,EAAE,WAAW,KAAK,SAAS,EAAE,CAAC;gBAC/B,WAAW,IAAI,CAAC,CAAC,IAAI,CAAC;gBACtB,IAAI,CAAC,CAAC,OAAO,EAAE,CAAC;oBACZ,KAAK,MAAM,MAAM,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC;wBAC1C,WAAW,IAAI,GAAG,CAAC;wBACnB,MAAM,KAAK,GAAW,CAAC,CAAC,OAAO,CAAC,MAAgB,CAAE,CAAC;wBACnD,WAAW,IAAI,GAAG,MAAM,KAAK,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAA;wBACjD,WAAW,IAAI,GAAG,CAAC;oBACvB,CAAC;gBACL,CAAC;gBACD,KAAK,MAAM,EAAE,IAAI,CAAC,CAAC,KAAK,EAAE,CAAC;oBACvB,IAAI,EAAE,EAAE,WAAW,KAAK,SAAS,EAAE,CAAC;wBAChC,eAAe,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;wBACzB,WAAW,IAAI,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC;oBACjC,CAAC;yBAAM,IAAI,EAAE,EAAE,WAAW,KAAK,WAAW,EAAE,CAAC;wBACzC,iBAAiB,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;wBAC3B,WAAW,IAAI,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC;oBACjC,CAAC;gBACL,CAAC;YACL,CAAC;iBAAM,IAAI,CAAC,EAAE,WAAW,KAAK,SAAS,EAAE,CAAC;gBACtC,eAAe,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;gBACxB,WAAW,IAAI,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC;YAChC,CAAC;iBAAM,IAAI,CAAC,EAAE,WAAW,KAAK,WAAW,EAAE,CAAC;gBACxC,iBAAiB,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;gBAC1B,WAAW,IAAI,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC;YAChC,CAAC;YACD,WAAW,IAAI,GAAG,CAAC;QACvB,CAAC;QACD,IAAI,WAAW,CAAC,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,GAAG,EAAE,CAAC;YAC9C,WAAW,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC,EAAE,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;QAC9D,CAAC;QACD,OAAO,UAAU,CAAC,gBAAgB,CAAC,WAAW,CAAC,CAAC;IACpD,CAAC;IACD,IAAI,EAAE,SAAS,IAAI,CAAC,KAAiC,EAAE,aAAyB,QAAQ;QACpF,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9C,CAAC;CACJ,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare class MOD2DOMError extends Error {
|
|
2
|
+
}
|
|
3
|
+
declare class BadData extends MOD2DOMError {
|
|
4
|
+
}
|
|
5
|
+
declare class InvalidTagName extends BadData {
|
|
6
|
+
}
|
|
7
|
+
declare class InvalidClassName extends BadData {
|
|
8
|
+
}
|
|
9
|
+
export declare const mod2domErrors: Readonly<{
|
|
10
|
+
BadData: typeof BadData;
|
|
11
|
+
InvalidTagName: typeof InvalidTagName;
|
|
12
|
+
InvalidClassName: typeof InvalidClassName;
|
|
13
|
+
}>;
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,YAAa,SAAQ,KAAK;CAAG;AAE1C,cAAM,OAAQ,SAAQ,YAAY;CAAG;AACrC,cAAM,cAAe,SAAQ,OAAO;CAAG;AACvC,cAAM,gBAAiB,SAAQ,OAAO;CAAG;AAEzC,eAAO,MAAM,aAAa;;;;EAIxB,CAAC"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export class MOD2DOMError extends Error {
|
|
2
|
+
}
|
|
3
|
+
;
|
|
4
|
+
class BadData extends MOD2DOMError {
|
|
5
|
+
}
|
|
6
|
+
;
|
|
7
|
+
class InvalidTagName extends BadData {
|
|
8
|
+
}
|
|
9
|
+
;
|
|
10
|
+
class InvalidClassName extends BadData {
|
|
11
|
+
}
|
|
12
|
+
;
|
|
13
|
+
export const mod2domErrors = Object.freeze({
|
|
14
|
+
BadData: BadData,
|
|
15
|
+
InvalidTagName: InvalidTagName,
|
|
16
|
+
InvalidClassName: InvalidClassName,
|
|
17
|
+
});
|
|
18
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,YAAa,SAAQ,KAAK;CAAG;AAAA,CAAC;AAE3C,MAAM,OAAQ,SAAQ,YAAY;CAAG;AAAA,CAAC;AACtC,MAAM,cAAe,SAAQ,OAAO;CAAG;AAAA,CAAC;AACxC,MAAM,gBAAiB,SAAQ,OAAO;CAAG;AAAA,CAAC;AAE1C,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC;IACvC,OAAO,EAAE,OAAO;IAChB,cAAc,EAAE,cAAc;IAC9B,gBAAgB,EAAE,gBAAgB;CACrC,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA"}
|
package/dist/query.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type QueryType = "element" | "classname" | "tagname";
|
|
2
|
+
export type QueryOption = {
|
|
3
|
+
__queryType: QueryType;
|
|
4
|
+
name: string;
|
|
5
|
+
options: {
|
|
6
|
+
[key: string]: string;
|
|
7
|
+
};
|
|
8
|
+
other: QueryOptions;
|
|
9
|
+
};
|
|
10
|
+
export type QueryOptions = QueryOption[];
|
|
11
|
+
export declare function Element(elementName: string, attributes?: {
|
|
12
|
+
[key: string]: string;
|
|
13
|
+
}, ...others: QueryOptions): QueryOption;
|
|
14
|
+
export declare function ClassName(name: string): QueryOption;
|
|
15
|
+
export declare function TagName(name: string): QueryOption;
|
|
16
|
+
//# sourceMappingURL=query.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,SAAS,GAAG,SAAS,GAAG,WAAW,GAAG,SAAS,CAAC;AAE5D,MAAM,MAAM,WAAW,GAAG;IACtB,WAAW,EAAE,SAAS,CAAC;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAC,CAAC;IACjC,KAAK,EAAE,YAAY,CAAA;CACtB,CAAC;AACF,MAAM,MAAM,YAAY,GAAG,WAAW,EAAE,CAAC;AAEzC,wBAAgB,OAAO,CAAC,WAAW,EAAE,MAAM,EAAE,UAAU,GAAE;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAM,EAAE,GAAG,MAAM,EAAE,YAAY,GAAG,WAAW,CAO3H;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,CAQnD;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,CAQjD"}
|
package/dist/query.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { validateClassName, validateTagName } from "./validate.js";
|
|
2
|
+
export function Element(elementName, attributes = {}, ...others) {
|
|
3
|
+
return {
|
|
4
|
+
__queryType: "element",
|
|
5
|
+
name: elementName,
|
|
6
|
+
options: attributes,
|
|
7
|
+
other: others
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export function ClassName(name) {
|
|
11
|
+
validateClassName(name);
|
|
12
|
+
return {
|
|
13
|
+
__queryType: "classname",
|
|
14
|
+
name: name.trim(),
|
|
15
|
+
options: {},
|
|
16
|
+
other: []
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export function TagName(name) {
|
|
20
|
+
validateTagName(name);
|
|
21
|
+
return {
|
|
22
|
+
__queryType: "tagname",
|
|
23
|
+
name: name.trim(),
|
|
24
|
+
options: {},
|
|
25
|
+
other: []
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=query.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.js","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAYnE,MAAM,UAAU,OAAO,CAAC,WAAmB,EAAE,aAAsC,EAAE,EAAE,GAAG,MAAoB;IAC1G,OAAO;QACH,WAAW,EAAE,SAAS;QACtB,IAAI,EAAE,WAAW;QACjB,OAAO,EAAE,UAAU;QACnB,KAAK,EAAE,MAAM;KAChB,CAAC;AACN,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,IAAY;IAClC,iBAAiB,CAAC,IAAI,CAAC,CAAC;IACxB,OAAO;QACH,WAAW,EAAE,WAAW;QACxB,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE;QACjB,OAAO,EAAE,EAAE;QACX,KAAK,EAAE,EAAE;KACZ,CAAC;AACN,CAAC;AAED,MAAM,UAAU,OAAO,CAAC,IAAY;IAChC,eAAe,CAAC,IAAI,CAAC,CAAC;IACtB,OAAO;QACH,WAAW,EAAE,SAAS;QACtB,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE;QACjB,OAAO,EAAE,EAAE;QACX,KAAK,EAAE,EAAE;KACZ,CAAA;AACL,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,KAAK,GAAG,QAAQ,GAAG,WAAW,GAAG,OAAO,GAAG,IAAI,CAAC;AAC5D,YAAY,EAAE,SAAS,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":"AAEA,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,QAU3C;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,QAU7C"}
|
package/dist/validate.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { mod2domErrors } from "./errors.js";
|
|
2
|
+
export function validateTagName(name) {
|
|
3
|
+
if (!name.trim())
|
|
4
|
+
throw new mod2domErrors.InvalidTagName(`Invalid tag name (empty).`);
|
|
5
|
+
const invalid = name.match(/[/\\\s.#\[\]]/g);
|
|
6
|
+
if (invalid) {
|
|
7
|
+
throw new mod2domErrors.InvalidTagName(`Invalid tag name: \`${name}\` (character: "${invalid[0]}")`);
|
|
8
|
+
}
|
|
9
|
+
let v;
|
|
10
|
+
if (v = name.match(/^[0-9]/))
|
|
11
|
+
throw new mod2domErrors.InvalidTagName(`Invalid tag name: \`${name}\` (character: "${v}")`);
|
|
12
|
+
}
|
|
13
|
+
export function validateClassName(name) {
|
|
14
|
+
if (!name.trim())
|
|
15
|
+
throw new mod2domErrors.InvalidTagName(`Invalid class name (empty).`);
|
|
16
|
+
const invalid = name.match(/[/\\\s.#\[\]]/g);
|
|
17
|
+
if (invalid) {
|
|
18
|
+
throw new mod2domErrors.InvalidClassName(`Invalid class name: \`${name}\` (character: "${invalid[0]}")`);
|
|
19
|
+
}
|
|
20
|
+
let v;
|
|
21
|
+
if (v = name.match(/^[0-9]/))
|
|
22
|
+
throw new mod2domErrors.InvalidClassName(`Invalid class name: \`${name}\` (character: "${v}")`);
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=validate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.js","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,MAAM,UAAU,eAAe,CAAC,IAAY;IACxC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE;QACZ,MAAM,IAAI,aAAa,CAAC,cAAc,CAAC,2BAA2B,CAAC,CAAC;IACxE,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAC;IAC7C,IAAI,OAAO,EAAE,CAAC;QACV,MAAM,IAAI,aAAa,CAAC,cAAc,CAAC,uBAAuB,IAAI,mBAAmB,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IACzG,CAAC;IACD,IAAI,CAAC,CAAC;IACN,IAAI,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;QACxB,MAAM,IAAI,aAAa,CAAC,cAAc,CAAC,uBAAuB,IAAI,mBAAmB,CAAC,IAAI,CAAC,CAAC;AACpG,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC1C,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE;QACZ,MAAM,IAAI,aAAa,CAAC,cAAc,CAAC,6BAA6B,CAAC,CAAC;IAC1E,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAC;IAC7C,IAAI,OAAO,EAAE,CAAC;QACV,MAAM,IAAI,aAAa,CAAC,gBAAgB,CAAC,yBAAyB,IAAI,mBAAmB,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAC7G,CAAC;IACD,IAAI,CAAC,CAAC;IACN,IAAI,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;QACxB,MAAM,IAAI,aAAa,CAAC,gBAAgB,CAAC,yBAAyB,IAAI,mBAAmB,CAAC,IAAI,CAAC,CAAC;AACxG,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mod2dom",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"buildprod": "npx tsc --incremental",
|
|
8
|
+
"test": "npx vitest --config config/vitest.config.ts"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/cornusandu-org/mod2dom.git"
|
|
13
|
+
},
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/cornusandu-org/mod2dom/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/cornusandu-org/mod2dom#readme",
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/node": "^25.8.0",
|
|
22
|
+
"jsdom": "^29.1.1",
|
|
23
|
+
"typescript": "^6.0.3",
|
|
24
|
+
"vitest": "^4.1.6"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./dist/index.js",
|
|
29
|
+
"./types": "./dist/types.js",
|
|
30
|
+
"./query": "./dist/query.js"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/domparser.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { QueryOption, QueryOptions } from "./query.js";
|
|
2
|
+
import type { DNode } from "./types.js";
|
|
3
|
+
import { validateClassName, validateTagName } from "./validate.js";
|
|
4
|
+
|
|
5
|
+
export const DOMParser = {
|
|
6
|
+
findAll: function findAll(query: QueryOptions | QueryOption, parentNode: ParentNode = document) {
|
|
7
|
+
if (!Array.isArray(query))
|
|
8
|
+
query = [query];
|
|
9
|
+
|
|
10
|
+
let queryString = "";
|
|
11
|
+
for (const q of query) {
|
|
12
|
+
if (q?.__queryType === "element") {
|
|
13
|
+
queryString += q.name;
|
|
14
|
+
if (q.options) {
|
|
15
|
+
for (const option of Object.keys(q.options)) {
|
|
16
|
+
queryString += "[";
|
|
17
|
+
const value: string = q.options[option as string]!;
|
|
18
|
+
queryString += `${option}="${CSS.escape(value)}"`
|
|
19
|
+
queryString += "]";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
for (const q2 of q.other) {
|
|
23
|
+
if (q2?.__queryType === "tagname") {
|
|
24
|
+
validateTagName(q2.name);
|
|
25
|
+
queryString += `#${q2.name}`;
|
|
26
|
+
} else if (q2?.__queryType === "classname") {
|
|
27
|
+
validateClassName(q2.name);
|
|
28
|
+
queryString += `.${q2.name}`;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
} else if (q?.__queryType === "tagname") {
|
|
32
|
+
validateTagName(q.name);
|
|
33
|
+
queryString += `#${q.name}`;
|
|
34
|
+
} else if (q?.__queryType === "classname") {
|
|
35
|
+
validateClassName(q.name);
|
|
36
|
+
queryString += `.${q.name}`;
|
|
37
|
+
}
|
|
38
|
+
queryString += " ";
|
|
39
|
+
}
|
|
40
|
+
if (queryString[queryString.length - 1] === " ") {
|
|
41
|
+
queryString = queryString.slice(0, queryString.length - 1)
|
|
42
|
+
}
|
|
43
|
+
return parentNode.querySelectorAll(queryString);
|
|
44
|
+
},
|
|
45
|
+
find: function find(query: QueryOptions | QueryOption, parentNode: ParentNode = document): DNode | undefined {
|
|
46
|
+
return this.findAll(query, parentNode)[0];
|
|
47
|
+
}
|
|
48
|
+
};
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export class MOD2DOMError extends Error {};
|
|
2
|
+
|
|
3
|
+
class BadData extends MOD2DOMError {};
|
|
4
|
+
class InvalidTagName extends BadData {};
|
|
5
|
+
class InvalidClassName extends BadData {};
|
|
6
|
+
|
|
7
|
+
export const mod2domErrors = Object.freeze({
|
|
8
|
+
BadData: BadData,
|
|
9
|
+
InvalidTagName: InvalidTagName,
|
|
10
|
+
InvalidClassName: InvalidClassName,
|
|
11
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DOMParser } from "./domparser.js"
|
package/src/query.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { validateClassName, validateTagName } from "./validate.js";
|
|
2
|
+
|
|
3
|
+
export type QueryType = "element" | "classname" | "tagname";
|
|
4
|
+
|
|
5
|
+
export type QueryOption = {
|
|
6
|
+
__queryType: QueryType,
|
|
7
|
+
name: string,
|
|
8
|
+
options: {[key: string]: string},
|
|
9
|
+
other: QueryOptions
|
|
10
|
+
};
|
|
11
|
+
export type QueryOptions = QueryOption[];
|
|
12
|
+
|
|
13
|
+
export function Element(elementName: string, attributes: {[key: string]: string} = {}, ...others: QueryOptions): QueryOption {
|
|
14
|
+
return {
|
|
15
|
+
__queryType: "element",
|
|
16
|
+
name: elementName,
|
|
17
|
+
options: attributes,
|
|
18
|
+
other: others
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function ClassName(name: string): QueryOption {
|
|
23
|
+
validateClassName(name);
|
|
24
|
+
return {
|
|
25
|
+
__queryType: "classname",
|
|
26
|
+
name: name.trim(),
|
|
27
|
+
options: {},
|
|
28
|
+
other: []
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function TagName(name: string): QueryOption {
|
|
33
|
+
validateTagName(name);
|
|
34
|
+
return {
|
|
35
|
+
__queryType: "tagname",
|
|
36
|
+
name: name.trim(),
|
|
37
|
+
options: {},
|
|
38
|
+
other: []
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/types.ts
ADDED
package/src/validate.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { mod2domErrors } from "./errors.js";
|
|
2
|
+
|
|
3
|
+
export function validateTagName(name: string) {
|
|
4
|
+
if (!name.trim())
|
|
5
|
+
throw new mod2domErrors.InvalidTagName(`Invalid tag name (empty).`);
|
|
6
|
+
const invalid = name.match(/[/\\\s.#\[\]]/g);
|
|
7
|
+
if (invalid) {
|
|
8
|
+
throw new mod2domErrors.InvalidTagName(`Invalid tag name: \`${name}\` (character: "${invalid[0]}")`);
|
|
9
|
+
}
|
|
10
|
+
let v;
|
|
11
|
+
if (v = name.match(/^[0-9]/))
|
|
12
|
+
throw new mod2domErrors.InvalidTagName(`Invalid tag name: \`${name}\` (character: "${v}")`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function validateClassName(name: string) {
|
|
16
|
+
if (!name.trim())
|
|
17
|
+
throw new mod2domErrors.InvalidTagName(`Invalid class name (empty).`);
|
|
18
|
+
const invalid = name.match(/[/\\\s.#\[\]]/g);
|
|
19
|
+
if (invalid) {
|
|
20
|
+
throw new mod2domErrors.InvalidClassName(`Invalid class name: \`${name}\` (character: "${invalid[0]}")`);
|
|
21
|
+
}
|
|
22
|
+
let v;
|
|
23
|
+
if (v = name.match(/^[0-9]/))
|
|
24
|
+
throw new mod2domErrors.InvalidClassName(`Invalid class name: \`${name}\` (character: "${v}")`);
|
|
25
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest"
|
|
2
|
+
import { DOMParser } from "../../src/index";
|
|
3
|
+
import { TagName as IDName, Element, ClassName } from "../../src/query";
|
|
4
|
+
|
|
5
|
+
describe("invalidClass.empty.none", () => {
|
|
6
|
+
it("finds an element", () => {
|
|
7
|
+
document.body.innerHTML = `
|
|
8
|
+
<div>
|
|
9
|
+
<button id="x" class="test">Hello, World!</button>
|
|
10
|
+
</div>
|
|
11
|
+
`
|
|
12
|
+
|
|
13
|
+
let errored = false;
|
|
14
|
+
try {
|
|
15
|
+
const btn = DOMParser.find(ClassName(""));
|
|
16
|
+
} catch {
|
|
17
|
+
errored = true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
expect(errored).toBe(true);
|
|
21
|
+
})
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe("invalidClass.empty.whitespace", () => {
|
|
25
|
+
it("finds an element", () => {
|
|
26
|
+
document.body.innerHTML = `
|
|
27
|
+
<div>
|
|
28
|
+
<button id="x" class="test">Hello, World!</button>
|
|
29
|
+
</div>
|
|
30
|
+
`
|
|
31
|
+
|
|
32
|
+
let errored = false;
|
|
33
|
+
try {
|
|
34
|
+
const btn = DOMParser.find(ClassName(" "));
|
|
35
|
+
} catch {
|
|
36
|
+
errored = true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
expect(errored).toBe(true);
|
|
40
|
+
})
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe("invalidClass.character(1)", () => {
|
|
44
|
+
it("finds an element", () => {
|
|
45
|
+
document.body.innerHTML = `
|
|
46
|
+
<div>
|
|
47
|
+
<button id="x" class="test">Hello, World!</button>
|
|
48
|
+
</div>
|
|
49
|
+
`
|
|
50
|
+
|
|
51
|
+
let errored = false;
|
|
52
|
+
try {
|
|
53
|
+
const btn = DOMParser.find(ClassName("#class"));
|
|
54
|
+
} catch {
|
|
55
|
+
errored = true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
expect(errored).toBe(true);
|
|
59
|
+
})
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe("invalidClass.character(2)", () => {
|
|
63
|
+
it("finds an element", () => {
|
|
64
|
+
document.body.innerHTML = `
|
|
65
|
+
<div>
|
|
66
|
+
<button id="x" class="test">Hello, World!</button>
|
|
67
|
+
</div>
|
|
68
|
+
`
|
|
69
|
+
|
|
70
|
+
let errored = false;
|
|
71
|
+
try {
|
|
72
|
+
const btn = DOMParser.find(ClassName(".class"));
|
|
73
|
+
} catch {
|
|
74
|
+
errored = true;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
expect(errored).toBe(true);
|
|
78
|
+
})
|
|
79
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest"
|
|
2
|
+
import { DOMParser } from "../../src/index";
|
|
3
|
+
import { TagName as IDName, Element, ClassName } from "../../src/query";
|
|
4
|
+
|
|
5
|
+
describe("findTheButton", () => {
|
|
6
|
+
it("finds an element", () => {
|
|
7
|
+
document.body.innerHTML = `
|
|
8
|
+
<button id="x" class="test">Hello, World!</button>
|
|
9
|
+
`
|
|
10
|
+
|
|
11
|
+
const btn = DOMParser.find(Element('button', {}, IDName("x"), ClassName("test")));
|
|
12
|
+
|
|
13
|
+
expect(btn?.textContent).toBe("Hello, World!")
|
|
14
|
+
})
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe("findTheButton2", () => {
|
|
18
|
+
it("finds an element", () => {
|
|
19
|
+
document.body.innerHTML = `
|
|
20
|
+
<div>
|
|
21
|
+
<button id="x" class="test">Hello, World!</button>
|
|
22
|
+
</div>
|
|
23
|
+
`
|
|
24
|
+
|
|
25
|
+
const btn = DOMParser.find([Element('div'), Element('button', {}, IDName("x"), ClassName("test"))]);
|
|
26
|
+
|
|
27
|
+
expect(btn?.textContent).toBe("Hello, World!")
|
|
28
|
+
})
|
|
29
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Visit https://aka.ms/tsconfig to read more about this file
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
// File Layout
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
|
|
8
|
+
// Environment Settings
|
|
9
|
+
// See also https://aka.ms/tsconfig/module
|
|
10
|
+
"module": "nodenext",
|
|
11
|
+
"target": "esnext",
|
|
12
|
+
"lib": ["esnext", "DOM", "DOM.Iterable"],
|
|
13
|
+
"types": ["node"],
|
|
14
|
+
// For nodejs:
|
|
15
|
+
// "lib": ["esnext"],
|
|
16
|
+
// "types": ["node"],
|
|
17
|
+
// and npm install -D @types/node
|
|
18
|
+
|
|
19
|
+
// Other Outputs
|
|
20
|
+
"sourceMap": true,
|
|
21
|
+
"declaration": true,
|
|
22
|
+
"declarationMap": true,
|
|
23
|
+
|
|
24
|
+
// Stricter Typechecking Options
|
|
25
|
+
"noUncheckedIndexedAccess": true,
|
|
26
|
+
"exactOptionalPropertyTypes": true,
|
|
27
|
+
|
|
28
|
+
// Style Options
|
|
29
|
+
// "noImplicitReturns": true,
|
|
30
|
+
// "noImplicitOverride": true,
|
|
31
|
+
// "noUnusedLocals": true,
|
|
32
|
+
// "noUnusedParameters": true,
|
|
33
|
+
// "noFallthroughCasesInSwitch": true,
|
|
34
|
+
// "noPropertyAccessFromIndexSignature": true,
|
|
35
|
+
|
|
36
|
+
// Recommended Options
|
|
37
|
+
"strict": true,
|
|
38
|
+
"jsx": "react-jsx",
|
|
39
|
+
"verbatimModuleSyntax": true,
|
|
40
|
+
"isolatedModules": true,
|
|
41
|
+
"noUncheckedSideEffectImports": true,
|
|
42
|
+
"moduleDetection": "force",
|
|
43
|
+
"skipLibCheck": true
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
"include": [
|
|
47
|
+
"src/*"
|
|
48
|
+
]
|
|
49
|
+
}
|