28 import re |
28 import re |
29 from StringIO import StringIO |
29 from StringIO import StringIO |
30 |
30 |
31 from logilab.mtconverter import xml_escape, html_unescape |
31 from logilab.mtconverter import xml_escape, html_unescape |
32 from logilab.common.date import ustrftime |
32 from logilab.common.date import ustrftime |
|
33 |
|
34 from cubicweb.utils import json_dumps |
33 |
35 |
34 |
36 |
35 def rql_for_eid(eid): |
37 def rql_for_eid(eid): |
36 """return the rql query necessary to fetch entity with the given eid. This |
38 """return the rql query necessary to fetch entity with the given eid. This |
37 function should only be used to generate link with rql inside, not to give |
39 function should only be used to generate link with rql inside, not to give |
225 return text[:length] + u'...' |
227 return text[:length] + u'...' |
226 |
228 |
227 |
229 |
228 |
230 |
229 # HTML generation helper functions ############################################ |
231 # HTML generation helper functions ############################################ |
|
232 |
|
233 class _JSId(object): |
|
234 def __init__(self, id, parent=None): |
|
235 self.id = id |
|
236 self.parent = parent |
|
237 def __str__(self): |
|
238 if self.parent: |
|
239 return '%s.%s' % (self.parent, self.id) |
|
240 return '%s' % self.id |
|
241 def __getattr__(self, attr): |
|
242 return _JSId(attr, self) |
|
243 def __call__(self, *args): |
|
244 return _JSCallArgs(args, self) |
|
245 |
|
246 class _JSCallArgs(_JSId): |
|
247 def __init__(self, args, parent=None): |
|
248 assert isinstance(args, tuple) |
|
249 self.args = args |
|
250 self.parent = parent |
|
251 def __str__(self): |
|
252 args = ','.join(json_dumps(arg) for arg in self.args) |
|
253 if self.parent: |
|
254 return '%s(%s)' % (self.parent, args) |
|
255 return args |
|
256 |
|
257 class _JS(object): |
|
258 def __getattr__(self, attr): |
|
259 return _JSId(attr) |
|
260 |
|
261 """magic object to return strings suitable to call some javascript function with |
|
262 the given arguments (which should be correctly typed). |
|
263 |
|
264 >>> str(js.pouet(1, "2")) |
|
265 'pouet(1,"2")' |
|
266 >>> str(js.cw.pouet(1, "2")) |
|
267 'cw.pouet(1,"2")' |
|
268 >>> str(js.cw.pouet(1, "2").pouet(None)) |
|
269 'cw.pouet(1,"2").pouet(null)') |
|
270 """ |
|
271 js = _JS() |
|
272 |
|
273 def domid(string): |
|
274 """return a valid DOM id from a string (should also be usable in jQuery |
|
275 search expression...) |
|
276 """ |
|
277 return string.replace('.', '_').replace('-', '_') |
230 |
278 |
231 HTML4_EMPTY_TAGS = frozenset(('base', 'meta', 'link', 'hr', 'br', 'param', |
279 HTML4_EMPTY_TAGS = frozenset(('base', 'meta', 'link', 'hr', 'br', 'param', |
232 'img', 'area', 'input', 'col')) |
280 'img', 'area', 'input', 'col')) |
233 |
281 |
234 def sgml_attributes(attrs): |
282 def sgml_attributes(attrs): |