예제 #1
0
 def test_attrRendering(self):
     """
     Test that a Element with an attr tag renders the vaule of its attr tag
     as an attribute of its containing tag.
     """
     element = Element(loader=XMLString(
         '<a xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">'
         '<t:attr name="href">http://example.com</t:attr>'
         'Hello, world.'
         '</a>'))
     return self.assertFlattensTo(
         element, '<a href="http://example.com">Hello, world.</a>')
예제 #2
0
 def test_lenientPrefixBehavior(self):
     """
     If the parser sees a prefix it doesn't recognize on an attribute, it
     will pass it on through to serialization.
     """
     theInput = (
         '<hello:world hello:sample="testing" '
         'xmlns:hello="http://made-up.example.com/ns/not-real">'
         "This is a made-up tag.</hello:world>"
     )
     element = Element(loader=XMLString(theInput))
     self.assertFlattensTo(element, theInput.encode("utf8"))
예제 #3
0
 def test_transparentRendering(self) -> None:
     """
     A C{transparent} element should be eliminated from the DOM and rendered as
     only its children.
     """
     element = Element(
         loader=XMLString(
             "<t:transparent "
             'xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">'
             "Hello, world."
             "</t:transparent>"
         )
     )
     self.assertFlattensImmediately(element, b"Hello, world.")
예제 #4
0
def defaultValidationFailureHandler(
    instance,  # type: Optional[object]
    request,  # type: IRequest
    fieldValues,  # type: FieldValues
):
    # type: (...) -> Element
    """
    This is the default validation failure handler, which will be used by form
    handlers (i.e. any routes which use L{klein.Requirer} to require a field)
    in the case of any input validation failure when no other validation
    failure handler is registered via L{Form.onValidationFailureFor}.

    Its behavior is to simply return an HTML rendering of the form object,
    which includes inline information about fields which failed to validate.

    @param instance: The instance associated with the router that the form
        handler was handled on.
    @type instance: L{object}

    @param request: The request including the form submission.
    @type request: L{twisted.web.iweb.IRequest}

    @return: Any object acceptable from a Klein route.
    """
    session = request.getComponent(ISession)  # type: ignore[misc]
    request.setResponseCode(400)
    enctype = (
        (
            request.getHeader(b"content-type")
            or RenderableForm.ENCTYPE_URL_ENCODED.encode("ascii")
        )
        .split(b";")[0]
        .decode("charmap")
    )
    renderable = RenderableForm(
        fieldValues.form,
        session,
        "/".join(
            segment.decode("utf-8", errors="replace")
            for segment in request.prepath
        ),
        request.method,
        enctype,
        "utf-8",
        fieldValues.prevalidationValues,
        fieldValues.validationErrors,
    )

    return Element(TagLoader(renderable))
예제 #5
0
 def test_missingRenderMethod(self) -> None:
     """
     Flattening an L{Element} with a C{loader} which has a tag with a render
     directive fails with L{FlattenerError} if there is no available render
     method to satisfy that directive.
     """
     element = Element(
         loader=XMLString(
             """
     <p xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"
       t:render="unknownMethod" />
     """
         )
     )
     self.assertFlatteningRaises(element, MissingRenderMethod)
예제 #6
0
 def test_attrRendering(self) -> None:
     """
     An Element with an attr tag renders the vaule of its attr tag as an
     attribute of its containing tag.
     """
     element = Element(
         loader=XMLString(
             '<a xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">'
             '<t:attr name="href">http://example.com</t:attr>'
             "Hello, world."
             "</a>"
         )
     )
     self.assertFlattensImmediately(
         element, b'<a href="http://example.com">Hello, world.</a>'
     )
예제 #7
0
 def test_roundTrip(self) -> None:
     """
     Given a series of parsable XML strings, verify that
     L{twisted.web._flatten.flatten} will flatten the L{Element} back to the
     input when sent on a round trip.
     """
     fragments = [
         b"<p>Hello, world.</p>",
         b"<p><!-- hello, world --></p>",
         b"<p><![CDATA[Hello, world.]]></p>",
         b'<test1 xmlns:test2="urn:test2">' b"<test2:test3></test2:test3></test1>",
         b'<test1 xmlns="urn:test2"><test3></test3></test1>',
         b"<p>\xe2\x98\x83</p>",
     ]
     for xml in fragments:
         self.assertFlattensImmediately(Element(loader=XMLString(xml)), xml)
예제 #8
0
 def renderMethod(
     self, request: Optional[IRequest], tag: Tag
 ) -> Flattenable:
     return tag(Element(loader=XMLString("<em>Hello, world.</em>")))
예제 #9
0
 def customFormRender(self, form: RenderableForm) -> Any:
     """
     Include just the glue necessary for CSRF protection and let the
     application render the rest of the form.
     """
     return Element(loader=TagLoader(tags.html(tags.body(form.glue()))))
예제 #10
0
 def Verbatim(data):
     return Element(
         XMLString((
             '<t:transparent xmlns:t="'
             'http://twistedmatrix.com/ns/twisted.web.template/0.1">'
             '{0}</t:transparent>').format(data)))