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

Validator rollup #31316

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 147 additions & 1 deletion validator/js/engine/htmlparser-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ const ParsedAttr = class {
};
exports.ParsedAttr = ParsedAttr;


/**
* An Html parser makes method calls with ParsedHtmlTags as arguments.
*/
Expand Down Expand Up @@ -208,12 +207,159 @@ const ParsedHtmlTag = class {
this.attrs_ = newAttrs;
}

/**
* Returns the value of a given attribute name. If it does not exist then
* returns null.
* @param {string} name
* @return {?string}
* @private
*/
getAttrValueOrNull_(name) {
return this.attrsByKey()[name] || null;
}

/**
* @return {boolean}
*/
isEmpty() {
return this.tagName_.length === 0;
}

/**
* Gets the name attribute for an extension script tag.
* @return {string}
* @private
*/
extensionScriptNameAttribute_() {
if (this.upperName() == 'SCRIPT') {
for (const attribute
of ['custom-element', 'custom-template', 'host-service']) {
if (attribute in this.attrsByKey()) {
return attribute;
}
}
}
return '';
}

/**
* Tests if this is an extension script tag.
* @return {boolean}
*/
isExtensionScript() {
return !!this.extensionScriptNameAttribute_();
}

/**
* Tests if this is an async script tag.
* @return {boolean}
* @private
*/
isAsyncScriptTag_() {
return this.upperName() == 'SCRIPT' && 'async' in this.attrsByKey() &&
'src' in this.attrsByKey();
}

/**
* Tests if this is the AMP runtime script tag.
* @return {boolean}
*/
isAmpRuntimeScript() {
const src = this.getAttrValueOrNull_('src');
if (src === null) return false;
return this.isAsyncScriptTag_() && !this.isExtensionScript() &&
src.startsWith('https://cdn.ampproject.org/') &&
(src.endsWith('/v0.js') || src.endsWith('/v0.mjs') ||
src.endsWith('/v0.mjs?f=sxg'));
}

/**
* Tests if this is the LTS version script tag.
* @return {boolean}
*/
isLtsScriptTag() {
// Examples:
// https://cdn.ampproject.org/lts/v0.js
// https://cdn.ampproject.org/lts/v0/amp-ad-0.1.js
const src = this.getAttrValueOrNull_('src');
if (src === null) return false;
const ltsScriptSrcRegex = new RegExp(
'^https://cdn\\.ampproject\\.org/lts/(v0|v0/amp-[a-z0-9-]*-[a-z0-9.]*)\\.js$',
'i');
return this.isAsyncScriptTag_() && ltsScriptSrcRegex.test(src);
}

/**
* Tests if this is the module version script tag.
* @return {boolean}
*/
isModuleScriptTag() {
// Examples:
// https://cdn.ampproject.org/v0.mjs
// https://cdn.ampproject.org/v0/amp-ad-0.1.mjs
const type = this.getAttrValueOrNull_('type');
if (type === null) return false;
const src = this.getAttrValueOrNull_('src');
if (src === null) return false;
const moduleScriptSrcRegex = new RegExp(
'^https://cdn\\.ampproject\\.org/(v0|v0/amp-[a-z0-9-]*-[a-z0-9.]*)\\.mjs$',
'i');
return this.isAsyncScriptTag_() && (type == 'module') &&
moduleScriptSrcRegex.test(src);
}

/**
* Tests if this is the nomodule version script tag.
* @return {boolean}
*/
isNomoduleScriptTag() {
// Examples:
// https://cdn.ampproject.org/v0.js
// https://cdn.ampproject.org/v0/amp-ad-0.1.js
const src = this.getAttrValueOrNull_('src');
if (src === null) return false;
const nomoduleScriptSrcRegex = new RegExp(
'^https://cdn\\.ampproject\\.org/(v0|v0/amp-[a-z0-9-]*-[a-z0-9.]*)\\.js$',
'i');
return this.isAsyncScriptTag_() && 'nomodule' in this.attrsByKey() &&
nomoduleScriptSrcRegex.test(src);
}

/**
* Tests if this is the module LTS version script tag.
* @return {boolean}
*/
isModuleLtsScriptTag() {
// Examples:
// https://cdn.ampproject.org/lts/v0.mjs
// https://cdn.ampproject.org/lts/v0/amp-ad-0.1.mjs
const type = this.getAttrValueOrNull_('type');
if (type === null) return false;
const src = this.getAttrValueOrNull_('src');
if (src === null) return false;
const moduleLtsScriptSrcRegex = new RegExp(
'^https://cdn\\.ampproject\\.org/lts/(v0|v0/amp-[a-z0-9-]*-[a-z0-9.]*)\\.mjs$',
'i');
return this.isAsyncScriptTag_() && (type == 'module') &&
moduleLtsScriptSrcRegex.test(src);
}

/**
* Tests if this is the nomodule LTS version script tag.
* @return {boolean}
*/
isNomoduleLtsScriptTag() {
// Examples:
// https://cdn.ampproject.org/lts/v0.js
// https://cdn.ampproject.org/lts/v0/amp-ad-0.1.js
const src = this.getAttrValueOrNull_('src');
if (src === null) return false;
const nomoduleLtsScriptSrcRegex = new RegExp(
'^https://cdn\\.ampproject\\.org/lts/(v0|v0/amp-[a-z0-9-]*-[a-z0-9.]*)\\.js$',
'i');
return this.isAsyncScriptTag_() && 'nomodule' in this.attrsByKey() &&
nomoduleLtsScriptSrcRegex.test(src);
}
};
exports.ParsedHtmlTag = ParsedHtmlTag;

Expand Down
Loading