How to use handleStartTag method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

functions_68.js

Source: functions_68.js Github

copy
1var searchData=
2[
3  ['handlelink',['handleLink',['../classcom_1_1spider_1_1jspiderlibrary2_1_1_spider_1_1_parser.html#aa7bd808ea40da16b1e666113db1b1616',1,'com::spider::jspiderlibrary2::Spider::Parser']]],
4  ['handlesimpletag',['handleSimpleTag',['../classcom_1_1spider_1_1jspiderlibrary2_1_1_spider_1_1_parser.html#ae14a442ac7232170e7db1e8f872d6f8a',1,'com::spider::jspiderlibrary2::Spider::Parser']]],
5  ['handlestarttag',['handleStartTag',['../classcom_1_1spider_1_1jspiderlibrary2_1_1_spider_1_1_parser.html#a1a351dca2ed508a436546687e782d329',1,'com::spider::jspiderlibrary2::Spider::Parser']]],
6  ['handletext',['handleText',['../classcom_1_1spider_1_1jspiderlibrary2_1_1_spider_1_1_parser.html#a293631e270d69e91a29de14c980bc9c8',1,'com::spider::jspiderlibrary2::Spider::Parser']]]
7];
8
Full Screen

all_68.js

Source: all_68.js Github

copy
1var searchData=
2[
3  ['handlelink',['handleLink',['../classcom_1_1spider_1_1jspiderlibrary2_1_1_spider_1_1_parser.html#aa7bd808ea40da16b1e666113db1b1616',1,'com::spider::jspiderlibrary2::Spider::Parser']]],
4  ['handlesimpletag',['handleSimpleTag',['../classcom_1_1spider_1_1jspiderlibrary2_1_1_spider_1_1_parser.html#ae14a442ac7232170e7db1e8f872d6f8a',1,'com::spider::jspiderlibrary2::Spider::Parser']]],
5  ['handlestarttag',['handleStartTag',['../classcom_1_1spider_1_1jspiderlibrary2_1_1_spider_1_1_parser.html#a1a351dca2ed508a436546687e782d329',1,'com::spider::jspiderlibrary2::Spider::Parser']]],
6  ['handletext',['handleText',['../classcom_1_1spider_1_1jspiderlibrary2_1_1_spider_1_1_parser.html#a293631e270d69e91a29de14c980bc9c8',1,'com::spider::jspiderlibrary2::Spider::Parser']]],
7  ['htmlparse',['HTMLParse',['../classcom_1_1spider_1_1jspiderlibrary2_1_1_h_t_m_l_parse.html',1,'com::spider::jspiderlibrary2']]],
8  ['htmlparse_2ejava',['HTMLParse.java',['../_h_t_m_l_parse_8java.html',1,'']]]
9];
10
Full Screen

html-parser.js

Source: html-parser.js Github

copy
1const comment = /^<!\--/;
2const conditionalComment = /^!\[/;
3const doctype = /^<!DOCTYPE [^>]+>/i;
4const ncname = '[a-zA-Z_][\\w\\-\\.]*';
5const qnameCapture = `((?:${ncname}\\:))?${ncname}`;
6const startTagOpen = new RegExp(`^<${qnameCapture}`);
7const startTagClose = /^\s*(\/?)>/
8const attribute = /^\s*([^\s"'<>\/=]+)(?:\s*(=)\s*(?:"([^"]*)"+|'([^']*)'+|([^\s"'=<>`]+)))?/
9export function parseHTML(html,options){
10	while(html){
11		//解析注释标签
12		if(comment.test(html)){
13			//若为注释,则继续查找是否存在'-->'
14			const commentEnd = html.indexOf('-->')
15			if(commentEnd >= 0){
16				//若存在 '-->',继续判断options中是否保留注释
17				if(options.shouldKeepComment){
18					/*
19							若保留注释,则把注释截取出来传给options.comment 
20							创建注释类型的AST节点
21					*/
22					options.comment(html,substring(4,commentEnd))
23				}
24				//若不保留注释,则将游标移动到'-->'之后,继续向后解析
25				advance(commentEnd + 3)
26				continue;
27			}
28		}
29		//解析条件注释
30		if(conditionalComment.test(html)){
31			//若为条件注释,则继续查找是否存在']>'
32			const conditionalEnd = html.indexOf(']>');
33			if(conditionalEnd >= 0){
34				//若存在 ']>',则从原本的html字符串中把条件注释截掉
35				//把剩下的内容重新赋给html,继续向后配置
36				advance(conditionalEnd + 2);
37				continue;
38			}
39		}
40		/*
41			解析DOCTYPE
42			解析DOCTYPE的原理同解析注释完全相同
43		*/
44		const doctypeMatch = html.match(doctype);
45		if(doctypeMatch){
46			advance(doctypeMatch[0].length);
47			continue;
48		}
49		//解析开始标签
50		const start = html.match(startTagOpen);
51		if(start){
52			const match = {
53				tagName : start[1],
54				attrs:[],
55				start:index
56			}
57		}
58	}
59}
60//解析开始标签
61function parseStartTag(){
62	const start = html.match(startTagOpen);
63	//'<div></div>'.match(startTagOpen) => ['<div','div',index:0,input:'<div></div>']
64	if(start){
65		const match = {
66			tageName : start[1],
67			attrs : [],
68			start : index
69		}
70		advance(start[0].length);
71		let end,attr;
72		/*
73			<div a=1 b=2 c=3></div>
74			从<div之后到开始标签的结束符号‘>’之前,一直匹配属性attrs
75			所有属性匹配完成之后,html字符串还剩下
76			1.自闭合标签剩下:'/>'
77			2.非自闭合标签剩下'></div>'
78		*/
79		while(!(end == html.match(startTagClose)) && (attr == html.match(attribute))){
80			advance(attr[0].length);
81			match.attr.push(attr);
82		}
83		/*
84			这里判断了该标签是否为自闭合标签
85			自闭合标签如<input type='text'/>
86			非自闭合标签如:<div></div>
87			'></div>'.match(html.match(startTagClose)) => [">", "", index: 0, input: "></div>", groups: undefined]
88			'/>'.match(html.match(startTagClose)) => ["/>", "/", index: 0, input: "/><div></div>", groups: undefined]
89			因此,我们可以通过end[1]是否是 '/'来判断该标签是否是自闭合标签
90		*/ 
91		if(end){
92			match.unarySlash = end[1];
93			advance(end[0].length);
94			match.end = index;
95			return match;
96		}
97	}
98}
99/*
100	handleStartTag 函数用来对parseStartTag 函数的解析结果进行进一步处理,
101	它接受parseStartTag函数的返回值作为参数
102	handleStartTag 函数的开始定义几个常量
103*/
104function handleStartTag(match){
105	const tagName = match.tagName; //开始标签的标签名;
106	const unarySlash = match.unarySlash;//是否为自闭合标签的标志,自闭合为"" , 非自闭合为"/"
107	const unary = isUnaryTag(tagName) || !!unarySlash; //布尔值,标志是否为自闭合标签
108	const l = match.attrs.length; //match.attrs数组的长度
109	const attrs = new Array(l); //一个与match.attrs数组长度相等的数组
110 //结下来是循环处理提取出来的标签属性数组match.attrs
111 for( let i=0;i<l;i++){
112 	const args = match.attrs[i];
113 	//const args = ["class="a"", "class", "=", "a", undefined, undefined, index: 0, input: "class="a" id="b"></div>", groups: undefined]
114 	const value = args[3] || args[4] || args[5] || ''
115 	const shouldDecodeNewLines = tagName == 'a' && args[1] == 'href' ? options.shouldDecodeNewLinesForHerf : options.shouldDecodeNewLines 
116 	//最后将处理好的结果存入之前定义好的与match.attrs数组长度相等的attrs数组中
117 	attrs[i] = {
118 		name : args[i],
119 		value : decodeArr(value,shouldDecodeNewLines)
120 	}
121 }
122 //如果标签为非自闭合标签,则将标签推入栈中
123 if(!unary){
124 	stack.push({tag:tagName,lowerCasedTag:tagName.toLowerCase(),attrs:attrs})
125 	lastTag = tagName;
126 }
127 //如果该标签是自闭合标签,现在就可以调用start钩子函数并传入处理好的参数来创建AST节点了
128 if(options.start){
129 	options.start(tagName,attrs,unary,match.start,match.end);
130 }
131}
132function advance(n){
133	index += n; //index为解析游标
134	html = html.substring(n);
135}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)