Skip to content

Commit ea11d8a

Browse files
committed
Fix issue with loading HTML from URL.
The code path for loading a demo from the gallery was still being run after we loaded from the URL, clobbering the URL data. The gist codepath only worked because the jsonp request made it so the gallery demo load was then overwritten by the correct data. Now we only parse and load from the demo if we actually need it.
1 parent f260c5f commit ea11d8a

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

Apps/Sandcastle/CesiumSandcastle.js

+32-36
Original file line numberDiff line numberDiff line change
@@ -717,27 +717,15 @@ require({
717717
return requestDemo(demo.name).then(function(value) {
718718
demo.code = value;
719719

720-
var parser = new DOMParser();
721-
var doc = parser.parseFromString(demo.code, 'text/html');
722-
723-
var script = doc.querySelector('script[id="cesium_sandcastle_script"]');
724-
if (!script) {
725-
appendConsole('consoleError', 'Error reading source file: ' + demo.name, true);
726-
return;
727-
}
728-
729-
var scriptMatch = scriptCodeRegex.exec(script.textContent);
730-
if (!scriptMatch) {
731-
appendConsole('consoleError', 'Error reading source file: ' + demo.name, true);
732-
return;
720+
if (typeof demo.bucket === 'string') {
721+
loadBucket(demo.bucket);
733722
}
734723

735-
var scriptCode = scriptMatch[1];
736-
demoCode = scriptCode.replace(/\s/g, '');
737-
738724
function applyLoadedDemo(code, html) {
739725
jsEditor.setValue(code);
726+
jsEditor.clearHistory();
740727
htmlEditor.setValue(html);
728+
htmlEditor.clearHistory();
741729
demoCode = code.replace(/\s/g, '');
742730
demoHtml = html.replace(/\s/g, '');
743731
CodeMirror.commands.runCesium(jsEditor);
@@ -754,9 +742,9 @@ require({
754742
var html = defined(htmlFile) ? htmlFile.content : defaultHtml; // Use the default html for old gists
755743
applyLoadedDemo(code, html);
756744
}).otherwise(function(error) {
757-
appendConsole('consoleError', 'Unable to GET from GitHub API. This could be due to too many request, try again in an hour or copy and paste the code from the gist: https://gist.github.com/' + queryObject.gist , true);
745+
appendConsole('consoleError', 'Unable to GET from GitHub API. This could be due to too many request, try again in an hour or copy and paste the code from the gist: https://gist.github.com/' + queryObject.gist, true);
758746
console.log(error);
759-
});
747+
});
760748
} else if (defined(queryObject.code)) {
761749
//The code query parameter is a Base64 encoded JSON string with `code` and `html` properties.
762750
json = JSON.parse(window.atob(queryObject.code));
@@ -782,26 +770,34 @@ require({
782770

783771
applyLoadedDemo(code, html);
784772
} else {
785-
jsEditor.setValue(scriptCode);
786-
}
787-
jsEditor.clearHistory();
788-
789-
var htmlText = '';
790-
var childIndex = 0;
791-
var childNode = doc.body.childNodes[childIndex];
792-
while (childIndex < doc.body.childNodes.length && childNode !== script) {
793-
htmlText += childNode.nodeType === 1 ? childNode.outerHTML : childNode.nodeValue;
794-
childNode = doc.body.childNodes[++childIndex];
795-
}
796-
htmlText = htmlText.replace(/^\s+/, '');
797-
demoHtml = htmlText.replace(/\s/g, '');
798-
htmlEditor.setValue(htmlText);
799-
htmlEditor.clearHistory();
773+
var parser = new DOMParser();
774+
var doc = parser.parseFromString(demo.code, 'text/html');
800775

801-
if (typeof demo.bucket === 'string') {
802-
loadBucket(demo.bucket);
776+
var script = doc.querySelector('script[id="cesium_sandcastle_script"]');
777+
if (!script) {
778+
appendConsole('consoleError', 'Error reading source file: ' + demo.name, true);
779+
return;
780+
}
781+
782+
var scriptMatch = scriptCodeRegex.exec(script.textContent);
783+
if (!scriptMatch) {
784+
appendConsole('consoleError', 'Error reading source file: ' + demo.name, true);
785+
return;
786+
}
787+
788+
var scriptCode = scriptMatch[1];
789+
790+
var htmlText = '';
791+
var childIndex = 0;
792+
var childNode = doc.body.childNodes[childIndex];
793+
while (childIndex < doc.body.childNodes.length && childNode !== script) {
794+
htmlText += childNode.nodeType === 1 ? childNode.outerHTML : childNode.nodeValue;
795+
childNode = doc.body.childNodes[++childIndex];
796+
}
797+
htmlText = htmlText.replace(/^\s+/, '');
798+
799+
applyLoadedDemo(scriptCode, htmlText);
803800
}
804-
CodeMirror.commands.runCesium(jsEditor);
805801
});
806802
}
807803

0 commit comments

Comments
 (0)