Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Fix #3372 check for valid server #3392

Merged
merged 5 commits into from Apr 10, 2013
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/LiveDevelopment/Documents/HTMLDocument.js
Expand Up @@ -61,17 +61,15 @@ define(function HTMLDocumentModule(require, exports, module) {
return;
}
this.editor = editor;
HTMLInstrumentation._markText(this.editor);
this.onCursorActivity = this.onCursorActivity.bind(this);
$(this.editor).on("cursorActivity", this.onCursorActivity);
this.onCursorActivity();

this.onCursorActivity = this.onCursorActivity.bind(this);
this.onDocumentSaved = this.onDocumentSaved.bind(this);

$(this.editor).on("cursorActivity", this.onCursorActivity);
$(DocumentManager).on("documentSaved", this.onDocumentSaved);

// Experimental code
if (LiveDevelopment.config.experimental) {

// Used by highlight agent to highlight editor text as selected in browser
this.onHighlight = this.onHighlight.bind(this);
$(HighlightAgent).on("highlight", this.onHighlight);
Expand All @@ -80,6 +78,34 @@ define(function HTMLDocumentModule(require, exports, module) {
$(this.editor).on("change", this.onChange);
}
};

/**
* Enable instrumented HTML
* @param enabled {boolean}
*/
HTMLDocument.prototype.setInstrumentationEnabled = function setInstrumentationEnabled(enabled) {
if (enabled && !this._instrumentationEnabled) {
HTMLInstrumentation.scanDocument(this.doc);
HTMLInstrumentation._markText(this.editor);
}

this._instrumentationEnabled = enabled;
};

HTMLDocument.prototype._instrumentationEnabled = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTMLDocument.prototype._instrumentationEnabled is not used. This works because this._instrumentationEnabled is initially undefined, which is falsy, but you should instead initialize this._instrumentationEnabled in the constructor.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


/**
* Returns a JSON object with HTTP response overrides
* @returns {{body: string}}
*/
HTMLDocument.prototype.getResponseData = function getResponseData(enabled) {
var body = (this._instrumentationEnabled) ?
HTMLInstrumentation.generateInstrumentedHTML(this.doc) : this.doc.getText();

return {
body: body
};
};

/** Close the document */
HTMLDocument.prototype.close = function close() {
Expand All @@ -92,7 +118,6 @@ define(function HTMLDocumentModule(require, exports, module) {

// Experimental code
if (LiveDevelopment.config.experimental) {

$(HighlightAgent).off("highlight", this.onHighlight);
this.onHighlight();

Expand Down
89 changes: 48 additions & 41 deletions src/LiveDevelopment/LiveDevelopment.js
Expand Up @@ -296,6 +296,12 @@ define(function LiveDevelopment(require, exports, module) {
_liveDocument.close();
_liveDocument = undefined;
}

if (_serverProvider) {
// Remove any "request" listeners that were added previously
$(_serverProvider).off(".livedev");
}

if (_relatedDocuments) {
_relatedDocuments.forEach(function (liveDoc) {
liveDoc.close();
Expand All @@ -315,13 +321,42 @@ define(function LiveDevelopment(require, exports, module) {
}
}

/** Open a live document
/**
* @private
* Open a live document
* @param {Document} source document to open
* @return {jQuery.Promise} A promise that is resolved once the live
* document is open, and is never explicitly rejected.
*/
function _openDocument(doc, editor) {
_closeDocument();
_liveDocument = _createDocument(doc, editor);

// Enable instrumentation
if (_liveDocument.setInstrumentationEnabled) {
var enableInstrumentation = false;

if (_serverProvider && _serverProvider.setRequestFilterPaths) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Primary fix for #3372 to check for setRequestFilterPaths.

enableInstrumentation = true;

_serverProvider.setRequestFilterPaths(
["/" + encodeURI(ProjectManager.makeProjectRelativeIfPossible(doc.file.fullPath))]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug fix for paths like /samples/root/Getting Started/index.html. Without encoding, the location.pathname does not match.

);

// Send custom HTTP response for the current live document
$(_serverProvider).on("request.livedev", function (event, request) {
var response = _liveDocument.getResponseData ? _liveDocument.getResponseData() : null;
request.send(response);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response can be null in which case the StaticServerDomain reverts to simple file serving.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a good comment to put in the code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

});
}

_liveDocument.setInstrumentationEnabled(enableInstrumentation);
}
}

/**
* @private
* Populate array of related documents reported by the browser agent(s)
*/
function _getRelatedDocuments() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate related documents from the current document (_liveDocument). This allows us to create the HTMLDocument earlier, before the agents load. Computing the related documents requires the root HTML file and it's agents to load.

function createLiveStylesheet(url) {
var stylesheetDeferred = $.Deferred();

Expand All @@ -348,9 +383,6 @@ define(function LiveDevelopment(require, exports, module) {
return stylesheetDeferred.promise();
}

_closeDocument();
_liveDocument = _createDocument(doc, editor);

// Gather related CSS documents.
// FUTURE: Gather related JS documents as well.
_relatedDocuments = [];
Expand Down Expand Up @@ -453,13 +485,12 @@ define(function LiveDevelopment(require, exports, module) {
return;
}

var editor = EditorManager.getCurrentFullEditor(),
status = STATUS_ACTIVE;
var status = STATUS_ACTIVE;

// Note: the following promise is never explicitly rejected, so there
// is no failure handler. If _openDocument is changed so that rejection
// is no failure handler. If _getRelatedDocuments is changed so that rejection
// is possible, failure should be managed accordingly.
_openDocument(doc, editor)
_getRelatedDocuments()
.done(function () {
if (doc.isDirty && _classForDocument(doc) !== CSSDocument) {
status = STATUS_OUT_OF_SYNC;
Expand Down Expand Up @@ -572,23 +603,7 @@ define(function LiveDevelopment(require, exports, module) {
function doLaunchAfterServerReady() {
_setStatus(STATUS_CONNECTING);

if (_serverProvider) {
// Install a request filter for the current document. In the future,
// we need to install filters for *all* files that need to be instrumented.
HTMLInstrumentation.scanDocument(doc);
_serverProvider.setRequestFilterPaths(
["/" + ProjectManager.makeProjectRelativeIfPossible(doc.file.fullPath)]
);

// Remove any "request" listeners that were added previously
$(_serverProvider).off(".livedev");

$(_serverProvider).on("request.livedev", function (event, request) {
var html = HTMLInstrumentation.generateInstrumentedHTML(doc);

request.send({ body: html });
});
}
_openDocument(doc, EditorManager.getCurrentFullEditor());

Inspector.connectToURL(launcherUrl).done(result.resolve).fail(function onConnectFail(err) {
if (err === "CANCEL") {
Expand Down Expand Up @@ -669,7 +684,6 @@ define(function LiveDevelopment(require, exports, module) {

if (!doc || !doc.root) {
showWrongDocError();

} else {
_serverProvider = LiveDevServerManager.getProvider(doc.file.fullPath);

Expand Down Expand Up @@ -841,9 +855,9 @@ define(function LiveDevelopment(require, exports, module) {
if (Inspector.connected()) {
hideHighlight();
if (agents.network && agents.network.wasURLRequested(doc.url)) {
_closeDocument();
var editor = EditorManager.getCurrentFullEditor();
promise = _openDocument(doc, editor);
_openDocument(doc, EditorManager.getCurrentFullEditor());

promise = _getRelatedDocuments();
} else {
if (exports.config.experimental || _isHtmlFileExt(doc.extension)) {
promise = close().done(open);
Expand Down Expand Up @@ -895,15 +909,10 @@ define(function LiveDevelopment(require, exports, module) {

/**
* Determines whether we can serve local file.
*
* @param {String} localPath
* A local path to file being served.
*
* @return {Boolean}
* true for yes, otherwise false.
* @param {String} localPath A local path to file being served.
* @return {Boolean} true for yes, otherwise false.
*/
UserServerProvider.prototype.canServe = function (localPath) {

var baseUrl = ProjectManager.getBaseUrl();
if (!baseUrl) {
return false;
Expand All @@ -918,9 +927,7 @@ define(function LiveDevelopment(require, exports, module) {

/**
* Returns a base url for current project.
*
* @return {String}
* Base url for current project.
* @return {String} Base url for current project.
*/
UserServerProvider.prototype.getBaseUrl = function () {
return ProjectManager.getBaseUrl();
Expand Down
1 change: 1 addition & 0 deletions test/spec/LiveDevelopment-test.js
Expand Up @@ -725,6 +725,7 @@ define(function (require, exports, module) {
instrumentedHtml = HTMLInstrumentationModule.generateInstrumentedHTML(testDocument);
createIdToTagMap(instrumentedHtml);
testHTMLDoc = new HTMLDocumentModule(testDocument, testEditor);
testHTMLDoc.setInstrumentationEnabled(true);
});
});

Expand Down