Skip to content

Commit 2033623

Browse files
committed
Several small fixes in demo page:
* Rewrite stringifyObjectKey in a sane way * Don't call outputUpdated twice on page load * Introduce `#css_is_too_big` to prevent browsers from handing
1 parent 75c8d58 commit 2033623

1 file changed

Lines changed: 19 additions & 25 deletions

File tree

docs/parse.js

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,9 @@ function buildPath(level) {
4949
* @return {string}
5050
*/
5151
function stringifyObjectKey(key) {
52-
return /^[a-zA-Z_$][A-Za-z0-9_$]*$/.test(key) ?
52+
return /^[a-z0-9_$]+$/i.test(key) ?
5353
key :
54-
'"' + escapeDoubleQuotes(key) + '"';
55-
}
56-
57-
58-
/**
59-
* @param {string} string
60-
* @return {string}
61-
* @see http://stackoverflow.com/questions/7382115/escape-quotes-in-a-string-with-backslash
62-
*/
63-
function escapeDoubleQuotes(string) {
64-
return string.replace(/(\\*)(")/g, function(all, backslashes, quote) {
65-
return backslashes.length % 2 ?
66-
all :
67-
backslashes + '\\' + quote;
68-
});
54+
JSON.stringify(key);
6955
}
7056

7157

@@ -106,7 +92,6 @@ function inspect(object, depth, stack) {
10692
}
10793

10894

109-
11095
var errors = [];
11196
if (!("__defineGetter__" in {})) {
11297
errors.push("Object.prototype.__defineGetter__ isn’t supported");
@@ -121,39 +106,48 @@ var style = document.getElementById("style");
121106
var output = document.getElementById("output");
122107
var serialized = document.getElementById("serialized");
123108

124-
function outputUpdated(){
109+
function outputUpdated() {
125110
var value = style.value;
126111
if (value != style.prevValue) {
112+
style.prevValue = value;
127113
var css = CSSOM.parse(value);
128114
output.innerHTML = inspect(css);
129115
serialized.innerHTML = css.toString();
130-
style.prevValue = value;
131116
}
132117
}
133118

134-
function hashChanged(){
119+
/**
120+
* @return {boolean} update happend or not
121+
*/
122+
function hashChanged() {
135123
var hash = location.hash;
136124
var splitted = hash.split("=");
137125
if (splitted.length < 2) {
138-
return;
126+
return false;
139127
}
140128
var name = splitted[0];
141129
var value = splitted[1];
142130
if (name == "#css") {
143131
style.value = decodeURIComponent(value);
144132
outputUpdated();
133+
return true;
145134
}
135+
return false;
146136
}
147137

148138
window.onload = function() {
149-
hashChanged();
150-
outputUpdated();
139+
hashChanged() || outputUpdated();
151140
};
152141

153142
window.onhashchange = hashChanged;
154143
style.onkeyup = style.onpaste = function changed(){
155144
outputUpdated();
156145
};
157-
style.onchange = function updateLocation(){
158-
location.hash = "css=" + encodeURIComponent(style.value);
146+
style.onchange = function updateLocation() {
147+
if (style.value.length < 1024) {
148+
location.hash = "css=" + encodeURIComponent(style.value);
149+
} else {
150+
// Huge location.hash slows down the browser :(
151+
location.hash = 'css_is_too_big';
152+
}
159153
};

0 commit comments

Comments
 (0)