scorninpc / php-gtk3

GTK3 extension for PHP
https://andor.com.br/
GNU Lesser General Public License v3.0
113 stars 10 forks source link

markup_escape_text #130

Closed d47081 closed 1 month ago

d47081 commented 1 month ago

Seems this method wanted to escape data for Label::set_markup properly https://docs.gtk.org/glib/func.markup_escape_text.html

I had used PHP function htmlspecialchars but

(php:76903): Gtk-WARNING **: 04:44:57.040: Failed to set text
...

Example text too long, but supporse it could be any char of these:

<?php

Gtk::init();

$label = new \GtkLabel;

$label->set_use_markup(true);

$label->set_markup("๐ŸŒง๏ธ ๐Ÿž๐Ÿ™");
scorninpc commented 1 month ago

You think i can escape by default ?

d47081 commented 1 month ago

I don't understand where is problem coming from, tried this code and everything work

<?php

\Gtk::init();

$win = new \GtkWindow();

$win->set_default_size(300, 200);

$label = new \GtkLabel;

$label->set_use_markup(true);

$label->set_markup("๐ŸŒง๏ธ ๐Ÿž๐Ÿ™");

$win->add($label);

$win->show_all();

\Gtk::main();

As my issue happened on browsing different pages, usually contain lot of text, I can't understand where is exactly wrong/unescaped char by this debug message

(php:76903): Gtk-WARNING **: 04:44:57.040: Failed to set text >> ORIGINAL BODY GOES HERE

but I wish to try any GTK escape method, and implement it by the way

d47081 commented 1 month ago

You think i can escape by default ?

not sure set_markup escapes text by default, because currently I'm using PHP htmlspecialchars to resolve special chars issues. but sometimes, some pages contain unsupported raw input that drop warning in terminal

scorninpc commented 1 month ago

i got , but you think i can escape everytime with g_markup_escape_text or expose the method to espace if you want?

d47081 commented 1 month ago

https://docs.gtk.org/glib/func.markup_escape_text.html

gchar*
g_markup_escape_text (
  const gchar* text,
  gssize length
)

it should return escaped text or not?

$label->set_markup(
    markup_escape_text("๐ŸŒง๏ธ ๐Ÿž๐Ÿ™") // something like this
);

I have opened this feature request because not found in php-gtk3 sources, so thoughts to add and try. Suppose some native htmlspecialchars alternative should be available in GTK for special chars escape operations

scorninpc commented 1 month ago

Your code works fine here

image

You need show what?

I think g_markup_escape_text will escape tags. For example, if you need to show <test> in your text

d47081 commented 1 month ago

By the fact I'm using following code to escape special chars in markup, then append tags: https://github.com/YGGverse/Yoda/blob/main/src/Entity/Browser/Container/Page/Content/Gemtext.php

In fact just want to replace htmlspecialchars withmarkup_escape_text but seems method not available in php-gtk3

Current solution mostly works (to convert gemtext to pango), but sometimes (maybe when page contain emoji) I have alert in terminal that markup given to set_markup contain errors or unescaped chars and could not be rendered properly.

Today can't catch this issue, will try to reproduce this problem later from laptop where I have data URL bookmarked.

d47081 commented 1 month ago

Finally I have added this feature by PR https://github.com/scorninpc/php-gtk3/pull/135

I see lot of useful function there for next integrations https://docs.gtk.org/glib/index.html#functions

suppose it's better than use php htmlspecialchars not adapted for pango

d47081 commented 1 month ago

Seems I need now (at least to try) https://docs.gtk.org/glib/func.utf8_make_valid.html https://docs.gtk.org/glib/func.utf8_normalize.html

and some others because native php functions is not about gtk tasks to find alternatives there all the time

and I vote for Glib namespace, because:

  1. Gtk already overloaded
  2. it would be much correct namespace preffix than Gtk, by the docs

That's not for right now but thoughts, and thoughts about this one API #151 I think these features usage is developers responsibility, not php-gtk provider problem to not implement them.

scorninpc commented 1 month ago

I think you dont complete understand escape methods, encodes, etc

this all is a pattern to manipulate strings. methods and languages are irrelevants

in this case, php has advances to use iconv and mb_string

you can convert any encoded string to utf8 with iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text); for example

scorninpc commented 1 month ago

anyway, if you want to implement, go aread

d47081 commented 1 month ago

Thanks, I knew this method. Just opinion.

d47081 commented 1 month ago

Tried iconv method but no luck

Here is my warning:

(php:38452): Pango-WARNING **: 21:45:40.966: pango_layout_set_markup_with_accel: Error on line 1 char 52: Invalid UTF-8 encoded text in name โ€” not valid โ€œ๐Ÿ‘ฅ Docked: 1464 ยท ๐Ÿ’ฌ Logs: 6430 ยท \xf0\x9f\x95โ€

I'm getting warning at this construction:

$layout = new PangoLayout(
    (new GtkDrawingArea)->create_pango_context()
);

$layout->set_markup(
    $markup, // iconv does not helps
    mb_strlen(
        $markup,
        self::ENCODING
    )
);

imho, sometimes encoding subject is harder than looks like. suppose the problem with emoji format version not supported yet or unknown by php function

When I provide this markup to GtkLabel->set_markup - no any problem, I believe that's because it uses native Glib functions with results expected by widgets, where php uses own versions.

Just one of possible reasons.