Skip to content

Commit

Permalink
✨Pass doc into validator ui using url (#20391)
Browse files Browse the repository at this point in the history
* read param

* use fragment

* set default to amp
  • Loading branch information
calebcordry authored Jan 30, 2019
1 parent 4f9c478 commit 6edaef0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
10 changes: 9 additions & 1 deletion validator/webui/@polymer/webui-mainpage/webui-mainpage.html
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,17 @@

// Interprets the parameters after the '#' in the URL.
var params = getLocationHashParams();
// Extracts the code to be validated if sent as a query param.
const incomingDocStr = getIncomingDoc(params);
if (incomingDocStr) {
editor.setEditorValue(incomingDocStr);
const format = params['htmlFormat'] || 'AMP';
urlForm.setHtmlFormat(format)
}

// If #url=<url> was provided, load it, otherwise, start with the
// minimum valid AMP document.
if (params.hasOwnProperty('url') && params['url']) {
else if (params.hasOwnProperty('url') && params['url']) {
this.$.urlForm.setURLAndValidate(params['url']);
// If filter=<error_category> was provided, filter error-list.
if (params.hasOwnProperty('filter') && params['filter']) {
Expand Down
2 changes: 1 addition & 1 deletion validator/webui/serve-standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
//
// Handle '/'.
//
if r.RequestURI == "/" {
if r.URL.Path == "/" {
bytes, err := ioutil.ReadFile("index.html")
if err != nil {
http.Error(w, "File not found.", http.StatusNotFound)
Expand Down
16 changes: 16 additions & 0 deletions validator/webui/webui.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the license.
*/

const DOC_INPUT_ATTR = 'doc';

// Extracts a dictionary of parameters from window.location.hash.
function getLocationHashParams() {
const paramStrings = window.location.hash.substr(1).split('&');
Expand Down Expand Up @@ -46,3 +48,17 @@ function setLocationHashParams(params) {
}
window.location.hash = out.join('&');
}

// Base64 encoded ascii to ucs-2 string.
function atou(str) {
return decodeURIComponent(escape(atob(str)));
}

// Get query param that may contain an encoded document to be validated. Decode
// and return.
function getIncomingDoc(params) {
if (!params || !params[DOC_INPUT_ATTR]) {
return null;
}
return atou(params[DOC_INPUT_ATTR]);
}

0 comments on commit 6edaef0

Please sign in to comment.