Finished
This commit is contained in:
@@ -8,8 +8,8 @@ import java.util.Objects;
|
||||
* Event passed from host into Lua.
|
||||
*
|
||||
* @param targetId The ID
|
||||
* @param type ID
|
||||
* @param data The data's
|
||||
* @param type ID
|
||||
* @param data The data's
|
||||
*/
|
||||
public record UiEvent(String targetId, String type, Map<String, Object> data) {
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.openautonomousconnection.luascript.fx;
|
||||
|
||||
import javafx.scene.web.WebEngine;
|
||||
import org.openautonomousconnection.luascript.fx.FxThreadBridge;
|
||||
import org.openautonomousconnection.luascript.hosts.DomHost;
|
||||
import org.w3c.dom.*;
|
||||
|
||||
@@ -30,6 +29,54 @@ public final class FxDomHost implements DomHost {
|
||||
this.engine = Objects.requireNonNull(engine, "engine");
|
||||
}
|
||||
|
||||
private static boolean hasUsableId(Element el) {
|
||||
String id = el.getAttribute("id");
|
||||
return id != null && !id.isBlank();
|
||||
}
|
||||
|
||||
private static Element ensureStagingContainer(Document doc) {
|
||||
Element body = (Element) doc.getElementsByTagName("body").item(0);
|
||||
if (body == null) throw new IllegalStateException("No <body> element available");
|
||||
|
||||
Element staging = doc.getElementById("__oac_staging");
|
||||
if (staging != null) return staging;
|
||||
|
||||
staging = doc.createElement("div");
|
||||
staging.setAttribute("id", "__oac_staging");
|
||||
staging.setAttribute("style", "display:none !important;");
|
||||
body.appendChild(staging);
|
||||
return staging;
|
||||
}
|
||||
|
||||
private static boolean hasClassToken(String classAttr, String cls) {
|
||||
String[] parts = classAttr.trim().split("\\s+");
|
||||
for (String p : parts) {
|
||||
if (p.equals(cls)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String normalizeTag(String tagName) {
|
||||
if (tagName == null) throw new IllegalArgumentException("tagName is null");
|
||||
String t = tagName.trim().toLowerCase(Locale.ROOT);
|
||||
if (t.isEmpty()) throw new IllegalArgumentException("tagName is empty");
|
||||
return t;
|
||||
}
|
||||
|
||||
private static String normalizeAttr(String name) {
|
||||
if (name == null) throw new IllegalArgumentException("attribute name is null");
|
||||
String n = name.trim();
|
||||
if (n.isEmpty()) throw new IllegalArgumentException("attribute name is empty");
|
||||
return n;
|
||||
}
|
||||
|
||||
private static String normalizeCssIdent(String s) {
|
||||
if (s == null) throw new IllegalArgumentException("identifier is null");
|
||||
String v = s.trim();
|
||||
if (v.isEmpty()) throw new IllegalArgumentException("identifier is empty");
|
||||
return v;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures every element has a stable id.
|
||||
*/
|
||||
@@ -292,11 +339,6 @@ public final class FxDomHost implements DomHost {
|
||||
return el;
|
||||
}
|
||||
|
||||
private static boolean hasUsableId(Element el) {
|
||||
String id = el.getAttribute("id");
|
||||
return id != null && !id.isBlank();
|
||||
}
|
||||
|
||||
private String generateUniqueId(Document doc) {
|
||||
while (true) {
|
||||
String id = "__auto_" + autoIdSeq.getAndIncrement();
|
||||
@@ -304,49 +346,6 @@ public final class FxDomHost implements DomHost {
|
||||
}
|
||||
}
|
||||
|
||||
private static Element ensureStagingContainer(Document doc) {
|
||||
Element body = (Element) doc.getElementsByTagName("body").item(0);
|
||||
if (body == null) throw new IllegalStateException("No <body> element available");
|
||||
|
||||
Element staging = doc.getElementById("__oac_staging");
|
||||
if (staging != null) return staging;
|
||||
|
||||
staging = doc.createElement("div");
|
||||
staging.setAttribute("id", "__oac_staging");
|
||||
staging.setAttribute("style", "display:none !important;");
|
||||
body.appendChild(staging);
|
||||
return staging;
|
||||
}
|
||||
|
||||
private static boolean hasClassToken(String classAttr, String cls) {
|
||||
String[] parts = classAttr.trim().split("\\s+");
|
||||
for (String p : parts) {
|
||||
if (p.equals(cls)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String normalizeTag(String tagName) {
|
||||
if (tagName == null) throw new IllegalArgumentException("tagName is null");
|
||||
String t = tagName.trim().toLowerCase(Locale.ROOT);
|
||||
if (t.isEmpty()) throw new IllegalArgumentException("tagName is empty");
|
||||
return t;
|
||||
}
|
||||
|
||||
private static String normalizeAttr(String name) {
|
||||
if (name == null) throw new IllegalArgumentException("attribute name is null");
|
||||
String n = name.trim();
|
||||
if (n.isEmpty()) throw new IllegalArgumentException("attribute name is empty");
|
||||
return n;
|
||||
}
|
||||
|
||||
private static String normalizeCssIdent(String s) {
|
||||
if (s == null) throw new IllegalArgumentException("identifier is null");
|
||||
String v = s.trim();
|
||||
if (v.isEmpty()) throw new IllegalArgumentException("identifier is empty");
|
||||
return v;
|
||||
}
|
||||
|
||||
private String normalizeOrGenerateId(String requestedId, Document doc) {
|
||||
if (requestedId != null) {
|
||||
String id = requestedId.trim();
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package org.openautonomousconnection.luascript.fx;
|
||||
|
||||
import org.openautonomousconnection.luascript.fx.FxDomHost;
|
||||
import org.openautonomousconnection.luascript.events.UiEventRegistry;
|
||||
import org.openautonomousconnection.luascript.fx.FxThreadBridge;
|
||||
import org.openautonomousconnection.luascript.hosts.EventHost;
|
||||
import org.openautonomousconnection.luascript.runtime.LuaEventRouter;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package org.openautonomousconnection.luascript.fx;
|
||||
|
||||
import javafx.scene.web.WebEngine;
|
||||
import org.openautonomousconnection.luascript.fx.FxDomHost;
|
||||
import org.openautonomousconnection.luascript.fx.FxThreadBridge;
|
||||
import org.openautonomousconnection.luascript.hosts.UiHost;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
@@ -30,6 +28,36 @@ public final class FxUiHost implements UiHost {
|
||||
this.dom = Objects.requireNonNull(dom, "dom");
|
||||
}
|
||||
|
||||
private static boolean hasClassToken(String classAttr, String cls) {
|
||||
String[] parts = classAttr.trim().split("\\s+");
|
||||
for (String p : parts) {
|
||||
if (p.equals(cls)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String removeCssProp(String style, String propLower) {
|
||||
if (style == null || style.isBlank()) return "";
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (String part : style.split(";")) {
|
||||
String p = part.trim();
|
||||
if (p.isEmpty()) continue;
|
||||
int idx = p.indexOf(':');
|
||||
if (idx <= 0) continue;
|
||||
|
||||
String k = p.substring(0, idx).trim().toLowerCase();
|
||||
if (k.equals(propLower)) continue;
|
||||
|
||||
if (!sb.isEmpty()) sb.append(';');
|
||||
sb.append(p);
|
||||
}
|
||||
|
||||
String out = sb.toString().trim();
|
||||
if (!out.isEmpty() && !out.endsWith(";")) out += ";";
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void alert(String message) {
|
||||
// No JS: use simple JavaFX dialog-less fallback (log-style). You can replace with real Dialogs later.
|
||||
@@ -260,34 +288,4 @@ public final class FxUiHost implements UiHost {
|
||||
public long nowMillis() {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
private static boolean hasClassToken(String classAttr, String cls) {
|
||||
String[] parts = classAttr.trim().split("\\s+");
|
||||
for (String p : parts) {
|
||||
if (p.equals(cls)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String removeCssProp(String style, String propLower) {
|
||||
if (style == null || style.isBlank()) return "";
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (String part : style.split(";")) {
|
||||
String p = part.trim();
|
||||
if (p.isEmpty()) continue;
|
||||
int idx = p.indexOf(':');
|
||||
if (idx <= 0) continue;
|
||||
|
||||
String k = p.substring(0, idx).trim().toLowerCase();
|
||||
if (k.equals(propLower)) continue;
|
||||
|
||||
if (!sb.isEmpty()) sb.append(';');
|
||||
sb.append(p);
|
||||
}
|
||||
|
||||
String out = sb.toString().trim();
|
||||
if (!out.isEmpty() && !out.endsWith(";")) out += ";";
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.openautonomousconnection.luascript.fx;
|
||||
|
||||
import javafx.scene.web.WebEngine;
|
||||
import org.openautonomousconnection.luascript.fx.FxThreadBridge;
|
||||
import org.openautonomousconnection.luascript.hosts.ResourceHost;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
|
||||
@@ -45,7 +45,7 @@ public interface DomHost {
|
||||
* Sets the element text content.
|
||||
*
|
||||
* @param elementId element id
|
||||
* @param text new text
|
||||
* @param text new text
|
||||
*/
|
||||
void setTextContent(String elementId, String text);
|
||||
|
||||
@@ -53,7 +53,7 @@ public interface DomHost {
|
||||
* Returns an attribute value or null if missing.
|
||||
*
|
||||
* @param elementId element id
|
||||
* @param name attribute name
|
||||
* @param name attribute name
|
||||
* @return value or null if missing
|
||||
*/
|
||||
String getAttribute(String elementId, String name);
|
||||
@@ -62,8 +62,8 @@ public interface DomHost {
|
||||
* Sets an attribute value (empty string allowed).
|
||||
*
|
||||
* @param elementId element id
|
||||
* @param name attribute name
|
||||
* @param value attribute value
|
||||
* @param name attribute name
|
||||
* @param value attribute value
|
||||
*/
|
||||
void setAttribute(String elementId, String name, String value);
|
||||
|
||||
@@ -71,7 +71,7 @@ public interface DomHost {
|
||||
* Removes an attribute from the element.
|
||||
*
|
||||
* @param elementId element id
|
||||
* @param name attribute name
|
||||
* @param name attribute name
|
||||
*/
|
||||
void removeAttribute(String elementId, String name);
|
||||
|
||||
@@ -94,7 +94,7 @@ public interface DomHost {
|
||||
/**
|
||||
* Creates an element and makes it addressable immediately.
|
||||
*
|
||||
* @param tagName tag name
|
||||
* @param tagName tag name
|
||||
* @param requestedId requested id or null
|
||||
* @return created element id
|
||||
*/
|
||||
@@ -111,15 +111,15 @@ public interface DomHost {
|
||||
* Appends a child element to a parent.
|
||||
*
|
||||
* @param parentId parent id
|
||||
* @param childId child id
|
||||
* @param childId child id
|
||||
*/
|
||||
void appendChild(String parentId, String childId);
|
||||
|
||||
/**
|
||||
* Inserts {@code childId} before {@code beforeChildId} within {@code parentId}.
|
||||
*
|
||||
* @param parentId parent id
|
||||
* @param childId child id
|
||||
* @param parentId parent id
|
||||
* @param childId child id
|
||||
* @param beforeChildId existing child id
|
||||
*/
|
||||
void insertBefore(String parentId, String childId, String beforeChildId);
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package org.openautonomousconnection.luascript.hosts;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Abstraction over DOM event subscription for scripting.
|
||||
*
|
||||
|
||||
@@ -58,11 +58,11 @@ public interface HostServices {
|
||||
/**
|
||||
* Creates a HostServices container.
|
||||
*
|
||||
* @param ui ui host
|
||||
* @param dom dom host
|
||||
* @param events event host
|
||||
* @param ui ui host
|
||||
* @param dom dom host
|
||||
* @param events event host
|
||||
* @param resources resource host
|
||||
* @param console console host
|
||||
* @param console console host
|
||||
*/
|
||||
public Default(UiHost ui, DomHost dom, EventHost events, ResourceHost resources, ConsoleHost console) {
|
||||
this.ui = ui;
|
||||
@@ -113,6 +113,10 @@ public interface HostServices {
|
||||
this.prefix = Objects.requireNonNull(prefix, "prefix");
|
||||
}
|
||||
|
||||
private static String safe(String s) {
|
||||
return s == null ? "" : s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(String message) {
|
||||
System.out.println(prefix + "[info] " + safe(message));
|
||||
@@ -137,9 +141,5 @@ public interface HostServices {
|
||||
public void exception(String message) {
|
||||
System.err.println(prefix + "[exception] " + safe(message));
|
||||
}
|
||||
|
||||
private static String safe(String s) {
|
||||
return s == null ? "" : s;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ public interface UiHost {
|
||||
/**
|
||||
* Displays a prompt-like query.
|
||||
*
|
||||
* @param message message
|
||||
* @param message message
|
||||
* @param defaultValue default value
|
||||
* @return user response or default
|
||||
*/
|
||||
@@ -33,7 +33,7 @@ public interface UiHost {
|
||||
* Sets element text.
|
||||
*
|
||||
* @param elementId element id
|
||||
* @param text text
|
||||
* @param text text
|
||||
*/
|
||||
void setText(String elementId, String text);
|
||||
|
||||
@@ -49,7 +49,7 @@ public interface UiHost {
|
||||
* Sets element HTML (best-effort for non-JS hosts).
|
||||
*
|
||||
* @param elementId element id
|
||||
* @param html html
|
||||
* @param html html
|
||||
*/
|
||||
void setHtml(String elementId, String html);
|
||||
|
||||
@@ -65,7 +65,7 @@ public interface UiHost {
|
||||
* Sets a form-like value.
|
||||
*
|
||||
* @param elementId element id
|
||||
* @param value value
|
||||
* @param value value
|
||||
*/
|
||||
void setValue(String elementId, String value);
|
||||
|
||||
@@ -81,7 +81,7 @@ public interface UiHost {
|
||||
* Enables/disables an element.
|
||||
*
|
||||
* @param elementId element id
|
||||
* @param enabled enabled
|
||||
* @param enabled enabled
|
||||
*/
|
||||
void setEnabled(String elementId, boolean enabled);
|
||||
|
||||
@@ -89,7 +89,7 @@ public interface UiHost {
|
||||
* Shows/hides an element.
|
||||
*
|
||||
* @param elementId element id
|
||||
* @param visible visible
|
||||
* @param visible visible
|
||||
*/
|
||||
void setVisible(String elementId, boolean visible);
|
||||
|
||||
@@ -131,8 +131,8 @@ public interface UiHost {
|
||||
* Sets a CSS property via style attribute (best-effort).
|
||||
*
|
||||
* @param elementId element id
|
||||
* @param property css property
|
||||
* @param value css value
|
||||
* @param property css property
|
||||
* @param value css value
|
||||
*/
|
||||
void setStyle(String elementId, String property, String value);
|
||||
|
||||
@@ -140,7 +140,7 @@ public interface UiHost {
|
||||
* Gets a CSS property via style attribute (best-effort).
|
||||
*
|
||||
* @param elementId element id
|
||||
* @param property css property
|
||||
* @param property css property
|
||||
* @return css value or empty string
|
||||
*/
|
||||
String getStyle(String elementId, String property);
|
||||
@@ -149,8 +149,8 @@ public interface UiHost {
|
||||
* Sets an attribute.
|
||||
*
|
||||
* @param elementId element id
|
||||
* @param name attribute name
|
||||
* @param value value
|
||||
* @param name attribute name
|
||||
* @param value value
|
||||
*/
|
||||
void setAttribute(String elementId, String name, String value);
|
||||
|
||||
@@ -158,7 +158,7 @@ public interface UiHost {
|
||||
* Gets an attribute value or null if missing.
|
||||
*
|
||||
* @param elementId element id
|
||||
* @param name attribute name
|
||||
* @param name attribute name
|
||||
* @return value or null
|
||||
*/
|
||||
String getAttribute(String elementId, String name);
|
||||
@@ -167,7 +167,7 @@ public interface UiHost {
|
||||
* Removes an attribute.
|
||||
*
|
||||
* @param elementId element id
|
||||
* @param name attribute name
|
||||
* @param name attribute name
|
||||
*/
|
||||
void removeAttribute(String elementId, String name);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user