[pypy-svn] r34586 - in pypy/dist/pypy/translator/js: . modules modules/test
guido at codespeak.net
guido at codespeak.net
Mon Nov 13 22:04:27 CET 2006
Author: guido
Date: Mon Nov 13 22:04:26 2006
New Revision: 34586
Modified:
pypy/dist/pypy/translator/js/modules/dom.py
pypy/dist/pypy/translator/js/modules/test/test_dom.py
pypy/dist/pypy/translator/js/support.py
Log:
Improved support for inline styles, they're now read from the 'style' attribute
and saved on serialization (.innerHTML).
Modified: pypy/dist/pypy/translator/js/modules/dom.py
==============================================================================
--- pypy/dist/pypy/translator/js/modules/dom.py (original)
+++ pypy/dist/pypy/translator/js/modules/dom.py Mon Nov 13 22:04:26 2006
@@ -13,6 +13,7 @@
"""
import time
+import re
from pypy.rpython.ootypesystem.bltregistry import BasicExternal, MethodDesc
from pypy.rlib.nonconst import NonConstant
@@ -115,7 +116,23 @@
def __init__(self, node=None):
super(Element, self).__init__(node)
- self.style = Style()
+
+ def _style(self):
+ style = getattr(self._original, '_style', None)
+ if style is not None:
+ return style
+ styles = {}
+ if self._original.hasAttribute('style'):
+ for t in self._original.getAttribute('style').split(';'):
+ name, value = t.split(':')
+ dashcharpairs = re.findall('-\w', name)
+ for p in dashcharpairs:
+ name = name.replace(p, p[1].upper())
+ styles[name.strip()] = value.strip()
+ style = Style(styles)
+ self._original._style = style
+ return style
+ style = property(_style)
def _nodeName(self):
return self._original.nodeName.upper()
@@ -199,11 +216,28 @@
pass
class Style(BasicExternal):
+ def __init__(self, styles={}):
+ for name, value in styles.iteritems():
+ setattr(self, name, value)
+
def __getattr__(self, name):
if name not in self._fields:
raise AttributeError, name
return None
+ def _tostring(self):
+ ret = []
+ for name in sorted(self._fields):
+ value = getattr(self, name, None)
+ if value is not None:
+ ret.append(' ')
+ for char in name:
+ if char.upper() == char:
+ char = '-%s' % (char.lower(),)
+ ret.append(char)
+ ret.append(': %s;' % (value,))
+ return ''.join(ret[1:])
+
# non-DOM ('DOM level 0') stuff
# Window is the main environment, the root node of the JS object tree
@@ -696,13 +730,19 @@
def _serialize_html(node):
ret = []
if node.nodeType == 1:
- nodeName = getattr(node, '_original', node).nodeName
+ original = getattr(node, '_original', node)
+ nodeName = original.nodeName
ret += ['<', nodeName]
if len(node.attributes):
for aname in node.attributes.keys():
+ if aname == 'style':
+ continue
attr = node.attributes[aname]
ret.append(' %s="%s"' % (attr.nodeName,
_quote_html(attr.nodeValue)))
+ styles = getattr(original, '_style', None)
+ if styles:
+ ret.append(' style="%s"' % (_quote_html(styles._tostring()),))
if len(node.childNodes) or nodeName not in _singletons:
ret.append('>')
for child in node.childNodes:
Modified: pypy/dist/pypy/translator/js/modules/test/test_dom.py
==============================================================================
--- pypy/dist/pypy/translator/js/modules/test/test_dom.py (original)
+++ pypy/dist/pypy/translator/js/modules/test/test_dom.py Mon Nov 13 22:04:26 2006
@@ -311,6 +311,31 @@
assert div.className == 'bar'
assert body.innerHTML == '<div class="bar">foo</div>'
+def code_read_styles():
+ window = get_window()
+ body = window.document.getElementsByTagName('body')[0]
+ body.innerHTML = ('<div style="color: red; background-color: green">foo'
+ '</div>')
+ return body.childNodes[0].style
+
+def test_read_styles():
+ style = code_read_styles()
+ assert style.color == 'red'
+ bgcolor = style.backgroundColor
+ assert bgcolor == 'green'
+
+def code_write_styles():
+ window = get_window()
+ body = window.document.getElementsByTagName('body')[0]
+ body.style.color = 'green'
+ body.style.backgroundColor = 'red'
+ return body
+
+def test_write_styles():
+ body = code_write_styles()
+ assert dom._serialize_html(body) == ('<body style="background-color: red; '
+ 'color: green;"></body>')
+
def test_build():
#py.test.skip('Borken')
global TRANSLATING
Modified: pypy/dist/pypy/translator/js/support.py
==============================================================================
--- pypy/dist/pypy/translator/js/support.py (original)
+++ pypy/dist/pypy/translator/js/support.py Mon Nov 13 22:04:26 2006
@@ -23,6 +23,9 @@
self.reserved[name] = True
#http://javascript.about.com/library/blclassobj.htm
+ # XXX WAAAHHH!!! IE alert :( there are a lot of objects here that are
+ # _not_ in standard JS, see
+ # http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.5/reference/
predefined_classes_and_objects = '''
Anchor anchors Applet applets Area Array Body
Button Checkbox Date document Error EvalError FileUpload
More information about the Pypy-commit
mailing list