escape XML string to tag element

32 views
Skip to first unread message

stephen lukacs

unread,
May 24, 2021, 2:58:21 PM5/24/21
to brython
hello one and all,

in web2py, for instance, they have an XML function to escape string so things like &check; will actually show as a check mark and not as "&check;".  or if there is like f"{x} {y} <span>{z}</span>" which will make sure the browser will show the span as those elements and not as text "<span>" or "</span>".

how do you escape a string sequence to do the above?  thank you in advance, Lucas

Ray Luo

unread,
May 24, 2021, 3:23:27 PM5/24/21
to brython
Hi Sjlu...,

Did you mean to say "do NOT escape xml string when rendering them"? That is how the XML(...) in web2py do.

By the way, I'm also a web2py user, too. :-)  #HandShake    I still find myself like the HTML tag api in web2py better.

Edward Elliott

unread,
May 24, 2021, 3:24:28 PM5/24/21
to bry...@googlegroups.com
Hi Lukas, don't worry about the range thing.  Happens to all of us. 🙂

On this question, it depends on the context.  I assume you mean having the string in your brython script, and wanting to add it to the page in rendered form (e.g. ☑️ not &check;).

In which case two ways come to mind.  Neither are particularly pythonic.

The simplest is to replace the .innerHTML property of an existing element.  Whatever string you set as innerHTML, the browser will parse as html before inserting in the document:

node = document ['foo']
node.innerHTML = '&check;'

This approach is crude but effective.  It replaces all the html children of node with your new string.  

Sometimes you don't want to replace the existing children.  Sometimes you want to swap out one node.  In this case, you first need to turn your string into an html tag, then insert it in the DOM.

JS provides the DOMParser object to turn strings into DOM nodes.  Here's a wrapper function I wrote for brython:

def make_html (text) :

    'Turn a string into DOM object'

    # see https://stackoverflow.com/questions/10585029/parse-an-html-string-with-js

    parser = window.DOMParser.new ()

    html = parser.parseFromString (text, 'text/html')

    return html.body.firstElementChild


The last line is needed because DOMParser returns a complete html document - which means it wraps your html elements in <html> <body> tags.  My function assumes a string only has a single top-level html element (a string with an html escape sequence like &check; inside is a single element).  Nested tags are ok (<table><tr><td></td></tr></table>).  If you have multiple top-level elements then you need to alter the last line.

Once you have a DOM element, just insert it in the page:
newnode = make_html (mytext)
document ['foo'].appendChild (newnode)

Or you can add it the brythonic way: document ['foo'] <= newnode

Personally I try to avoid brython-specific DOM manipulation like <= so my code has some degree of portability.  But either works perfectly fine.

HTH,
Ed

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/f3286a44-b86b-4a12-b42a-c9b1c36e073cn%40googlegroups.com.

stephen lukacs

unread,
Nov 12, 2021, 9:05:58 PM11/12/21
to brython
most of the time, i do have multiple top-level elements which also contain nested.  adding the XML function like web2py would make things a lot more smooth and readable.  or like the CAT function also.  can't that be added to browser.html?  i've been using SPAN to do the work of XML but i really don't need to add the extra html elements to my doc.  thank you in advance, lucas
Reply all
Reply to author
Forward
0 new messages