* Fix Issue #588. Only highlight runtime errors at the appropriate line for file in which the error occurred. * Modifications to handle js in html <script> tag Part 1 * Correctly highlight errors in JavaScript in a <script> tag
This commit is contained in:
parent
aa318e0d2d
commit
68c1e48c99
2 changed files with 26 additions and 7 deletions
|
@ -188,7 +188,7 @@ class Editor extends React.Component {
|
|||
for (let i = 0; i < this._cm.lineCount(); i += 1) {
|
||||
this._cm.removeLineClass(i, 'background', 'line-runtime-error');
|
||||
}
|
||||
if (this.props.runtimeErrorWarningVisible && this._cm.getDoc().modeOption === 'javascript') {
|
||||
if (this.props.runtimeErrorWarningVisible) {
|
||||
this.props.consoleEvents.forEach((consoleEvent) => {
|
||||
if (consoleEvent.method === 'error') {
|
||||
if (consoleEvent.data &&
|
||||
|
@ -197,7 +197,11 @@ class Editor extends React.Component {
|
|||
consoleEvent.data[0].indexOf(')') > -1) {
|
||||
const n = consoleEvent.data[0].replace(')', '').split(' ');
|
||||
const lineNumber = parseInt(n[n.length - 1], 10) - 1;
|
||||
if (!Number.isNaN(lineNumber)) {
|
||||
const { source } = consoleEvent;
|
||||
const fileName = this.props.file.name;
|
||||
const errorFromJavaScriptFile = (`${source}.js` === fileName);
|
||||
const errorFromIndexHTML = ((source === fileName) && (fileName === 'index.html'));
|
||||
if (!Number.isNaN(lineNumber) && (errorFromJavaScriptFile || errorFromIndexHTML)) {
|
||||
this._cm.addLineClass(lineNumber, 'background', 'line-runtime-error');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,21 +46,36 @@ export const startTag = '@fs-';
|
|||
|
||||
export const getAllScriptOffsets = (htmlFile) => {
|
||||
const offs = [];
|
||||
let found = true;
|
||||
const hijackConsoleErrorsScriptLength = 36;
|
||||
const embeddedJSStart = 'script crossorigin=""';
|
||||
let foundJSScript = true;
|
||||
let foundEmbeddedJS = true;
|
||||
let lastInd = 0;
|
||||
let ind = 0;
|
||||
let endFilenameInd = 0;
|
||||
let filename = '';
|
||||
let lineOffset = 0;
|
||||
while (found) {
|
||||
while (foundJSScript) {
|
||||
ind = htmlFile.indexOf(startTag, lastInd);
|
||||
if (ind === -1) {
|
||||
found = false;
|
||||
foundJSScript = false;
|
||||
} else {
|
||||
endFilenameInd = htmlFile.indexOf('.js', ind + startTag.length + 3);
|
||||
filename = htmlFile.substring(ind + startTag.length, endFilenameInd);
|
||||
// the length of hijackConsoleErrorsScript is 36 lines
|
||||
lineOffset = htmlFile.substring(0, ind).split('\n').length + 36;
|
||||
lineOffset = htmlFile.substring(0, ind).split('\n').length + hijackConsoleErrorsScriptLength;
|
||||
offs.push([lineOffset, filename]);
|
||||
lastInd = ind + 1;
|
||||
}
|
||||
}
|
||||
lastInd = 0;
|
||||
while (foundEmbeddedJS) {
|
||||
ind = htmlFile.indexOf(embeddedJSStart, lastInd);
|
||||
if (ind === -1) {
|
||||
foundEmbeddedJS = false;
|
||||
} else {
|
||||
filename = 'index.html';
|
||||
// not sure where the offset of 25 comes from
|
||||
lineOffset = htmlFile.substring(0, ind).split('\n').length + 25;
|
||||
offs.push([lineOffset, filename]);
|
||||
lastInd = ind + 1;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue