Best JavaScript code snippet using wpt
extended-attributes.js
Source:extended-attributes.js
1import { Base } from "./base.js";2import { ArrayBase } from "./array-base.js";3import { WrappedToken } from "./token.js";4import { list, argument_list, autoParenter, unescape } from "./helpers.js";5import { validationError } from "../error.js";6/**7 * @param {import("../tokeniser.js").Tokeniser} tokeniser8 * @param {string} tokenName9 */10function tokens(tokeniser, tokenName) {11 return list(tokeniser, {12 parser: WrappedToken.parser(tokeniser, tokenName),13 listName: tokenName + " list",14 });15}16const extAttrValueSyntax = ["identifier", "decimal", "integer", "string"];17const shouldBeLegacyPrefixed = [18 "NoInterfaceObject",19 "LenientSetter",20 "LenientThis",21 "TreatNonObjectAsNull",22 "Unforgeable",23];24const renamedLegacies = new Map([25 .../** @type {[string, string][]} */ (26 shouldBeLegacyPrefixed.map((name) => [name, `Legacy${name}`])27 ),28 ["NamedConstructor", "LegacyFactoryFunction"],29 ["OverrideBuiltins", "LegacyOverrideBuiltIns"],30 ["TreatNullAs", "LegacyNullToEmptyString"],31]);32/**33 * This will allow a set of extended attribute values to be parsed.34 * @param {import("../tokeniser.js").Tokeniser} tokeniser35 */36function extAttrListItems(tokeniser) {37 for (const syntax of extAttrValueSyntax) {38 const toks = tokens(tokeniser, syntax);39 if (toks.length) {40 return toks;41 }42 }43 tokeniser.error(44 `Expected identifiers, strings, decimals, or integers but none found`45 );46}47export class ExtendedAttributeParameters extends Base {48 /**49 * @param {import("../tokeniser.js").Tokeniser} tokeniser50 */51 static parse(tokeniser) {52 const tokens = { assign: tokeniser.consume("=") };53 const ret = autoParenter(54 new ExtendedAttributeParameters({ source: tokeniser.source, tokens })55 );56 ret.list = [];57 if (tokens.assign) {58 tokens.asterisk = tokeniser.consume("*");59 if (tokens.asterisk) {60 return ret.this;61 }62 tokens.secondaryName = tokeniser.consumeKind(...extAttrValueSyntax);63 }64 tokens.open = tokeniser.consume("(");65 if (tokens.open) {66 ret.list = ret.rhsIsList67 ? // [Exposed=(Window,Worker)]68 extAttrListItems(tokeniser)69 : // [LegacyFactoryFunction=Audio(DOMString src)] or [Constructor(DOMString str)]70 argument_list(tokeniser);71 tokens.close =72 tokeniser.consume(")") ||73 tokeniser.error("Unexpected token in extended attribute argument list");74 } else if (tokens.assign && !tokens.secondaryName) {75 tokeniser.error("No right hand side to extended attribute assignment");76 }77 return ret.this;78 }79 get rhsIsList() {80 return (81 this.tokens.assign && !this.tokens.asterisk && !this.tokens.secondaryName82 );83 }84 get rhsType() {85 if (this.rhsIsList) {86 return this.list[0].tokens.value.type + "-list";87 }88 if (this.tokens.asterisk) {89 return "*";90 }91 if (this.tokens.secondaryName) {92 return this.tokens.secondaryName.type;93 }94 return null;95 }96 /** @param {import("../writer.js").Writer} w */97 write(w) {98 const { rhsType } = this;99 return w.ts.wrap([100 w.token(this.tokens.assign),101 w.token(this.tokens.asterisk),102 w.reference_token(this.tokens.secondaryName, this.parent),103 w.token(this.tokens.open),104 ...this.list.map((p) => {105 return rhsType === "identifier-list"106 ? w.identifier(p, this.parent)107 : p.write(w);108 }),109 w.token(this.tokens.close),110 ]);111 }112}113export class SimpleExtendedAttribute extends Base {114 /**115 * @param {import("../tokeniser.js").Tokeniser} tokeniser116 */117 static parse(tokeniser) {118 const name = tokeniser.consumeKind("identifier");119 if (name) {120 return new SimpleExtendedAttribute({121 source: tokeniser.source,122 tokens: { name },123 params: ExtendedAttributeParameters.parse(tokeniser),124 });125 }126 }127 constructor({ source, tokens, params }) {128 super({ source, tokens });129 params.parent = this;130 Object.defineProperty(this, "params", { value: params });131 }132 get type() {133 return "extended-attribute";134 }135 get name() {136 return this.tokens.name.value;137 }138 get rhs() {139 const { rhsType: type, tokens, list } = this.params;140 if (!type) {141 return null;142 }143 const value = this.params.rhsIsList144 ? list145 : this.params.tokens.secondaryName146 ? unescape(tokens.secondaryName.value)147 : null;148 return { type, value };149 }150 get arguments() {151 const { rhsIsList, list } = this.params;152 if (!list || rhsIsList) {153 return [];154 }155 return list;156 }157 *validate(defs) {158 const { name } = this;159 if (name === "LegacyNoInterfaceObject") {160 const message = `\`[LegacyNoInterfaceObject]\` extended attribute is an \161undesirable feature that may be removed from Web IDL in the future. Refer to the \162[relevant upstream PR](https://github.com/whatwg/webidl/pull/609) for more \163information.`;164 yield validationError(165 this.tokens.name,166 this,167 "no-nointerfaceobject",168 message,169 { level: "warning" }170 );171 } else if (renamedLegacies.has(name)) {172 const message = `\`[${name}]\` extended attribute is a legacy feature \173that is now renamed to \`[${renamedLegacies.get(name)}]\`. Refer to the \174[relevant upstream PR](https://github.com/whatwg/webidl/pull/870) for more \175information.`;176 yield validationError(this.tokens.name, this, "renamed-legacy", message, {177 level: "warning",178 autofix: renameLegacyExtendedAttribute(this),179 });180 }181 for (const arg of this.arguments) {182 yield* arg.validate(defs);183 }184 }185 /** @param {import("../writer.js").Writer} w */186 write(w) {187 return w.ts.wrap([188 w.ts.trivia(this.tokens.name.trivia),189 w.ts.extendedAttribute(190 w.ts.wrap([191 w.ts.extendedAttributeReference(this.name),192 this.params.write(w),193 ])194 ),195 w.token(this.tokens.separator),196 ]);197 }198}199/**200 * @param {SimpleExtendedAttribute} extAttr201 */202function renameLegacyExtendedAttribute(extAttr) {203 return () => {204 const { name } = extAttr;205 extAttr.tokens.name.value = renamedLegacies.get(name);206 if (name === "TreatNullAs") {207 extAttr.params.tokens = {};208 }209 };210}211// Note: we parse something simpler than the official syntax. It's all that ever212// seems to be used213export class ExtendedAttributes extends ArrayBase {214 /**215 * @param {import("../tokeniser.js").Tokeniser} tokeniser216 */217 static parse(tokeniser) {218 const tokens = {};219 tokens.open = tokeniser.consume("[");220 const ret = new ExtendedAttributes({ source: tokeniser.source, tokens });221 if (!tokens.open) return ret;222 ret.push(223 ...list(tokeniser, {224 parser: SimpleExtendedAttribute.parse,225 listName: "extended attribute",226 })227 );228 tokens.close =229 tokeniser.consume("]") ||230 tokeniser.error(231 "Expected a closing token for the extended attribute list"232 );233 if (!ret.length) {234 tokeniser.unconsume(tokens.close.index);235 tokeniser.error("An extended attribute list must not be empty");236 }237 if (tokeniser.probe("[")) {238 tokeniser.error(239 "Illegal double extended attribute lists, consider merging them"240 );241 }242 return ret;243 }244 *validate(defs) {245 for (const extAttr of this) {246 yield* extAttr.validate(defs);247 }248 }249 /** @param {import("../writer.js").Writer} w */250 write(w) {251 if (!this.length) return "";252 return w.ts.wrap([253 w.token(this.tokens.open),254 ...this.map((ea) => ea.write(w)),255 w.token(this.tokens.close),256 ]);257 }...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) return console.error(err);4 console.log(data);5 wpt.renamedLegacies(data.data.testId, function(err, data) {6 if (err) return console.error(err);7 console.log(data);8 });9});
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var path = require('path');3var fs = require('fs');4var legacyDir = path.join(__dirname,'legacies');5var renamedLegacyDir = path.join(__dirname,'renamedLegacies');6fs.readdir(legacyDir,function(err,files){7 if(err){8 console.log(err);9 return;10 }11 for(var i=0;i<files.length;i++){12 var fileName = files[i];13 var filePath = path.join(legacyDir,fileName);14 var renamedLegacyFilePath = path.join(renamedLegacyDir,fileName);15 wptoolkit.renameLegacy(filePath,renamedLegacyFilePath,function(err){16 if(err){17 console.log(err);18 return;19 }20 });21 }22});
Using AI Code Generation
1var wptools = require('wptools');2var wiki = wptools.page('Albert Einstein');3wiki.get(function (err, resp) {4});5### `page.get()`6var wptools = require('wptools');7var wiki = wptools.page('Albert Einstein');8wiki.get(function (err, resp) {9 console.log(resp);10});11### `page.getRedirects()`12var wptools = require('wptools');13var wiki = wptools.page('Albert Einstein');14wiki.getRedirects(function (err, resp) {15 console.log(resp);16});17### `page.getLangLinks()`18var wptools = require('wptools');19var wiki = wptools.page('Albert Einstein');20wiki.getLangLinks(function (err, resp) {21 console.log(resp);22});23### `page.getCategories()`24var wptools = require('wptools');25var wiki = wptools.page('Albert Einstein');26wiki.getCategories(function (err, resp) {27 console.log(resp);28});29### `page.getBacklinks()`
Using AI Code Generation
1var wpt = require('webpagetest');2var fs = require('fs');3var path = require('path');4var csv = require('fast-csv');5var async = require('async');6var _ = require('underscore');7var config = require('./config.js');8var wpt = new wpt('www.webpagetest.org', config.apiKey);9var csvStream = csv.createWriteStream({headers: true}),10 writableStream = fs.createWriteStream("test.csv");11writableStream.on("finish", function(){12 console.log("DONE!");13});14csvStream.pipe(writableStream);15var csvData = [];16var data = fs.readFileSync('test.csv', 'utf8');17var lines = data.split('\n');18var testIds = [];19for(var i = 0; i < lines.length; i++){20 var line = lines[i];21 var lineSplit = line.split(',');22 var testId = lineSplit[0];23 testIds.push(testId);24}25console.log(testIds);26async.forEachSeries(testIds, function(testId, callback){27 wpt.renameLegacy(testId, function(err, data){28 if(err){29 console.log(err);30 }31 else{32 console.log(data);33 }34 callback();35 });36}, function(err){37 if(err){38 console.log(err);39 }40 else{41 console.log("done");42 }43});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!