Edgewall Software

Changeset 698

Show
Ignore:
Timestamp:
08/05/07 19:33:20 (2 years ago)
Author:
jonas
Message:

Fixed a few cases where HTMLFormFiller didn't work well with option elements:

  • Make sure all TEXT events are retained inside option elements.
  • Handle value attributes with non-ascii values.
Location:
trunk/genshi/filters
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/genshi/filters/html.py

    r690 r698  
    7070        in_form = in_select = in_option = in_textarea = False 
    7171        select_value = option_value = textarea_value = None 
    72         option_start = option_text = None 
     72        option_start = None 
     73        option_text = [] 
     74        no_option_value = False 
    7375 
    7476        for kind, data, pos in stream: 
     
    9597                                if isinstance(value, (list, tuple)): 
    9698                                    if declval: 
    97                                         checked = declval in [str(v) for v 
     99                                        checked = declval in [unicode(v) for v 
    98100                                                              in value] 
    99101                                    else: 
     
    101103                                else: 
    102104                                    if declval: 
    103                                         checked = declval == str(value) 
     105                                        checked = declval == unicode(value) 
    104106                                    elif type == 'checkbox': 
    105107                                        checked = bool(value) 
     
    131133                        option_start = kind, data, pos 
    132134                        option_value = attrs.get('value') 
     135                        if option_value is None: 
     136                            no_option_value = True 
     137                            option_value = '' 
    133138                        in_option = True 
    134139                        continue 
     
    137142            elif in_form and kind is TEXT: 
    138143                if in_select and in_option: 
    139                     if option_value is None: 
    140                         option_value = data 
    141                     option_text = kind, data, pos 
     144                    if no_option_value: 
     145                        option_value += data 
     146                    option_text.append((kind, data, pos)) 
    142147                    continue 
    143148                elif in_textarea: 
     
    154159                elif in_select and tagname == 'option': 
    155160                    if isinstance(select_value, (tuple, list)): 
    156                         selected = option_value in [str(v) for v 
     161                        selected = option_value in [unicode(v) for v 
    157162                                                    in select_value] 
    158163                    else: 
    159                         selected = option_value == str(select_value) 
     164                        selected = option_value == unicode(select_value) 
    160165                    okind, (tag, attrs), opos = option_start 
    161166                    if selected: 
     
    165170                    yield okind, (tag, attrs), opos 
    166171                    if option_text: 
    167                         yield option_text 
     172                        for event in option_text: 
     173                            yield event 
    168174                    in_option = False 
    169                     option_start = option_text = option_value = None 
     175                    no_option_value = False 
     176                    option_start = option_value = None 
     177                    option_text = [] 
    170178                elif tagname == 'textarea': 
    171179                    if textarea_value: 
  • trunk/genshi/filters/tests/html.py

    r682 r698  
    1717from genshi.input import HTML, ParseError 
    1818from genshi.filters.html import HTMLFormFiller, HTMLSanitizer 
    19  
     19from genshi.template import MarkupTemplate 
    2020 
    2121class HTMLFormFillerTestCase(unittest.TestCase): 
     
    270270          </select> 
    271271        </p></form>""", unicode(html)) 
     272 
     273    def test_fill_option_segmented_text(self): 
     274        html = MarkupTemplate("""<form> 
     275          <select name="foo"> 
     276            <option value="1">foo $x</option> 
     277          </select> 
     278        </form>""").generate(x=1) | HTMLFormFiller(data={'foo': '1'}) 
     279        self.assertEquals("""<form> 
     280          <select name="foo"> 
     281            <option value="1" selected="selected">foo 1</option> 
     282          </select> 
     283        </form>""", unicode(html)) 
     284 
     285    def test_fill_option_segmented_text_no_value(self): 
     286        html = MarkupTemplate("""<form> 
     287          <select name="foo"> 
     288            <option>foo $x bar</option> 
     289          </select> 
     290        </form>""").generate(x=1) | HTMLFormFiller(data={'foo': 'foo 1 bar'}) 
     291        self.assertEquals("""<form> 
     292          <select name="foo"> 
     293            <option selected="selected">foo 1 bar</option> 
     294          </select> 
     295        </form>""", unicode(html)) 
     296 
     297    def test_fill_option_unicode_value(self): 
     298        html = HTML(u"""<form> 
     299          <select name="foo"> 
     300            <option value="&ouml;">foo</option> 
     301          </select> 
     302        </form>""") | HTMLFormFiller(data={'foo': u'ö'}) 
     303        self.assertEquals(u"""<form> 
     304          <select name="foo"> 
     305            <option value="ö" selected="selected">foo</option> 
     306          </select> 
     307        </form>""", unicode(html)) 
    272308 
    273309