Package gluon :: Module html :: Class DIV
[hide private]
[frames] | no frames]

Class DIV

source code

  object --+    
           |    
XmlComponent --+
               |
              DIV
Known Subclasses:

HTML helper, for easy generating and manipulating a DOM structure. Little or no validation is done.

Behaves like a dictionary regarding updating of attributes. Behaves like a list regarding inserting/appending components.

Examples:

>>> DIV('hello', 'world', _style='color:red;').xml()
'<div style="color:red;">helloworld</div>'

All other HTML helpers are derived from `DIV`.

`_something="value"` attributes are transparently translated into `something="value"` HTML attributes

Instance Methods [hide private]
 
__delitem__(self, i)
Deletes attribute with name 'i' or component #i.
source code
 
__getitem__(self, i)
Gets attribute with name 'i' or component #i.
source code
 
__init__(self, *components, **attributes)
Args: components: any components that should be nested in this element attributes: any attributes you want to give to this element
source code
 
__len__(self)
Returns the number of included components
source code
 
__nonzero__(self)
Always returns True
source code
 
__setitem__(self, i, value)
Sets attribute with name 'i' or component #i.
source code
 
__str__(self)
str(COMPONENT) returns COMPONENT.xml()
source code
 
_fixup(self)
Handling of provided components.
source code
 
_postprocessing(self)
Handling of attributes (normally the ones not prefixed with '_').
source code
 
_setnode(self, value) source code
 
_traverse(self, status, hideerror=False) source code
 
_validate(self)
nothing to validate yet.
source code
 
_wrap_components(self, allowed_parents, wrap_parent=None, wrap_lambda=None)
helper for _fixup.
source code
 
_xml(self)
Helper for xml generation.
source code
 
append(self, value)
list style appending of components
source code
 
element(self, *args, **kargs)
Finds the first component that matches the supplied attribute dictionary, or None if nothing could be found
source code
 
elements(self, *args, **kargs)
Find all components that match the supplied attribute dictionary, or None if nothing could be found
source code
 
flatten(self, render=None)
Returns the text stored by the DIV object rendered by the render function the render function must take text, tagname, and attributes `render=None` is equivalent to `render=lambda text, tag, attr: text`
source code
 
insert(self, i, value)
List-style inserting of components
source code
 
sibling(self, *args, **kargs)
Finds the first sibling component that match the supplied argument list and attribute dictionary, or None if nothing could be found
source code
 
siblings(self, *args, **kargs)
Finds all sibling components that match the supplied argument list and attribute dictionary, or None if nothing could be found
source code
 
update(self, **kargs)
dictionary like updating of the tag attributes
source code
 
xml(self)
generates the xml for this component.
source code

Inherited from XmlComponent: __add__, __mul__, add_class, remove_class

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __subclasshook__

Class Variables [hide private]
  regex_attr = re.compile(r'\[([\w-:]+)=(.*?)\]')
  regex_class = re.compile(r'\.([\w-]+)')
  regex_id = re.compile(r'#([\w-]+)')
  regex_tag = re.compile(r'^[\w-:]+')
  tag = 'div'
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__delitem__(self, i)
(Index deletion operator)

source code 

Deletes attribute with name 'i' or component #i.

Args:
    i: index. If i is a string: the name of the attribute
        otherwise references to number of the component

__getitem__(self, i)
(Indexing operator)

source code 

Gets attribute with name 'i' or component #i.
If attribute 'i' is not found returns None

Args:
    i: index. If i is a string: the name of the attribute
        otherwise references to number of the component

__init__(self, *components, **attributes)
(Constructor)

source code 

Args:
    components: any components that should be nested in this element
    attributes: any attributes you want to give to this element

Raises:
    SyntaxError: when a stand alone tag receives components

Overrides: object.__init__

__setitem__(self, i, value)
(Index assignment operator)

source code 

Sets attribute with name 'i' or component #i.

Args:
    i: index. If i is a string: the name of the attribute
        otherwise references to number of the component
    value: the new value

__str__(self)
(Informal representation operator)

source code 

str(COMPONENT) returns COMPONENT.xml()

Overrides: object.__str__

_fixup(self)

source code 

Handling of provided components.

Nothing to fixup yet. May be overridden by subclasses, eg for wrapping some components in another component or blocking them.

_postprocessing(self)

source code 

Handling of attributes (normally the ones not prefixed with '_').

Nothing to postprocess yet. May be overridden by subclasses

_validate(self)

source code 

nothing to validate yet. May be overridden by subclasses

_wrap_components(self, allowed_parents, wrap_parent=None, wrap_lambda=None)

source code 

helper for _fixup. Checks if a component is in allowed_parents,
otherwise wraps it in wrap_parent

Args:
    allowed_parents: (tuple) classes that the component should be an
        instance of
    wrap_parent: the class to wrap the component in, if needed
    wrap_lambda: lambda to use for wrapping, if needed

_xml(self)

source code 

Helper for xml generation. Returns separately:
- the component attributes
- the generated xml of the inner components

Component attributes start with an underscore ('_') and
do not have a False or None value. The underscore is removed.
A value of True is replaced with the attribute name.

Returns:
    tuple: (attributes, components)

append(self, value)

source code 

list style appending of components

Examples:

>>> a=DIV()
>>> a.append(SPAN('x'))
>>> print a
<div><span>x</span></div>

element(self, *args, **kargs)

source code 

Finds the first component that matches the supplied attribute dictionary, or None if nothing could be found

Also the components of the components are searched.

elements(self, *args, **kargs)

source code 

Find all components that match the supplied attribute dictionary, or None if nothing could be found

All components of the components are searched.

Examples:

>>> a = DIV(DIV(SPAN('x'),3,DIV(SPAN('y'))))
>>> for c in a.elements('span',first_only=True): c[0]='z'
>>> print a
<div><div><span>z</span>3<div><span>y</span></div></div></div>
>>> for c in a.elements('span'): c[0]='z'
>>> print a
<div><div><span>z</span>3<div><span>z</span></div></div></div>

It also supports a syntax compatible with jQuery

Examples:

>>> a=TAG('<div><span><a id="1-1" u:v=$>hello</a></span><p class="this is a test">world</p></div>')
>>> for e in a.elements('div a#1-1, p.is'): print e.flatten()
hello
world
>>> for e in a.elements('#1-1'): print e.flatten()
hello
>>> a.elements('a[u:v=$]')[0].xml()
'<a id="1-1" u:v="$">hello</a>'
>>> a=FORM( INPUT(_type='text'), SELECT(range(1)), TEXTAREA() )
>>> for c in a.elements('input, select, textarea'): c['_disabled'] = 'disabled'
>>> a.xml()
'<form action="#" enctype="multipart/form-data" method="post"><input disabled="disabled" type="text" /><select disabled="disabled"><option value="0">0</option></select><textarea cols="40" disabled="disabled" rows="10"></textarea></form>'

Elements that are matched can also be replaced or removed by specifying a "replace" argument (note, a list of the original matching elements is still returned as usual).

Examples:

>>> a = DIV(DIV(SPAN('x', _class='abc'), DIV(SPAN('y', _class='abc'), SPAN('z', _class='abc'))))
>>> b = a.elements('span.abc', replace=P('x', _class='xyz'))
>>> print a
<div><div><p class="xyz">x</p><div><p class="xyz">x</p><p class="xyz">x</p></div></div></div>

"replace" can be a callable, which will be passed the original element and should return a new element to replace it.

Examples:

>>> a = DIV(DIV(SPAN('x', _class='abc'), DIV(SPAN('y', _class='abc'), SPAN('z', _class='abc'))))
>>> b = a.elements('span.abc', replace=lambda el: P(el[0], _class='xyz'))
>>> print a
<div><div><p class="xyz">x</p><div><p class="xyz">y</p><p class="xyz">z</p></div></div></div>

If replace=None, matching elements will be removed completely.

Examples:

>>> a = DIV(DIV(SPAN('x', _class='abc'), DIV(SPAN('y', _class='abc'), SPAN('z', _class='abc'))))
>>> b = a.elements('span', find='y', replace=None)
>>> print a
<div><div><span class="abc">x</span><div><span class="abc">z</span></div></div></div>

If a "find_text" argument is specified, elements will be searched for text components that match find_text, and any matching text components will be replaced (find_text is ignored if "replace" is not also specified). Like the "find" argument, "find_text" can be a string or a compiled regex.

Examples:

>>> a = DIV(DIV(SPAN('x', _class='abc'), DIV(SPAN('y', _class='abc'), SPAN('z', _class='abc'))))
>>> b = a.elements(find_text=re.compile('x|y|z'), replace='hello')
>>> print a
<div><div><span class="abc">hello</span><div><span class="abc">hello</span><span class="abc">hello</span></div></div></div>

If other attributes are specified along with find_text, then only components that match the specified attributes will be searched for find_text.

Examples:

>>> a = DIV(DIV(SPAN('x', _class='abc'), DIV(SPAN('y', _class='efg'), SPAN('z', _class='abc'))))
>>> b = a.elements('span.efg', find_text=re.compile('x|y|z'), replace='hello')
>>> print a
<div><div><span class="abc">x</span><div><span class="efg">hello</span><span class="abc">z</span></div></div></div>

flatten(self, render=None)

source code 

Returns the text stored by the DIV object rendered by the render function the render function must take text, tagname, and attributes `render=None` is equivalent to `render=lambda text, tag, attr: text`

Examples:

>>> markdown = lambda text,tag=None,attributes={}:                         {None: re.sub('\s+',' ',text),                          'h1':'#'+text+'\n\n',                          'p':text+'\n'}.get(tag,text)
>>> a=TAG('<h1>Header</h1><p>this is a     test</p>')
>>> a.flatten(markdown)
'#Header\n\nthis is a test\n'

insert(self, i, value)

source code 

List-style inserting of components

Examples:

>>> a=DIV()
>>> a.insert(0,SPAN('x'))
>>> print a
<div><span>x</span></div>

xml(self)

source code 

generates the xml for this component.

Overrides: XmlComponent.xml