"""Browser tasks with explicit semantic DOM and computed-style checks.""" from html import escape HTML_BASES=[ ('heading','h1','

Welcome

','an h1 heading reading Welcome',{}), ('paragraph','p','

Hello reader

','a paragraph reading Hello reader',{}), ('link','a','Help','a Help link to /help',{'href':'/help'}), ('button','button','','a Save button with type button',{'type':'button'}), ('image','img','Star','an image sourced from icon.png with alt text Star',{'src':'icon.png','alt':'Star'}), ('email','input','','an email input named email',{'type':'email','name':'email'}), ('checkbox','input','','a checkbox named agree',{'type':'checkbox','name':'agree'}), ('form','form','
','a POST form targeting /send containing a Send button',{'action':'/send','method':'post'}), ('list','ul','','an unordered list containing Tea then Water',{}), ('ordered_list','ol','
  1. Open
  2. Close
','an ordered list containing Open then Close',{}), ('navigation','nav','','a navigation element with a Home link to /',{}), ('article','article','

News

Today

','an article with an h2 News and a paragraph Today',{}), ('footer','footer','','a footer reading Copyright',{}), ('quote','blockquote','
Be kind
','a blockquote reading Be kind citing /source',{'cite':'/source'}), ('code','pre','
x = 1
','a preformatted code block reading x = 1',{}), ('details','details','
More

Details

','a details element with summary More and paragraph Details',{}), ('select','select','','a select named color with Red/red and Blue/blue options',{'name':'color'}), ('textarea','textarea','','a textarea named note with three rows initially reading Note',{'name':'note','rows':'3'}), ('label','label','','a Name label associated with input ID username',{'for':'username'}), ('progress','progress','4/10','a progress element with maximum 10 and value 4',{'max':'10','value':'4'}), ] GLOBAL=[('title','Information','a title attribute of Information'),('lang','en','English language via lang'), ('dir','rtl','right-to-left direction via dir'),('hidden','','the hidden attribute'), ('tabindex','0','keyboard focus via tabindex 0'),('contenteditable','true','contenteditable set to true')] def html_assertions(markup,prefix=''): # Derive structural checks from the authored spec tree, not candidate text. from html.parser import HTMLParser class Parse(HTMLParser): def __init__(self):super().__init__();self.stack=[];self.counts={};self.out=[] def handle_starttag(self,tag,attrs): parent=self.stack[-1][0] if self.stack else prefix key=(parent,tag);self.counts[key]=self.counts.get(key,0)+1 selector=(parent+' > ' if parent else '')+tag+f':nth-of-type({self.counts[key]})' self.out.append({'kind':'exists','selector':selector}) for k,v in attrs:self.out.append({'kind':'attribute','selector':selector,'name':k,'value':v or ''}) if tag not in ('img','input','br','hr','meta','link'):self.stack.append((selector,tag)) def handle_endtag(self,tag): if self.stack and self.stack[-1][1]==tag:self.stack.pop() def handle_data(self,text): if text.strip() and self.stack:self.out.append({'kind':'text','selector':self.stack[-1][0],'value':text}) p=Parse();p.feed(markup);return p.out def html_tasks(): tasks=[] for name,tag,code,desc,attrs in HTML_BASES: for attr,value,detail in GLOBAL: markup=code.replace('<'+tag,'<'+tag+' '+attr+'="'+value+'"',1) tasks.append(('easy',{'kind':'html','base':name,'feature':attr},'Create '+desc+'. On its outer element, set '+detail+'. Return an HTML fragment.',markup)) for i,(name,tag,code,desc,attrs) in enumerate(HTML_BASES): for offset,wrapper in ((1,'section'),(4,'aside'),(9,'div')): _,_,code2,desc2,_=HTML_BASES[(i+offset)%len(HTML_BASES)] markup=f'<{wrapper}>{code}{code2}' tasks.append(('medium',{'kind':'html','pair':[i,(i+offset)%20],'wrapper':wrapper},f'Create a {wrapper} containing {desc}, followed by {desc2}. Return an HTML fragment.',markup)) for i,(_,_,code,desc,_) in enumerate(HTML_BASES): _,_,code2,desc2,_=HTML_BASES[(i+7)%20] markup=f'
{code}
' tasks.append(('moderate',{'kind':'html','layout':[i,(i+7)%20]},f'Create a main element. Put {desc} inside its section, and {desc2} inside its following aside. Return an HTML fragment.',markup)) return [dict(difficulty=d,spec=s,prompt=p,reference=c,fixture='',checks=html_assertions(c)) for d,s,p,c in tasks] # Fixed values are chosen to have exact, stable computed representations. CSS_PROPERTIES=[ ('color','rgb(10, 20, 30)'),('background-color','rgb(240, 220, 200)'),('font-size','20px'),('font-weight','700'), ('font-style','italic'),('text-align','center'),('text-transform','uppercase'),('letter-spacing','2px'), ('word-spacing','4px'),('line-height','28px'),('text-indent','12px'),('white-space','pre'), ('padding-top','8px'),('padding-right','10px'),('padding-bottom','12px'),('padding-left','14px'), ('margin-top','6px'),('margin-right','8px'),('margin-bottom','10px'),('margin-left','12px'), ('border-top-width','3px'),('border-bottom-color','rgb(0, 80, 120)'),('border-radius','6px'),('opacity','0.5'), ('width','120px'),('height','60px'),('min-width','90px'),('max-width','180px'),('box-sizing','border-box'), ('display','flex'),('flex-direction','column'),('justify-content','center'),('align-items','center'),('gap','8px'), ('position','relative'),('top','5px'),('overflow-x','hidden'),('visibility','hidden'),('cursor','pointer'),('list-style-type','square')] BASE_CSS='* { box-sizing: content-box; } .target,.other { border: 1px solid black; width:200px; height:80px; min-width:0; max-width:none; margin:0; padding:0; display:block; position:static; }' FIXTURE='
AlphaOneTwo
Beta
Outside
' def css_tasks(): tasks=[] for prop,val in CSS_PROPERTIES: for sel,description,affected in [('.target','every element with class target',['#target','#outside']),('#target','only the element with ID target',['#target']),('#scope > .target','only direct children of #scope having class target',['#target'])]: spec={'kind':'css','selector':sel,'declarations':{prop:val}} checks=[{'kind':'style','selector':x,'property':prop,'value':val} for x in affected] extra = 'display: flex;' if prop in ('flex-direction','justify-content','align-items','gap') else ('position: relative;' if prop=='top' else '') if extra: extra_prop='display' if prop!='top' else 'position' extra_val='flex' if prop!='top' else 'relative' checks += [{'kind':'style','selector':x,'property':extra_prop,'value':extra_val} for x in affected] spec['declarations'][extra_prop]=extra_val if prop=='flex-direction':checks += [{'kind':'layout_column','selector':x} for x in affected] if prop=='display':checks += [{'kind':'layout_row','selector':x} for x in affected] checks +=[{'kind':'unchanged','selector':'.other','property':prop}] if len(affected)==1:checks +=[{'kind':'unchanged','selector':'#outside','property':prop}] extra_instruction = ' Use flex display.' if extra.startswith('display') else (' Use relative positioning.' if extra else '') tasks.append(dict(difficulty='easy',spec=spec,prompt=f'Style {description}: set {prop} to {val}.{extra_instruction} Leave other elements unchanged. Return CSS only.',reference=f'{sel} {{ {prop}: {val}; {extra} }}',fixture=FIXTURE,checks=checks)) pairs=[] # Avoid interactions that would invalidate literal computed values. for i in range(20): for offset in (4,7,11):pairs.append((i,(i+offset)%20)) for i,j in pairs: p,v=CSS_PROPERTIES[i];q,w=CSS_PROPERTIES[j] spec={'kind':'css','selector':'#scope > .target','declarations':{p:v,q:w}} checks=[{'kind':'style','selector':'#target','property':k,'value':x} for k,x in ((p,v),(q,w))] checks +=[{'kind':'unchanged','selector':'.other','property':k} for k in (p,q)] tasks.append(dict(difficulty='medium',spec=spec,prompt=f'For direct children of #scope with class target, set {p} to {v} and {q} to {w}. Leave .other unchanged. Return CSS only.',reference=f'#scope > .target {{ {p}: {v}; {q}: {w}; }}',fixture=FIXTURE,checks=checks)) for i,(prop,val) in enumerate(CSS_PROPERTIES[:20]): # Baseline captured before candidate CSS; test both sides of the breakpoint. spec={'kind':'css','selector':'#target','declarations':{prop:val},'max_width':600} checks=[{'kind':'style','selector':'#target','property':prop,'value':val,'viewport':480}, {'kind':'unchanged','selector':'#target','property':prop,'viewport':900}, {'kind':'unchanged','selector':'.other','property':prop,'viewport':480}] tasks.append(dict(difficulty='moderate',spec=spec,prompt=f'At viewport widths at most 600px, set {prop} to {val} on #target. At wider widths, preserve its original style. Never change .other. Return CSS only.',reference=f'@media (max-width: 600px) {{ #target {{ {prop}: {val}; }} }}',fixture=FIXTURE,checks=checks)) return tasks DOM_FIXTURE='

0

' DOM_ACTIONS=[ ('greeting','make #output read Hello',"o.textContent='Hello'",[{'kind':'text','selector':'#output','value':'Hello'}]), ('increment','increase the integer in #output by one',"o.textContent=String(Number(o.textContent)+1)",[{'kind':'text','selector':'#output','value':'2'}]), ('decrement','decrease the integer in #output by one',"o.textContent=String(Number(o.textContent)-1)",[{'kind':'text','selector':'#output','value':'-2'}]), ('mirror','copy the input value into #output',"o.textContent=i.value",[{'kind':'text','selector':'#output','value':'Ab 9'}]), ('uppercase','copy the uppercased input into #output',"o.textContent=i.value.toUpperCase()",[{'kind':'text','selector':'#output','value':'AB 9'}]), ('length','put the input character count in #output',"o.textContent=String(i.value.length)",[{'kind':'text','selector':'#output','value':'4'}]), ('clear_input','clear the input value',"i.value=''",[{'kind':'value','selector':'#input','value':''}]), ('disable_input','disable the input',"i.disabled=true",[{'kind':'property','selector':'#input','name':'disabled','value':True}]), ('hide_output','hide #output using its hidden property',"o.hidden=true",[{'kind':'property','selector':'#output','name':'hidden','value':True}]), ('toggle_class','toggle class active on #output',"o.classList.toggle('active')",[{'kind':'attribute','selector':'#output','name':'class','value':''}]), ('append_item','append a list item containing the input to #items',"const li=document.createElement('li'); li.textContent=i.value; document.querySelector('#items').appendChild(li)",[{'kind':'count','selector':'#items > li','value':2},{'kind':'text','selector':'#items > li:first-child','value':'Ab 9'}]), ('check','check the checkbox',"document.querySelector('#check').checked=true",[{'kind':'property','selector':'#check','name':'checked','value':True}]), ('title','set #output title to the input value',"o.title=i.value",[{'kind':'attribute','selector':'#output','name':'title','value':'Ab 9'}]), ('reverse','copy the reversed input into #output',"o.textContent=i.value.split('').reverse().join('')",[{'kind':'text','selector':'#output','value':'9 bA'}]), ('mark_empty','set #output to Empty if input is empty, otherwise Filled',"o.textContent=i.value.length ? 'Filled':'Empty'",[{'kind':'text','selector':'#output','value':'Filled'}]), ] def dom_tasks(): tasks=[] for name,desc,body,checks in DOM_ACTIONS: spec={'kind':'dom','event':'click','action':name} code="function setup() {\n const i=document.querySelector('#input'), o=document.querySelector('#output');\n document.querySelector('#action').addEventListener('click', () => { "+body+"; });\n}\n" tasks.append(dict(difficulty='medium',spec=spec,prompt=f'Implement setup(). When #action is clicked, {desc}. Attach an event listener; do not perform the action before a click.',reference=code,fixture=DOM_FIXTURE,checks=checks,steps=[{'action':'fill','selector':'#input','value':'Ab 9'},{'action':'click','selector':'#action'},{'action':'click','selector':'#action'}])) for name,desc,body,checks in DOM_ACTIONS[3:8]: code="function setup() {\n const i=document.querySelector('#input'), o=document.querySelector('#output');\n i.addEventListener('input', () => { "+body+"; });\n}\n" tasks.append(dict(difficulty='moderate',spec={'kind':'dom','event':'input','action':name},prompt=f'Implement setup(). On an input event from #input, {desc}. Use a listener, leave the initial page unchanged, and handle repeated events.',reference=code,fixture=DOM_FIXTURE,checks=checks,steps=[{'action':'set_value','selector':'#input','value':'Ab 9'},{'action':'event','selector':'#input','value':'input'}])) return tasks