From 29e5d1bd37eaccaa9e9cf75f544714c0c3687fbe Mon Sep 17 00:00:00 2001 From: Snider Date: Wed, 15 Apr 2026 16:47:59 +0100 Subject: [PATCH] Align webview console and evaluate behavior --- console.go | 38 +++++++++++++++++++++++--------------- webview.go | 2 +- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/console.go b/console.go index 27e42a2..9e27faf 100644 --- a/console.go +++ b/console.go @@ -58,6 +58,9 @@ func NewConsoleWatcher(wv *Webview) *ConsoleWatcher { } // normalizeConsoleType converts CDP event types to package-level values. +// +// This keeps the legacy warn alias for compatibility. Use canonicalConsoleType +// when the exact RFC type names are required. func normalizeConsoleType(raw string) string { normalized := strings.ToLower(core.Trim(core.Sprint(raw))) if normalized == "warning" { @@ -66,6 +69,15 @@ func normalizeConsoleType(raw string) string { return normalized } +// canonicalConsoleType returns the RFC-canonical console type name. +func canonicalConsoleType(raw string) string { + normalized := strings.ToLower(core.Trim(core.Sprint(raw))) + if normalized == "warn" || normalized == "warning" { + return "warning" + } + return normalized +} + // consoleTextFromArgs extracts message text from Runtime.consoleAPICalled args. func consoleTextFromArgs(args []any) string { text := core.NewBuilder() @@ -270,7 +282,7 @@ func (cw *ConsoleWatcher) ErrorsAll() iter.Seq[ConsoleMessage] { defer cw.mu.RUnlock() for _, msg := range cw.messages { - if msg.Type == "error" { + if canonicalConsoleType(msg.Type) == "error" { if !yield(msg) { return } @@ -291,7 +303,7 @@ func (cw *ConsoleWatcher) WarningsAll() iter.Seq[ConsoleMessage] { defer cw.mu.RUnlock() for _, msg := range cw.messages { - if isWarningType(msg.Type) { + if canonicalConsoleType(msg.Type) == "warning" { if !yield(msg) { return } @@ -352,7 +364,7 @@ func (cw *ConsoleWatcher) HasErrors() bool { defer cw.mu.RUnlock() for _, msg := range cw.messages { - if msg.Type == "error" { + if canonicalConsoleType(msg.Type) == "error" { return true } } @@ -373,7 +385,7 @@ func (cw *ConsoleWatcher) ErrorCount() int { count := 0 for _, msg := range cw.messages { - if msg.Type == "error" { + if canonicalConsoleType(msg.Type) == "error" { count++ } } @@ -382,7 +394,7 @@ func (cw *ConsoleWatcher) ErrorCount() int { // handleConsoleEvent processes incoming console events. func (cw *ConsoleWatcher) handleConsoleEvent(params map[string]any) { - msgType := normalizeConsoleType(core.Sprint(params["type"])) + msgType := canonicalConsoleType(core.Sprint(params["type"])) // Extract args args, _ := params["args"].([]any) @@ -451,13 +463,9 @@ func (cw *ConsoleWatcher) matchesFilter(msg ConsoleMessage) bool { // matchesSingleFilter checks if a message matches a specific filter. func (cw *ConsoleWatcher) matchesSingleFilter(msg ConsoleMessage, filter ConsoleFilter) bool { if filter.Type != "" { - filterType := normalizeConsoleType(filter.Type) - messageType := normalizeConsoleType(msg.Type) - if isWarningType(filterType) { - if !isWarningType(messageType) { - return false - } - } else if messageType != filterType { + filterType := canonicalConsoleType(filter.Type) + messageType := canonicalConsoleType(msg.Type) + if messageType != filterType { return false } } @@ -471,7 +479,7 @@ func (cw *ConsoleWatcher) matchesSingleFilter(msg ConsoleMessage, filter Console } func isWarningType(messageType string) bool { - return messageType == "warn" || messageType == "warning" + return canonicalConsoleType(messageType) == "warning" } // containsString checks if s contains substr (case-sensitive). @@ -684,10 +692,10 @@ func FormatConsoleOutput(messages []ConsoleMessage) string { output := core.NewBuilder() for _, msg := range messages { prefix := "" - switch normalizeConsoleType(msg.Type) { + switch canonicalConsoleType(msg.Type) { case "error": prefix = "[ERROR]" - case "warn": + case "warning": prefix = "[WARN]" case "info": prefix = "[INFO]" diff --git a/webview.go b/webview.go index fd97814..591b996 100644 --- a/webview.go +++ b/webview.go @@ -594,7 +594,7 @@ func (wv *Webview) evaluate(ctx context.Context, script string) (any, error) { return resultObj["value"], nil } - return nil, nil + return nil, coreerr.E("Webview.evaluate", "missing evaluation result", nil) } // querySelector finds an element by selector.