Skip to content
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

JavaScript validation API - get validation result #1967

Closed
dandv opened this issue Feb 12, 2016 · 9 comments
Closed

JavaScript validation API - get validation result #1967

dandv opened this issue Feb 12, 2016 · 9 comments

Comments

@dandv
Copy link
Contributor

dandv commented Feb 12, 2016

Is there a way to get the result of the validation when the page is loaded with #development=1?

I've poked around the amp object but all properties and functions seem to be or return null. Even this:

var x = amp.validator.validateUrlAndLog(window.location.href)

returns null when run at http://carnaval.uol.com.br/2016/album/2016/02/05/bloco-das-carmelitas-desfila-pelas-ruas-de-santa-teresa-no-rio-de-janeiro.htm?amp#development=1, before errors are output to the console. amp.validator.errors and amp.validator.ValidationResult() are null before and after calling validateUrlAndLog.

Usable return values would allow headless browsers to extract the validation result into a variable, making automation in-situ possible, without needing to use #937.

@cramforce
Copy link
Member

@Gregable Are you comfortable with declaring the existing API public or should we expose something from AMP?

@Gregable
Copy link
Member

Yes. I commented on this here: #999 (comment)
We should clean this up some and make some documentation on it.

You can also build the validator from github. There are already instructions here: https://github.com/ampproject/amphtml/tree/master/validator

@dandv
Copy link
Contributor Author

dandv commented Feb 17, 2016

@Gregable: thanks for the fiddle in #999. So to validate the current document, one would inject this:

result = amp.validator.validateString(document.head.innerHTML + document.body.innerHTML)

The problem is I see vastly different results between the console message and the result of the code above. For example, these URLs display AMP validation successful. in the console, but amp.validator.validateString(document.head.innerHTML + document.body.innerHTML) shows a FAIL status with 50+ .errors:

Similarly, when the console shows "AMP validation had errors", amp.validator.validateString(document.head.innerHTML + document.body.innerHTML).errors had far more errors than those dumped to the console - e.g. 130 vs. 2 at http://www.lastampa.it/2016/02/05/esteri/il-corpo-di-regeni-domani-a-roma-per-lautopsia-bUQmA4aeAuLXyyVsI1NA8O/amphtml/pagina.amp.html#development=1

@Gregable
Copy link
Member

If you are using:

amp.validator.validateString(document.head.innerHTML + document.body.innerHTML)

On a document like:

<html>
<head>
  <title>hello world</title>
</head>
<body>
  body content
</body>
</html>

Then the string being evaluated will be roughly:

<title>hello world</title>
body content

Which is missing significant parts of the document, so the validator will emit more errors.

@cramforce
Copy link
Member

I'd recommend doing an XHR to the doc (and get the direct string) to
validate it.

On Tue, Feb 16, 2016 at 10:15 PM, Greg Grothaus notifications@github.com
wrote:

If you are using:

amp.validator.validateString(document.head.innerHTML + document.body.innerHTML)

On a document like:

<title>hello world</title> body content

Then the string being evaluated will be roughly:

<title>hello world</title> body content

Which is missing significant parts of the document, so the validator will
emit more errors.


Reply to this email directly or view it on GitHub
#1967 (comment)
.

@dandv
Copy link
Contributor Author

dandv commented Feb 17, 2016

@Gregable: Duh, silly me.

@cramforce: I think this is faster and as complete:

document.documentElement.outerHTML

Does the document.doctype matter?

@cramforce
Copy link
Member

@dandv The problem is that the mechanism doesn't catch stuff like

<script>
document.write('whatever');
document.body.removeChild(document.currentScript);
</script>

which is invalid AMP but might pass your validator.

@rudygalfi rudygalfi modified the milestones: M1, Up Next Mar 4, 2016
@honeybadgerdontcare
Copy link
Contributor

honeybadgerdontcare commented May 18, 2016

Here's a code snippet that would allow one to get the result for validation using XHR. One can also get the specific warnings/errors from the result as well via result.errors.

var xhr = new XMLHttpRequest();
xhr.open('GET', document.location, true);
xhr.onreadystatechange = validationResult;
xhr.send();
function validationResult() {
  if (xhr.readyState === 4) {
    result = amp.validator.validateString(xhr.responseText);
    console.log(result.status);
  }
};

@Gregable
Copy link
Member

I think the solutions here are probably sufficient. We now also have http://npmjs.org/amphtml-validator and https://validator.ampproject.org/ which are probably what most people will use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants