Brython - Python in the browser

Chris Angelico rosuav at gmail.com
Fri Dec 21 16:48:09 CET 2012


On Sat, Dec 22, 2012 at 2:36 AM, Pierre Quentel
<pierre.quentel at gmail.com> wrote:
>> doc.add(Tag('DIV').add('hello ').add(Tag('B').add('world')))
>>
> No, with this syntax, the result of Tag('B').add('world') is below 'hello' in the tree structure, not at the same level (just below Tag('DIV')) as it should be

No; look at the expanded form for a more readable (but syntactically
identical) version:

doc.add(
  Tag('DIV')
    .add('hello ')
    .add(Tag('B').add('world'))
)

'world' is below Tag('B') - look at the parentheses - but
Tag('DIV').add('hello ') returns the DIV, not the hello, so B and
hello are peers.

> In this case it's not a real problem, but it's obvious if you want to produce <ul><li>one<li>two</ul> : you would need 2 different 'add'
> top = Tag('UL')
> top.add(Tag('LI').add('one'))
> top.add(Tag('LI').add('two'))
>
> With the syntax used in Brython : UL(LI('one')+LI('two'))

They can be directly combined, because Tag.add(self,other) returns
self, not other.

> Yes : a+b returns the string a+str(b)
>
>>>> 'hello '+B('world')
> 'hello <b>world</b>'

Hmm. So when that gets added into a DIV, it has to get parsed for
tags? How does this work? This seems very odd. I would have expected
it to remain as DOM objects.

What happens if I do, for instance:

'blah blah x<y: '+B('True!')

I would expect B('True!') to mean the word "True!" in bold; what
happens with the angle bracket in the text? Am I supposed to manually
escape that as < to protect it? If so, your library isn't doing
much - B might just as well be defined as

lambda s: '<b>'+s+'</b>'

rather than any sort of class.

ChrisA



More information about the Python-list mailing list