-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhq.no-pretty.ts
More file actions
121 lines (106 loc) · 3.66 KB
/
Copy pathhq.no-pretty.ts
File metadata and controls
121 lines (106 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import {
HTMLElement,
Node,
parse,
TextNode,
} from "https://esm.sh/node-html-parser@5.2.0";
// import beautify from "https://esm.sh/js-beautify@1.14.0";
export function query_html(
query: string,
html_data: string,
whitespace = !true,
) {
// if (_is_dot(query)) return beautify.html(html_data);
let html: HTMLElement = parse(html_data);
for (const expression of _commands(query)) {
_run(expression, html);
html = parse(html?.toString());
}
return _html(html, whitespace);
}
function _html(html: HTMLElement, whitespace: boolean): string {
return html.removeWhitespace().toString();
// return whitespace
// ? beautify.html(html.removeWhitespace().toString())
// : html.removeWhitespace().toString();
}
function _run(expression: string, html: HTMLElement) {
const [program, ...params] = expression.split(" ");
for (const param of params) {
if (program == "iab") {
const [selector, tagToInsert, nodeValueOrExpression] = param.split(",");
// todo: handle "." in raw strings, f.i. -> "hello . with . dots."
const [_placeholder, attribute] = nodeValueOrExpression?.split(".") ?? [];
html.querySelectorAll(selector).forEach(function (node) {
const elementToInsert = new HTMLElement(tagToInsert, {}, "", null);
const value = attribute
? node.getAttribute(attribute)!
: nodeValueOrExpression;
elementToInsert.appendChild(new TextNode(value, elementToInsert));
node.insertAdjacentHTML("afterbegin", elementToInsert.outerHTML);
});
html.set_content(html?.toString());
} else if (program == "wp") {
_wrap_program(param, html);
} else if (program == "img") {
_img_program(param, html);
} else if (program == "rm") {
_rm_program(html, param);
} else {
_tag_program(html, param, program);
}
}
}
function _tag_program(html: HTMLElement, param: string, program: string) {
html.querySelectorAll(param).forEach(function (node) {
node.tagName = program;
});
html.set_content(html?.toString());
}
function _rm_program(html: HTMLElement, param: string) {
html.querySelectorAll(param).forEach(function (node) {
node.remove();
});
html.set_content(html?.toString());
}
function _img_program(param: string, html: HTMLElement) {
let [query, height = "", width = ""] = param.split(",");
height = height !== "" ? height : "auto";
width = width !== "" ? width : "auto";
html.querySelectorAll(query).forEach(function (node) {
const src = node.textContent;
const id = node.id;
node.replaceWith(_img_element(id, height, width, src));
});
}
function _img_element(
id: string,
height: string,
width: string,
src: string,
): string | Node {
return new HTMLElement(
"img",
{ id },
`height="${height}" width="${width}" src="${src}" alt="image"`,
null,
);
}
function _wrap_program(param: string, html: HTMLElement) {
const [tagName, ...attributes] = param.split(",");
const [attrName, attrValue] = attributes;
const rawAttributes =
attributes.length > 0 ? `${attrName}="${attrValue}"` : "";
const wrapper = new HTMLElement(tagName, {}, rawAttributes, null);
wrapper.appendChild(html);
html.set_content(wrapper.toString());
}
function _is_dot(query: string) {
return query.match(/^.$/g)?.length;
}
function _commands(query: string): string[] {
const [_cat, ...commands] = query
.split("|")
.map((match: string) => match.trim());
return commands;
}