GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ModularTemplate/scripts/HTMLObjects.js at master · FedgeNo/ModularTemplate

Expand file tree

/

HTMLObjects.js

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
188 lines (144 loc) · 6.2 KB
/

HTMLObjects.js

Copy path

File metadata and controls

188 lines (144 loc) · 6.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const STRUCTURAL_PROPERTIES = new Set([
'tagName', 'class', 'attributes', 'contents', 'rendered', 'items', 'contentType',
]);
const PROPERTY_DEFAULTS = new WeakMap();
function copyDefault(value) {
if (Array.isArray(value)) return value.map(copyDefault);
if (value !== null && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype) {
return Object.fromEntries(Object.entries(value).map(([name, item]) => [name, copyDefault(item)]));
}
return value;
}
/** Client-side DOM-building core mirroring PHP's HTMLObject contract. */
export class HTMLObject {
static tagName = null;
static className = null;
static properties = { id: null };
#rendered = false;
constructor(properties = null) {
this.class = null;
this.attributes = {};
this.contents = [];
const defaults = this.constructor.propertyDefaults();
for (const [name, value] of Object.entries(defaults)) {
this[name] = copyDefault(value);
}
if (properties !== null) {
for (const [name, value] of Object.entries(properties)) {
if (Object.hasOwn(defaults, name)) this[name] = value;
}
}
}
static propertyDefaults() {
if (PROPERTY_DEFAULTS.has(this)) return PROPERTY_DEFAULTS.get(this);
const levels = [];
for (let type = this; type && type !== Function.prototype; type = Object.getPrototypeOf(type)) {
levels.push(type);
if (type === HTMLObject) break;
}
const defaults = {};
levels.reverse().forEach(type => {
if (!Object.hasOwn(type, 'properties')) return;
for (const [name, value] of Object.entries(type.properties)) {
if (!STRUCTURAL_PROPERTIES.has(name)) defaults[name] = value;
}
});
PROPERTY_DEFAULTS.set(this, Object.freeze(defaults));
return defaults;
}
static compoundedClassName() {
const levels = [];
for (let type = this; type && type !== HTMLObject; type = Object.getPrototypeOf(type)) {
levels.push(type);
}
let first = null;
levels.forEach((type, index) => {
if (Object.hasOwn(type, 'className') && type.className !== null) first = index;
});
if (first === null) return '';
const names = [];
for (let index = first; index >= 0; index--) {
const type = levels[index];
names.push(Object.hasOwn(type, 'className') && type.className !== null ? type.className : type.name);
}
return names.join(' ');
}
addContent(item) { this.contents.push(item); }
addContents(items) { items.forEach(item => this.addContent(item)); }
toDOM() {
if (this.#rendered) {
throw new Error(this.constructor.name + ' produced output twice; build a fresh instance per output step.');
}
this.#rendered = true;
const tagName = this.constructor.tagName;
if (!tagName) throw new Error(this.constructor.name + ' has no tagName.');
const element = document.createElement(tagName);
if (this.id !== null) element.setAttribute('id', this.id);
const inherited = this.constructor.compoundedClassName();
const inheritedNames = inherited.split(' ').filter(Boolean);
const added = String(this.class || '').split(' ')
.filter(name => name && !inheritedNames.includes(name));
const className = [...inheritedNames, ...added].join(' ');
if (className) element.setAttribute('class', className);
for (const [name, value] of Object.entries(this.attributes)) {
if (value === null) {
console.warn(this.constructor.name + ' left the "' + name + '" attribute null');
continue;
}
element.setAttribute(name, value);
}
for (const item of this.contents) {
let node = null;
if (item instanceof HTMLObject) node = item.toDOM();
else if (typeof item === 'string') node = document.createTextNode(item);
else if (item instanceof Node) node = item;
if (node) element.appendChild(node);
}
return element;
}
}
export class Article extends HTMLObject { static tagName = 'article'; }
export class Div extends HTMLObject { static tagName = 'div'; }
export class Section extends HTMLObject { static tagName = 'section'; }
export class Figure extends HTMLObject { static tagName = 'figure'; }
export class Heading2 extends HTMLObject { static tagName = 'h2'; }
export class Heading3 extends HTMLObject { static tagName = 'h3'; }
export class ListItem extends HTMLObject { static tagName = 'li'; }
export class Paragraph extends HTMLObject { static tagName = 'p'; }
export class Span extends HTMLObject { static tagName = 'span'; }
export class Table extends HTMLObject { static tagName = 'table'; }
export class UnorderedList extends HTMLObject { static tagName = 'ul'; }
export class TableRow extends HTMLObject { static tagName = 'tr'; }
export class TableHeader extends HTMLObject { static tagName = 'th'; }
export class TableData extends HTMLObject { static tagName = 'td'; }
export class Button extends HTMLObject {
static tagName = 'button';
static properties = { type: 'button' };
toDOM() {
this.attributes.type = this.type;
return super.toDOM();
}
}
export class ButtonButton extends Button { static className = 'Button'; }
export class Anchor extends HTMLObject {
static tagName = 'a';
static properties = { href: null };
constructor(href = null, text = null) {
super(href !== null && typeof href === 'object' ? href : { href });
if (text !== null) this.addContent(text);
}
toDOM() {
if (this.href !== null) this.attributes.href = this.href;
return super.toDOM();
}
}
export class Image extends HTMLObject {
static tagName = 'img';
static properties = { src: null, alt: null };
toDOM() {
if (this.src !== null) this.attributes.src = this.src;
if (this.alt !== null) this.attributes.alt = this.alt;
return super.toDOM();
}
}
export class Card extends Div { static className = 'Card'; }
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.