Best JavaScript code snippet using playwright-internal
index.umd.js
Source:index.umd.js
...115 var source = context.source;116 advancePositionWithMutation(context, source, numberOfCharacters);117 context.source = source.slice(numberOfCharacters);118 }119 function advanceSpaces(context) {120 var match = /^[\t\r\n\f ]+/.exec(context.source);121 if (match && match[0]) {122 advanceBy(context, match[0].length);123 }124 }125 function parseData(context, keyName) {126 advanceSpaces(context);127 var start = getCursor(context);128 var key = keyName || parseKey(context);129 var _parseValue = parseValue(context),130 value = _parseValue.value,131 type = _parseValue.type;132 var loc = getLoc(context, start);133 advanceSpaces(context);134 if (context.source[0] === ",") {135 advanceBy(context, 1);136 advanceSpaces(context);137 }138 return {139 key: key,140 value: value,141 type: type,142 loc: loc143 };144 }145 function parseChildren(context) {146 var nodes = [];147 while (!isEnd(context)) {148 advanceSpaces(context);149 var s = context.source;150 if (s[0] === "{") {151 advanceBy(context, 1);152 } else if (s[0] === "}") {153 advanceBy(context, 1);154 advanceSpaces(context);155 return nodes;156 } else if (s[0] === "/") {157 if (s[1] === "/") {158 var lastNode = nodes[nodes.length - 1];159 var lastLine = -1;160 if (lastNode) {161 lastLine = lastNode.loc.end.line;162 }163 nodes.push(parseComment(context, lastLine));164 advanceSpaces(context);165 } else {166 throw new Error(COMMENT_ERROR_MESSAGE);167 }168 } else {169 nodes.push(parseData(context));170 }171 }172 return nodes;173 }174 function parseKey(context) {175 var s = context.source[0];176 var match = [];177 if (s === '"') {178 match = /^"(.[^"]*)/i.exec(context.source);179 } else if (s === "'") {180 match = /^'(.[^']*)/i.exec(context.source);181 } else {182 match = /(.[^:]*)/i.exec(context.source);183 match[1] = match[1].trim();184 }185 advanceBy(context, match[0].length + 1);186 advanceSpaces(context);187 if (context.source[0] === ":") {188 advanceBy(context, 1);189 advanceSpaces(context);190 }191 return match[1];192 }193 function parseNumber(context) {194 var match = /^([0-9]*)/i.exec(context.source);195 advanceBy(context, match[0].length);196 return match[1];197 }198 function parseString(context) {199 var match = /^["|']([^"|']*)/i.exec(context.source);200 advanceBy(context, match[0].length + 1);201 return match[1];202 }203 function parseNull(context) {204 advanceBy(context, 4);205 return "null";206 }207 function parseBoolean(context) {208 var match = /^(true|false)/i.exec(context.source);209 advanceBy(context, match[0].length);210 return match[1];211 }212 function parseUndefined(context) {213 advanceBy(context, 9);214 return "undefined";215 }216 function parseValue(context) {217 var value = null;218 var type = null;219 var code = context.source[0];220 if (/^[0-9]/.test(code)) {221 value = parseNumber(context);222 type = NUMBER_TYPE;223 } else if (code === '"' || code === "'") {224 value = parseString(context);225 type = STRING_TYPE;226 } else if (code === "[") {227 advanceBy(context, 1);228 value = parseArray(context);229 type = ARRAY_TYPE;230 } else if (code === "{") {231 value = parseChildren(context);232 type = OBJECT_TYPE;233 } else if (context.source.indexOf("null") === 0) {234 value = parseNull(context);235 type = NULL_TYPE;236 } else if (context.source.indexOf("true") === 0 || context.source.indexOf("false") === 0) {237 value = parseBoolean(context);238 type = BOOLEAN_TYPE;239 } else if (context.source.indexOf("undefined") === 0) {240 value = parseUndefined(context);241 type = UNDEFINED_TYPE;242 } else {243 throw new Error(VALUE_ILLEGAL_ERROR_MESSAGE);244 }245 return {246 value: value,247 type: type248 };249 }250 function parseArray(context) {251 var nodes = [];252 var lastLine = getCursor(context).line;253 advanceSpaces(context);254 while (!isEnd(context)) {255 if (context.source.indexOf("//") === 0) {256 var cv = parseComment(context, lastLine);257 nodes.push(cv);258 advanceSpaces(context);259 } else {260 var itemValue = parseData(context, ARRAY_ITEM);261 lastLine = itemValue.loc.end.line;262 nodes.push(itemValue);263 }264 var s = context.source[0];265 if (s === "]") {266 advanceBy(context, 1);267 return nodes;268 }269 if (s === "}" || s === ":") {270 throw new Error(ARRAY_ERROR_MESSAGE);271 }272 }...
index.cjs
Source:index.cjs
...82 var source = context.source;83 advancePositionWithMutation(context, source, numberOfCharacters);84 context.source = source.slice(numberOfCharacters);85}86function advanceSpaces(context) {87 var match = /^[\t\r\n\f ]+/.exec(context.source);88 if (match && match[0]) {89 advanceBy(context, match[0].length);90 }91}92function parseData(context, keyName) {93 advanceSpaces(context);94 var start = getCursor(context);95 var key = keyName || parseKey(context);96 var _parseValue = parseValue(context),97 value = _parseValue.value,98 type = _parseValue.type;99 var loc = getLoc(context, start);100 advanceSpaces(context);101 if (context.source[0] === ",") {102 advanceBy(context, 1);103 advanceSpaces(context);104 }105 return {106 key: key,107 value: value,108 type: type,109 loc: loc110 };111}112function parseChildren(context) {113 var nodes = [];114 while (!isEnd(context)) {115 advanceSpaces(context);116 var s = context.source;117 if (s[0] === "{") {118 advanceBy(context, 1);119 } else if (s[0] === "}") {120 advanceBy(context, 1);121 advanceSpaces(context);122 return nodes;123 } else if (s[0] === "/") {124 if (s[1] === "/") {125 var lastNode = nodes[nodes.length - 1];126 var lastLine = -1;127 if (lastNode) {128 lastLine = lastNode.loc.end.line;129 }130 nodes.push(parseComment(context, lastLine));131 advanceSpaces(context);132 } else {133 throw new Error(COMMENT_ERROR_MESSAGE);134 }135 } else {136 nodes.push(parseData(context));137 }138 }139 return nodes;140}141function parseKey(context) {142 var s = context.source[0];143 var match = [];144 if (s === '"') {145 match = /^"(.[^"]*)/i.exec(context.source);146 } else if (s === "'") {147 match = /^'(.[^']*)/i.exec(context.source);148 } else {149 match = /(.[^:]*)/i.exec(context.source);150 match[1] = match[1].trim();151 }152 advanceBy(context, match[0].length + 1);153 advanceSpaces(context);154 if (context.source[0] === ":") {155 advanceBy(context, 1);156 advanceSpaces(context);157 }158 return match[1];159}160function parseNumber(context) {161 var match = /^([0-9]*)/i.exec(context.source);162 advanceBy(context, match[0].length);163 return match[1];164}165function parseString(context) {166 var match = /^["|']([^"|']*)/i.exec(context.source);167 advanceBy(context, match[0].length + 1);168 return match[1];169}170function parseNull(context) {171 advanceBy(context, 4);172 return "null";173}174function parseBoolean(context) {175 var match = /^(true|false)/i.exec(context.source);176 advanceBy(context, match[0].length);177 return match[1];178}179function parseUndefined(context) {180 advanceBy(context, 9);181 return "undefined";182}183function parseValue(context) {184 var value = null;185 var type = null;186 var code = context.source[0];187 if (/^[0-9]/.test(code)) {188 value = parseNumber(context);189 type = NUMBER_TYPE;190 } else if (code === '"' || code === "'") {191 value = parseString(context);192 type = STRING_TYPE;193 } else if (code === "[") {194 advanceBy(context, 1);195 value = parseArray(context);196 type = ARRAY_TYPE;197 } else if (code === "{") {198 value = parseChildren(context);199 type = OBJECT_TYPE;200 } else if (context.source.indexOf("null") === 0) {201 value = parseNull(context);202 type = NULL_TYPE;203 } else if (context.source.indexOf("true") === 0 || context.source.indexOf("false") === 0) {204 value = parseBoolean(context);205 type = BOOLEAN_TYPE;206 } else if (context.source.indexOf("undefined") === 0) {207 value = parseUndefined(context);208 type = UNDEFINED_TYPE;209 } else {210 throw new Error(VALUE_ILLEGAL_ERROR_MESSAGE);211 }212 return {213 value: value,214 type: type215 };216}217function parseArray(context) {218 var nodes = [];219 var lastLine = getCursor(context).line;220 advanceSpaces(context);221 while (!isEnd(context)) {222 if (context.source.indexOf("//") === 0) {223 var cv = parseComment(context, lastLine);224 nodes.push(cv);225 advanceSpaces(context);226 } else {227 var itemValue = parseData(context, ARRAY_ITEM);228 lastLine = itemValue.loc.end.line;229 nodes.push(itemValue);230 }231 var s = context.source[0];232 if (s === "]") {233 advanceBy(context, 1);234 return nodes;235 }236 if (s === "}" || s === ":") {237 throw new Error(ARRAY_ERROR_MESSAGE);238 }239 }...
parser.js
Source:parser.js
...51 advanceBy(num) {52 // æ¶è´¹æå®é¿åº¦çå符串53 context.source = context.source.slice(num);54 },55 advanceSpaces() {56 // å¹é
空ç½å符串ï¼ç¶åæ¶è´¹57 const match = /^[\t\r\n\f ]+/.exec(context.source);58 if (match) {59 context.advanceBy(match[0].length);60 }61 },62 };63 const nodes = parseChildren(context, []);64 return {65 type: "Root",66 children: nodes,67 };68}69function parseChildren(context, ancestors) {70 let nodes = [];71 const { mode, advanceSpaces } = context;72 // æ¯æ¬¡è¿è¡åæ¸
é¤ç©ºç½å符73 advanceSpaces();74 // å¼å¯while循ç¯ï¼å¦æ没æç»æï¼ä¸ç´å¾ªç¯ä¸å»75 while (!isEnd(context, ancestors)) {76 let node;77 // åªæDATAåRCDATAæ¯æ对æå¼èç¹ç解æ78 if (mode === TextModes.DATA || mode === TextModes.RCDATA) {79 // åªæDATA模å¼ææ¯ææ ç¾èç¹ç解æ80 if (mode === TextModes.DATA && context.source[0] === "<") {81 if (context.source[1] === "!") {82 if (context.source.startsWith("<!--")) {83 // 解æ注é84 node = parseComment(context);85 } else if (context.source.startsWith("<![CDATA[")) {86 // 解æCDATA87 node = parseCDATA(context);88 }89 } else if (context.source[1] === "/") {90 // ç±äºè§£æåèç¹ä¼å¼å¯æ°çç¶ææºï¼å¨æ°çç¶ææºä¸ä¼å°ç»ææ ç¾å¤çæ91 // å¦ææ¤æ¶éå°äºç»ææ ç¾æ å¿ï¼å认为该ç»ææ ç¾æ²¡æ对åºçå¼å§æ ç¾ï¼æåºé误92 console.error("åºéäº");93 continue;94 } else if (/[a-z]/i.test(context.source[1])) {95 // 解ææ ç¾96 node = parseElement(context, ancestors);97 }98 } else if (context.source.startsWith("{{")) {99 // 解ææå¼100 node = parseInterpolation(context);101 }102 }103 // ä¸ä¸è½®ä¸æ¥é½æ²¡æèç¹è¢«è§£æï¼è§£æ为ææ¬104 if (!node) {105 node = parseText(context);106 }107 nodes.push(node);108 }109 return nodes;110}111function parseCDATA(context) {112 context.advanceBy("<![CDATA[".length); // æ¶è´¹CDATAå¼å§å符113 let endIndex = context.source.indexOf("]]>"); // 寻æ¾CDATAç»ææ å¿114 if (endIndex === -1) {115 endIndex = context.source.length;116 }117 let content = context.source.slice(0, endIndex); // CDATAä¹é´çææå
容å°ä¼è¯å«ä¸ºæ®éææ¬118 return {119 type: "CDATA",120 content,121 };122}123function parseText(context) {124 let endIndex = context.source.length; // endIndexé»è®¤ä¸ºå符串é¿åº¦125 const ltIndex = context.source.indexOf("<"); // 寻æ¾<ä½ç½®126 const delimiterIndex = context.source.indexOf("{{"); // 寻æ¾å®ç符{{çä½ç½®127 // ltIndexåå¨ï¼ä¸å°äºå符串é¿åº¦ï¼åendIndex为ltIndex128 if (ltIndex > -1 && ltIndex <= endIndex) {129 endIndex = ltIndex;130 }131 // delimiterIndexåå¨ä¸å°äºendIndexï¼åendIndex为delimiterIndex132 if (delimiterIndex > -1 && delimiterIndex < endIndex) {133 endIndex = delimiterIndex;134 }135 // åå¾content136 const content = context.source.slice(0, endIndex);137 // æ¶è´¹contenté¿åº¦çå符串138 context.advanceBy(content.length);139 return {140 type: "Text",141 content: decodeHtml(content),142 };143}144// 第ä¸ä¸ªåæ°æ¯è¦è§£ç çå符串145// 第äºä¸ªåæ°ä»£è¡¨ææ¬å
容æ¯å¦ä½ä¸ºå±æ§å¼146function decodeHtml(rawText, asAttr = false) {147 let offset = 0;148 const end = rawText.length;149 let decodedText = ""; // æç»è§£ç çå符串150 let maxCRNameLength = 0; // å¼ç¨è¡¨ä¸å®ä½å称çæ大é¿åº¦151 // æ¶è´¹æå®é¿åº¦çå符串152 function advance(length) {153 offset += length;154 rawText = rawText.slice(length);155 }156 while (offset < end) {157 // å¹é
å符å¼ç¨çå¼å§é¨åï¼158 // æä¸ç§æ
åµï¼head[0]='&'ãhead[0]='&#'ãhead[0]='&#x'159 const head = /&(?:#x?)?/i.exec(rawText);160 // å¦æ没æå¹é
161 if (!head) {162 // 计ç®å©ä½å
容é¿åº¦163 const remaining = end - offset;164 // å°å©ä½å
容追å å°decodedText165 decodedText += rawText.slice(0, remaining);166 // æ¶è´¹å©ä½å
容é¿åº¦çå符167 advance(remaining);168 break;169 }170 // å¦ææ¾å°äºå符å¼ç¨ï¼æªå&ä¹åçå符ï¼è¿½å å°decoidedText171 decodedText += rawText.slice(0, head.index);172 // æ¶è´¹å符&ä¹åçå
容173 advance(head.index);174 // å¦æå¹é
å°çhead第ä¸ä¸ªå符æ¯&ï¼è¯´ææ¯å½åå符å¼ç¨ï¼å¦å为æ°åå符å¼ç¨175 if (head[0] === "&") {176 let name = "";177 let value;178 // å¦æmatchå°äº&ï¼ä¸ä¸ä¸ä¸ªå符æ¯ASCIIåæ¯ææ°åï¼éè¦è¿ä¸æ¥æ¥æ¾å符å¼ç¨è¡¨179 if (/[0-9a-z]/i.test(rawText[1])) {180 // æ¾åºå符å¼ç¨è¡¨ä¸å称æé¿çå符é¿åº¦181 if (!maxCRNameLength) {182 maxCRNameLength = Object.keys(namedChararacterReferences).reduce(183 (max, name) => Math.max(max, name.length),184 0185 );186 }187 // ä»å½åææ¬è¿è¡æªåæå®é¿åº¦çå符串ï¼å°è¯å»å符å¼ç¨è¡¨ä¸æ¥æ¾å¯¹åºç项188 for (let length = maxCRNameLength; !value && length > 0; --length) {189 name = rawText.substr(1, length);190 value = namedChararacterReferences[name];191 }192 // å¦ææ¾å°äºï¼çä»æ没æ以;ç»å°¾193 if (value) {194 const semi = name.endsWith(";");195 // å¦æ没æ以;ç»å°¾ä¸è§£ç çææ¬ä½ä¸ºå±æ§å¼ï¼å¹¶ä¸æåä¸ä¸ªå符æ¯=ãASCIIåæ¯ææ°å196 // ç±äºåå²åå ï¼å°å符&åå®ä½å称nameä¸èµ·ä½ä¸ºæ®éææ¬197 if (198 asAttr &&199 !semi &&200 /[=a-z0-9]/i.test(rawText[name.length + 1] || "")201 ) {202 decodedText += "&" + name;203 advance(1 + name.length);204 } else {205 // å
¶ä»æ
åµï¼æ£å¸¸æ¼æ¥å
¶å¼å°decodedTextï¼å¹¶æ¶è´¹nameé¿åº¦çå符串206 decodedText += value;207 advance(1 + name.length);208 }209 } else {210 // å¦æ没æ¾å°ï¼åç´æ¥æ¼æ¥&ånameä½ä¸ºæ®éææ¬å°decodedTextï¼å¹¶æ¶è´¹211 decodedText += "&" + name;212 advance(1 + name.length);213 }214 } else {215 // å¦æmatchå°äº&ï¼ä½æ¯ä¸ä¸ä¸ªå符ä¸æ¯ASCIIåæ¯ææ°åï¼åå°å符&ä½ä¸ºæ®éææ¬216 decodedText += "&";217 advance(1);218 }219 } else {220 // å¦æå¹é
å°çhead以&#xæ&#å¼å¤´ï¼è¯´ææ¯æ°åå符å¼ç¨ï¼èæ°åå符å¼ç¨å为10è¿å¶å16è¿å¶221 // æ ¹æ®è¿å¶ä¸åï¼å¹é
çæ£åä¹ä¸å222 const hex = head[0] === "&#x";223 const pattern = hex ? /^&#x([0-9a-f]+);?/i : /^&#([0-9]+);?/i;224 const body = pattern.exec(rawText);225 // å¦æè½å¹é
ä¸å¯¹åºçè¿å¶226 if (body) {227 // éè¿parseIntæ¿å°å
¶åè¿å¶çç ç¹ï¼éè¿ç ç¹æ¿å°å¯¹åºçå符228 const codePoint = parseInt(bdoy[1], hex ? 16 : 10);229 // æ£æ¥å¾å°çç ç¹åæ³æ§230 if (codePoint === 0) {231 // å¦æç ç¹å¼ä¸º0x00ï¼æ¿æ¢ä¸º0xfffd232 codePoint = 0xfffd;233 } else if (codePoint > 0x10ffff) {234 // å¦æç ç¹å¼è¶
è¿äºUnicodeæ大å¼ï¼æ¿æ¢ä¸º0xfffd235 codePoint = 0xfffd;236 } else if (codePoint >= 0xd800 && codePoint <= 0xdfff) {237 // å¦æç ç¹å¼å¤äºä»£ç对èå´å
ï¼æ¿æ¢ä¸º0xfffd238 codePoint = 0xfffd;239 } else if (240 (codePoint >= 0xfdd0 && codePoint <= 0xfdef) ||241 (codePoint & 0xfffe) === 0xfffe242 ) {243 // å¦æç ç¹å¼å¤äºnoncharacterèå´å
ï¼å¥é½ä¸å¤ç244 } else if (245 (codePoint >= 0x01 && codePoint <= 0x08) ||246 codePoint === 0x0b ||247 (codePoint >= 0x0d && codePoint <= 0x1f) ||248 (codePoint >= 0x7f && codePoint <= 0x9f)249 ) {250 // å¦æç ç¹å¼å¨æ§å¶å符éèå´ï¼[0x01, 0x1f]+[0x7f, 0x9f]ï¼å
251 // å»æASCII空ç½å符ï¼0x09ï¼TABï¼ï¼0x0Aï¼LFï¼ã0x0Cï¼FFï¼ã0x0Dï¼CRï¼252 // å»CCR_REPLACEMENTS寻æ¾æ¿æ¢çç ç¹ï¼å¦ææ¾ä¸å°å使ç¨åç ç¹253 codePoint = CCR_REPLACEMENTS[codePoint] || codePoint;254 }255 const char = String.fromCodePoint(codePoint);256 decodedText += char;257 advance(body[0].length); // æ¶è´¹å¹é
å°çbodyé¿åº¦258 } else {259 // å¦æ没æå¹é
å°æ°åå符å¼ç¨ï¼åä¸è¿è¡è§£ç æä½ï¼åªæ¯è¿½å head[0]å°decodedTextï¼å¹¶æ¶è´¹head[0]260 decodedText += head[0];261 advance(head[0].length);262 }263 }264 }265 return decodedText;266}267function parseInterpolation(context) {268 const { advanceBy } = context;269 advanceBy("{{".length); // æ¶è´¹æå¼è¡¨è¾¾å¼å¼å¤´ç{{270 let closeIndex = context.source.indexOf("}}");271 if (closeIndex < 0) {272 console.error("æå¼ç¼ºå°ç»æçå®ç¬¦");273 }274 // ä»å¼å§å°æå¼ç»æçå®ç¬¦ä¹é´çå
容ä½ä¸ºcontent275 const content = context.source.slice(0, closeIndex);276 advanceBy(content.length); // æ¶è´¹å
容é¿åº¦277 advanceBy("}}".length); // æ¶è´¹æå¼ç»æçå®ç¬¦278 return {279 type: "Interpolation",280 // æå¼èç¹çç±»åæ¯ä¸ä¸ªè¡¨è¾¾å¼èç¹281 content: {282 type: "Expression",283 content: decodeHtml(content), // 对æå¼è¡¨è¾¾å¼çå
容åä¸æ¬¡è§£ç ï¼å
¶ç»æä½ä¸ºçæ£çå
容è¿å284 },285 };286}287function parseComment(context) {288 const { advanceBy } = context;289 advanceBy("<!--".length);290 let closeIndex = context.source.indexOf("-->");291 const content = context.source.slice(0, closeIndex);292 advanceBy(content.length);293 advanceBy("-->".length);294 return {295 type: "Comment",296 content,297 };298}299function parseElement(context, ancestors) {300 const element = parseTag(context); // 解æå¼å§æ ç¾301 // æ ç¾æ¯ä¸ä¸ªèªéåæ ç¾ï¼ç´æ¥è¿å302 if (element.isSelfClosing) {303 return element;304 }305 // å¦ætagæ¯textareaåtitleçè¯ï¼åæ¢å°RCDATA模å¼306 if (element.tag === "textarea" || element.tag === "title") {307 context.mode = TextModes.RCDATA;308 } else if (/style|xmp|iframe|noembed|noframes|noscript/.test(element.tag)) {309 // åæ¢å°RAWTEXT模å¼310 context.mode = TextModes.RAWTEXT;311 } else {312 // å
¶ä»åæ¢å°DATA模å¼313 context.mode === TextModes.DATA;314 }315 ancestors.push(element);316 element.children = parseChildren(context, ancestors); // 解æåèç¹317 ancestors.pop(); // éåå®äºåèç¹ï¼å°å½åèç¹ä»ç¥å
èç¹å¼¹åº318 // 解æå®åèç¹ä¹åï¼å¦æå½åå符串以element.tagä½ä¸ºç»ææ ç¾ï¼å认为已ç»æãå¦å认为没æä¸ä¹å¯¹åºçéåæ ç¾319 if (context.source.startsWith(`</${element.tag}>`)) {320 parseTag(context, "end");321 } else {322 console.error(`${element.tag}æ ç¾ç¼ºå°éåæ ç¾`);323 }324 return element;325}326function parseTag(context, type = "start") {327 const { advanceBy, advanceSpaces } = context;328 const match =329 type === "start"330 ? /^<([a-z][^\t\r\n\f />]*)/i.exec(context.source) // å¹é
å¼å§æ ç¾331 : /^<\/([a-z][^\t\r\n\f />]*)/i.exec(context.source); // å¹é
ç»ææ ç¾332 // æ¶è´¹å¹é
ç»ç第ä¸ä¸ªï¼execè½å¹é
çè¯ï¼ç¬¬ä¸ä¸ªæ¯è½å¹é
çå符串ï¼ä¹åæ¯æè·ç»ï¼çé¿åº¦333 const tag = match[1];334 advanceBy(match[0].length);335 advanceSpaces(); // æ¶è´¹ç©ºç½å符串336 const props = parseAttribute(context);337 const isSelfClosing = context.source.startsWith("/"); // æ¯å¦æ¯èªéåæ ç¾ï¼å¦ææ¯çè¯åæ¶è´¹ä¸¤ä¸ªå符串(/>)ï¼å¦ååªæ¶è´¹ä¸ä¸ª(>)338 advanceBy(isSelfClosing ? 2 : 1);339 return {340 type: "Element",341 tag,342 props: props,343 children: [],344 isSelfClosing,345 };346}347function parseAttribute(context) {348 const { advanceBy, advanceSpaces } = context;349 const props = [];350 // å¼å¯while循ç¯ï¼åªè¦æ²¡éå°ç»ææ å¿ï¼å°±ä¸ç´è¿è¡351 while (!context.source.startsWith(">") && !context.source.startsWith("/>")) {352 // 解ææ令åå±æ§353 // 读åå±æ§å354 const match = /^[^\t\r\n\f />][^\t\r\n\f />=]*/.exec(context.source);355 const name = match[0];356 advanceBy(name.length);357 advanceSpaces();358 advanceBy(1); // æ¶è´¹çå·359 advanceSpaces(); // æ¶è´¹çå·åå±æ§å¼ä¹é´ç空ç½å符串360 let value = "";361 const quote = context.source[0];362 const isQuote = quote === '"' || quote === "'";363 if (isQuote) {364 advanceBy(1); // æ¶è´¹å¼å§å¼å·365 const endQuoteIndex = context.source.indexOf(quote); // å¨å符串éå»æ¾ç»æå¼å·366 if (endQuoteIndex > -1) {367 // æ¾å°äºç»æå¼å·ï¼å¾å°ä¸é´çå±æ§å¼368 value = context.source.slice(0, endQuoteIndex);369 advanceBy(value.length); // æ¶è´¹å±æ§å¼370 advanceBy(1); // æ¶è´¹ç»æå¼å·371 } else {372 console.error("缺å°å¼å·");373 }374 } else {375 // å¦æå±æ§å¼æ²¡æ被å¼å·å¼ç¨ï¼é£ä¹å°ç»æåçä¸ä¸ä¸ªç©ºç½å符ä¹åçå
容ä½ä¸ºå
¶å±æ§å¼376 const match = /^[^\t\r\n\f >]+/.exec(context.source);377 value = match[0];378 advanceBy(value.length);379 }380 advanceSpaces(); // æ¶è´¹å®å±æ§å¼ä¹åï¼æ¶è´¹å±æ§å¼åç空ç½ç»ä»åºæ381 // æ·»å props382 props.push({383 type: "Attribute",384 name,385 value,386 });387 }388 return props;389}390function isEnd(context, ancestors) {391 // 模æ¿è§£æå®æ¯=>ç»æ392 if (!context.source) {393 return true;394 }...
index.mjs
Source:index.mjs
...78 var source = context.source;79 advancePositionWithMutation(context, source, numberOfCharacters);80 context.source = source.slice(numberOfCharacters);81}82function advanceSpaces(context) {83 var match = /^[\t\r\n\f ]+/.exec(context.source);84 if (match && match[0]) {85 advanceBy(context, match[0].length);86 }87}88function parseData(context, keyName) {89 advanceSpaces(context);90 var start = getCursor(context);91 var key = keyName || parseKey(context);92 var _parseValue = parseValue(context),93 value = _parseValue.value,94 type = _parseValue.type;95 var loc = getLoc(context, start);96 advanceSpaces(context);97 if (context.source[0] === ",") {98 advanceBy(context, 1);99 advanceSpaces(context);100 }101 return {102 key: key,103 value: value,104 type: type,105 loc: loc106 };107}108function parseChildren(context) {109 var nodes = [];110 while (!isEnd(context)) {111 advanceSpaces(context);112 var s = context.source;113 if (s[0] === "{") {114 advanceBy(context, 1);115 } else if (s[0] === "}") {116 advanceBy(context, 1);117 advanceSpaces(context);118 return nodes;119 } else if (s[0] === "/") {120 if (s[1] === "/") {121 var lastNode = nodes[nodes.length - 1];122 var lastLine = -1;123 if (lastNode) {124 lastLine = lastNode.loc.end.line;125 }126 nodes.push(parseComment(context, lastLine));127 advanceSpaces(context);128 } else {129 throw new Error(COMMENT_ERROR_MESSAGE);130 }131 } else {132 nodes.push(parseData(context));133 }134 }135 return nodes;136}137function parseKey(context) {138 var s = context.source[0];139 var match = [];140 if (s === '"') {141 match = /^"(.[^"]*)/i.exec(context.source);142 } else if (s === "'") {143 match = /^'(.[^']*)/i.exec(context.source);144 } else {145 match = /(.[^:]*)/i.exec(context.source);146 match[1] = match[1].trim();147 }148 advanceBy(context, match[0].length + 1);149 advanceSpaces(context);150 if (context.source[0] === ":") {151 advanceBy(context, 1);152 advanceSpaces(context);153 }154 return match[1];155}156function parseNumber(context) {157 var match = /^([0-9]*)/i.exec(context.source);158 advanceBy(context, match[0].length);159 return match[1];160}161function parseString(context) {162 var match = /^["|']([^"|']*)/i.exec(context.source);163 advanceBy(context, match[0].length + 1);164 return match[1];165}166function parseNull(context) {167 advanceBy(context, 4);168 return "null";169}170function parseBoolean(context) {171 var match = /^(true|false)/i.exec(context.source);172 advanceBy(context, match[0].length);173 return match[1];174}175function parseUndefined(context) {176 advanceBy(context, 9);177 return "undefined";178}179function parseValue(context) {180 var value = null;181 var type = null;182 var code = context.source[0];183 if (/^[0-9]/.test(code)) {184 value = parseNumber(context);185 type = NUMBER_TYPE;186 } else if (code === '"' || code === "'") {187 value = parseString(context);188 type = STRING_TYPE;189 } else if (code === "[") {190 advanceBy(context, 1);191 value = parseArray(context);192 type = ARRAY_TYPE;193 } else if (code === "{") {194 value = parseChildren(context);195 type = OBJECT_TYPE;196 } else if (context.source.indexOf("null") === 0) {197 value = parseNull(context);198 type = NULL_TYPE;199 } else if (context.source.indexOf("true") === 0 || context.source.indexOf("false") === 0) {200 value = parseBoolean(context);201 type = BOOLEAN_TYPE;202 } else if (context.source.indexOf("undefined") === 0) {203 value = parseUndefined(context);204 type = UNDEFINED_TYPE;205 } else {206 throw new Error(VALUE_ILLEGAL_ERROR_MESSAGE);207 }208 return {209 value: value,210 type: type211 };212}213function parseArray(context) {214 var nodes = [];215 var lastLine = getCursor(context).line;216 advanceSpaces(context);217 while (!isEnd(context)) {218 if (context.source.indexOf("//") === 0) {219 var cv = parseComment(context, lastLine);220 nodes.push(cv);221 advanceSpaces(context);222 } else {223 var itemValue = parseData(context, ARRAY_ITEM);224 lastLine = itemValue.loc.end.line;225 nodes.push(itemValue);226 }227 var s = context.source[0];228 if (s === "]") {229 advanceBy(context, 1);230 return nodes;231 }232 if (s === "}" || s === ":") {233 throw new Error(ARRAY_ERROR_MESSAGE);234 }235 }...
index.js
Source:index.js
...16 advanceBy(num) {17 context.source = context.source.slice(num);18 },19 // åè¿è¿ç»ç n ä¸ªç©ºæ ¼20 advanceSpaces() {21 const match = /^[\t\r\n\f ]+/.exec(context.source);22 if (match) {23 context.advanceBy(match[0].length);24 }25 },26 };27 // å¼å§è§£æèç¹28 const nodes = parseChildren(context, []);29 return {30 type: "Root",31 children: nodes,32 };33}34/**35 * 解æåèç¹36 * @param {*} context ä¸ä¸æ37 * @param {*} ancestors ç¶èç¹æ 38 */39function parseChildren(context, ancestors) {40 let nodes = [];41 while (!isEnd(context, ancestors)) {42 let node = null;43 if (context.source[0] === "<") {44 // æ ç¾ < å¼å¤´ // <div...45 node = parseElement(context, ancestors);46 } else if (context.source.startsWith("{{")) {47 // 解ææå¼ `{{title}}</div>`48 node = parseInterpolation(context);49 }50 // ä¸åå¨å¹é
çç±»å解æ为ææ¬51 if (!node) {52 // `Hello {{title}}</div>`53 node = parseText(context);54 }55 nodes.push(node);56 }57 return nodes;58}59/**60 * 解æå
ç´ 61 */62function parseElement(context, ancestors) {63 // 解ææ ç¾64 const element = parseTag(context);65 ancestors.push(element);66 // éå½åèç¹67 element.children = parseChildren(context, ancestors);68 ancestors.pop();69 // åèç¹è§£æç»æ70 if (context.source.startsWith(`</${element.tag}`)) {71 // æ¶è´¹å¯¹åºçéåæ ç¾72 parseTag(context, "end");73 }74 return element;75}76/**77 * 解ææ ç¾å称78 */79function parseTag(context, type = "start") {80 const { advanceBy, advanceSpaces } = context;81 // å¹é
æ ç¾å称82 let match = null;83 if (type === "start") {84 // /^<([a-z][^\t\r\n\f />]*)/i.exec(`<div class="home">Hello {{title}}</div>`) ==> ['<div', 'div']85 match = /^<([a-z][^\t\r\n\f />]*)/i.exec(context.source);86 } else {87 // /^<([a-z][^\t\r\n\f />]*)/i.exec(`</div>`) ==> ['</div', 'div']88 match = /^<\/([a-z][^\t\r\n\f />]*)/i.exec(context.source);89 }90 // æ¶è´¹æ ç¾91 advanceBy(match[0].length);92 // æ¶è´¹ç©ºæ ¼93 advanceSpaces();94 // å©ä½ ==> `class="home">Hello {{title}}</div>`95 // 解æå±æ§96 const props = parseAttributes(context);97 // æ¶è´¹è§£æå±æ§ä¹åç ">"98 advanceBy(1);99 // å©ä½ ==> `Hello {{title}}</div>`100 // è¿åèç¹ä¿¡æ¯101 return {102 type: "Element",103 tag: match[1],104 props,105 children: [],106 };107}108/**109 * 解ææ ç¾å±æ§110 */111function parseAttributes(context) {112 const { advanceBy, advanceSpaces } = context;113 const props = [];114 // æ ç¾ "<div" å¼å§ä¹å â>â ä¹åçé½æ¯å±æ§115 while (!context.source.startsWith(">")) {116 // 解æå±æ§ key117 // /^[^\t\r\n\f />][^\t\r\n\f />=]*/.exec(`class="home">Hello {{title}}</div>`) ==> ['class']118 const match = /^[^\t\r\n\f />][^\t\r\n\f />=]*/.exec(context.source);119 const name = match[0];120 // æ¶è´¹ name121 advanceBy(name.length);122 // æ¶è´¹ç©ºæ ¼123 advanceSpaces();124 // æ¶è´¹ = å·125 advanceBy(1);126 // æ¶è´¹ç©ºæ ¼127 advanceSpaces();128 // å©ä½ ==> `"home">Hello {{title}}</div>`129 // 解æå±æ§ value130 // æ¶è´¹å¼å· "131 advanceBy(1);132 // æªåå°ä¸ä¸ä¸ªå¼å·ä¹å为 value133 const value = context.source.slice(0, context.source.indexOf('"'));134 // æ¶è´¹ value135 advanceBy(value.length);136 // æ¶è´¹å¼å· "137 advanceBy(1);138 // æ¶è´¹ç©ºæ ¼139 advanceSpaces();140 // 解æä¸ä¸ª value141 props.push({142 type: "Attribute",143 name,144 value,145 });146 // å©ä½ ==> `>Hello {{title}}</ãdiv>`147 }148 // è¿åå±æ§ä¿¡æ¯149 return props;150}151/**152 * 解æææ¬153 */...
parse.js
Source:parse.js
...119function parseTag(context) {120 const match = /^<\/?([a-z][^\t\r\n\f />]*)/i.exec(context.source);121 const tag = match[1];122 advanceBy(context, match[0].length);123 advanceSpaces(context);124 const { props, directives } = parseAttributes(context);125 const isSelfClosing = context.source.startsWith('/>');126 advanceBy(context, isSelfClosing ? 2 : 1);127 const tagType = isComponent(tag, context) ?128 ElementTypes.COMPONENT :129 ElementTypes.ELEMENT;130 return {131 type: NodeTypes.ELEMENT,132 tag, // æ ç¾å,133 tagType, // æ¯ç»ä»¶è¿æ¯åçå
ç´ ,134 props, // å±æ§èç¹æ°ç»,135 directives, // æ令æ°ç»136 isSelfClosing, // æ¯å¦æ¯èªéåæ ç¾,137 children: [],138 };139}140//å¤ææ¯componentç»ä»¶å141function isComponent(tag, context) {142 return !context.options.isNativeTag(tag);143}144//解æelementèç¹çå±æ§åæ令145function parseAttributes(context) {146 const props = [];147 const directives = [];148 while (149 context.source.length &&150 !context.source.startsWith('>') &&151 !context.source.startsWith('/>')152 ) {153 let attr = parseAttribute(context);154 if (attr.type === NodeTypes.DIRECTIVE) {155 directives.push(attr);156 } else {157 props.push(attr);158 }159 }160 return { props, directives };161}162//解æä¾å¦ï¼id='foo',æ¿å°name->id,value->foo163function parseAttribute(context) {164 const match = /^[^\t\r\n\f />][^\t\r\n\f />=]*/.exec(context.source);165 const name = match[0];166 advanceBy(context, name.length);167 advanceSpaces(context);168 let value;169 if (context.source[0] === '=') {170 advanceBy(context, 1);171 advanceSpaces(context);172 value = parseAttributeValue(context);173 advanceSpaces(context);174 }175 // æ令 v-bind @click176 if (/^(:|@|v-)/.test(name)) {177 let dirName, argContent;178 if (name[0] === ':') {179 dirName = 'bind';180 argContent = name.slice(1);181 } else if (name[0] === '@') {182 dirName = 'on';183 argContent = name.slice(1);184 } else if (name.startsWith('v-')) {185 [dirName, argContent] = name.slice(2).split(':');186 }187 return {188 type: NodeTypes.DIRECTIVE,189 name: dirName,190 exp: value && {191 type: NodeTypes.SIMPLE_EXPRESSION,192 content: value.content,193 isStatic: false,194 }, // 表达å¼èç¹195 arg: argContent && {196 type: NodeTypes.SIMPLE_EXPRESSION,197 content: camelize(argContent),198 isStatic: true,199 }, // 表达å¼èç¹200 };201 }202 // å±æ§203 return {204 type: NodeTypes.ATTRIBUTE,205 name,206 value: value && {207 type: NodeTypes.TEXT,208 content: value.content,209 },210 };211}212function parseAttributeValue(context) {213 const quote = context.source[0];214 advanceBy(context, 1);215 const endIndex = context.source.indexOf(quote);216 const content = parseTextData(context, endIndex);217 advanceBy(context, 1);218 return { content };219}220function isEnd(context) {221 const s = context.source;222 return s.startsWith('</') || !s;223}224//å·¥å
·ï¼å»æåé¢çå符225function advanceBy(context, numberOfCharacters) {226 context.source = context.source.slice(numberOfCharacters);227}228//å·¥å
·,å»æç©ºæ ¼229function advanceSpaces(context) {230 const match = /^[\t\r\n\f ]+/.exec(context.source);231 if (match) {232 advanceBy(context, match[0].length);233 }...
compiler-dom.js
Source:compiler-dom.js
...17 return true;18 }19 return !source;20}21function advanceSpaces(context) {22 const match = /^[ \t\r\n]+/.exec(context.source);23 if (match) {24 advanceBy(context, match[0].length);25 }26}27function parseTag(context) {28 const start = getCursor(context); //<div/>29 // æåºæ¬çå
å符30 const match = /^<\/?([a-z][^ \t\r\n/>]*)/.exec(context.source); // igm31 const tag = match[1];32 advanceBy(context, match[0].length);33 advanceSpaces(context);34 const isSelfClosing = context.source.startsWith("/>");35 advanceBy(context, isSelfClosing ? 2 : 1);36 return {37 type: NodeTypes.ElEMENT,38 tag,39 isSelfClosing,40 loc: getSelection(context, start),41 };42}43function parseElement(context) {44 // 1.解ææ ç¾å45 let ele = parseTag(context); // <div></div>46 // è¿éå¯è½æå¿åï¼47 const children = parseChildren(context); // è¿éå¤çå¿åçæ¶å æå¯è½æ²¡æå¿åï¼é£å°±ç´æ¥è·³åºï¼ å¦æéå°ç»ææ ç¾å°±ç´æ¥è·³åºå°±å¥½äº...
03-parseTag.js
Source:03-parseTag.js
...11 const tag = match[1]12 const ns = context.options.getNamespace(tag, parent)13 // ååéå代ç 14 advanceBy(context, match[0].length)15 advanceSpaces(context)16 17 // save current state in case we need to re-parse attributes with v-pre18 const cursor = getCursor(context)19 const currentSource = context.source20 21 // check <pre> tag 22 if (context.options.isPreTag(tag)) {23 context.inPre = true24 }25 // Attributes.26 // 解æå±æ§27 let props = parseAttributes(context, type)28 // check v-pre29 if (){...}...
Using AI Code Generation
1const { chromium } = require('playwright');2const path = require('path');3(async () => {4 const browser = await chromium.launch();5 const page = await browser.newPage();6 await page.click('text=Docs');7 await page.click('text=API');8 await page.click('text=class: Page');9 await page.waitForSelector('text=Page');10 const pageElement = await page.$('text=Page');11 await pageElement.evaluate(element => element.scrollIntoViewIfNeeded());
Using AI Code Generation
1const { advanceTo } = require('@playwright/test');2const { advanceTime } = require('@playwright/test');3const { clear } = require('@playwright/test');4const { click } = require('@playwright/test');5const { close } = require('@playwright/test');6const { doubleClick } = require('@playwright/test');7const { dragAndDrop } = require('@playwright/test');8const { evalOnSelector } = require('@playwright/test');9const { evalOnSelectorAll } = require('@playwright/test');10const { evalOnSelectorAll } = require('@playwright/test');11const { fill } = require('@playwright/test');12const { focus } = require('@playwright/test');13const { frame } = require('@playwright/test');14const { goto } = require('@playwright/test');15const { hover } = require('@playwright/test');16const { innerHTML } = require('@playwright/test');17const { innerText } = require('@playwright/test');18const { inputValue } = require('@playwright/test');19const { isVisible } = require('@playwright/test');20const { isHidden } = require('@playwright/test');21const { isChecked } =
Using AI Code Generation
1const { advanceSpaces } = require('@playwright/test/lib/page');2const { test, expect } = require('@playwright/test');3test('should advance spaces', async ({ page }) => {4 await advanceSpaces(page, 2);5 await advanceSpaces(page, 2);6 await advanceSpaces(page, -1);7});8const { test, expect } = require('@playwright/test');9test('should advance spaces', async ({ page }) => {10 await page.keyboard.press('Tab');11 await page.keyboard.press('Tab');12 await page.keyboard.press('Shift+Tab');13});14Your name to display (optional):
Using AI Code Generation
1const { advanceSpaces } = require('@playwright/test/lib/utils/keyboardLayout');2const { test } = require('@playwright/test');3test('test', async ({ page }) => {4 await page.click('input[name="q"]');5 await page.keyboard.press('Shift+Tab');6 await page.keyboard.type(advanceSpaces(5));7 await page.keyboard.type('Hello');8});
Using AI Code Generation
1const { advanceSpaces } = require('playwright/lib/server/keyboardLayouts.js');2const text = advanceSpaces('hello world', 6);3const { advanceSpaces } = require('playwright/lib/server/keyboardLayouts.js');4const text = advanceSpaces('hello world', 6);5const { advanceSpaces } = require('playwright/lib/server/keyboardLayouts.js');6const text = advanceSpaces('hello world', 6);7const { advanceSpaces } = require('playwright/lib/server/keyboardLayouts.js');8const text = advanceSpaces('hello world', 6);9In the above code, we are importing the advanceSpaces method from the keyboardLayouts.js file of Playwright internal library. This method is used to advance the string by the given
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!