Skip to content
This repository was archived by the owner on Sep 26, 2020. It is now read-only.

Commit 6d44512

Browse files
committed
Cut circular dependency between c.g.g.core.linker and c.g.g.dev.js
git fetch https://gwt.googlesource.com/gwt refs/changes/31/1031/1 && git cherry-pick FETCH_HEAD Change-Id: Id33cdf94725981c9c5779553444c1ac64659892c
1 parent 37e81bf commit 6d44512

File tree

5 files changed

+144
-149
lines changed

5 files changed

+144
-149
lines changed

dev/core/src/com/google/gwt/core/linker/CrossSiteIframeLinker.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
import com.google.gwt.core.ext.linker.impl.ResourceInjectionUtil;
3434
import com.google.gwt.core.ext.linker.impl.SelectionScriptLinker;
3535
import com.google.gwt.dev.About;
36-
import com.google.gwt.dev.js.JsToStringGenerationVisitor;
3736
import com.google.gwt.dev.util.DefaultTextOutput;
3837
import com.google.gwt.dev.util.TextOutput;
3938
import com.google.gwt.thirdparty.guava.common.base.Joiner;
4039
import com.google.gwt.thirdparty.guava.common.base.Splitter;
4140
import com.google.gwt.util.tools.Utility;
41+
import com.google.gwt.util.tools.shared.StringUtils;
4242

4343
import java.io.IOException;
4444
import java.util.ArrayList;
@@ -584,7 +584,7 @@ protected String wrapDeferredFragment(TreeLogger logger,
584584
return String.format("$wnd.%s.runAsyncCallback%d(%s)\n",
585585
context.getModuleFunctionName(),
586586
fragment,
587-
JsToStringGenerationVisitor.javaScriptString(js));
587+
StringUtils.javaScriptString(js));
588588
}
589589

590590
@Override
@@ -608,7 +608,7 @@ protected String wrapPrimaryFragment(TreeLogger logger, LinkerContext context, S
608608
Iterable<String> chunks = Splitter.on(getScriptChunkSeparator(logger, context)).split(script);
609609
List<String> newChunks = new ArrayList<String>();
610610
for (String chunk : chunks) {
611-
newChunks.add(JsToStringGenerationVisitor.javaScriptString(chunk));
611+
newChunks.add(StringUtils.javaScriptString(chunk));
612612
}
613613
out.append(Joiner.on(",\n").join(newChunks));
614614
out.append("]);\n");

dev/core/src/com/google/gwt/core/linker/XSLinker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import com.google.gwt.core.ext.linker.Shardable;
2424
import com.google.gwt.core.ext.linker.impl.SelectionScriptLinker;
2525
import com.google.gwt.dev.About;
26-
import com.google.gwt.dev.js.JsToStringGenerationVisitor;
2726
import com.google.gwt.dev.util.DefaultTextOutput;
27+
import com.google.gwt.util.tools.shared.StringUtils;
2828

2929
/**
3030
* Generates a cross-site compatible bootstrap sequence.
@@ -88,7 +88,7 @@ protected String wrapDeferredFragment(TreeLogger logger,
8888
return String.format("%s.runAsyncCallback%d(%s)\n",
8989
context.getModuleFunctionName(),
9090
fragment,
91-
JsToStringGenerationVisitor.javaScriptString(js));
91+
StringUtils.javaScriptString(js));
9292
}
9393

9494
private String getModulePrefix(LinkerContext context, String strongName,

dev/core/src/com/google/gwt/dev/js/JsToStringGenerationVisitor.java

Lines changed: 2 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import com.google.gwt.dev.js.ast.JsWhile;
7474
import com.google.gwt.dev.util.TextOutput;
7575
import com.google.gwt.dev.util.collect.HashSet;
76+
import com.google.gwt.util.tools.shared.StringUtils;
7677

7778
import java.util.ArrayList;
7879
import java.util.Iterator;
@@ -109,10 +110,6 @@ public class JsToStringGenerationVisitor extends JsVisitor {
109110
private static final char[] CHARS_TRY = "try".toCharArray();
110111
private static final char[] CHARS_VAR = "var".toCharArray();
111112
private static final char[] CHARS_WHILE = "while".toCharArray();
112-
private static final char[] HEX_DIGITS = {
113-
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
114-
'E', 'F'};
115-
116113
/**
117114
* How many lines of code to print inside of a JsBlock when printing terse.
118115
*/
@@ -126,135 +123,6 @@ public class JsToStringGenerationVisitor extends JsVisitor {
126123
*/
127124
private static final Pattern VALID_NAME_PATTERN = Pattern.compile("[a-zA-Z_$][\\w$]*");
128125

129-
/**
130-
* Generate JavaScript code that evaluates to the supplied string. Adapted
131-
* from {@link com.google.gwt.dev.js.rhino.ScriptRuntime#escapeString(String)}
132-
* . The difference is that we quote with either &quot; or &apos; depending on
133-
* which one is used less inside the string.
134-
*/
135-
public static String javaScriptString(String value) {
136-
char[] chars = value.toCharArray();
137-
final int n = chars.length;
138-
int quoteCount = 0;
139-
int aposCount = 0;
140-
for (int i = 0; i < n; ++i) {
141-
switch (chars[i]) {
142-
case '"':
143-
++quoteCount;
144-
break;
145-
case '\'':
146-
++aposCount;
147-
break;
148-
}
149-
}
150-
151-
StringBuffer result = new StringBuffer(value.length() + 16);
152-
153-
char quoteChar = (quoteCount < aposCount) ? '"' : '\'';
154-
result.append(quoteChar);
155-
156-
for (int i = 0; i < n; ++i) {
157-
char c = chars[i];
158-
159-
if (' ' <= c && c <= '~' && c != quoteChar && c != '\\') {
160-
// an ordinary print character (like C isprint())
161-
result.append(c);
162-
continue;
163-
}
164-
165-
int escape = -1;
166-
switch (c) {
167-
case '\b':
168-
escape = 'b';
169-
break;
170-
case '\f':
171-
escape = 'f';
172-
break;
173-
case '\n':
174-
escape = 'n';
175-
break;
176-
case '\r':
177-
escape = 'r';
178-
break;
179-
case '\t':
180-
escape = 't';
181-
break;
182-
case '"':
183-
escape = '"';
184-
break; // only reach here if == quoteChar
185-
case '\'':
186-
escape = '\'';
187-
break; // only reach here if == quoteChar
188-
case '\\':
189-
escape = '\\';
190-
break;
191-
}
192-
193-
if (escape >= 0) {
194-
// an \escaped sort of character
195-
result.append('\\');
196-
result.append((char) escape);
197-
} else {
198-
/*
199-
* Emit characters from 0 to 31 that don't have a single character
200-
* escape sequence in octal where possible. This saves one or two
201-
* characters compared to the hexadecimal format '\xXX'.
202-
*
203-
* These short octal sequences may only be used at the end of the string
204-
* or where the following character is a non-digit. Otherwise, the
205-
* following character would be incorrectly interpreted as belonging to
206-
* the sequence.
207-
*/
208-
if (c < ' ' && (i == n - 1 || chars[i + 1] < '0' || chars[i + 1] > '9')) {
209-
result.append('\\');
210-
if (c > 0x7) {
211-
result.append((char) ('0' + (0x7 & (c >> 3))));
212-
}
213-
result.append((char) ('0' + (0x7 & c)));
214-
} else {
215-
int hexSize;
216-
if (c < 256) {
217-
// 2-digit hex
218-
result.append("\\x");
219-
hexSize = 2;
220-
} else {
221-
// Unicode.
222-
result.append("\\u");
223-
hexSize = 4;
224-
}
225-
// append hexadecimal form of ch left-padded with 0
226-
for (int shift = (hexSize - 1) * 4; shift >= 0; shift -= 4) {
227-
int digit = 0xf & (c >> shift);
228-
result.append(HEX_DIGITS[digit]);
229-
}
230-
}
231-
}
232-
}
233-
result.append(quoteChar);
234-
escapeClosingTags(result);
235-
String resultString = result.toString();
236-
return resultString;
237-
}
238-
239-
/**
240-
* Escapes any closing XML tags embedded in <code>str</code>, which could
241-
* potentially cause a parse failure in a browser, for example, embedding a
242-
* closing <code>&lt;script&gt;</code> tag.
243-
*
244-
* @param str an unescaped literal; May be null
245-
*/
246-
private static void escapeClosingTags(StringBuffer str) {
247-
if (str == null) {
248-
return;
249-
}
250-
251-
int index = 0;
252-
253-
while ((index = str.indexOf("</", index)) != -1) {
254-
str.insert(index + 1, '\\');
255-
}
256-
}
257-
258126
protected boolean needSemi = true;
259127
/**
260128
* "Global" blocks are either the global block of a fragment, or a block
@@ -1415,7 +1283,7 @@ private void outdent() {
14151283
}
14161284

14171285
private void printStringLiteral(String value) {
1418-
String resultString = javaScriptString(value);
1286+
String resultString = StringUtils.javaScriptString(value);
14191287
p.print(resultString);
14201288
}
14211289
}

dev/core/src/com/google/gwt/util/tools/shared/StringUtils.java

Lines changed: 134 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2006 Google Inc.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
66
* the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
1212
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -21,8 +21,7 @@
2121
public class StringUtils {
2222

2323
public static char[] HEX_CHARS = new char[] {
24-
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
25-
'E', 'F'};
24+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
2625

2726
/**
2827
* A 4-digit hex result.
@@ -34,10 +33,120 @@ public static void hex4(char c, StringBuffer sb) {
3433
sb.append(HEX_CHARS[c & 0x000F]);
3534
}
3635

36+
/**
37+
* Generate JavaScript code that evaluates to the supplied string. Adapted
38+
* from {@link com.google.gwt.dev.js.rhino.ScriptRuntime#escapeString(String)}
39+
* . The difference is that we quote with either &quot; or &apos; depending on
40+
* which one is used less inside the string.
41+
*/
42+
public static String javaScriptString(String value) {
43+
char[] chars = value.toCharArray();
44+
final int n = chars.length;
45+
int quoteCount = 0;
46+
int aposCount = 0;
47+
for (int i = 0; i < n; ++i) {
48+
switch (chars[i]) {
49+
case '"':
50+
++quoteCount;
51+
break;
52+
case '\'':
53+
++aposCount;
54+
break;
55+
}
56+
}
57+
58+
StringBuffer result = new StringBuffer(value.length() + 16);
59+
60+
char quoteChar = (quoteCount < aposCount) ? '"' : '\'';
61+
result.append(quoteChar);
62+
63+
for (int i = 0; i < n; ++i) {
64+
char c = chars[i];
65+
66+
if (' ' <= c && c <= '~' && c != quoteChar && c != '\\') {
67+
// an ordinary print character (like C isprint())
68+
result.append(c);
69+
continue;
70+
}
71+
72+
int escape = -1;
73+
switch (c) {
74+
case '\b':
75+
escape = 'b';
76+
break;
77+
case '\f':
78+
escape = 'f';
79+
break;
80+
case '\n':
81+
escape = 'n';
82+
break;
83+
case '\r':
84+
escape = 'r';
85+
break;
86+
case '\t':
87+
escape = 't';
88+
break;
89+
case '"':
90+
escape = '"';
91+
break; // only reach here if == quoteChar
92+
case '\'':
93+
escape = '\'';
94+
break; // only reach here if == quoteChar
95+
case '\\':
96+
escape = '\\';
97+
break;
98+
}
99+
100+
if (escape >= 0) {
101+
// an \escaped sort of character
102+
result.append('\\');
103+
result.append((char) escape);
104+
} else {
105+
/*
106+
* Emit characters from 0 to 31 that don't have a single character
107+
* escape sequence in octal where possible. This saves one or two
108+
* characters compared to the hexadecimal format '\xXX'.
109+
*
110+
* These short octal sequences may only be used at the end of the string
111+
* or where the following character is a non-digit. Otherwise, the
112+
* following character would be incorrectly interpreted as belonging to
113+
* the sequence.
114+
*/
115+
if (c < ' ' && (i == n - 1 || chars[i + 1] < '0' || chars[i + 1] > '9')) {
116+
result.append('\\');
117+
if (c > 0x7) {
118+
result.append((char) ('0' + (0x7 & (c >> 3))));
119+
}
120+
result.append((char) ('0' + (0x7 & c)));
121+
} else {
122+
int hexSize;
123+
if (c < 256) {
124+
// 2-digit hex
125+
result.append("\\x");
126+
hexSize = 2;
127+
} else {
128+
// Unicode.
129+
result.append("\\u");
130+
hexSize = 4;
131+
}
132+
// append hexadecimal form of ch left-padded with 0
133+
for (int shift = (hexSize - 1) * 4; shift >= 0; shift -= 4) {
134+
int digit = 0xf & (c >> shift);
135+
result.append(HEX_CHARS[digit]);
136+
}
137+
}
138+
}
139+
}
140+
result.append(quoteChar);
141+
StringUtils.escapeClosingTags(result);
142+
String resultString = result.toString();
143+
return resultString;
144+
}
145+
37146
/**
38147
* Returns a string representation of the byte array as a series of
39148
* hexadecimal characters.
40-
*
149+
*
41150
* @param bytes byte array to convert
42151
* @return a string representation of the byte array as a series of
43152
* hexadecimal characters
@@ -51,4 +160,23 @@ public static String toHexString(byte[] bytes) {
51160
}
52161
return new String(hexString);
53162
}
163+
164+
/**
165+
* Escapes any closing XML tags embedded in <code>str</code>, which could
166+
* potentially cause a parse failure in a browser, for example, embedding a
167+
* closing <code>&lt;script&gt;</code> tag.
168+
*
169+
* @param str an unescaped literal; May be null
170+
*/
171+
private static void escapeClosingTags(StringBuffer str) {
172+
if (str == null) {
173+
return;
174+
}
175+
176+
int index = 0;
177+
178+
while ((index = str.indexOf("</", index)) != -1) {
179+
str.insert(index + 1, '\\');
180+
}
181+
}
54182
}

0 commit comments

Comments
 (0)