diff src/fschmidt/html/HtmlScript.java @ 68:00520880ad02

add fschmidt source
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 05 Oct 2025 17:24:15 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fschmidt/html/HtmlScript.java	Sun Oct 05 17:24:15 2025 -0600
@@ -0,0 +1,144 @@
+/*
+Copyright (c) 2008  Franklin Schmidt <fschmidt@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+package fschmidt.html;
+
+import java.util.List;
+import java.util.ArrayList;
+
+
+public final class HtmlScript extends HtmlTextContainer {
+
+	public HtmlScript(HtmlTag startTag,String text,HtmlTag endTag) {
+		super(startTag,text,endTag);
+	}
+
+
+	// odd strings are string constants
+	public static String[] split(String text) {
+		List<String> list = new ArrayList<String>();
+		char[] a = text.toCharArray();
+		int len = a.length;
+		StringBuilder buf = new StringBuilder();
+outer:
+		for( int i=0; i<len; i++ ) {
+			char c = a[i];
+			switch(c) {
+			case '"':
+			case '\'':
+				StringBuilder bufS = new StringBuilder();
+				bufS.append(c);
+				for( int j=i+1; j<len && j!='\n'; j++ ) {
+					char c2 = a[j];
+					bufS.append(c2);
+					if( c2=='\\' ) {
+						bufS.append(a[++j]);
+						continue;
+					}
+					if( c2==c ) {
+						list.add(buf.toString());
+						list.add(bufS.toString());
+						i = j;
+						buf.setLength(0);
+						continue outer;
+					}
+				}
+			default:
+				buf.append(c);
+			}
+		}
+		list.add(buf.toString());
+		return (String[])list.toArray(new String[0]);
+	}
+
+	public static String unquote(String s) {
+		char c = s.charAt(0);
+		if( c!='"' && c!='\'' || c!=s.charAt(s.length()-1) )
+			throw new RuntimeException();
+		s = s.substring(1,s.length()-1);
+		StringBuilder buf = new StringBuilder();
+		int i = 0;
+		while(true) {
+			int i2 = s.indexOf('\\',i);
+			if( i2 == -1 )
+				break;
+			buf.append(s.substring(i,i2));
+			c = s.charAt(i2+1);
+			switch(c) {
+			case 'b':
+				buf.append('\b');
+				break;
+			case 'f':
+				buf.append('\f');
+				break;
+			case 'n':
+				buf.append('\n');
+				break;
+			case 'r':
+				buf.append('\r');
+				break;
+			case 't':
+				buf.append('\t');
+				break;
+			default:
+				buf.append(c);
+			}
+			i = i2 + 2;
+		}
+		buf.append(s.substring(i));
+		return buf.toString();
+	}
+
+	public static String quote(String s,char quote) {
+		StringBuilder buf = new StringBuilder();
+		char[] a = s.toCharArray();
+		buf.append(quote);
+		for( int i=0; i<a.length; i++ ) {
+			char c = a[i];
+			switch(c) {
+			case '\b':
+				buf.append("\\b");
+				break;
+			case '\f':
+				buf.append("\\f");
+				break;
+			case '\n':
+				buf.append("\\n");
+				break;
+			case '\r':
+				buf.append("\\r");
+				break;
+			case '\t':
+				buf.append("\\t");
+				break;
+			case '\\':
+			case '"':
+			case '\'':
+				buf.append('\\');
+			default:
+				buf.append(c);
+			}
+		}
+		buf.append(quote);
+		return buf.toString();
+	}
+}