Skip to content

Core: Report browser errors in parseXML #4816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 8, 2020
Merged

Conversation

mgol
Copy link
Member

@mgol mgol commented Dec 1, 2020

Summary

Fixes gh-4784

In IE 11 we'd just throw an empty error now, in others - the reported error. We wouldn't include input data in the thrown error anymore.

+10 bytes, although I cheated a bit - I also removed an assignment of xml to undefined in catch as any error would happen before the assignment so the variable would already hold the undefined value. Without this optimization, it'd be +13 bytes.

That +10 can be changed to -3 if we just call:

jQuery.error( parserErrorElem && parserErrorElem.textContent )

but that has two issues. Here's the returned element parsererror (after pretty-printing) in Blink & WebKit:

<parsererror
	xmlns="http://www.w3.org/1999/xhtml"
	style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black">
	<h3>This page contains the following errors:</h3>
	<div style="font-family:monospace;font-size:12px">error on line 14 at column 9: Opening and ending tag mismatch: th line 0 and tr
</div>
	<h3>Below is a rendering of the page up to the first error.</h3>
</parsererror>

and here's the one in Gecko:

<parsererror xmlns="http://www.mozilla.org/newlayout/xml/parsererror.xml">
	XML Parsing Error: mismatched tag. Expected: &lt;/th&gt;.
Location: http://sandbox.l/test.xhtml
Line Number 14, Column 6:
	<sourcetext>			&lt;/tr&gt;\n--------------------------^</sourcetext>
</parsererror>

If we just print textContent, in Chrome the text will miss spaces in some places:

This page contains the following errors:error on line 14 at column 9: Opening and ending tag mismatch: th line 0 and tr
Below is a rendering of the page up to the first error.

That may not be enough a reason to add bytes but here's what ends up in Firefox:

XML Parsing Error: mismatched tag. Expected: </th>.
Location: http://sandbox.l/test.xhtml
Line Number 14, Column 6:			</tr>
--------------------------^

Firefox prints ASCII art which is misaligned now. With the code from this PR, here's what gets printed:

XML Parsing Error: mismatched tag. Expected: </th>.
Location: http://sandbox.l/test.html
Line Number 14, Column 6:
			</tr>
--------------------------^

On the other hand, it actually uses tabs to pad </tr> and the alignment depends on tab being rendered as 8 characters... So maybe we can give up on that.

EDIT: Actually, this will mostly be printed in the DevTools of the respective browser, here: Firefox. So it should show up correctly in the console.

Let's make a decision at the next meeting.

Checklist

Sorry, something went wrong.

@mgol mgol self-assigned this Dec 1, 2020
@mgol mgol added this to the 3.6.0 milestone Dec 1, 2020
@mgol mgol added Core Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. Needs review labels Dec 1, 2020
Copy link
Member

@dmethvin dmethvin left a comment

Choose a reason for hiding this comment

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

I wonder if we should preserve the old Invalid XML: prefix for compat reasons and just check for that in the unit test? We don't control the browser's reporting of XML errors so we can't ensure it will show any specific error message. What do you think? In either case it is an improvement!

@mgol
Copy link
Member Author

mgol commented Dec 2, 2020

@dmethvin

I wonder if we should preserve the old Invalid XML: prefix for compat reasons and just check for that in the unit test? We don't control the browser's reporting of XML errors so we can't ensure it will show any specific error message. What do you think? In either case it is an improvement!

I'm not sure either way. Judging by the messages I cited in the PR description, the Chrome error kind of misses the context so the Invalid XML: prefix would help. If we don't include user data in the error, though, in IE the message just ends like that with nothing appearing after the colon. Should we preserve that for IE? @timmywil was against but I don't see the risk - we're sure data is a string at this point and throwing an error is safe with any provided string.

@mgol
Copy link
Member Author

mgol commented Dec 2, 2020

I updated the PR with proposed changes; unfortunately, now it's at +25 bytes.

@mgol mgol removed Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. Needs review labels Dec 7, 2020
@mgol
Copy link
Member Author

mgol commented Dec 7, 2020

@dmethvin does the current version look good to you?

@mgol mgol requested a review from dmethvin December 7, 2020 17:33
Copy link
Member

@dmethvin dmethvin left a comment

Choose a reason for hiding this comment

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

LGTM!

@mgol mgol merged commit 8969732 into jquery:master Dec 8, 2020
@mgol mgol deleted the parseXML-error-4784 branch December 8, 2020 10:22
mgol added a commit that referenced this pull request Dec 8, 2020
Fixes gh-4784
Closes gh-4816

(cherry picked from commit 8969732)
@mgol
Copy link
Member Author

mgol commented Dec 8, 2020

Landed on:

mgol added a commit to mgol/jquery that referenced this pull request Dec 8, 2020
Legacy Edge, similarly to IE, doesn't report XML parsing errors but just tries
to render the invalid document. Skip the error reporting test there, Edge Legacy
will return a generic "Invalid XML" error, just like IE.

Ref jquerygh-4816
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

jQuery.parseXML should add detailed information about parse errors
4 participants