[postgres] fix limit_size regexp to properly remove one char tags like <p>.
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 06 Apr 2015 21:46:52 +0200
changeset 10328 d7ff46d958f4
parent 10327 a504a7840915
child 10329 f92823a66f18
[postgres] fix limit_size regexp to properly remove one char tags like <p>. Closes #5221847
misc/migration/3.20.7_Any.py
schemas/_regproc.postgres.sql
server/test/unittest_postgres.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/misc/migration/3.20.7_Any.py	Mon Apr 06 21:46:52 2015 +0200
@@ -0,0 +1,2 @@
+if repo.system_source.dbdriver == 'postgres':
+    install_custom_sql_scripts()
--- a/schemas/_regproc.postgres.sql	Wed Apr 15 22:08:08 2015 +0200
+++ b/schemas/_regproc.postgres.sql	Mon Apr 06 21:46:52 2015 +0200
@@ -26,7 +26,6 @@
 );;
 
 
-
 DROP FUNCTION IF EXISTS limit_size (fulltext text, format text, maxsize integer);
 CREATE FUNCTION limit_size (fulltext text, format text, maxsize integer) RETURNS text AS $$
 DECLARE
@@ -36,7 +35,7 @@
        RETURN fulltext;
     END IF;
     IF format = 'text/html' OR format = 'text/xhtml' OR format = 'text/xml' THEN
-       plaintext := regexp_replace(fulltext, '<[\\w/][^>]+>', '', 'g');
+       plaintext := regexp_replace(fulltext, '<[a-zA-Z/][^>]*>', '', 'g');
     ELSE
        plaintext := fulltext;
     END IF;
--- a/server/test/unittest_postgres.py	Wed Apr 15 22:08:08 2015 +0200
+++ b/server/test/unittest_postgres.py	Mon Apr 06 21:46:52 2015 +0200
@@ -113,6 +113,24 @@
             self.assertEqual(datenaiss.tzinfo, None)
             self.assertEqual(datenaiss.utctimetuple()[:5], (1977, 6, 7, 2, 0))
 
+class PostgresLimitSizeTC(CubicWebTC):
+    configcls = PostgresApptestConfiguration
+
+    def test(self):
+        with self.admin_access.repo_cnx() as cnx:
+            def sql(string):
+                return cnx.system_sql(string).fetchone()[0]
+            yield self.assertEqual, sql("SELECT limit_size('<p>hello</p>', 'text/html', 20)"), \
+                '<p>hello</p>'
+            yield self.assertEqual, sql("SELECT limit_size('<p>hello</p>', 'text/html', 2)"), \
+                'he...'
+            yield self.assertEqual, sql("SELECT limit_size('<br/>hello', 'text/html', 2)"), \
+                'he...'
+            yield self.assertEqual, sql("SELECT limit_size('<span class=\"1\">he</span>llo', 'text/html', 2)"), \
+                'he...'
+            yield self.assertEqual, sql("SELECT limit_size('<span>a>b</span>', 'text/html', 2)"), \
+                'a>...'
+
 if __name__ == '__main__':
     from logilab.common.testlib import unittest_main
     unittest_main()