aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/config.md8
-rw-r--r--src/html/dom.nim6
-rw-r--r--src/html/env.nim5
-rw-r--r--src/server/buffer.nim1
4 files changed, 16 insertions, 4 deletions
diff --git a/doc/config.md b/doc/config.md
index 1fee73a3..a56ad47c 100644
--- a/doc/config.md
+++ b/doc/config.md
@@ -186,6 +186,7 @@ option disabled, only enabling it for specific sites in `[[siteconf]]`.
<td>boolean</td>
<td>When set to true, elements with an "autofocus" attribute are focused on
automatically after the buffer is loaded.<br>
+If scripting is enabled, this also allows scripts to focus on elements.<br>
Defaults to false.</td>
</tr>
@@ -908,8 +909,11 @@ are doing.</td>
<tr>
<td>autofocus</td>
<td>boolean</td>
-<td>When set to true, elements with an "autofocus" attribute are focused on
-automatically after the buffer is loaded. Overrides `buffer.autofocus`.</td>
+<td>When set to true, elements with an "autofocus" attribute are focused
+on automatically after the buffer is loaded.<br>
+If scripting is enabled, this also allows scripts to focus on
+elements.<br>
+Overrides `buffer.autofocus`.</td>
</tr>
<tr>
diff --git a/src/html/dom.nim b/src/html/dom.nim
index e1f841e7..1a916b5e 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -109,6 +109,7 @@ type
urandom*: PosixStream
imageTypes*: Table[string, string]
userAgent*: string
+ autofocus*: bool
# Navigator stuff
Navigator* = object
@@ -2700,6 +2701,11 @@ proc setFocus*(document: Document; element: Element) =
if element != nil:
element.invalidDeps.incl(dtFocus)
+proc focus(ctx: JSContext; element: Element) {.jsfunc.} =
+ let window = ctx.getWindow()
+ if window != nil and window.autofocus:
+ element.document.setFocus(element)
+
func target*(document: Document): Element =
return document.internalTarget
diff --git a/src/html/env.nim b/src/html/env.nim
index cb38fd1c..3405229c 100644
--- a/src/html/env.nim
+++ b/src/html/env.nim
@@ -342,7 +342,7 @@ proc runJSJobs*(window: Window) =
let ctx = r.error
ctx.writeException(window.console.err)
-proc newWindow*(scripting: ScriptingMode; images, styling: bool;
+proc newWindow*(scripting: ScriptingMode; images, styling, autofocus: bool;
attrs: WindowAttributes; factory: CAtomFactory; loader: FileLoader;
url: URL; urandom: PosixStream; imageTypes: Table[string, string];
userAgent: string): Window =
@@ -361,7 +361,8 @@ proc newWindow*(scripting: ScriptingMode; images, styling: bool;
factory: factory,
urandom: urandom,
imageTypes: imageTypes,
- userAgent: userAgent
+ userAgent: userAgent,
+ autofocus: autofocus
)
window.location = window.newLocation()
if scripting != smFalse:
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index dd0f8dc9..3012c639 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -1939,6 +1939,7 @@ proc launchBuffer*(config: BufferConfig; url: URL; attrs: WindowAttributes;
config.scripting,
config.images,
config.styling,
+ config.autofocus,
attrs,
factory,
loader,