SF.net SVN: docutils:[9931 ] trunk/docutils

milde--- via Docutils-checkins <[email protected]>
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 9931
          http://sourceforge.net/p/docutils/code/9931
Author:   milde
Date:     2024-09-15 11:13:43 +0000 (Sun, 15 Sep 2024)
Log Message:
-----------
HTML writers: Add "px" to unitless table "width" values.

The table "width" attribute is of type "length", with optional unit.
The writers must ensure there is a unit when converting a length to
a HTML "style" attribute rule.
For HTML writers, the default length unit is "px".

Modified Paths:
--------------
    trunk/docutils/HISTORY.rst
    trunk/docutils/docutils/writers/_html_base.py
    trunk/docutils/docutils/writers/html4css1/__init__.py
    trunk/docutils/docutils/writers/html5_polyglot/__init__.py
    trunk/docutils/test/test_writers/test_html4css1.py
    trunk/docutils/test/test_writers/test_html5_polyglot.py

Modified: trunk/docutils/HISTORY.rst
===================================================================
--- trunk/docutils/HISTORY.rst	2024-09-13 08:13:03 UTC (rev 9930)
+++ trunk/docutils/HISTORY.rst	2024-09-15 11:13:43 UTC (rev 9931)
@@ -147,10 +147,12 @@
   - Make MathML the default math_output_.
   - Revise image size handling methods,
     use "width" and "height" attributes for unitless values.
+  - Add "px" to unitless table "width" values.
 
 * docutils/writers/html4css1/__init__.py
 
   - Keep default math_output_ value "HTML math.css".
+  - Add "px" to unitless table "width" values.
 
 * docutils/writers/latex2e/__init__.py
 

Modified: trunk/docutils/docutils/writers/_html_base.py
===================================================================
--- trunk/docutils/docutils/writers/_html_base.py	2024-09-13 08:13:03 UTC (rev 9930)
+++ trunk/docutils/docutils/writers/_html_base.py	2024-09-15 11:13:43 UTC (rev 9931)
@@ -1693,7 +1693,10 @@
         if 'align' in node:
             atts['classes'].append('align-%s' % node['align'])
         if 'width' in node:
-            atts['style'] = 'width: %s;' % node['width']
+            width = node['width']
+            if width[-1:] in '0123456789.':  # unitless value
+                width += 'px'  # add default length unit
+            atts['style'] = f'width: {width};'
         tag = self.starttag(node, 'table', **atts)
         self.body.append(tag)
 

Modified: trunk/docutils/docutils/writers/html4css1/__init__.py
===================================================================
--- trunk/docutils/docutils/writers/html4css1/__init__.py	2024-09-13 08:13:03 UTC (rev 9930)
+++ trunk/docutils/docutils/writers/html4css1/__init__.py	2024-09-15 11:13:43 UTC (rev 9931)
@@ -866,7 +866,10 @@
         if 'align' in node:
             classes.append('align-%s' % node['align'])
         if 'width' in node:
-            atts['style'] = 'width: %s' % node['width']
+            width = node['width']
+            if width[-1:] in '0123456789.':  # unitless value
+                width += 'px'  # add default length unit
+            atts['style'] = f'width: {width}'
         self.body.append(
             self.starttag(node, 'table', CLASS=' '.join(classes), **atts))
 

Modified: trunk/docutils/docutils/writers/html5_polyglot/__init__.py
===================================================================
--- trunk/docutils/docutils/writers/html5_polyglot/__init__.py	2024-09-13 08:13:03 UTC (rev 9930)
+++ trunk/docutils/docutils/writers/html5_polyglot/__init__.py	2024-09-15 11:13:43 UTC (rev 9931)
@@ -200,7 +200,7 @@
     # use new HTML5 <figure> and <figcaption> elements
     def visit_figure(self, node) -> None:
         atts = {}
-        if node.get('width'):
+        if 'width' in node:
             atts['style'] = f"width: {node['width']}"
         if node.get('align'):
             atts['class'] = f"align-{node['align']}"

Modified: trunk/docutils/test/test_writers/test_html4css1.py
===================================================================
--- trunk/docutils/test/test_writers/test_html4css1.py	2024-09-13 08:13:03 UTC (rev 9930)
+++ trunk/docutils/test/test_writers/test_html4css1.py	2024-09-15 11:13:43 UTC (rev 9931)
@@ -183,6 +183,7 @@
 ["""\
 .. table::
    :align: right
+   :width: 320
 
    +-----+-----+
    |  1  |  2  |
@@ -191,7 +192,7 @@
    +-----+-----+
 """,
 """\
-<table border="1" class="docutils align-right">
+<table border="1" class="docutils align-right" style="width: 320px">
 <colgroup>
 <col width="50%" />
 <col width="50%" />

Modified: trunk/docutils/test/test_writers/test_html5_polyglot.py
===================================================================
--- trunk/docutils/test/test_writers/test_html5_polyglot.py	2024-09-13 08:13:03 UTC (rev 9930)
+++ trunk/docutils/test/test_writers/test_html5_polyglot.py	2024-09-15 11:13:43 UTC (rev 9931)
@@ -218,6 +218,7 @@
 ["""\
 .. table::
    :align: right
+   :width: 320
 
    +-----+-----+
    |  1  |  2  |
@@ -226,7 +227,7 @@
    +-----+-----+
 """,
 """\
-<table class="align-right">
+<table class="align-right" style="width: 320px;">
 <tbody>
 <tr><td><p>1</p></td>
 <td><p>2</p></td>

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.