List for cgit developers and users
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: cgit@lists.zx2c4.com
Subject: [PATCH 4/5] ui-diff: preserve spaces w/o CSS on context lines
Date: Fri, 19 Mar 2021 20:23:36 +0000
Message-ID: <20210319202337.21676-5-e@80x24.org> (raw)
In-Reply-To: <20210319202337.21676-1-e@80x24.org>

We need to use a non-breaking space entity to preserve
spacing for browsers without CSS support.

Signed-off-by: Eric Wong <e@80x24.org>
---
 html.c    | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 html.h    |  1 +
 ui-diff.c |  5 +----
 3 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/html.c b/html.c
index 7f81965..138c649 100644
--- a/html.c
+++ b/html.c
@@ -156,6 +156,52 @@ ssize_t html_ntxt(const char *txt, size_t len)
 	return slen;
 }
 
+ssize_t html_ntxt_pre(const char *txt, size_t len)
+{
+	const char *t = txt;
+	ssize_t slen;
+	int prev = 0;
+
+	if (len > SSIZE_MAX)
+		return -1;
+
+	slen = (ssize_t) len;
+	while (t && *t && slen--) {
+		int c = *t;
+		if (c == '<' || c == '>' || c == '&' || c == ' ' || c == '\t') {
+			html_raw(txt, t - txt);
+			if (c == '>')
+				html("&gt;");
+			else if (c == '<')
+				html("&lt;");
+			else if (c == '&')
+				html("&amp;");
+			else if (c == ' ') {
+				if (!prev || prev == ' ' || prev == '\t') {
+					html("&#160;");
+					/* next byte can be unescaped ' ' */
+					c = 160;
+				} else {
+					html(" ");
+				}
+			} else if (c == '\t') {
+				html("&#160;&#160;&#160;&#160;"
+				     "&#160;&#160;&#160;&#160;");
+				/* next byte can be unescaped ' ' */
+				c = 160;
+			}
+			txt = t + 1;
+		}
+		prev = c;
+		t++;
+	}
+	if (t != txt)
+		html_raw(txt, t - txt);
+	return slen;
+}
+
+
+
 void html_attrf(const char *fmt, ...)
 {
 	va_list ap;
diff --git a/html.h b/html.h
index fa4de77..4479b8e 100644
--- a/html.h
+++ b/html.h
@@ -20,6 +20,7 @@ extern void html_attrf(const char *format,...);
 
 extern void html_txt(const char *txt);
 extern ssize_t html_ntxt(const char *txt, size_t len);
+extern ssize_t html_ntxt_pre(const char *txt, size_t len);
 extern void html_attr(const char *txt);
 extern void html_url_path(const char *txt);
 extern void html_url_arg(const char *txt);
diff --git a/ui-diff.c b/ui-diff.c
index 5ed5990..a0f2bb7 100644
--- a/ui-diff.c
+++ b/ui-diff.c
@@ -222,7 +222,6 @@ static void cgit_print_diffstat(const struct object_id *old_oid,
 static void print_line(char *line, int len)
 {
 	char *class = "ctx";
-	char c = line[len-1];
 
 	if (line[0] == '+')
 		class = "add";
@@ -232,10 +231,8 @@ static void print_line(char *line, int len)
 		class = "hunk";
 
 	htmlf("<div class='%s'>", class);
-	line[len-1] = '\0';
-	html_txt(line);
+	html_ntxt_pre(line, len - 1);
 	html("</div>");
-	line[len-1] = c;
 }
 
 static void header(const struct object_id *oid1, char *path1, int mode1,

  parent reply	other threads:[~2021-03-19 20:31 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-19 20:23 [PATCH (resend) 0/5] improve rendering w/o CSS reliance Eric Wong
2021-03-19 20:23 ` [PATCH 3/5] ui-log: improve decoration display for browsers without CSS Eric Wong
2021-03-19 20:23 ` Eric Wong [this message]
2021-03-19 20:23 ` [PATCH 5/5] ui-{tree, repolist}: improve button spacing for browsers w/o CSS Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210319202337.21676-5-e@80x24.org \
    --to=e@80x24.org \
    --cc=cgit@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

List for cgit developers and users

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/cgit

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 cgit cgit/ http://inbox.vuxu.org/cgit \
		cgit@lists.zx2c4.com
	public-inbox-index cgit

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.cgit


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git