Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
<title>react-amphtml</title>
{ampScripts.getScriptElements()}
,
);
/* eslint-enable */
const htmlPage = `
${html}
`;
expect(htmlPage).toMatchSnapshot();
const validator = await amphtmlValidator.getInstance();
const result = validator.validateString(htmlPage);
result.errors.forEach(
({ line, col, message, specUrl, severity }): void => {
// eslint-disable-next-line no-console
(severity === 'ERROR' ? console.error : console.warn)(
// eslint-disable-line no-console
`line ${line}, col ${col}: ${message} ${
specUrl ? ` (see ${specUrl})` : ''
}`,
);
},
);
expect(result.status).toBe('PASS');
});
'use strict';
var Promise = require('bluebird');
var pathFn = require('path');
var assign = require('object-assign');
var amphtmlValidator = require('amphtml-validator').getInstance();
var lg = require('../log.js');
var util = require('../util.js');
var validPassCnt = 0;
var validErrorCnt = 0;
//------------------------------------
// AMP HTML Validate
//------------------------------------
module.exports.amp_validate = function(result){
if(result.tempData.isCacheUse)return Promise.resolve(result);
lg.setConfig(result.config);
return new Promise(function(resolve , reject){
if(result.config.generator_amp.validateAMP){
///
import {readFileSync} from "fs";
import {resolve, URL} from "url";
// tslint:disable-next-line:no-var-requires
const validator = require("amphtml-validator").newInstance(
// Let's not fetch over the network on every run.
// Use `yarn run update-validator` to update.
// tslint:disable-next-line:no-var-requires
readFileSync(`${__dirname}/validator.js`).toString(),
);
import * as cheerio from "cheerio";
import throat = require("throat");
import {default as fetch, Request, RequestInit, Response} from "node-fetch";
import {basename} from "path";
import * as probe from "probe-image-size";
import * as punycode from "punycode";
import * as readline from "readline";
const CONCURRENCY = 8;
const UA_GOOGLEBOT_MOBILE = [
export async function validateAMP(html) {
const validator = await amphtmlValidator.getInstance()
const result = validator.validateString(html)
if (result.status !== 'PASS') {
for (let ii = 0; ii < result.errors.length; ii++) {
const error = result.errors[ii]
let msg =
'line ' + error.line + ', col ' + error.col + ': ' + error.message
if (error.specUrl !== null) {
msg += ' (see ' + error.specUrl + ')'
}
;(error.severity === 'ERROR' ? console.error : console.warn)(msg)
}
}
expect(result.status).toBe('PASS')
}
(() => {
const validator = ampHtmlValidator.newInstance("");
const result = validator.validateString("", "AMP4ADS");
const { status, errors } = result;
})();
function lib_init_validator(next) {
if (amphtml_validator_instance_time < (Date.now() - (60*60*1000))) {
amphtml_validator_instance = null;
}
if (amphtml_validator_instance === null) {
console.log('[VALIDATOR REFRESH]: validator not found or out of date, loading new validator');
amphtml_validator_instance = amphtml_validator.getInstance().then((instance) => {
amphtml_validator_instance = instance;
amphtml_validator_instance_time = Date.now();
next();
});
} else {
next();
}
}
async function registerValidator (options) {
const amphtmlValidator = require('amphtml-validator')
const validator = await amphtmlValidator.getInstance()
this.nuxt.hook('render:route', (url, { html }, { req }) => {
const isAMP = req.isAMP
if (isAMP) {
const result = validator.validateString(html)
const isValid = result.status === 'PASS'
logger.log({
type: result.status,
message: (isValid ? chalk.green(result.status) : chalk.red(result.status)) + ' ' + url,
icon: isValid ? chalk.green('✓') : chalk.red('✕')
})
for (const error of result.errors) {
let msg = 'line ' + error.line + ', col ' + error.col + ': ' + error.message
if (error.specUrl !== null) {
msg += ' (see ' + error.specUrl + ')'
}
;(this.renderOpts as any).ampValidator = (
html: string,
pathname: string
) => {
const validatorPath =
this.nextConfig.experimental &&
this.nextConfig.experimental.amp &&
this.nextConfig.experimental.amp.validator
return AmpHtmlValidator.getInstance(validatorPath).then(validator => {
const result = validator.validateString(html)
ampValidation(
pathname,
result.errors
.filter(e => e.severity === 'ERROR')
.filter(e => this._filterAmpDevelopmentScript(html, e)),
result.errors.filter(e => e.severity !== 'ERROR')
)
})
}
if (fs.existsSync(join(this.dir, 'static'))) {
validate(css) {
this.template = fs.readFileSync(AMP_CSS_HTML_TEMPLATE_PATH, 'utf8');
var html = this.template.replace(CSS_PLACEHOLDER, css);
return Validator.getInstance().then(validator => {
var result = validator.validateString(html);
result.errors = result.errors.map(_convertAMPCSSError);
return result;
});
}
};
const validateAmp = async (html, page, validatorPath) => {
const validator = await AmpHtmlValidator.getInstance(validatorPath)
const result = validator.validateString(html)
const errors = result.errors.filter(e => e.severity === 'ERROR')
const warnings = result.errors.filter(e => e.severity !== 'ERROR')
if (warnings.length || errors.length) {
results.ampValidations.push({
page,
result: {
errors,
warnings,
},
})
}
}