aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2025-01-10 20:33:13 +0100
committerbptato <nincsnevem662@gmail.com>2025-01-10 20:33:13 +0100
commitf6200928a08767d5336d090fa818010b189c5e44 (patch)
treee54c7045092cdaaee80e800da5d1e4d5cd7764dd
parentf269971cc52f534f1deeceb5cfb0049dee4dcc93 (diff)
downloadchawan-f6200928a08767d5336d090fa818010b189c5e44.tar.gz
buffer, ua.css, dom: more select adjustments
ok, now it works like in w3m. (mostly, barring CSS limitations...) we'll see how this works out Also adds/fixes some select and option DOM APIs.
-rw-r--r--res/ua.css28
-rw-r--r--src/html/dom.nim55
-rw-r--r--src/local/container.nim15
-rw-r--r--src/local/pager.nim14
-rw-r--r--src/local/select.nim62
-rw-r--r--src/server/buffer.nim57
6 files changed, 88 insertions, 143 deletions
diff --git a/res/ua.css b/res/ua.css
index ffe7cc2d..e9282de7 100644
--- a/res/ua.css
+++ b/res/ua.css
@@ -1,7 +1,7 @@
:root { display: block !important }
area, base, basefont, datalist, head, link, meta, noembed,
-noframes, param, rp, script, style, template, title { display: none }
+param, rp, script, style, template, title { display: none }
address, article, aside, blockquote, body, center, dd, details, dialog,
dir, div, dl, dt, figcaption, figure, footer, form, h1, h2, h3, h4,
@@ -133,15 +133,15 @@ dl[compact] dt + br { display: none }
select { display: inline-flex; white-space: nowrap }
select[multiple] { white-space: initial }
-select::before { content: '['; color: initial; float: left }
-select::after { content: ']'; color: initial; float: right }
+select::before { content: '['; color: initial }
+select::after { content: ']'; color: initial }
select > :is(option, optgroup > option) { display: none }
select > :is(option, optgroup > option):checked { display: block; color: red }
-select[multiple] { display: flow-root; overflow: hidden }
+select[multiple] { display: inline-block; overflow: auto }
select[multiple]::before, select[multiple]::after { content: '' }
select[multiple] > :is(option, optgroup > option) {
- display: block; color: inherit
+ display: flex; color: inherit
}
select[multiple] > :is(option, optgroup > option)::before { content: '[ ]' }
select[multiple] > :is(option, optgroup > option):checked::before {
@@ -170,31 +170,19 @@ table[align=center i] { margin-left: auto; margin-right: auto }
:is(div, p, td, th, thead, tbody, tfoot, tr)[align=left i] {
text-align: -cha-left
}
-
:is(div, p, td, th, thead, tbody, tfoot, tr)[align=center i] {
text-align: -cha-center
}
-
:is(div, p, td, th, thead, tbody, tfoot, tr)[align=middle i] {
text-align: -cha-center
}
-
:is(div, p, td, th, thead, tbody, tfoot, tr)[align=right i] {
text-align: -cha-right
}
-:is(td, th, thead, tbody, tfoot, tr)[valign=top i] {
- vertical-align: top
-}
-
-:is(td, th, thead, tbody, tfoot, tr)[valign=middle i] {
- vertical-align: middle
-}
-
-:is(td, th, thead, tbody, tfoot, tr)[valign=bottom i] {
- vertical-align: bottom
-}
-
+:is(td, th, thead, tbody, tfoot, tr)[valign=top i] { vertical-align: top }
+:is(td, th, thead, tbody, tfoot, tr)[valign=middle i] { vertical-align: middle }
+:is(td, th, thead, tbody, tfoot, tr)[valign=bottom i] { vertical-align: bottom }
:is(td, th, thead, tbody, tfoot, tr)[valign=baseline i] {
vertical-align: baseline
}
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 716d6776..10d6ee4b 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -313,7 +313,7 @@ type
HTMLOptGroupElement* = ref object of HTMLElement
HTMLOptionElement* = ref object of HTMLElement
- selected*: bool
+ selected* {.jsget.}: bool
dirty: bool
HTMLHeadingElement* = ref object of HTMLElement
@@ -526,6 +526,7 @@ proc parseColor(element: Element; s: string): ARGBColor
proc reflectAttr(element: Element; name: CAtom; value: Option[string])
proc remove*(node: Node)
proc setInvalid*(element: Element)
+proc setInvalid*(element: Element; dep: DependencyType)
# Forward declaration hacks
# set in css/match
@@ -1047,6 +1048,7 @@ const ReflectTable0 = [
makeb("required", TAG_INPUT, TAG_SELECT, TAG_TEXTAREA),
makes("name", TAG_INPUT, TAG_SELECT, TAG_TEXTAREA, TAG_META),
makeb("novalidate", "noValidate", TAG_FORM),
+ makeb("selected", "defaultSelected", TAG_OPTION),
makes("rel", TAG_A, TAG_LINK, TAG_LABEL),
makes("for", "htmlFor", TAG_LABEL),
makes("http-equiv", "httpEquiv", TAG_META),
@@ -2731,11 +2733,10 @@ func focus*(document: Document): Element =
proc setFocus*(document: Document; element: Element) =
if document.focus != nil:
- document.focus.invalidDeps.incl(dtFocus)
+ document.focus.setInvalid(dtFocus)
document.internalFocus = element
- document.invalid = true
if element != nil:
- element.invalidDeps.incl(dtFocus)
+ element.setInvalid(dtFocus)
proc focus(ctx: JSContext; element: Element) {.jsfunc.} =
let window = ctx.getWindow()
@@ -2750,19 +2751,17 @@ func target*(document: Document): Element =
proc setTarget*(document: Document; element: Element) =
if document.target != nil:
- document.target.invalidDeps.incl(dtTarget)
+ document.target.setInvalid(dtTarget)
document.internalTarget = element
if element != nil:
- element.invalidDeps.incl(dtTarget)
+ element.setInvalid(dtTarget)
func hover*(element: Element): bool =
return element.internalHover
proc setHover*(element: Element; hover: bool) =
- element.invalidDeps.incl(dtHover)
+ element.setInvalid(dtHover)
element.internalHover = hover
- if element.document != nil:
- element.document.invalid = true
func findAutoFocus*(document: Document): Element =
for child in document.elements:
@@ -2961,12 +2960,10 @@ func checked*(input: HTMLInputElement): bool {.inline.} =
proc setChecked*(input: HTMLInputElement; b: bool) {.jsfset: "checked".} =
if input.inputType == itRadio:
for radio in input.radiogroup:
- radio.invalidDeps.incl(dtChecked)
+ radio.setInvalid(dtChecked)
radio.internalChecked = false
- radio.setInvalid()
- input.invalidDeps.incl(dtChecked)
+ input.setInvalid(dtChecked)
input.internalChecked = b
- input.setInvalid()
func inputString*(input: HTMLInputElement): string =
case input.inputType
@@ -3050,6 +3047,27 @@ func select*(option: HTMLOptionElement): HTMLSelectElement =
return HTMLSelectElement(anc)
return nil
+proc setSelected*(option: HTMLOptionElement; selected: bool)
+ {.jsfset: "selected".} =
+ option.setInvalid(dtChecked)
+ option.selected = selected
+ let select = option.select
+ if select != nil and not select.attrb(satMultiple):
+ var firstOption: HTMLOptionElement = nil
+ var prevSelected: HTMLOptionElement = nil
+ for option in select.options:
+ if firstOption == nil:
+ firstOption = option
+ if option.selected:
+ if prevSelected != nil:
+ prevSelected.selected = false
+ prevSelected.setInvalid(dtChecked)
+ prevSelected = option
+ if select.attrul(satSize).get(1) == 1 and
+ prevSelected == nil and firstOption != nil:
+ firstOption.selected = true
+ firstOption.setInvalid(dtChecked)
+
# <select>
func jsForm(this: HTMLSelectElement): HTMLFormElement {.jsfget: "form".} =
return this.form
@@ -3115,7 +3133,7 @@ proc selectedOptions(ctx: JSContext; this: HTMLSelectElement): JSValue
JS_FreeValue(ctx, this)
return selectedOptions
-proc selectedIndex(this: HTMLSelectElement): int {.jsfget.} =
+proc selectedIndex*(this: HTMLSelectElement): int {.jsfget.} =
var i = 0
for it in this.options:
if it.selected:
@@ -3123,7 +3141,7 @@ proc selectedIndex(this: HTMLSelectElement): int {.jsfget.} =
inc i
return -1
-proc setSelectedIndex(this: HTMLSelectElement; n: int)
+proc setSelectedIndex*(this: HTMLSelectElement; n: int)
{.jsfset: "selectedIndex".} =
var i = 0
for it in this.options:
@@ -3132,6 +3150,7 @@ proc setSelectedIndex(this: HTMLSelectElement; n: int)
it.dirty = true
else:
it.selected = false
+ it.setInvalid(dtChecked)
it.invalidateCollections()
inc i
@@ -3610,6 +3629,11 @@ proc setInvalid*(element: Element) =
if element.document != nil:
element.document.invalid = true
+proc setInvalid*(element: Element; dep: DependencyType) =
+ element.invalidDeps.incl(dep)
+ if element.document != nil:
+ element.document.invalid = true
+
proc delAttr(element: Element; i: int; keep = false) =
let map = element.cachedAttributes
let name = element.attrs[i].qualifiedName
@@ -4411,6 +4435,7 @@ proc resetElement*(element: Element) =
if option.selected:
if not multiple and prevSelected != nil:
prevSelected.selected = false
+ prevSelected.setInvalid(dtChecked)
prevSelected = option
if not multiple and select.attrul(satSize).get(1) == 1 and
prevSelected == nil and firstOption != nil:
diff --git a/src/local/container.nim b/src/local/container.nim
index 7ba1a4bf..7e426581 100644
--- a/src/local/container.nim
+++ b/src/local/container.nim
@@ -1582,22 +1582,15 @@ proc reshape(container: Container): EmptyPromise {.jsfunc.} =
return container.requestLines()
)
-proc selectFinish(opaque: RootRef; select: Select; res: SubmitResult) =
+proc selectFinish(opaque: RootRef; select: Select) =
let container = Container(opaque)
- case res
- of srCancel:
- container.iface.select(select.oselected).then(proc(res: ClickResult) =
- container.onclick(res, save = false)
- )
- of srSubmit:
- container.iface.select(select.selected).then(proc(res: ClickResult) =
- container.onclick(res, save = false)
- )
+ container.iface.select(select.selected).then(proc(res: ClickResult) =
+ container.onclick(res, save = false)
+ )
container.select = nil
proc displaySelect(container: Container; selectResult: SelectResult) =
container.select = newSelect(
- selectResult.multiple,
selectResult.options,
selectResult.selected,
max(container.acursorx - 1, 0),
diff --git a/src/local/pager.nim b/src/local/pager.nim
index 5dc6bad9..859722e2 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -704,8 +704,7 @@ proc handleMouseInput(pager: Pager; input: MouseInput; select: Select) =
select.cursorLeft()
of mitRelease:
let at = (input.col, input.row)
- if at == pager.pressed and
- (inside or select.multiple and at == (select.x, select.y)):
+ if at == pager.pressed and inside:
# clicked inside the select
select.setCursorY(y)
select.click()
@@ -2949,11 +2948,10 @@ const MenuMap = [
("Open history (C-h)", "cmd.pager.openHistory(1)"),
]
-proc menuFinish(opaque: RootRef; select: Select; sr: SubmitResult) =
+proc menuFinish(opaque: RootRef; select: Select) =
let pager = Pager(opaque)
- case sr
- of srCancel: discard
- of srSubmit: pager.scommand = MenuMap[select.selected[0]][1]
+ if select.selected != -1:
+ pager.scommand = MenuMap[select.selected][1]
pager.menu = nil
if pager.container != nil:
pager.container.queueDraw()
@@ -2971,8 +2969,8 @@ proc openMenu(pager: Pager; x = -1; y = -1) {.jsfunc.} =
var options: seq[SelectOption] = @[]
for (s, cmd) in MenuMap:
options.add(SelectOption(s: s, nop: cmd == ""))
- pager.menu = newSelect(false, options, @[], x, y, pager.bufWidth,
- pager.bufHeight, menuFinish, pager)
+ pager.menu = newSelect(options, -1, x, y, pager.bufWidth, pager.bufHeight,
+ menuFinish, pager)
proc handleEvent0(pager: Pager; container: Container; event: ContainerEvent):
bool =
diff --git a/src/local/select.nim b/src/local/select.nim
index eba97c27..6be10478 100644
--- a/src/local/select.nim
+++ b/src/local/select.nim
@@ -6,8 +6,7 @@ import utils/strwidth
import utils/twtstr
type
- SelectFinish* = proc(opaque: RootRef; select: Select; res: SubmitResult)
- {.nimcall.}
+ SelectFinish* = proc(opaque: RootRef; select: Select) {.nimcall.}
SelectOption* = object
nop*: bool
@@ -15,11 +14,9 @@ type
Select* = ref object
options: seq[SelectOption]
- multiple* {.jsget.}: bool
- oselected*: seq[int] # old selection
- selected*: seq[int] # new selection
+ selected*: int # new selection
fromy* {.jsget.}: int # first index to display
- cursory {.jsget.}: int # selected index
+ cursory {.jsget.}: int # hover index
maxw: int # widest option
maxh: int # maximum number of options on screen
# location on screen
@@ -32,9 +29,6 @@ type
opaque: RootRef
finishImpl: SelectFinish
- SubmitResult* = enum
- srCancel, srSubmit
-
jsDestructor(Select)
proc queueDraw(select: Select) =
@@ -50,9 +44,7 @@ func height*(select: Select): int =
return select.maxh + 2
proc setCursorY*(select: Select; y: int) =
- var y = clamp(y, -1, select.options.high)
- if not select.multiple:
- y = max(y, 0)
+ let y = clamp(y, 0, select.options.high)
if select.options[max(y, 0)].nop:
if not select.unselected:
select.unselected = true
@@ -137,40 +129,22 @@ proc gotoLine(select: Select; n: int) {.jsfunc.} =
select.setCursorY(n + 1)
proc cancel(select: Select) {.jsfunc.} =
- select.finishImpl(select.opaque, select, srCancel)
+ select.finishImpl(select.opaque, select)
proc submit(select: Select) {.jsfunc.} =
- if not select.multiple:
- select.selected = @[select.cursory]
- select.finishImpl(select.opaque, select, srSubmit)
+ select.selected = select.cursory
+ select.finishImpl(select.opaque, select)
proc click*(select: Select) {.jsfunc.} =
if select.unselected or
select.cursory >= 0 and select.cursory < select.options.len and
select.options[select.cursory].nop:
discard
- elif not select.multiple:
- select.submit()
- elif select.cursory == -1:
- select.submit()
else:
- var k = select.selected.len
- let i = select.cursory
- for j in 0 ..< select.selected.len:
- if select.selected[j] >= i:
- k = j
- break
- if k < select.selected.len and select.selected[k] == i:
- select.selected.delete(k)
- else:
- select.selected.insert(i, k)
- select.queueDraw()
+ select.submit()
proc cursorLeft*(select: Select) {.jsfunc.} =
- if select.multiple:
- select.submit()
- else:
- select.cancel()
+ select.cancel()
proc cursorRight(select: Select) {.jsfunc.} =
select.click()
@@ -321,26 +295,17 @@ proc drawSelect*(select: Select; display: var FixedGrid) =
let upmore = select.fromy > 0
let downmore = select.fromy + mh < select.options.len
drawBorders(display, sx, ex, sy, ey, upmore, downmore)
- if select.multiple and not upmore:
- display[sy * display.width + sx].str = "X"
# move inside border
inc sy
inc sx
- var k = 0
var format = Format()
- while k < select.selected.len and select.selected[k] < si:
- inc k
for y in sy ..< ey:
let i = y - sy + si
var j = 0
var x = sx
let dls = y * display.width
- if (select.multiple and k < select.selected.len and
- select.selected[k] == i or
- not select.multiple and select.getCursorY() == y and
- not select.unselected):
+ if select.getCursorY() == y and not select.unselected:
format.flags.incl(ffReverse)
- inc k
else:
format.flags.excl(ffReverse)
while j < select.options[i].s.len:
@@ -377,13 +342,11 @@ proc windowChange*(select: Select; width, height: int) =
select.setCursorY(select.cursory)
select.queueDraw()
-proc newSelect*(multiple: bool; options: seq[SelectOption]; selected: seq[int];
+proc newSelect*(options: seq[SelectOption]; selected: int;
x, y, width, height: int; finishImpl: SelectFinish; opaque: RootRef):
Select =
let select = Select(
- multiple: multiple,
options: options,
- oselected: selected,
selected: selected,
x: x,
y: y,
@@ -395,8 +358,7 @@ proc newSelect*(multiple: bool; options: seq[SelectOption]; selected: seq[int];
opt.s = ' ' & opt.s & ' '
select.maxw = max(select.maxw, opt.s.width())
select.windowChange(width, height)
- if selected.len > 0:
- select.setCursorY(selected[0])
+ select.setCursorY(selected)
return select
proc addSelectModule*(ctx: JSContext) =
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index 8166978c..02a2b8ee 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -669,9 +669,8 @@ type
hide*: bool
SelectResult* = ref object
- multiple*: bool
options*: seq[SelectOption]
- selected*: seq[int]
+ selected*: int
ClickResult* = object
open*: Request
@@ -1349,20 +1348,12 @@ proc submitForm(buffer: Buffer; form: HTMLFormElement; submitter: Element): Requ
return buffer.makeFormRequest(parsedAction, httpMethod, entryList, enctype)
proc setFocus(buffer: Buffer; e: Element): bool =
- if buffer.document.focus != e:
- buffer.document.setFocus(e)
- e.setInvalid()
- buffer.maybeReshape()
- return true
- return false
+ buffer.document.setFocus(e)
+ return buffer.maybeReshape()
proc restoreFocus(buffer: Buffer): bool =
- if buffer.document.focus != nil:
- buffer.document.focus.setInvalid()
- buffer.document.setFocus(nil)
- buffer.maybeReshape()
- return true
- return false
+ buffer.document.setFocus(nil)
+ return buffer.maybeReshape()
type ReadSuccessResult* = object
open*: Request
@@ -1424,25 +1415,21 @@ proc click(buffer: Buffer; label: HTMLLabelElement): ClickResult =
return ClickResult()
proc click(buffer: Buffer; select: HTMLSelectElement): ClickResult =
+ if select.attrb(satMultiple):
+ return ClickResult()
let repaint = buffer.setFocus(select)
var options: seq[SelectOption] = @[]
- var selected: seq[int] = @[]
+ var selected = -1
var i = 0
for option in select.options:
#TODO: add nop options for each optgroup
- options.add(SelectOption(
- s: option.textContent.stripAndCollapse()
- ))
- if option.selected:
- selected.add(i)
+ options.add(SelectOption(s: option.textContent.stripAndCollapse()))
+ if selected == -1 and option.selected:
+ selected = i
inc i
return ClickResult(
repaint: repaint,
- select: some(SelectResult(
- multiple: select.attrb(satMultiple),
- options: options,
- selected: selected
- ))
+ select: some(SelectResult(options: move(options), selected: selected))
)
proc baseURL(buffer: Buffer): URL =
@@ -1487,6 +1474,9 @@ proc click(buffer: Buffer; anchor: HTMLAnchorElement): ClickResult =
proc click(buffer: Buffer; option: HTMLOptionElement): ClickResult =
let select = option.select
if select != nil:
+ if select.attrb(satMultiple):
+ option.setSelected(not option.selected)
+ return ClickResult(repaint: buffer.maybeReshape())
return buffer.click(select)
return ClickResult()
@@ -1662,24 +1652,13 @@ proc click*(buffer: Buffer; cursorx, cursory: int): ClickResult {.proxy.} =
return res
return ClickResult(repaint: repaint)
-proc select*(buffer: Buffer; selected: seq[int]): ClickResult {.proxy.} =
+proc select*(buffer: Buffer; selected: int): ClickResult {.proxy.} =
if buffer.document.focus != nil and
buffer.document.focus of HTMLSelectElement:
let select = HTMLSelectElement(buffer.document.focus)
- var i = 0
- var j = 0
- var repaint = false
- for option in select.options:
- var wasSelected = option.selected
- if i < selected.len and selected[i] == j:
- option.selected = true
- inc i
- else:
- option.selected = false
- if not repaint:
- repaint = wasSelected != option.selected
- inc j
+ select.setSelectedIndex(selected)
return ClickResult(repaint: buffer.restoreFocus())
+ return ClickResult()
proc readCanceled*(buffer: Buffer): bool {.proxy.} =
return buffer.restoreFocus()