Open Bug 2087773 Opened 24 days ago Updated 21 days ago

Line numbers in errors from inline event handler attributes are relative to the attribute, not the document

Categories

(Core :: DOM: Core & HTML, defect)

defect

Tracking

()

People

(Reporter: jesup, Unassigned)

References

Details

Split out of bug 11240, which is otherwise fixed: errors from inline event handlers now report the document URL as the filename.

The line number is still relative to the attribute value, not the document. Testcase:

<!DOCTYPE html>
<html><body>
<p>



</p>
<button id="b" onclick="
  var x = 1;
  throw new Error('inline boom');">go</button>
</body></html>

The throw is on line 10 of the file. Clicking the button reports:

JavaScript error: file:///.../t11240.html, line 3: Error: inline boom

Line 3 is the position within the attribute text. On a large page the report cannot be used to locate the handler.

EventListenerManager::SetEventHandler compiles the handler with setFileAndLine(url, 0), so line 1 is the first line of the attribute value.

nsHtml5Tokenizer already tracks attributeLine and passes it to nsHtml5HtmlAttributes::addAttribute, but that function ignores its aLine argument, so the line never reaches the DOM. A fix would need to:

  1. Keep the line in nsHtml5AttributeEntry.
  2. Carry it to the element for on* attributes through a tree operation.
  3. Store it per element.
  4. Offset the compile line in SetEventHandler by it.

Caveats: attributeLine is where the attribute name starts, not where the value starts, so multi-line values need the tokenizer to record the value start line. This would only cover parser-created elements; handlers set via setAttribute, innerHTML or fragment parsing have no document position.

I tested the testcase with the throw on line 13 of a multi-line onclick, plus a single-line onclick on line 14, and read lineno from the window error event (error.stack agrees):

  • Firefox Nightly: 3 and 1 (relative to the attribute value)
  • Chrome Canary: 15 and 14 (the multi-line case seems to count from the end of the start tag)
  • Safari TP: 13 and 14

For handlers set from script with setAttribute() or innerHTML, Chrome and Safari TP add the parser's current line (the line of the </script> that was running), while Firefox stays relative to the value.

The spec says the handler's location is "the location where the script body originated, as given by eventHandler's value" (https://html.spec.whatwg.org/#getting-the-current-value-of-the-event-handler), but only uses it for reporting a SyntaxError, so it doesn't say what line numbers to report.

(AI-assisted triage, with review.)

Severity: -- → S3
You need to log in before you can comment on or make changes to this bug.