Major codebase cleanup after .insertr-add functionality overhaul

Consolidates duplicate code and removes technical debt accumulated during rapid development. This cleanup improves maintainability while preserving all functionality.

Backend cleanup:
- Remove unused legacy function findViableChildrenLegacy()
- Consolidate duplicate SQL null string helper functions into shared utils
- Unify text extraction functions across utils, engine, and id_generator
- Consolidate duplicate attribute getter functions into single implementation

Frontend cleanup:
- Remove duplicate authentication methods (authenticateWithOAuth vs performOAuthFlow)
- Remove unused hasPermission() method from auth.js
- Centralize repetitive API endpoint construction in api-client.js
- Reduce excessive console logging while preserving important error logs

Impact: -144 lines of code, improved maintainability, no functionality changes
All tests pass and builds succeed

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-23 19:00:41 +02:00
parent 1ae4176f23
commit 3e5cb76d1d
8 changed files with 71 additions and 215 deletions

View File

@@ -1,6 +1,7 @@
package engine
import (
"database/sql"
"strings"
"golang.org/x/net/html"
@@ -37,28 +38,6 @@ func getAttribute(node *html.Node, key string) string {
return ""
}
// extractTextContent gets the text content from an HTML node
func extractTextContent(node *html.Node) string {
var text strings.Builder
extractTextRecursive(node, &text)
return strings.TrimSpace(text.String())
}
// extractTextRecursive recursively extracts text from node and children
func extractTextRecursive(node *html.Node, text *strings.Builder) {
if node.Type == html.TextNode {
text.WriteString(node.Data)
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
// Skip script and style elements
if child.Type == html.ElementNode &&
(child.Data == "script" || child.Data == "style") {
continue
}
extractTextRecursive(child, text)
}
}
// hasOnlyTextContent checks if a node contains only text content (no nested HTML elements)
// DEPRECATED: Use hasEditableContent for more sophisticated detection
@@ -324,32 +303,6 @@ func hasInsertrClass(node *html.Node) bool {
return false
}
// findViableChildrenLegacy uses the old text-only logic for backwards compatibility
func findViableChildrenLegacy(node *html.Node) []*html.Node {
var viable []*html.Node
for child := node.FirstChild; child != nil; child = child.NextSibling {
if child.Type == html.TextNode {
if strings.TrimSpace(child.Data) == "" {
continue
}
}
if child.Type != html.ElementNode {
continue
}
if isSelfClosing(child) {
continue
}
if hasOnlyTextContent(child) {
viable = append(viable, child)
}
}
return viable
}
// isSelfClosing checks if an element is typically self-closing
func isSelfClosing(node *html.Node) bool {
@@ -389,7 +342,7 @@ func findElementWithContent(node *html.Node, targetTag, targetContent string) *h
classes := GetClasses(node)
if ContainsClass(classes, "insertr") {
// Content-based validation for precise matching
textContent := extractTextContent(node)
textContent := ExtractTextContent(node)
nodeContent := strings.TrimSpace(textContent)
if nodeContent == normalizedTarget {
@@ -422,3 +375,45 @@ func HasEditableContent(node *html.Node) bool {
func FindViableChildren(node *html.Node) []*html.Node {
return findViableChildren(node)
}
// SQL utility functions for consistent null string handling
// ToNullString converts a string to sql.NullString
func ToNullString(s string) sql.NullString {
if s == "" {
return sql.NullString{Valid: false}
}
return sql.NullString{String: s, Valid: true}
}
// FromNullString converts sql.NullString to string
func FromNullString(ns sql.NullString) string {
if ns.Valid {
return ns.String
}
return ""
}
// Text extraction utility functions
// ExtractTextContent extracts all text content from an HTML node recursively
func ExtractTextContent(node *html.Node) string {
var text strings.Builder
extractTextRecursiveUnified(node, &text)
return strings.TrimSpace(text.String())
}
// extractTextRecursiveUnified is the internal unified implementation
func extractTextRecursiveUnified(node *html.Node, text *strings.Builder) {
if node.Type == html.TextNode {
text.WriteString(node.Data)
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
// Skip script and style elements
if child.Type == html.ElementNode &&
(child.Data == "script" || child.Data == "style") {
continue
}
extractTextRecursiveUnified(child, text)
}
}