Gui: Strip `about:` from links during `Copy` [#823]

master
gnosygnu 3 years ago
parent 1a6a203cfd
commit 4187dc4a76

@ -1,6 +1,6 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
Copyright (C) 2012-2021 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
@ -13,8 +13,16 @@ The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.xowa.htmls.hrefs; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*;
import gplx.core.btries.*; import gplx.core.primitives.*;
package gplx.xowa.htmls.hrefs;
import gplx.Bry_;
import gplx.Byte_ascii;
import gplx.Err_;
import gplx.String_;
import gplx.core.btries.Btrie_slim_mgr;
import gplx.core.primitives.Byte_obj_val;
import gplx.gfui.kits.swts.Swt_html_utl;
public class Xoh_href_gui_utl {
public static String Html_extract_text(String site, String page, String text_str) {
byte[] text_bry = Bry_.new_u8(text_str);
@ -26,18 +34,19 @@ public class Xoh_href_gui_utl {
case Text_tid_href: break; // fall through to below
default: throw Err_.new_unhandled(text_tid);
}
int href_bgn = 2; // 2 to skip "2|"
if (Bry_.Has_at_bgn(text_bry, Xoh_href_.Bry__file, href_bgn, text_len))
href_bgn += Xoh_href_.Len__file; // skip "file://"
Byte_obj_val href_tid = (Byte_obj_val)href_trie.Match_bgn(text_bry, href_bgn, text_len);
String href_str = String_.Mid(String_.new_u8(text_bry), 2);
href_str = Swt_html_utl.NormalizeSwtUrl(href_str);
if (String_.Has_at_bgn(href_str, Xoh_href_.Str__file))
href_str = Standardize_xowa_link(href_str); // skip "file://"
Byte_obj_val href_tid = (Byte_obj_val)href_trie.Match_bgn(Bry_.new_u8(href_str), 0, href_str.length());
if (href_tid != null) {
switch (href_tid.Val()) {
case Href_tid_wiki: return site + String_.new_u8(text_bry, href_bgn, text_len);
case Href_tid_site: return String_.new_u8(text_bry, href_bgn + 6, text_len); // +6 to skip "site/"
case Href_tid_anch: return site + "/wiki/" + page + String_.new_u8(text_bry, href_bgn, text_len);
case Href_tid_wiki: return site + href_str;
case Href_tid_site: return String_.Mid(href_str, 6); // +6 to skip "site/"
case Href_tid_anch: return site + "/wiki/" + page + href_str;
}
}
return String_.new_u8(text_bry, 2, text_len); // 2 to skip "2|"; handles "http://" text as well as any fall-thru from above
return href_str;
}
public static String Standardize_xowa_link(String str) {
byte[] bry = Bry_.new_u8(str);
@ -49,8 +58,13 @@ public class Xoh_href_gui_utl {
int pos = bgn + Xoh_href_.Len__file; // skip "file://"
Object tid_obj = href_trie.Match_bgn(src, pos, src_len);
if (tid_obj == null) {
if (src_len - pos > 0 && src[pos] == Byte_ascii.Slash) { // handle "file:///C:/dir/fil.png"
return pos + 1;
}
else {
return bgn; // if not a known xowa link, return original bgn;
}
}
switch (((Byte_obj_val)tid_obj).Val()) {
case Href_tid_site: return pos;
case Href_tid_wiki: return pos;

@ -1,6 +1,6 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
Copyright (C) 2012-2021 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
@ -13,25 +13,30 @@ The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.xowa.htmls.hrefs; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*;
import org.junit.*;
import gplx.core.primitives.*; import gplx.xowa.htmls.hrefs.*; import gplx.xowa.guis.views.*;
package gplx.xowa.htmls.hrefs;
import gplx.Tfds;
import gplx.core.primitives.String_obj_ref;
import gplx.xowa.guis.views.Xog_html_itm;
import org.junit.Before;
import org.junit.Test;
public class Xoh_href_gui_utl_tst {
@Before public void init() {fxt.Clear();} private Xoh_href_gui_utl_fxt fxt = new Xoh_href_gui_utl_fxt();
@Test public void Extract_href__text() {
fxt.Test_extract_href("0|" , "");
fxt.Test_extract_href("1|selected_text" , "selected_text");
fxt.Test_extract_href("2|http://a.org" , "http://a.org");
fxt.Test_extract_text("0|" , "");
fxt.Test_extract_text("1|selected_text" , "selected_text");
fxt.Test_extract_text("2|http://a.org" , "http://a.org");
}
@Test public void Extract_href__file() {
fxt.Test_extract_href("2|file:///site/en.wiktionary.org/wiki/Page_1" , "en.wiktionary.org/wiki/Page_1");
fxt.Test_extract_href("2|file:///wiki/Page_2" , "en.wikipedia.org/wiki/Page_2");
fxt.Test_extract_href("2|file://#anchor" , "en.wikipedia.org/wiki/Page_0#anchor");
fxt.Test_extract_text("2|file:///site/en.wiktionary.org/wiki/Page_1" , "en.wiktionary.org/wiki/Page_1");
fxt.Test_extract_text("2|file:///wiki/Page_2" , "en.wikipedia.org/wiki/Page_2");
fxt.Test_extract_text("2|file://#anchor" , "en.wikipedia.org/wiki/Page_0#anchor");
}
@Test public void Extract_href__internal() {
fxt.Test_extract_href("2|/site/en.wiktionary.org/wiki/Page_1" , "en.wiktionary.org/wiki/Page_1");
fxt.Test_extract_href("2|/wiki/Page_2" , "en.wikipedia.org/wiki/Page_2");
fxt.Test_extract_href("2|#anchor" , "en.wikipedia.org/wiki/Page_0#anchor");
fxt.Test_extract_text("2|/site/en.wiktionary.org/wiki/Page_1" , "en.wiktionary.org/wiki/Page_1");
fxt.Test_extract_text("2|/wiki/Page_2" , "en.wikipedia.org/wiki/Page_2");
fxt.Test_extract_text("2|#anchor" , "en.wikipedia.org/wiki/Page_0#anchor");
}
@Test public void Html_window_vpos_parse() {
fxt.Test_Html_window_vpos_parse("0|0,1,2", "0", "'0','1','2'");
@ -41,6 +46,13 @@ public class Xoh_href_gui_utl_tst {
fxt.Test_standardize_xowa_link("file:///site/en.wikipedia.org/wiki/A" , "/site/en.wikipedia.org/wiki/A");
fxt.Test_standardize_xowa_link("file:///wiki/A" , "/wiki/A");
fxt.Test_standardize_xowa_link("file://#A" , "#A");
fxt.Test_standardize_xowa_link("file:///C:/dir/fil.png" , "C:/dir/fil.png");
}
@Test public void Swt() { // 2021-01-03|ISSUE#:823|Copy fails for links `about:/wiki/PAGE_NAME` or `about:/site/WIKI_NAME/wiki/PAGE_NAME`
fxt.Test_extract_text("2|about:/site/en.wiktionary.org/wiki/Page_1" , "en.wiktionary.org/wiki/Page_1");
fxt.Test_extract_text("2|about:/wiki/Page_2" , "en.wikipedia.org/wiki/Page_2");
fxt.Test_extract_text("2|about:#anchor" , "en.wikipedia.org/wiki/Page_0#anchor");
fxt.Test_extract_text("2|about:file:///C:/dir/fil.png" , "C:/dir/fil.png");
}
}
class Xoh_href_gui_utl_fxt {
@ -50,7 +62,7 @@ class Xoh_href_gui_utl_fxt {
}
public String Cur_wiki() {return cur_wiki;} public Xoh_href_gui_utl_fxt Cur_wiki_(String v) {cur_wiki = v; return this;} private String cur_wiki;
public String Cur_page() {return cur_page;} public Xoh_href_gui_utl_fxt Cur_page_(String v) {cur_page = v; return this;} private String cur_page;
public void Test_extract_href(String text_str, String expd) {
public void Test_extract_text(String text_str, String expd) {
Tfds.Eq(expd, Xoh_href_gui_utl.Html_extract_text(cur_wiki, cur_page, text_str));
}
private String_obj_ref scroll_top = String_obj_ref.null_(), node_path = String_obj_ref.null_();

Loading…
Cancel
Save