Commit db533c00 authored by Guillaume Vandevelde's avatar Guillaume Vandevelde
Browse files

[utils] Add the possibility to add elements in the head of an UStringIO list

It is a graft of cc71a801b846 from version 3.26.15,
less Python 2 specific code.

--HG--
branch : 3.27
parent 1ac49d6677ef
......@@ -370,6 +370,9 @@ class HTMLHeadTC(CubicWebTC):
head.add_js(base_url + u'bob1.js')
head.add_js(u'http://ext.com/bob2.js')
head.add_js(u'http://ext.com/bob3.js')
head.write_front(
'<script type="text/javascript">console.log("FIRST SCRIPT ADDED HERE")</script>'
)
head.add_css(base_url + u'bob4.css')
head.add_css(base_url + u'bob5.css')
head.add_css(base_url + u'bob6.css', 'print')
......@@ -378,6 +381,7 @@ class HTMLHeadTC(CubicWebTC):
head.add_ie_css(base_url + u'bob9.css', 'print', u'[if lt IE 7]')
result = head.getvalue()
expected = u"""<head>
<script type="text/javascript">console.log("FIRST SCRIPT ADDED HERE")</script>
<link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob4.css"/>
<link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob5.css"/>
<link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/bob6.css"/>
......
......@@ -27,6 +27,7 @@ import re
import json
from operator import itemgetter
from inspect import getfullargspec as getargspec
from functools import wraps
from itertools import repeat
from uuid import uuid4
from threading import Lock
......@@ -181,6 +182,20 @@ class RepeatList(object):
self._size -= 1
def handle_writing_constraints(method):
@wraps(method)
def wrapper(self, value):
if self.tracewrites:
from traceback import format_stack
stack = format_stack(None)[:-1]
escaped_stack = xml_escape(json_dumps(u'\n'.join(stack)))
escaped_html = xml_escape(value).replace('\n', '<br/>\n')
tpl = u'<span onclick="alert(%s)">%s</span>'
value = tpl % (escaped_stack, escaped_html)
return method(self, value)
return wrapper
class UStringIO(list):
"""a file wrapper which automatically encode unicode string to an encoding
specifed in the constructor
......@@ -195,16 +210,14 @@ class UStringIO(list):
__nonzero__ = __bool__
@handle_writing_constraints
def write(self, value):
if self.tracewrites:
from traceback import format_stack
stack = format_stack(None)[:-1]
escaped_stack = xml_escape(json_dumps(u'\n'.join(stack)))
escaped_html = xml_escape(value).replace('\n', '<br/>\n')
tpl = u'<span onclick="alert(%s)">%s</span>'
value = tpl % (escaped_stack, escaped_html)
self.append(value)
@handle_writing_constraints
def write_front(self, value):
self.insert(0, value)
def getvalue(self):
return u''.join(self)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment