changeset 11302:e1dde7363601

color: labeled text should be passed to ui.write() as ui.labeled Some implementations of ui.label() (HTML versions in particular) must escape the provided text and then markup the text with their tags. When this marked up text is then passed to ui.write(), we must label the text as 'ui.labeled' so the implementation knows not to escape it a second time (exposing the initial markup). This required the addition of a 'ui.plain' label for text that is purposefully not marked up. I was a little pedantic here, passing even ' ' strings to ui.label() when it would be included with other labeled text in a ui.write() call. But it seemed appropriate to lean to the side of caution.
author Steve Borho <steve@borho.org>
date Thu, 03 Jun 2010 23:18:18 -0500
parents 3d0591a66118
children a1aad8333864 ac873ecfc3c2
files hgext/churn.py hgext/color.py hgext/mq.py mercurial/commands.py mercurial/ui.py
diffstat 5 files changed, 31 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/churn.py	Mon Jun 07 18:35:54 2010 +0200
+++ b/hgext/churn.py	Thu Jun 03 23:18:18 2010 -0500
@@ -150,8 +150,10 @@
     if opts.get('diffstat'):
         width -= 15
         def format(name, (added, removed)):
-            return "%s %15s %s%s\n" % (pad(name, maxname),
-                                       '+%d/-%d' % (added, removed),
+            return "%s %15s %s%s\n" % (ui.label(pad(name, maxname),
+                                                'ui.plain'),
+                                       ui.label('+%d/-%d' % (added, removed),
+                                                'ui.plain'),
                                        ui.label('+' * charnum(added),
                                                 'diffstat.inserted'),
                                        ui.label('-' * charnum(removed),
@@ -159,14 +161,14 @@
     else:
         width -= 6
         def format(name, count):
-            return "%s %6d %s\n" % (pad(name, maxname), sum(count),
-                                    '*' * charnum(sum(count)))
+            return ui.label("%s %6d %s\n" % (pad(name, maxname), sum(count),
+                            '*' * charnum(sum(count))), 'ui.plain')
 
     def charnum(count):
         return int(round(count * width / maxcount))
 
     for name, count in rate:
-        ui.write(format(name, count))
+        ui.write(format(name, count), label='ui.labeled')
 
 
 cmdtable = {
--- a/hgext/color.py	Mon Jun 07 18:35:54 2010 +0200
+++ b/hgext/color.py	Thu Jun 03 23:18:18 2010 -0500
@@ -108,7 +108,9 @@
            'status.ignored': 'black bold',
            'status.modified': 'blue bold',
            'status.removed': 'red bold',
-           'status.unknown': 'magenta bold underline'}
+           'status.unknown': 'magenta bold underline',
+           'ui.labeled': 'none',
+           'ui.plain': 'none'}
 
 
 def render_effects(text, effects):
@@ -142,6 +144,8 @@
 
 _buffers = None
 def style(msg, label):
+    if label in ('ui.plain', 'ui.labeled'):
+        return msg
     effects = []
     for l in label.split():
         s = _styles.get(l, '')
--- a/hgext/mq.py	Mon Jun 07 18:35:54 2010 +0200
+++ b/hgext/mq.py	Thu Jun 03 23:18:18 2010 -0500
@@ -2145,17 +2145,17 @@
     '''
     def status(idx):
         guards = q.series_guards[idx] or ['unguarded']
-        ui.write('%s: ' % ui.label(q.series[idx], 'qguard.patch'))
+        out = ['%s: ' % ui.label(q.series[idx], 'qguard.patch')]
         for i, guard in enumerate(guards):
             if guard.startswith('+'):
-                ui.write(guard, label='qguard.positive')
+                out.append(ui.label(guard, 'qguard.positive'))
             elif guard.startswith('-'):
-                ui.write(guard, label='qguard.negative')
+                out.append(ui.label(guard, 'qguard.negative'))
             else:
-                ui.write(guard, label='qguard.unguarded')
+                out.append(ui.label(guard, 'qguard.unguarded'))
             if i != len(guards) - 1:
-                ui.write(' ')
-        ui.write('\n')
+                out.append(ui.label(' ', 'ui.plain'))
+        ui.write(''.join(out) + '\n', label='ui.labeled')
     q = repo.mq
     patch = None
     args = list(args)
@@ -2799,7 +2799,8 @@
     if u:
         m.append(ui.label(_("%d unapplied"), 'qseries.unapplied') % u)
     if m:
-        ui.write("mq:     %s\n" % ', '.join(m))
+        ui.write("mq:     ")
+        ui.write(', '.join(m) + '\n', label='ui.labeled')
     else:
         ui.note(_("mq:     (empty queue)\n"))
     return r
--- a/mercurial/commands.py	Mon Jun 07 18:35:54 2010 +0200
+++ b/mercurial/commands.py	Thu Jun 03 23:18:18 2010 -0500
@@ -3280,22 +3280,22 @@
     cleanworkdir = False
 
     if len(parents) > 1:
-        t += _(' (merge)')
+        t += ui.label(_(' (merge)'), 'ui.plain')
     elif branch != parents[0].branch():
-        t += _(' (new branch)')
+        t += ui.label(_(' (new branch)'), 'ui.plain')
     elif (parents[0].extra().get('close') and
           pnode in repo.branchheads(branch, closed=True)):
-        t += _(' (head closed)')
+        t += ui.label(_(' (head closed)'), 'ui.plain')
     elif (not st[0] and not st[1] and not st[2] and not st[7]):
-        t += _(' (clean)')
+        t += ui.label(_(' (clean)'), 'ui.plain')
         cleanworkdir = True
     elif pnode not in bheads:
-        t += _(' (new branch head)')
+        t += ui.label(_(' (new branch head)'), 'ui.plain')
 
     if cleanworkdir:
-        ui.status(_('commit: %s\n') % t.strip())
+        ui.status(_('commit: %s\n') % t.strip(), label='ui.labeled')
     else:
-        ui.write(_('commit: %s\n') % t.strip())
+        ui.write(_('commit: %s\n') % t.strip(), label='ui.labeled')
 
     # all ancestors of branch heads - all ancestors of parent = new csets
     new = [0] * len(repo)
--- a/mercurial/ui.py	Mon Jun 07 18:35:54 2010 +0200
+++ b/mercurial/ui.py	Thu Jun 03 23:18:18 2010 -0500
@@ -546,5 +546,9 @@
 
         ui.write(s, 'label') is equivalent to
         ui.write(ui.label(s, 'label')).
+
+        Callers of ui.label() should pass labeled text back to
+        ui.write() with a label of 'ui.labeled' so implementations know
+        that the text has already been escaped and marked up.
         '''
         return msg