mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.4.2.1
This commit is contained in:
70
400_xowa/src/gplx/web/js/Js_wtr.java
Normal file
70
400_xowa/src/gplx/web/js/Js_wtr.java
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.web.js; import gplx.*; import gplx.web.*;
|
||||
public class Js_wtr {
|
||||
private Bry_bfr bfr = Bry_bfr.reset_(255);
|
||||
public byte Quote_char() {return quote_char;} public Js_wtr Quote_char_(byte v) {quote_char = v; return this;} private byte quote_char = Byte_ascii.Quote;
|
||||
public void Clear() {bfr.Clear();}
|
||||
public String Xto_str() {return bfr.Xto_str();}
|
||||
public String Xto_str_and_clear() {return bfr.Xto_str_and_clear();}
|
||||
public Js_wtr Add_comma() {bfr.Add_byte(Byte_ascii.Comma); return this;}
|
||||
public Js_wtr Add_paren_bgn() {bfr.Add_byte(Byte_ascii.Paren_bgn); return this;}
|
||||
public Js_wtr Add_paren_end() {bfr.Add_byte(Byte_ascii.Paren_end); return this;}
|
||||
public Js_wtr Add_brack_bgn() {bfr.Add_byte(Byte_ascii.Brack_bgn); return this;}
|
||||
public Js_wtr Add_brack_end() {bfr.Add_byte(Byte_ascii.Brack_end); return this;}
|
||||
public Js_wtr Add_str(byte[] v) {bfr.Add(v); return this;}
|
||||
public Js_wtr Add_str(String v) {bfr.Add_str(v); return this;}
|
||||
public Js_wtr Add_paren_end_semic() {bfr.Add_byte(Byte_ascii.Paren_end); bfr.Add_byte(Byte_ascii.Semic); return this;}
|
||||
public Js_wtr Add_str_arg(int i, byte[] bry){
|
||||
if (i != 0) bfr.Add_byte(Byte_ascii.Comma);
|
||||
Add_str_quote(bry);
|
||||
return this;
|
||||
}
|
||||
public Js_wtr Add_str_quote(byte[] bry) {
|
||||
bfr.Add_byte(quote_char);
|
||||
int len = bry.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
byte b = bry[i];
|
||||
if (b == quote_char) bfr.Add_byte(Byte_ascii.Backslash);
|
||||
bfr.Add_byte(b);
|
||||
}
|
||||
bfr.Add_byte(quote_char);
|
||||
return this;
|
||||
}
|
||||
public Js_wtr Add_str_quote_html(byte[] bry) {
|
||||
bfr.Add_byte(quote_char);
|
||||
int len = bry.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
byte b = bry[i];
|
||||
if (b == quote_char) { // double up quotes
|
||||
bfr.Add_byte(Byte_ascii.Backslash);
|
||||
bfr.Add_byte(b);
|
||||
}
|
||||
else {
|
||||
switch (b) {
|
||||
case Byte_ascii.Backslash: bfr.Add_byte_repeat(Byte_ascii.Backslash, 2); break; // "\" -> "\\"; needed else js will usurp \ as escape; EX: "\&" -> "&"; DATE:2014-06-24
|
||||
case Byte_ascii.NewLine: bfr.Add_byte(Byte_ascii.Backslash).Add_byte(Byte_ascii.Ltr_n); break; // "\n" -> "\\n"
|
||||
case Byte_ascii.CarriageReturn: break;// skip
|
||||
default: bfr.Add_byte(b); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
bfr.Add_byte(quote_char);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
40
400_xowa/src/gplx/web/js/Js_wtr_tst.java
Normal file
40
400_xowa/src/gplx/web/js/Js_wtr_tst.java
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.web.js; import gplx.*; import gplx.web.*;
|
||||
import org.junit.*;
|
||||
public class Js_wtr_tst {
|
||||
@Before public void Init() {fxt.Clear();} private Js_wtr_fxt fxt = new Js_wtr_fxt();
|
||||
@Test public void Basic() {
|
||||
fxt.Test_write_val_html("abc" , "'abc'");
|
||||
fxt.Test_write_val_html("a'b" , "'a\\'b'");
|
||||
fxt.Test_write_val_html("a\nb" , "'a\\nb'");
|
||||
fxt.Test_write_val_html("a\rb" , "'ab'");
|
||||
fxt.Test_write_val_html("a\\&b" , "'a\\\\&b'"); // PURPOSE: backslashes need to be escaped; need for MathJax and "\&"; PAGE:Electromagnetic_field_tensor; DATE:2014-06-24
|
||||
}
|
||||
}
|
||||
class Js_wtr_fxt {
|
||||
private Js_wtr wtr = new Js_wtr();
|
||||
public void Clear() {
|
||||
wtr.Clear();
|
||||
wtr.Quote_char_(Byte_ascii.Apos);
|
||||
}
|
||||
public void Test_write_val_html(String raw, String expd) {
|
||||
wtr.Add_str_quote_html(Bry_.new_utf8_(raw));
|
||||
Tfds.Eq(expd, wtr.Xto_str_and_clear());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user