mirror of
https://codeberg.org/setop/elm-scripting
synced 2025-11-08 21:49:57 +00:00
feat: can pass content as flags
This commit is contained in:
55
dom.js
55
dom.js
@@ -2,30 +2,39 @@ function Node(parent, tag) {
|
||||
this.parentNode = parent; // Node
|
||||
this.pos = 0; // position in siblings list
|
||||
this.tagName = tag;
|
||||
this.text = null;
|
||||
// attributes are stored directly on the node object
|
||||
// see https://github.com/elm/virtual-dom/blob/master/src/Elm/Kernel/VirtualDom.js#L511
|
||||
// but Html.Attributes.attribute/2 uses Element.setAttribute
|
||||
this.setAttribute = (key, val) => {
|
||||
// as of now, there is no check if key conflict with the ones defined by Dom implementation
|
||||
this[key] = val;
|
||||
}
|
||||
this.children = []; // List of Node
|
||||
this.replaceChild = (newNode, domNode) => {
|
||||
this.children[domNode.pos-1] = newNode;
|
||||
newNode.pos = domNode.pos;
|
||||
};
|
||||
};
|
||||
if (parent != null) {
|
||||
this.pos = parent.children.push(this);
|
||||
}
|
||||
this.appendChild = (node) => {
|
||||
node.pos = this.children.push(node);
|
||||
}
|
||||
this.dump = (d=0) => {
|
||||
if (this.text) {
|
||||
print(this.text);
|
||||
return
|
||||
}
|
||||
if (this.innerHTML) {
|
||||
print(this.innerHTML);
|
||||
return
|
||||
}
|
||||
std.printf("<%s", this.tagName)
|
||||
// Set.difference(other) is not avalable in qjs
|
||||
this.replaceData = (offset, count, data) => {
|
||||
this.text = data; // elm runtime will always replace the whole text
|
||||
}
|
||||
this.dump = (d=0) => {
|
||||
if (this.text != null) {
|
||||
print(this.text);
|
||||
return
|
||||
}
|
||||
if (this.innerHTML) {
|
||||
print(this.innerHTML);
|
||||
return
|
||||
}
|
||||
std.printf("<%s", this.tagName)
|
||||
// Set.difference(other) is not avalable in qjs
|
||||
for (a of Object.keys(this)) {
|
||||
if (!NodeKeys.has(a)) {
|
||||
std.printf(' %s="%s"', a, this[a])
|
||||
@@ -34,18 +43,18 @@ function Node(parent, tag) {
|
||||
if (this.children.length==0) {
|
||||
print("/>")
|
||||
} else {
|
||||
print(">")
|
||||
for (c of this.children) {
|
||||
c.dump(d+1)
|
||||
}
|
||||
print("</"+this.tagName+">");
|
||||
print(">")
|
||||
for (c of this.children) {
|
||||
c.dump(d+1)
|
||||
}
|
||||
print("</"+this.tagName+">");
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
var document = new Node(null, "document");
|
||||
var target = new Node(document, "target");
|
||||
const document = new Node(null, "document");
|
||||
const target = new Node(document, "target");
|
||||
const NodeKeys = new Set(Object.keys(target));
|
||||
|
||||
// getElementById is only used once, to get node Elm must hook into.
|
||||
@@ -56,8 +65,8 @@ document.getElementById = (_id) => { return target}
|
||||
document.createElement = (tag) => new Node(null, tag);
|
||||
document.createTextNode = (text) => { t = new Node(null, "#text" ); t.text = text; return t }
|
||||
|
||||
var global = {};
|
||||
|
||||
try {
|
||||
// workaround for elm-explorations/markdown Markdown.toHtml
|
||||
const global = {};
|
||||
|
||||
|
||||
// here will come the Elm app code
|
||||
|
||||
Reference in New Issue
Block a user