Beispiel #1
0
    def _parse_fields(self):
        fields = OrderedDict()
        field_order = []
        tags = ('input', 'select', 'textarea', 'button')
        for pos, node in enumerate(self.html.findAll(tags)):
            attrs = dict(node.attrs)
            tag = node.name
            name = None
            if 'name' in attrs:
                name = attrs.pop('name')

            if tag == 'textarea':
                if node.text.startswith('\r\n'):
                    text = node.text[2:]
                elif node.text.startswith('\n'):
                    text = node.text[1:]
                else:
                    text = node.text
                attrs['value'] = text

            tag_type = attrs.get('type', 'text').lower()
            if tag == 'select':
                tag_type = 'select'
            if tag_type == "select" and "multiple" in attrs:
                tag_type = "multiple_select"
            if tag == 'button':
                tag_type = 'submit'

            FieldClass = self.FieldClass.classes.get(tag_type,
                                                     self.FieldClass)
            if tag == 'input':
                if tag_type == 'radio':
                    field = fields.get(name)
                    if not field:
                        field = FieldClass(self, tag, name, pos, **attrs)
                        fields.setdefault(name, []).append(field)
                        field_order.append((name, field))
                    else:
                        field = field[0]
                        assert isinstance(field,
                                          self.FieldClass.classes['radio'])
                    field.options.append((attrs.get('value'),
                                          'checked' in attrs))
                    continue
                elif tag_type == 'file':
                    if 'value' in attrs:
                        del attrs['value']

            field = FieldClass(self, tag, name, pos, **attrs)
            fields.setdefault(name, []).append(field)
            field_order.append((name, field))

            if tag == 'select':
                for option in node('option'):
                    field.options.append((option.attrs.get('value', option.text),
                                          'selected' in option.attrs))

        self.field_order = field_order
        self.fields = fields
Beispiel #2
0
    def _parse_fields(self):
        fields = OrderedDict()
        field_order = []
        tags = ("input", "select", "textarea", "button")
        for pos, node in enumerate(self.html.findAll(tags)):
            attrs = node.attrs
            tag = node.name
            name = None
            if "name" in attrs:
                name = attrs.pop("name")

            if tag == "textarea":
                attrs["value"] = node.text

            tag_type = attrs.get("type", "text").lower()
            if tag == "select":
                tag_type = "select"
            if tag_type == "select" and attrs.get("multiple"):
                tag_type = "multiple_select"
            if tag == "button":
                tag_type = "submit"

            FieldClass = self.FieldClass.classes.get(tag_type, self.FieldClass)
            if tag == "input":
                if tag_type == "radio":
                    field = fields.get(name)
                    if not field:
                        field = FieldClass(self, tag, name, pos, **attrs)
                        fields.setdefault(name, []).append(field)
                        field_order.append((name, field))
                    else:
                        field = field[0]
                        assert isinstance(field, self.FieldClass.classes["radio"])
                    field.options.append((attrs.get("value"), "checked" in attrs))
                    continue

            field = FieldClass(self, tag, name, pos, **attrs)
            fields.setdefault(name, []).append(field)
            field_order.append((name, field))

            if tag == "select":
                for option in node("option"):
                    field.options.append((option.attrs.get("value"), "selected" in option.attrs))

        self.field_order = field_order
        self.fields = fields
Beispiel #3
0
    def _parse_fields(self):
        fields = OrderedDict()
        field_order = []
        tags = ('input', 'select', 'textarea', 'button')
        for pos, node in enumerate(self.html.findAll(tags)):
            attrs = dict(node.attrs)
            tag = node.name
            name = None
            if 'name' in attrs:
                name = attrs.pop('name')

            if tag == 'textarea':
                if node.text.startswith('\r\n'):  # pragma: no cover
                    text = node.text[2:]
                elif node.text.startswith('\n'):
                    text = node.text[1:]
                else:
                    text = node.text
                attrs['value'] = text

            tag_type = attrs.get('type', 'text').lower()
            if tag == 'select':
                tag_type = 'select'
            if tag_type == "select" and "multiple" in attrs:
                tag_type = "multiple_select"
            if tag == 'button':
                tag_type = 'submit'

            FieldClass = self.FieldClass.classes.get(tag_type,
                                                     self.FieldClass)

            # https://github.com/Pylons/webtest/issues/73
            if sys.version_info[:2] <= (2, 6):
                attrs = dict((k.encode('utf-8') if isinstance(k, unicode)
                              else k, v) for k, v in attrs.items())

            if tag == 'input':
                if tag_type == 'radio':
                    field = fields.get(name)
                    if not field:
                        field = FieldClass(self, tag, name, pos, **attrs)
                        fields.setdefault(name, []).append(field)
                        field_order.append((name, field))
                    else:
                        field = field[0]
                        assert isinstance(field,
                                          self.FieldClass.classes['radio'])
                    field.options.append((attrs.get('value'),
                                          'checked' in attrs,
                                          None))
                    continue
                elif tag_type == 'file':
                    if 'value' in attrs:
                        del attrs['value']

            field = FieldClass(self, tag, name, pos, **attrs)
            fields.setdefault(name, []).append(field)
            field_order.append((name, field))

            if tag == 'select':
                for option in node('option'):
                    field.options.append(
                        (option.attrs.get('value', option.text),
                         'selected' in option.attrs,
                         option.text))

        self.field_order = field_order
        self.fields = fields
Beispiel #4
0
    def _parse_fields(self):
        fields = OrderedDict()
        field_order = []
        tags = ('input', 'select', 'textarea', 'button')
        for pos, node in enumerate(self.html.findAll(tags)):
            attrs = dict(node.attrs)
            tag = node.name
            name = None
            if 'name' in attrs:
                name = attrs.pop('name')

            if tag == 'textarea':
                if node.text.startswith('\r\n'):  # pragma: no cover
                    text = node.text[2:]
                elif node.text.startswith('\n'):
                    text = node.text[1:]
                else:
                    text = node.text
                attrs['value'] = text

            tag_type = attrs.get('type', 'text').lower()
            if tag == 'select':
                tag_type = 'select'
            if tag_type == "select" and "multiple" in attrs:
                tag_type = "multiple_select"
            if tag == 'button':
                tag_type = 'submit'

            FieldClass = self.FieldClass.classes.get(tag_type, self.FieldClass)

            # https://github.com/Pylons/webtest/issues/73
            if sys.version_info[:2] <= (2, 6):
                attrs = dict(
                    (k.encode('utf-8') if isinstance(k, unicode) else k, v)
                    for k, v in attrs.items())

            # https://github.com/Pylons/webtest/issues/131
            reserved_attributes = ('form', 'tag', 'pos')
            for attr in reserved_attributes:
                if attr in attrs:
                    del attrs[attr]

            if tag == 'input':
                if tag_type == 'radio':
                    field = fields.get(name)
                    if not field:
                        field = FieldClass(self, tag, name, pos, **attrs)
                        fields.setdefault(name, []).append(field)
                        field_order.append((name, field))
                    else:
                        field = field[0]
                        assert isinstance(field,
                                          self.FieldClass.classes['radio'])
                    field.options.append((attrs.get('value'), 'checked'
                                          in attrs, None))
                    continue
                elif tag_type == 'file':
                    if 'value' in attrs:
                        del attrs['value']

            field = FieldClass(self, tag, name, pos, **attrs)
            fields.setdefault(name, []).append(field)
            field_order.append((name, field))

            if tag == 'select':
                for option in node('option'):
                    field.options.append(
                        (option.attrs.get('value', option.text), 'selected'
                         in option.attrs, option.text))

        self.field_order = field_order
        self.fields = fields
Beispiel #5
0
 def _parse_fields(self):
     in_select = None
     in_textarea = None
     fields = OrderedDict()
     field_order = []
     for match in self._tag_re.finditer(self.text):
         end = match.group(1) == '/'
         tag = match.group(2).lower()
         if tag not in ('input', 'select', 'option', 'textarea',
                        'button'):
             continue
         if tag == 'select' and end:
             assert in_select, (
                 '%r without starting select' % match.group(0))
             in_select = None
             continue
         if tag == 'textarea' and end:
             assert in_textarea, (
                 "</textarea> with no <textarea> at %s" % match.start())
             in_textarea[0].value = utils.html_unquote(
                                 self.text[in_textarea[1]:match.start()])
             in_textarea = None
             continue
         if end:
             continue
         attrs = utils.parse_attrs(match.group(3))
         if 'name' in attrs:
             name = attrs.pop('name')
         else:
             name = None
         if tag == 'option':
             in_select.options.append((attrs.get('value'),
                                       'selected' in attrs))
             continue
         if tag == 'input' and attrs.get('type') == 'radio':
             field = fields.get(name)
             if not field:
                 field = self.FieldClass.classes['radio'](
                                    self, tag, name, match.start(), **attrs)
                 fields.setdefault(name, []).append(field)
                 field_order.append((name, field))
             else:
                 field = field[0]
                 assert isinstance(field, self.FieldClass.classes['radio'])
             field.options.append((attrs.get('value'),
                                   'checked' in attrs))
             continue
         tag_type = tag
         if tag == 'input':
             tag_type = attrs.get('type', 'text').lower()
         if tag_type == "select" and attrs.get("multiple"):
             FieldClass = self.FieldClass.classes.get("multiple_select",
                                                      self.FieldClass)
         else:
             FieldClass = self.FieldClass.classes.get(tag_type,
                                                      self.FieldClass)
         field = FieldClass(self, tag, name, match.start(), **attrs)
         if tag == 'textarea':
             assert not in_textarea, (
                 "Nested textareas: %r and %r"
                 % (in_textarea, match.group(0)))
             in_textarea = field, match.end()
         elif tag == 'select':
             assert not in_select, (
                 "Nested selects: %r and %r"
                 % (in_select, match.group(0)))
             in_select = field
         fields.setdefault(name, []).append(field)
         field_order.append((name, field))
     self.field_order = field_order
     self.fields = fields