rphtml 0.5.12

A html parser written in rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
#![allow(clippy::unnecessary_wraps)]
use crate::config::{ParseOptions, RenderOptions};
use crate::error::{ErrorKind, ParseError};
use crate::position::CodeRegion;
use crate::types::{BoxDynError, GenResult, HResult};
use htmlentity::entity::{decode_chars_to, encode_char, CharacterSet, EncodeType, Entity};
use lazy_static::lazy_static;
use std::collections::HashMap;
use std::fmt;
use std::rc::{Rc, Weak};
use std::{
	cell::{Ref, RefCell},
	env,
	fs::File,
	io::prelude::*,
	io::BufReader,
	path::Path,
};

/*
* constants
*/
const TAG_BEGIN_CHAR: char = '<';
const TAG_END_CHAR: char = '>';
const WS_CHAR: char = ' ';
const END_SLASH_CHAR: char = '/';
const EQUAL_CHAR: char = '=';
const DOUBLE_QUOTE_CHAR: char = '"';
const SINGLE_QUOTE_CHAR: char = '\'';
const EOF_CHAR: char = '\0';
const DASH_CHAR: char = '-';
const LEFT_BRACKET_CHAR: char = '[';
const RIGHT_BRACKET_CHAR: char = ']';
const ALLOC_CHAR_CAPACITY: usize = 200;
const ALLOC_NODES_CAPACITY: usize = 20;
// tagname's characters
const HTML_TAG_NAME: [char; 4] = ['h', 't', 'm', 'l'];
const PRE_TAG_NAME: [char; 3] = ['p', 'r', 'e'];
const SCRIPT_TAG_NAME: [char; 6] = ['s', 'c', 'r', 'i', 'p', 't'];
const STYLE_TAG_NAME: [char; 5] = ['s', 't', 'y', 'l', 'e'];
const TITLE_TAG_NAME: [char; 5] = ['t', 'i', 't', 'l', 'e'];
const TEXTAREA_TAG_NAME: [char; 8] = ['t', 'e', 'x', 't', 'a', 'r', 'e', 'a'];

lazy_static! {
	static ref DETECT_CHAR_MAP: HashMap<DetectChar, Vec<char>> = {
		use DetectChar::*;
		let mut map = HashMap::new();
		map.insert(Comment, vec![DASH_CHAR, DASH_CHAR]);
		map.insert(DOCTYPE, vec!['D', 'O', 'C', 'T', 'Y', 'P', 'E']);
		map.insert(
			XMLCDATA,
			vec![
				LEFT_BRACKET_CHAR,
				'C',
				'D',
				'A',
				'T',
				'A',
				LEFT_BRACKET_CHAR,
			],
		);
		map
	};
	static ref VOID_ELEMENTS: Vec<Vec<char>> = vec![
		vec!['i', 'm', 'g'],
		vec!['i', 'n', 'p', 'u', 't'],
		vec!['m', 'e', 't', 'a'],
		vec!['l', 'i', 'n', 'k'],
		vec!['b', 'r'],
		vec!['h', 'r'],
		vec!['c', 'o', 'l'],
		vec!['b', 'a', 's', 'e'],
		vec!['p', 'a', 'r', 'a', 'm'],
		vec!['s', 'o', 'u', 'r', 'c', 'e'],
		vec!['a', 'r', 'e', 'a'],
		vec!['e', 'm', 'b', 'e', 'd'],
		vec!['t', 'r', 'a', 'c', 'k'],
		vec!['w', 'b', 'r'],
	];
	static ref SPECIAL_TAG_MAP: HashMap<Vec<char>, SpecialTag> = {
		use SpecialTag::*;
		let mut map = HashMap::new();
		map.insert(vec!['s', 'v', 'g'], Svg);
		map.insert(vec!['m', 'a', 't', 'h'], MathML);
		map
	};
	static ref MUST_QUOTE_ATTR_CHARS: Vec<char> = vec![
		DOUBLE_QUOTE_CHAR,
		SINGLE_QUOTE_CHAR,
		TAG_BEGIN_CHAR,
		TAG_END_CHAR,
		EQUAL_CHAR,
		'`',
	];
}

// gather previous characters
fn chars_to_string(content: &[char]) -> String {
	content.iter().collect::<String>()
}

// create parse error
fn create_parse_error(kind: ErrorKind, position: usize, context: &str) -> HResult {
	let err = ParseError::new(kind, CodeRegion::from_context_index(context, position));
	Err(err)
}

fn is_void_tag(name: &[char]) -> bool {
	for cur_name in VOID_ELEMENTS.iter() {
		if is_equal_chars(cur_name, name, &None) {
			return true;
		}
	}
	false
}

fn is_plain_text_tag(name: &[char], case: &Option<NameCase>) -> bool {
	is_equal_chars(name, &TEXTAREA_TAG_NAME, case) || is_equal_chars(name, &TITLE_TAG_NAME, case)
}

fn is_script_or_style(name: &[char], case: &Option<NameCase>) -> bool {
	is_equal_chars(name, &STYLE_TAG_NAME, case) || is_equal_chars(name, &SCRIPT_TAG_NAME, case)
}

pub fn is_content_tag(name: &[char], case: &Option<NameCase>) -> bool {
	is_script_or_style(name, case) || is_plain_text_tag(name, case)
}

pub fn allow_insert(name: &[char], node_type: NodeType) -> bool {
	let lc_name = name
		.iter()
		.map(|ch| ch.to_ascii_lowercase())
		.collect::<Vec<char>>();
	// VOID TAGS
	if is_void_tag(&lc_name) {
		return false;
	}
	// TITLE/TEXTAREA
	if is_plain_text_tag(name, &None) {
		return node_type == NodeType::Text || node_type == NodeType::SpacesBetweenTag;
	}
	// XMLCDATA
	if node_type == NodeType::XMLCDATA {
		return SPECIAL_TAG_MAP.get(&lc_name).is_some();
	}
	true
}

#[derive(PartialEq, Eq, Hash)]
pub enum DetectChar {
	Comment,
	DOCTYPE,
	XMLCDATA,
}

#[derive(PartialEq, Eq, Debug, Clone, Copy, Default)]
pub enum NodeType {
	#[default]
	AbstractRoot = 0, // abstract root node
	HTMLDOCTYPE = 1,      // html doctype
	Comment = 2,          // comment
	Text = 3,             // text node
	SpacesBetweenTag = 4, // spaces between tag
	Tag = 5,              // the start tag\self-closing tag\autofix empty tag
	TagEnd = 6,           // the end tag node
	XMLCDATA = 7,         // XML CDATA, IN SVG OR MATHML
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CodeTypeIn {
	AbstractRoot,      // abstract root node,the begin node of document
	Unkown,            // wait for detect node
	UnkownTag,         // is a tag begin with '<', but need more diagnosis
	Tag,               // the start tag\self-closing tag\autofix empty tag
	TagEnd,            // the end tag
	ExclamationBegin,  // tag begin with '!' maybe Comment|HTMLDOCTYPE
	Comment,           // comment tag
	HTMLDOCTYPE,       // html doctype
	EscapeableRawText, // escapeable raw text, <title> and <textarea>
	HTMLScript,        // html script
	HTMLStyle,         // html style
	XMLCDATA,          // XMLCDATA section
	TextNode,          // text node
}

fn write_content_encode(content: &[char], chars: &mut Vec<char>) {
	let character_set = CharacterSet::Html;
	let encode_type = EncodeType::Named;
	content.iter().for_each(|ch| {
		if character_set.contains(ch) {
			if let Some(entity) = encode_char(ch, &encode_type) {
				entity.write_chars(chars);
			} else {
				chars.push(*ch);
			}
		} else {
			chars.push(*ch);
		}
	});
}

// trim chars end whitespaces
fn chars_trim_end(target: &[char]) -> &[char] {
	let mut end_index: usize = target.len();
	for ch in target.iter().rev() {
		if !ch.is_ascii_whitespace() {
			break;
		}
		end_index -= 1;
	}
	&target[..end_index]
}

// check if equal
fn is_equal_chars(target: &[char], cmp: &[char], case: &Option<NameCase>) -> bool {
	if target.len() != cmp.len() {
		return false;
	}
	// check characters
	let is_equal = if let Some(case) = case {
		match case {
			NameCase::Lower => |a: &char, b: &char| -> bool { a == b || &a.to_ascii_lowercase() == b },
			NameCase::Upper => |a: &char, b: &char| -> bool { a == b || &a.to_ascii_uppercase() == b },
		}
	} else {
		|a: &char, b: &char| -> bool { a == b }
	};
	for (index, ch) in target.iter().enumerate() {
		if !is_equal(ch, &cmp[index]) {
			return false;
		}
	}
	true
}

fn is_equal_chars_ignore_case(target: &[char], cmp: &[char]) -> (bool, bool) {
	if target.len() != cmp.len() {
		return (false, false);
	}
	let mut is_total_same = true;
	for (index, ch) in target.iter().enumerate() {
		let cmp_ch = &cmp[index];
		if cmp_ch == ch {
			continue;
		}
		// test if totally same
		is_total_same = false;
		match cmp_ch {
			'a'..='z' => {
				if &cmp_ch.to_ascii_uppercase() != ch {
					return (false, false);
				}
			}
			'A'..='Z' => {
				if &cmp_ch.to_ascii_lowercase() != ch {
					return (false, false);
				}
			}
			_ => {
				// not equal
				return (false, false);
			}
		}
	}
	(true, is_total_same)
}
/**
 * Attr
 * attribute data
 * if value is None, it's a boolean attribute
 * if key is None,it's a value with quote
 */

#[derive(Debug, Default, Clone)]
pub struct Attr {
	pub key: Option<AttrData>,
	pub value: Option<AttrData>,
	pub quote: Option<char>,
	pub need_quote: bool,
}

#[derive(Debug, Default, Clone)]
pub struct AttrData {
	pub content: Vec<char>,
}

impl Attr {
	// build attribute code
	pub fn build(&self, remove_quote: bool) -> Vec<char> {
		let mut ret = Vec::with_capacity(ALLOC_CHAR_CAPACITY);
		let mut has_key = false;
		if let Some(AttrData { content, .. }) = &self.key {
			ret.extend_from_slice(content);
			has_key = true;
		}
		if let Some(AttrData { content, .. }) = &self.value {
			if has_key {
				ret.push(EQUAL_CHAR);
			}
			if let Some(quote) = self.quote {
				if self.need_quote || !remove_quote {
					ret.push(quote);
					ret.extend_from_slice(content);
					ret.push(quote);
					return ret;
				}
			}
			ret.extend_from_slice(content);
		}
		ret
	}
	// check if the char need quote
	pub fn need_quoted_char(ch: &char) -> bool {
		ch.is_ascii_whitespace() || MUST_QUOTE_ATTR_CHARS.contains(ch)
	}
	// check if id
	pub fn check_if_id(&self) -> Option<String> {
		if let Some(key) = &self.key {
			if is_equal_chars(&key.content, &['i', 'd'], &Some(NameCase::Lower)) {
				if let Some(value) = &self.value {
					return Some(chars_to_string(&value.content));
				}
			}
		}
		None
	}
}

pub enum NameCase {
	Upper,
	Lower,
}
/**
 * Tag
 * is_end: if the tag end with '>'
 * self_closed: if the tag is self-closing '/>'
 * auto_fix: if the tag either self-closing nor closed with a end tag, may auto fix by the parser
 * name: the tag name
 * attrs: the attribute list
*/
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub enum TagCodeIn {
	#[default]
	Wait,
	Key,
	KeyEnd,
	WaitValue,
	Value,
	ValueEnd,
}

#[derive(Debug, Default, Clone)]
pub struct TagMeta {
	code_in: TagCodeIn,
	pub is_void: bool,
	pub self_closed: bool,
	pub auto_fix: bool,
	pub name: Vec<char>,
	pub attrs: Vec<Attr>,
	pub lc_name_map: HashMap<String, usize>,
}

impl TagMeta {
	pub fn attrs_to_string(&self, remove_quote: bool) -> Vec<char> {
		self
			.attrs
			.iter()
			.flat_map(|attr| {
				let mut attr_content = attr.build(remove_quote);
				attr_content.splice(0..0, vec![WS_CHAR]);
				attr_content
			})
			.collect()
	}
	// add a key attr
	pub fn add_attr_key(&mut self) {
		self.attrs.push(Attr {
			key: Some(AttrData::default()),
			..Default::default()
		});
	}
	// add a only value attr
	pub fn add_attr_value(&mut self, quote: Option<char>) {
		self.attrs.push(Attr {
			value: Some(AttrData::default()),
			quote,
			..Default::default()
		});
	}
	// set attr key
	pub fn set_attr_key(&mut self, key: Vec<char>) {
		let attr = self
			.attrs
			.last_mut()
			.expect("Attrs must not be empty when call set_attr_key");
		attr.key = Some(AttrData { content: key });
	}
	// set attr value
	pub fn set_attr_value(&mut self, value: Vec<char>, quote: Option<char>) -> &Attr {
		let attr = self
			.attrs
			.last_mut()
			.expect("Attrs must not be empty when call set_attr_key");
		attr.value = Some(AttrData { content: value });
		attr.quote = quote;
		attr
	}
}

pub type RefNode = Rc<RefCell<Node>>;

type RefDoc = Rc<RefCell<Doc>>;

#[derive(Default, Clone)]
struct RenderStatus {
	inner_type: RenderStatuInnerType,
	is_in_pre: bool,
	root: bool,
}
#[derive(Clone, Default)]
enum RenderStatuInnerType {
	#[default]
	None,
	Html,
	Text,
}

/**
 *
 */
#[derive(Default)]
pub struct Node {
	// the node's index of the parent's all childs
	pub index: usize,

	// the node's type
	pub node_type: NodeType,

	// the node's start position '<'
	pub begin_at: usize,

	// the node's end position '>'
	pub end_at: usize,

	// the end tag </xx> of the tag node
	pub end_tag: Option<RefNode>,

	// prev node
	pub prev: Option<Weak<RefCell<Node>>>,

	// parent node, use weak reference,prevent reference loop
	pub parent: Option<Weak<RefCell<Node>>>,

	// root node
	pub root: Option<Weak<RefCell<Node>>>,

	// document
	pub document: Option<Weak<RefCell<Doc>>>,

	// the content,for text/comment/style/script nodes
	pub content: Option<Vec<char>>,

	// the child nodes
	pub childs: Option<Vec<RefNode>>,

	// the tag node meta information
	pub meta: Option<RefCell<TagMeta>>,
}

impl fmt::Debug for Node {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		f.debug_struct("Node")
			.field("index", &self.index)
			.field("node_type", &self.node_type)
			.field("begin_at", &self.begin_at)
			.field("end_at", &self.end_at)
			.field("content", &self.content)
			.field("childs", &self.childs)
			.field("meta", &self.meta)
			.field("end_tag", &self.end_tag)
			.field("parent", &self.parent.is_some())
			.field("root", &self.root.is_some())
			.field("document", &self.document.is_some())
			.finish()
	}
}

impl Clone for Node {
	fn clone(&self) -> Self {
		let ref_node = self.clone_node();
		let cell_node = Rc::try_unwrap(ref_node).unwrap();
		cell_node.into_inner()
	}
}

impl Node {
	// create a new node
	pub fn new(node_type: NodeType, code_at: usize) -> Self {
		Node {
			node_type,
			begin_at: code_at,
			end_at: code_at,
			..Default::default()
		}
	}

	// clone node but without childs
	fn clone_node_without_childs(&self, reduce_code_at: usize) -> RefNode {
		Rc::new(RefCell::new(Node {
			begin_at: self.begin_at - reduce_code_at,
			end_at: self.end_at - reduce_code_at,
			index: 0,
			node_type: self.node_type,
			root: self.root.clone(),
			parent: None,
			content: self.content.clone(),
			document: self.document.clone(),
			meta: self
				.meta
				.as_ref()
				.map(|mt| RefCell::new(mt.borrow().clone())),
			childs: None,
			prev: None,
			end_tag: self
				.end_tag
				.as_ref()
				.map(|end_tag| end_tag.borrow().clone_node_with(reduce_code_at)),
		}))
	}

	// clone node with reduced code at
	fn clone_node_with(&self, reduce_code_at: usize) -> RefNode {
		let parent_node = self.clone_node_without_childs(reduce_code_at);
		let mut cur_childs: Vec<RefNode> = vec![];
		if let Some(childs) = &self.childs {
			for node in childs {
				let cur_node = node.borrow().clone_node_with(reduce_code_at);
				cur_node.borrow_mut().parent = Some(Rc::downgrade(&parent_node));
				cur_node.borrow_mut().prev = cur_childs.last().map(Rc::downgrade);
				cur_node.borrow_mut().index = node.borrow().index;
				cur_childs.push(cur_node);
			}
			parent_node.borrow_mut().childs = Some(cur_childs);
		}
		parent_node
	}

	// clone node
	pub fn clone_node(&self) -> RefNode {
		self.clone_node_with(self.begin_at)
	}

	// create text node
	pub fn create_text_node(content: Vec<char>, code_at: Option<usize>) -> Self {
		let mut is_all_spaces = true;
		for ch in &content {
			if !ch.is_ascii_whitespace() {
				is_all_spaces = false;
				break;
			}
		}
		let node_type = if is_all_spaces {
			NodeType::SpacesBetweenTag
		} else {
			NodeType::Text
		};
		let code_at = code_at.unwrap_or_default();
		Node {
			node_type,
			begin_at: code_at,
			end_at: code_at,
			content: Some(content),
			..Default::default()
		}
	}
	// build node
	fn build_node(&self, options: &RenderOptions, status: &mut RenderStatus, result: &mut Vec<char>) {
		let is_in_pre = status.is_in_pre;
		let is_root = status.root;
		// when get a tag's inner html or inner text
		// the result should not contains itself(tag start or tag end)
		// get inner text always do not contains itself
		let need_tag = if is_root {
			matches!(status.inner_type, RenderStatuInnerType::None)
		} else {
			// should not be inner text
			!matches!(status.inner_type, RenderStatuInnerType::Text)
		};
		use NodeType::*;
		match self.node_type {
			Text => {
				// original content
				let content = self
					.content
					.as_ref()
					.expect("Text node's content must not empty");
				// check if need decode
				if !is_in_pre && options.minify_spaces {
					// just keep one space
					let mut prev_is_space = false;
					if options.decode_entity {
						let mut start_index: usize = 0;
						let mut is_in_entity = false;
						for (index, ch) in content.iter().enumerate() {
							if !is_in_entity {
								if ch == &'&' {
									is_in_entity = true;
									start_index = index;
								} else {
									// judge if whitespace
									if ch.is_ascii_whitespace() {
										if prev_is_space {
											continue;
										}
										prev_is_space = true;
									} else {
										prev_is_space = false;
									}
									result.push(*ch);
								}
							} else if ch == &';' {
								// entity end
								let entity = &content[start_index + 1..index];
								if let Ok(decoded) = Entity::decode_chars(entity) {
									result.push(decoded);
								} else {
									result.push('&');
									result.extend_from_slice(entity);
									result.push(';');
								}
								is_in_entity = false;
							}
						}
						// has suffix wrong entity
						if is_in_entity {
							result.extend(&content[start_index..]);
						}
					} else {
						// just add one space when meet repeated whitespaces
						for &c in content {
							if c.is_ascii_whitespace() {
								if prev_is_space {
									continue;
								}
								prev_is_space = true;
							} else {
								prev_is_space = false;
							}
							result.push(c);
						}
					}
				} else {
					// decode entity
					if options.decode_entity {
						// when need decode, the content should append to result
						decode_chars_to(content, result);
					} else {
						result.extend_from_slice(content);
					}
				}
			}
			Tag => {
				let meta = self
					.meta
					.as_ref()
					.expect("tag's meta data must have.")
					.borrow();
				let tag_name = &meta.name;
				// check if is in pre, only check if not in pre
				status.is_in_pre =
					is_in_pre || is_equal_chars(tag_name, &PRE_TAG_NAME, &Some(NameCase::Lower));
				if need_tag {
					// add tag name
					result.push('<');
					if !options.lowercase_tagname {
						result.extend_from_slice(tag_name);
					} else {
						for ch in tag_name {
							result.push(ch.to_ascii_lowercase());
						}
					}
					// add attrs
					if !meta.attrs.is_empty() {
						let attrs = meta.attrs_to_string(options.remove_attr_quote);
						result.extend_from_slice(&attrs);
					}
					// add self closing
					if meta.self_closed || (meta.auto_fix && options.always_close_void) {
						result.push(WS_CHAR);
						result.push('/');
					}
					// add end char
					result.push(TAG_END_CHAR);
				}
				// content for some special tags, such as style/script
				if let Some(content) = &self.content {
					let need_encode =
						options.encode_content && is_plain_text_tag(tag_name, &Some(NameCase::Lower));
					if !need_encode {
						result.extend_from_slice(content);
					} else {
						// content tag's html need encode
						write_content_encode(content, result);
					}
				}
			}
			TagEnd => {
				let content = self
					.content
					.as_ref()
					.expect("End tag's tag name must not empty");
				let mut content = &content[..];
				if is_in_pre
					&& is_equal_chars(
						chars_trim_end(content),
						&PRE_TAG_NAME,
						&Some(NameCase::Lower),
					) {
					status.is_in_pre = false;
				}
				if need_tag {
					result.extend_from_slice(&['<', '/']);
					if options.remove_endtag_space {
						content = chars_trim_end(content);
					}
					if options.lowercase_tagname {
						let content = content
							.iter()
							.map(|e| e.to_ascii_lowercase())
							.collect::<Vec<char>>();
						result.extend(content);
					} else {
						result.extend_from_slice(content);
					}
					result.push('>');
				}
			}
			SpacesBetweenTag => {
				if !is_in_pre && options.minify_spaces {
					// do nothing
				} else {
					let content = self
						.content
						.as_ref()
						.expect("Spaces between node must have whitespcaes");
					result.extend_from_slice(content);
				}
			}
			HTMLDOCTYPE => {
				let meta = self
					.meta
					.as_ref()
					.expect("tag's meta data must have.")
					.borrow();
				result.extend_from_slice(&['<', '!']);
				result.extend_from_slice(&meta.name);
				if !meta.attrs.is_empty() {
					result.extend_from_slice(&meta.attrs_to_string(options.remove_attr_quote));
				}
				result.push('>');
			}
			Comment => {
				if !options.remove_comment {
					let is_inner_text = matches!(status.inner_type, RenderStatuInnerType::Text);
					if is_root || !is_inner_text {
						// when need wrap, use <!-- -->
						// only when get the comment node itself's text
						// get inner text of tag node, doesn't contains the comment node
						let need_wrap = !(is_root && is_inner_text);
						// comment
						if need_wrap {
							result.extend_from_slice(&['<', '!', '-', '-']);
						}
						if let Some(content) = &self.content {
							result.extend_from_slice(content);
						}
						if need_wrap {
							result.extend_from_slice(&['-', '-', '>']);
						}
					}
				}
			}
			XMLCDATA => {
				result.extend_from_slice(&['<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[']);
				if let Some(content) = &self.content {
					result.extend_from_slice(content);
				}
				result.extend_from_slice(&[']', ']', '>']);
			}
			_ => {}
		}
	}
	// build node tree
	fn build_tree(&self, options: &RenderOptions, status: &mut RenderStatus, result: &mut Vec<char>) {
		self.build_node(options, status, result);
		if let Some(childs) = &self.childs {
			if status.root {
				// change status root
				let mut sub_status = status.clone();
				sub_status.root = false;
				// get inner html
				for child in childs {
					child.borrow().build_tree(options, &mut sub_status, result);
				}
			} else {
				for child in childs {
					// keep the original inner type
					child.borrow().build_tree(options, status, result);
				}
			}
		}
		if let Some(end_tag) = &self.end_tag {
			end_tag.borrow().build_node(options, status, result);
		}
	}
	// build
	pub fn build(&self, options: &RenderOptions, inner_text: bool) -> Vec<char> {
		let inner_type = if inner_text {
			// wrong render options when call inner_text
			if options.inner_html {
				panic!("The 'inner_html' render option can't set true when 'inner_text' is true");
			}
			RenderStatuInnerType::Text
		} else if options.inner_html {
			RenderStatuInnerType::Html
		} else {
			RenderStatuInnerType::None
		};
		let throw_wrong_node = |node_type: &NodeType| -> ! {
			panic!(
				"`inner_html` should only used for tag node, but found '{:?}'",
				node_type
			);
		};
		let status = &mut RenderStatus {
			inner_type,
			root: true,
			..Default::default()
		};
		let mut result: Vec<char> = Vec::with_capacity(50);
		// inner_html or inner_text
		if matches!(
			status.inner_type,
			RenderStatuInnerType::Html | RenderStatuInnerType::Text
		) {
			if matches!(self.node_type, NodeType::AbstractRoot) {
				// inner html for abstract root
				if let Some(childs) = &self.childs {
					let mut finded = false;
					let mut child_node: Option<Rc<RefCell<Node>>> = None;
					for child in childs {
						if child.borrow().node_type == NodeType::Tag {
							if finded {
								panic!("`inner_html` can't used in abstract root node which has multiple tag node childs.");
							}
							child_node = Some(Rc::clone(child));
							finded = true;
						}
					}
					if let Some(child_node) = child_node {
						child_node.borrow().build_tree(options, status, &mut result);
						return result;
					}
					// no tag child node finded
					throw_wrong_node(&childs[childs.len() - 1].borrow().node_type);
				}
				// abstract without any child
				return vec![];
			}
			match status.inner_type {
				RenderStatuInnerType::Html => {
					// only tag node allowed build html
					if self.node_type != NodeType::Tag {
						throw_wrong_node(&self.node_type);
					}
				}
				RenderStatuInnerType::Text => {
					// tag/comment/cdata allowed build text
					if !(matches!(
						self.node_type,
						NodeType::Tag | NodeType::Comment | NodeType::XMLCDATA
					)) {
						throw_wrong_node(&self.node_type);
					}
				}
				_ => {}
			}
		}
		self.build_tree(options, status, &mut result);
		result
	}

	// append a child
	// is document node
	pub fn is_document(&self) -> (bool, bool) {
		let mut is_document = false;
		let mut syntax_ok = true;
		use NodeType::*;
		if self.node_type == AbstractRoot {
			if let Some(childs) = &self.childs {
				let mut find_html = false;
				for child in childs {
					let child_node = child.borrow();
					match child_node.node_type {
						Comment | SpacesBetweenTag | HTMLDOCTYPE => {
							// the above types are allowed in document
						}
						Tag => {
							if find_html {
								syntax_ok = false;
								break;
							} else {
								// check if is html tag
								if let Some(meta) = &child_node.meta {
									if is_equal_chars(
										meta.borrow().name.as_slice(),
										&HTML_TAG_NAME,
										&Some(NameCase::Lower),
									) {
										find_html = true;
										is_document = true;
									} else {
										syntax_ok = false;
									}
								} else {
									syntax_ok = false;
								}
							}
						}
						_ => {
							syntax_ok = false;
						}
					}
				}
			}
			if !is_document {
				syntax_ok = true;
			}
		}
		(is_document, syntax_ok)
	}
	// check if two RefNode is same
	pub fn is_same(cur: &RefNode, other: &RefNode) -> bool {
		std::ptr::eq(cur.as_ptr() as *const _, other.as_ptr() as *const _)
	}
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum SpecialTag {
	MathML,
	Svg,
}

type NextHandle = fn(&mut Doc, char, &str) -> HResult;

/*
* No operation, just for initialization
*/
fn noop(_d: &mut Doc, _c: char, _content: &str) -> HResult {
	Ok(())
}

/*
 * code_in:  Unkown | AbstractRoot
*/
fn parse_wait(doc: &mut Doc, c: char, _: &str) -> HResult {
	match c {
		// match the tag start '<'
		TAG_BEGIN_CHAR => {
			doc.mem_position = doc.position;
			doc.set_code_in(CodeTypeIn::UnkownTag);
		}
		_ => {
			if doc.parse_options.auto_fix_unexpected_endtag
				&& matches!(
					doc.current_node.borrow().node_type,
					NodeType::Text | NodeType::SpacesBetweenTag
				) {
				// fix the unexpected endtag
				let mut current_node = doc.current_node.borrow_mut();
				// remove content to prev chars
				if let Some(content) = &mut current_node.content {
					doc.prev_chars.append(content);
				}
				if matches!(current_node.node_type, NodeType::Text) {
					doc.repeat_whitespace = false;
				} else {
					doc.repeat_whitespace = c.is_ascii_whitespace();
				}
			} else {
				// new text node
				doc.add_new_node(Rc::new(RefCell::new(Node::new(
					NodeType::Text,
					doc.position,
				))));
				doc.repeat_whitespace = c.is_ascii_whitespace();
			}
			doc.prev_chars.push(c);
			doc.set_code_in(CodeTypeIn::TextNode);
		}
	}
	Ok(())
}

/**
 * code_in: Text
*/
fn parse_text(doc: &mut Doc, c: char, _: &str) -> HResult {
	use CodeTypeIn::*;
	match c {
		// match the tag start '<'
		TAG_BEGIN_CHAR => {
			let content = doc.clean_chars_to_vec();
			doc.current_node.borrow_mut().content = Some(content);
			doc.check_textnode = if doc.repeat_whitespace {
				Some(Rc::clone(&doc.current_node))
			} else {
				None
			};
			doc.mem_position = doc.position;
			doc.set_tag_end_info();
			doc.set_code_in(UnkownTag);
		}
		_ => {
			// check if repeat whitespace
			if doc.repeat_whitespace {
				doc.repeat_whitespace = c.is_ascii_whitespace();
			}
			doc.prev_chars.push(c);
		}
	}
	Ok(())
}

/**
* code_in: Doctype name
*/
fn parse_doctype_name(doc: &mut Doc, c: char, context: &str) -> HResult {
	if c.is_ascii_whitespace() {
		doc.set_tag_meta();
		doc.set_tag_code_in(TagCodeIn::Wait);
		return Ok(());
	}
	// wrong doctype
	doc.error(ErrorKind::WrongHtmlDoctype(c), context)
}

/**
 * code_in: Tag name
 */
// parse tag name
fn parse_tag_name(doc: &mut Doc, c: char, _: &str) -> HResult {
	// tag name will setted in this process
	// 1. <a>
	// 2. <a/>
	// 3. <a /> <a href="">
	use CodeTypeIn::*;
	match c {
		TAG_END_CHAR => {
			// detect if void tag
			let is_void = doc.set_tag_meta();
			if is_void {
				// tag is end
				doc.chain_nodes.pop();
				doc.set_code_in(Unkown);
			} else {
				// check the tag type
				doc.check_tag_type_do();
			}
		}
		END_SLASH_CHAR => {
			// maybe self closing tag
			// if is doctype, not a good doctype
			doc.set_tag_meta();
			doc.handle = parse_tag_self_closing;
		}
		_ => {
			if c.is_ascii_whitespace() {
				// wait for attribute or end
				doc.set_tag_meta();
				doc.set_tag_code_in(TagCodeIn::Wait);
			} else {
				doc.prev_chars.push(c);
			}
		}
	};
	Ok(())
}

/**
 * Tag self closing
 */
// parse tag self closing
fn parse_tag_self_closing(doc: &mut Doc, c: char, context: &str) -> HResult {
	// The self closing tag will parsed in this proces
	// 1. /> ///>
	// 2. /a="33"  //a="33"
	match c {
		TAG_END_CHAR => {
			if let Some(meta) = &doc.current_node.borrow_mut().meta {
				if !doc.parse_options.allow_self_closing && !matches!(doc.code_in, CodeTypeIn::HTMLDOCTYPE)
				{
					// void elements or self closing element
					// sub element in Svg or MathML allow self-closing
					if !(meta.borrow().is_void || doc.in_special.is_some()) {
						return doc.error(
							ErrorKind::WrongSelfClosing(chars_to_string(&meta.borrow().name)),
							context,
						);
					}
				}
				meta.borrow_mut().self_closed = true;
			} else {
				panic!("self-closing tag's meta is emtpy");
			}
			doc.chain_nodes.pop();
			doc.set_code_in(CodeTypeIn::Unkown);
		}
		END_SLASH_CHAR => {
			// ignore the end slash tag name character
			// take the end slash equal to empty character
			// e.g. <a//> tag name is 'a'
		}
		_ => {
			// just delegate to wait process
			parse_tag_wait(doc, c, context)?;
		}
	}
	Ok(())
}

/// in tag, wait attribute or end
fn parse_tag_wait(doc: &mut Doc, c: char, context: &str) -> HResult {
	// All tag wait status will parsed in this process
	// 1. spaces between attributes TagCodeIn::Wait
	// 2. (spaces)?= TagCodeIn::KeyEnd
	// 3. new attribute key
	// 4. attribute value
	// 5. double quote or single quote value attribute
	// 6. tag end
	// 7. self closing
	match c {
		DOUBLE_QUOTE_CHAR | SINGLE_QUOTE_CHAR => {
			// only attribute value without attribute key
			// add a attribute value
			let is_in_wait_value = doc.is_tag_code_in(&TagCodeIn::WaitValue);
			if !is_in_wait_value {
				// attribute key = "value"
				// no need add attribute to queue, just change the previous attribute's value
				if matches!(doc.code_in, CodeTypeIn::HTMLDOCTYPE) {
					doc.add_tag_attr_value(Some(c));
				} else {
					// take quote value as attribute key too
					doc.add_tag_attr_key();
					doc.prev_chars.push(c);
					doc.set_tag_code_in(TagCodeIn::Key);
					return Ok(());
				}
			}
			doc.mark_char = c;
			doc.set_tag_code_in(TagCodeIn::Value);
		}
		TAG_END_CHAR => {
			if doc
				.current_node
				.borrow()
				.meta
				.as_ref()
				.expect("When parse tag in wait, the tag meta must not empty.")
				.borrow()
				.is_void
			{
				// if void tag, pop from chains, set in unkown
				doc.chain_nodes.pop();
				doc.set_code_in(CodeTypeIn::Unkown);
			} else {
				// need check the tag if content tags or other normal tags
				doc.check_tag_type_do();
			}
		}
		END_SLASH_CHAR => {
			// parse self closing
			doc.handle = parse_tag_self_closing;
		}
		EQUAL_CHAR => {
			// value
			if doc.is_tag_code_in(&TagCodeIn::KeyEnd) {
				// the prev process is parse attr key
				doc.set_tag_code_in(TagCodeIn::WaitValue);
			} else if doc.is_tag_code_in(&TagCodeIn::Wait) {
				// equal char is not a valid attribute key name
				if !doc.parse_options.allow_attr_key_starts_with_equal_sign {
					return create_parse_error(
						ErrorKind::WrongTag(String::from("'=' is not a valid attribute name")),
						doc.position,
						context,
					);
				}
				// take it as normal key
				doc.prev_chars.push(c);
				// add attr key
				doc.add_tag_attr_key();
				// parse attr key
				doc.set_tag_code_in(TagCodeIn::Key);
			} else {
				// take as non quoted attribute key value
				doc.prev_chars.push(c);
				// jump to parse value
				doc.mark_char = WS_CHAR;
				doc.set_tag_code_in(TagCodeIn::Value);
			}
		}
		_ => {
			// ignore whitespaces
			if !c.is_ascii_whitespace() {
				// parse tag attribute
				doc.prev_chars.push(c);
				if doc.is_tag_code_in(&TagCodeIn::WaitValue) {
					// set the value end character as empty character
					doc.mark_char = WS_CHAR;
					// parse tag value
					doc.set_tag_code_in(TagCodeIn::Value);
				} else {
					// add attr key
					doc.add_tag_attr_key();
					// parse attr key
					doc.set_tag_code_in(TagCodeIn::Key);
				}
			}
			// ignore whitespaces, just change the detect prev char
		}
	}
	Ok(())
}

// parse tag attribute key
fn parse_tag_attr_key(doc: &mut Doc, c: char, context: &str) -> HResult {
	// attr key will parsed in this process
	match c {
		EQUAL_CHAR => {
			// attribute end
			doc.set_tag_attr_key();
			// set tag wait
			doc.set_tag_code_in(TagCodeIn::WaitValue);
		}
		TAG_END_CHAR => {
			// attribute key end
			doc.set_tag_attr_key();
			doc.set_tag_code_in(TagCodeIn::KeyEnd);
			// just delegate to parse wait
			parse_tag_wait(doc, c, context)?;
		}
		END_SLASH_CHAR => {
			// self closing
			doc.set_tag_attr_key();
			// set tag code in
			doc.set_tag_code_in(TagCodeIn::Wait);
			doc.handle = parse_tag_self_closing;
		}
		_ => {
			if c.is_ascii_whitespace() {
				// end of the attribute key
				// attribute end
				doc.set_tag_attr_key();
				// set in wait
				doc.set_tag_code_in(TagCodeIn::KeyEnd);
				// set handle
				doc.handle = parse_tag_wait;
			} else {
				// all charactes except the characters above was allowed
				doc.prev_chars.push(c);
			}
		}
	};
	Ok(())
}

// parse tag attribute double quote value
fn parse_tag_attr_value(doc: &mut Doc, c: char, context: &str) -> HResult {
	// logic
	let quote = doc.mark_char;
	let (is_end, attr_quote) = if quote == WS_CHAR {
		if c == TAG_END_CHAR {
			// unquoted value
			// check if avaiable character in value
			// set tag value parse end
			doc.set_tag_attr_value(None);
			doc.set_tag_code_in(TagCodeIn::ValueEnd);
			// delegate to parse wait
			parse_tag_wait(doc, c, context)?;
			return Ok(());
		}
		(c.is_ascii_whitespace(), None)
	} else {
		(c == quote, Some(quote))
	};
	if is_end {
		// reset the detect char
		doc.mark_char = EOF_CHAR;
		doc.set_tag_attr_value(attr_quote);
		doc.set_tag_code_in(TagCodeIn::Wait);
	} else {
		doc.prev_chars.push(c);
	}
	Ok(())
}

/**
 * code_in: TagEnd
 */
fn parse_tagend(doc: &mut Doc, c: char, context: &str) -> HResult {
	use CodeTypeIn::*;
	// the end tag
	match c {
		TAG_END_CHAR => {
			let end_tag_name = doc.prev_chars.clone();
			let fix_end_tag_name = chars_trim_end(&end_tag_name);
			let mut is_endtag_ok = false;
			// check if the tag matched
			if doc.chain_nodes.len() > 1 {
				if let Some(tag) = doc.chain_nodes.last() {
					let tag = tag.borrow();
					let meta = tag
						.meta
						.as_ref()
						.expect("Tag node must have a meta of tag name")
						.borrow();
					let start_tag_name = &meta.name;
					let (is_equal, is_total_same) =
						is_equal_chars_ignore_case(start_tag_name, fix_end_tag_name);
					if is_equal {
						if doc.parse_options.case_sensitive_tagname && !is_total_same {
							return doc.error(
								ErrorKind::WrongCaseSensitive(doc.chars_to_string()),
								context,
							);
						}
						is_endtag_ok = true;
					}
				}
			}
			// meet the right end tag
			if is_endtag_ok {
				// pop from chain nodes
				let last_tag = doc
					.chain_nodes
					.pop()
					.expect("End tag must have matched tag in chain nodes");
				// set end tag
				last_tag.borrow_mut().end_tag = Some(Rc::clone(&doc.current_node));
				// set space between tag
				let is_only_text_child = match &last_tag.borrow().childs {
					Some(childs) => childs.len() == 1 && childs[0].borrow().node_type == NodeType::Text,
					None => false,
				};
				if !is_only_text_child {
					doc.set_text_spaces_between();
				}
				// set node end
				doc.set_tag_end_info();
				// set code in
				doc.set_code_in(Unkown);
				// end of special tag
				if doc.in_special.is_some()
					&& is_equal_chars(&doc.in_special.as_ref().unwrap().1, fix_end_tag_name, &None)
				{
					doc.in_special = None;
				}
				// set end tag more info
				let content = doc.clean_chars_to_vec();
				let mut current_node = doc.current_node.borrow_mut();
				current_node.parent = Some(Rc::downgrade(&last_tag));
				current_node.content = Some(content);
			} else {
				// no matched start tag
				if !doc.parse_options.auto_fix_unexpected_endtag {
					// wrong end tag without start tag
					return doc.error(ErrorKind::WrongEndTag(doc.chars_to_string()), context);
				} else {
					// set orig current node
					let mut orig_current_node: Option<RefNode> = None;
					if let Some(prev_node) = &doc.current_node.borrow().prev {
						orig_current_node = Some(prev_node.upgrade().expect(""));
					}
					if let Some(orig_current_node) = orig_current_node {
						doc.current_node = orig_current_node;
					}
					// just ignore the end tag
					doc.prev_chars.clear();
					doc.set_code_in(Unkown);
					return Ok(());
				}
			}
		}
		_ => {
			// just add char to prev chars
			doc.prev_chars.push(c);
		}
	}
	// return
	Ok(())
}

/**
 * code_in: HTMLScript | HTMLStyle | EscapeableRawText
 */
fn parse_content_tag(doc: &mut Doc, c: char, _: &str) -> HResult {
	use CodeTypeIn::*;
	// parse html script tag and style tag
	if c != TAG_END_CHAR {
		// not end yet, just add character
		doc.prev_chars.push(c);
		return Ok(());
	}
	let end_tag = doc
		.detect
		.as_ref()
		.expect("detect chars must set before set_code_in.");
	let mut detect_len = end_tag.len();
	let mut prev_len = doc.prev_chars.len();
	if prev_len >= detect_len {
		let prev_chars = &doc.prev_chars;
		// remove suffix whitespaces
		while prev_len > 0 {
			let cur_index = prev_len - 1;
			let prev_char = prev_chars[cur_index];
			if !prev_char.is_ascii_whitespace() {
				break;
			} else {
				prev_len = cur_index;
			}
		}
		// check if need detect again
		if prev_len >= detect_len {
			let case_sensitive = doc.parse_options.case_sensitive_tagname;
			while detect_len > 0 {
				let detect_index = detect_len - 1;
				let detect_char = end_tag[detect_index];
				let prev_index = prev_len - 1;
				let prev_char = prev_chars[prev_index];
				let is_matched = if detect_char == prev_char {
					true
				} else {
					// check if case sensitive
					if case_sensitive {
						false
					} else {
						match detect_char {
							'A'..='Z' => detect_char.to_ascii_lowercase() == prev_char,
							'a'..='z' => detect_char.to_ascii_uppercase() == prev_char,
							_ => false,
						}
					}
				};
				if is_matched {
					// move forward detect
					detect_len = detect_index;
					// move forward prev chars
					prev_len = prev_index;
				} else {
					break;
				}
			}
		}
		// when detect_len equal 0, means all detect characteres are matched
		if detect_len == 0 {
			// set code in unkown
			doc.set_code_in(Unkown);
			// first remove end tag from prev_chars, and then remove '</' from end tag to get the name
			let end_tag_name = doc.prev_chars.split_off(prev_len).split_off(2);
			// add an end tag
			let mut end = Node::new(NodeType::TagEnd, doc.position + 1);
			end.content = Some(end_tag_name);
			end.parent = Some(Rc::downgrade(&doc.current_node));
			// set tag node's content, end_tag
			let node = Rc::new(RefCell::new(end));
			// here split off is quickly than clean_chars_to_vec
			let content = doc.prev_chars.split_off(0);
			let mut current_node = doc.current_node.borrow_mut();
			current_node.end_tag = Some(Rc::clone(&node));
			current_node.content = Some(content);
			// remove current tag
			doc.chain_nodes.pop();
			doc.detect = None;
			return Ok(());
		}
	}
	doc.prev_chars.push(c);
	Ok(())
}

/**
 * code_in: Comment|CDATA
 */
fn parse_comment_or_cdata(doc: &mut Doc, c: char, _: &str) -> HResult {
	use CodeTypeIn::*;
	// comment node
	if c == TAG_END_CHAR {
		let chars = &doc.prev_chars;
		let total = chars.len();
		if total > 2 {
			let end_symbol = doc.mark_char;
			let prev_char = chars[total - 1];
			if prev_char == end_symbol && chars[total - 2] == end_symbol {
				// remove suffix <comment>'--' or <cdata>']]'
				doc.prev_chars.truncate(total - 2);
				// set to content
				let content = doc.clean_chars_to_vec();
				doc.current_node.borrow_mut().content = Some(content);
				// reset mark char
				doc.mark_char = EOF_CHAR;
				doc.set_tag_end_info();
				doc.set_code_in(Unkown);
				return Ok(());
			}
		}
	}
	doc.prev_chars.push(c);
	Ok(())
}

/**
 * code_in: UnkownTag
 */
fn parse_unkown_tag(doc: &mut Doc, c: char, context: &str) -> HResult {
	use CodeTypeIn::*;
	// check the tag type
	match c {
		'a'..='z' | 'A'..='Z' => {
			// new tag node
			let inner_node = Node::new(NodeType::Tag, doc.mem_position);
			let node = Rc::new(RefCell::new(inner_node));
			doc.add_new_node(node);
			doc.set_code_in(Tag);
			doc.set_text_spaces_between();
			doc.prev_chars.push(c);
		}
		END_SLASH_CHAR => {
			// if allow auto fix unclosed tag
			if !doc.parse_options.auto_fix_unclosed_tag {
				doc.add_new_node(Rc::new(RefCell::new(Node::new(
					NodeType::TagEnd,
					doc.mem_position,
				))));
			} else {
				let prev_node = Rc::downgrade(&doc.current_node);
				// tag end
				doc.add_new_node(Rc::new(RefCell::new(Node::new(
					NodeType::TagEnd,
					doc.mem_position,
				))));
				doc.current_node.borrow_mut().prev = Some(prev_node);
			}
			// set code in first
			doc.set_code_in(TagEnd);
		}
		'!' => {
			// Comment|DOCTYPE
			doc.set_code_in(ExclamationBegin);
		}
		_ => {
			if !doc.parse_options.auto_fix_unescaped_lt {
				return create_parse_error(
					ErrorKind::WrongTag(c.to_string()),
					doc.mem_position,
					context,
				);
			}
			// fix unescaped left angle bracket
			doc.fix_unescaped_lt(c, context)?;
		}
	};
	Ok(())
}

/**
 * code_in: ExclamationBegin
 */
fn parse_exclamation_begin(doc: &mut Doc, c: char, context: &str) -> HResult {
	use CodeTypeIn::*;
	// maybe Comment | DOCTYPE<HTML> | XMLCDATA
	let mut ignore_char = false;
	if let Some(next_chars) = &doc.detect {
		let total_len = doc.prev_chars.len();
		let actual_len = next_chars.len();
		if total_len < actual_len {
			let cur_should_be = next_chars.get(total_len).unwrap();
			if cur_should_be == &c.to_ascii_uppercase() {
				if total_len == actual_len - 1 {
					let begin_at = doc.mem_position;
					match c {
						DASH_CHAR | LEFT_BRACKET_CHAR => {
							let code_in: CodeTypeIn;
							let node_type: NodeType;
							if c == DASH_CHAR {
								code_in = Comment;
								doc.mark_char = DASH_CHAR;
								// new comment node
								node_type = NodeType::Comment;
							} else {
								code_in = XMLCDATA;
								doc.mark_char = RIGHT_BRACKET_CHAR;
								// new cdata
								node_type = NodeType::XMLCDATA;
							}
							doc.set_code_in(code_in);
							// new comment node
							doc.add_new_node(Rc::new(RefCell::new(Node::new(node_type, begin_at))));
							doc.prev_chars.clear();
							doc.set_text_spaces_between();
							// ignore the character
							ignore_char = true;
						}
						'E' | 'e' => {
							doc.set_code_in(HTMLDOCTYPE);
							// new html doctype node
							doc.add_new_node(Rc::new(RefCell::new(Node::new(
								NodeType::HTMLDOCTYPE,
								begin_at,
							))));
							doc.set_text_spaces_between();
						}
						_ => unreachable!(),
					};
					doc.detect = None;
				}
			} else {
				return create_parse_error(
					ErrorKind::UnrecognizedTag(doc.chars_to_string(), chars_to_string(next_chars)),
					doc.mem_position,
					context,
				);
			}
		}
	} else {
		let detect_type: DetectChar;
		match c {
			DASH_CHAR => {
				detect_type = DetectChar::Comment;
			}
			'D' | 'd' => {
				detect_type = DetectChar::DOCTYPE;
			}
			LEFT_BRACKET_CHAR => {
				// CDATA
				let special_tag_name = doc.in_special.as_ref().map(|(_, name)| name);
				if let Some(tag_name) = special_tag_name {
					if is_equal_chars(
						tag_name,
						&doc
							.chain_nodes
							.last()
							.unwrap()
							.borrow()
							.meta
							.as_ref()
							.expect("Chain nodes must all be tag nodes")
							.borrow()
							.name,
						&None,
					) {
						return create_parse_error(
							ErrorKind::CommonError("<![CDATA tag can in sub node".into()),
							doc.position,
							context,
						);
					} else {
						detect_type = DetectChar::XMLCDATA;
					}
				} else {
					return create_parse_error(
						ErrorKind::CommonError("wrong <![CDATA tag can only used in Svg or MathML".into()),
						doc.position,
						context,
					);
				}
			}
			_ => {
				return create_parse_error(
					ErrorKind::WrongTag(doc.chars_to_string()),
					doc.mem_position,
					context,
				);
			}
		};
		doc.detect = Some(DETECT_CHAR_MAP.get(&detect_type).unwrap().to_vec());
	}
	if !ignore_char {
		doc.prev_chars.push(c);
	}
	Ok(())
}

/**
 * Doc
 * the html syntax: https://www.w3.org/TR/2011/WD-html-markup-20110113/syntax.html
*/

pub struct Doc {
	code_in: CodeTypeIn,
	position: usize,
	mem_position: usize,
	detect: Option<Vec<char>>,
	prev_chars: Vec<char>,
	mark_char: char,
	chain_nodes: Vec<RefNode>,
	current_node: RefNode,
	in_special: Option<(SpecialTag, Vec<char>)>,
	repeat_whitespace: bool,
	check_textnode: Option<RefNode>,
	handle: NextHandle,
	pub chars: Vec<char>,
	pub parse_options: ParseOptions,
	pub root: RefNode,
	pub id_tags: Rc<RefCell<StringNodeMap>>,
	pub onerror: Rc<RefCell<Option<Rc<ErrorHandle>>>>,
}

pub type StringNodeMap = HashMap<String, RefNode>;
pub type ErrorHandle = Box<dyn Fn(BoxDynError)>;

impl Doc {
	// create new parser
	fn new() -> Self {
		let node = Rc::new(RefCell::new(Node::new(NodeType::AbstractRoot, 0)));
		let ref_node = Rc::clone(&node);
		let current_node = Rc::clone(&node);
		let root = Rc::clone(&node);
		let mut nodes = Vec::with_capacity(ALLOC_NODES_CAPACITY);
		let mut chain_nodes = Vec::with_capacity(ALLOC_NODES_CAPACITY);
		// set root for root node
		node.borrow_mut().root = Some(Rc::downgrade(&root));
		nodes.push(node);
		chain_nodes.push(ref_node);
		let mut doc = Doc {
			code_in: CodeTypeIn::AbstractRoot,
			position: 0,
			mem_position: 0,
			mark_char: EOF_CHAR,
			prev_chars: Vec::with_capacity(ALLOC_CHAR_CAPACITY),
			chain_nodes,
			current_node,
			detect: None,
			in_special: None,
			parse_options: Default::default(),
			repeat_whitespace: false,
			check_textnode: None,
			handle: noop,
			chars: Vec::with_capacity(100),
			root,
			id_tags: Rc::new(RefCell::new(HashMap::new())),
			onerror: Rc::new(RefCell::new(None)),
		};
		doc.init();
		doc
	}

	// init, set handle
	fn init(&mut self) {
		self.handle = parse_wait;
	}

	// into root
	pub fn into_root(self) -> DocHolder {
		let doc = Rc::new(RefCell::new(self));
		doc.borrow_mut().root.borrow_mut().document = Some(Rc::downgrade(&doc));
		DocHolder { doc }
	}

	// parse with string
	pub fn parse(content: &str, options: ParseOptions) -> GenResult<DocHolder> {
		let mut doc = Doc::new();
		doc.parse_options = options;
		for c in content.chars() {
			doc.next(c, content)?;
		}
		doc.eof(content)?;
		Ok(doc.into_root())
	}

	// parse file
	pub fn parse_file<P>(filename: P, options: ParseOptions) -> GenResult<DocHolder>
	where
		P: AsRef<Path>,
	{
		let file_path = filename.as_ref();
		let file_path = if file_path.is_absolute() {
			file_path.to_path_buf()
		} else {
			env::current_dir()?.join(filename).canonicalize().unwrap()
		};
		let file = File::open(file_path)?;
		let file = BufReader::new(file);
		let mut doc = Doc::new();
		let mut content = String::with_capacity(500);
		doc.parse_options = options;
		for line in file.lines() {
			let line_content = line.unwrap();
			content.push_str(&line_content);
			for c in line_content.chars() {
				doc.next(c, &content)?;
			}
			doc.next('\n', &content)?;
		}
		doc.eof(&content)?;
		Ok(doc.into_root())
	}

	// chars to string
	fn chars_to_string(&self) -> String {
		chars_to_string(&self.prev_chars)
	}

	// clean the previous characters and return
	fn clean_chars_to_vec(&mut self) -> Vec<char> {
		let mut content: Vec<char> = Vec::with_capacity(self.prev_chars.len());
		content.append(&mut self.prev_chars);
		content
	}

	// set code_in
	fn set_code_in(&mut self, code_in: CodeTypeIn) {
		self.code_in = code_in;
		use CodeTypeIn::*;
		match code_in {
			Unkown => {
				self.handle = parse_wait;
			}
			Tag => {
				self.handle = parse_tag_name;
			}
			TagEnd => {
				self.handle = parse_tagend;
			}
			TextNode => {
				self.handle = parse_text;
			}
			HTMLScript | HTMLStyle | EscapeableRawText => {
				self.handle = parse_content_tag;
			}
			HTMLDOCTYPE => {
				self.handle = parse_doctype_name;
			}
			AbstractRoot => {
				self.handle = parse_wait;
			}
			Comment => {
				self.handle = parse_comment_or_cdata;
			}
			XMLCDATA => {
				self.handle = parse_comment_or_cdata;
			}
			UnkownTag => {
				self.handle = parse_unkown_tag;
			}
			ExclamationBegin => {
				self.handle = parse_exclamation_begin;
			}
		};
	}

	// read chars one by one
	fn next(&mut self, c: char, content: &str) -> HResult {
		let handle = self.handle;
		handle(self, c, content)?;
		self.position += 1;
		// parse ok
		Ok(())
	}

	// add a new node to the queue
	fn add_new_node(&mut self, node: RefNode) {
		use NodeType::*;
		let node_type = node.borrow().node_type;
		if node_type != TagEnd {
			// set parent node
			let parent_node = self.chain_nodes.last().unwrap();
			node.borrow_mut().parent = Some(Rc::downgrade(parent_node));
			// add the node to parent's child list
			let mut parent_node = parent_node.borrow_mut();
			let child = Rc::clone(&node);
			// add cur node to parent's child nodes
			if let Some(childs) = &mut parent_node.childs {
				child.borrow_mut().index = childs.len();
				childs.push(child);
			} else {
				child.borrow_mut().index = 0;
				parent_node.childs = Some(vec![child]);
			}
			// set node root
			node.borrow_mut().root = Some(Rc::downgrade(&self.root));
		}
		// set current node to be new node
		self.current_node = Rc::clone(&node);
		// if is a tag node, add the tag node to chain nodes
		if node_type == Tag {
			self.chain_nodes.push(Rc::clone(&node));
		}
	}
	// set tag end info
	fn set_tag_end_info(&mut self) {
		use NodeType::*;
		let mut current_node = self.current_node.borrow_mut();
		let node_type = current_node.node_type;
		current_node.end_at = if node_type == Text {
			self.position
		} else {
			self.position + 1
		};
	}
	// set spaces between tag
	fn set_text_spaces_between(&mut self) {
		if let Some(text_node) = &mut self.check_textnode {
			text_node.borrow_mut().node_type = NodeType::SpacesBetweenTag;
			self.check_textnode = None;
		}
	}

	// fix unclosed tag
	fn fix_unclosed_tag(&mut self, unclosed: &[RefNode]) {
		for tag_node in unclosed {
			let mut end_tag: Option<Node> = None;
			if let Some(meta) = &tag_node.borrow_mut().meta {
				// set it's meta as auto fix
				meta.borrow_mut().auto_fix = true;
				// make end tag
				let tag_name = &meta.borrow().name;
				let mut end = Node::new(NodeType::TagEnd, self.position);
				end.content = Some(tag_name.clone());
				end.parent = Some(Rc::downgrade(tag_node));
				end_tag = Some(end);
			}
			if let Some(end_tag) = end_tag {
				tag_node.borrow_mut().end_tag = Some(Rc::new(RefCell::new(end_tag)));
			}
		}
	}
	// fix unescaped left angle bracket
	fn fix_unescaped_lt(&mut self, c: char, context: &str) -> HResult {
		let mut chars = vec!['&', 'l', 't', ';'];
		let mut parent: Option<RefNode> = None;
		let mut need_parent = false;
		let node_type = self.current_node.borrow().node_type;
		let is_end_text = c == TAG_BEGIN_CHAR;
		if !is_end_text {
			chars.push(c);
		}
		match node_type {
			NodeType::Text | NodeType::SpacesBetweenTag => {
				// text node or spaces between
				if let Some(content) = &mut self.current_node.borrow_mut().content {
					let mut prev_chars: Vec<char> = Vec::with_capacity(content.len() + chars.len());
					prev_chars.append(content);
					prev_chars.append(&mut chars);
					self.prev_chars = prev_chars;
				}
				// change node type if spaces between
				if matches!(node_type, NodeType::SpacesBetweenTag) {
					self.current_node.borrow_mut().node_type = NodeType::Text;
				}
			}
			NodeType::Tag => {
				// tag, check if auto fix or self closing
				let is_closed = self
					.current_node
					.borrow()
					.meta
					.as_ref()
					.map_or(false, |meta| {
						let meta = &meta.borrow();
						meta.self_closed || meta.auto_fix
					});
				if !is_closed {
					parent = Some(Rc::clone(&self.current_node));
				} else {
					need_parent = true;
				}
			}
			_ => {
				need_parent = true;
			}
		}
		if need_parent {
			if !self.chain_nodes.is_empty() {
				let index = self.chain_nodes.len() - 1;
				parent = Some(Rc::clone(&self.chain_nodes[index]));
			} else {
				parent = Some(Rc::clone(&self.root));
			}
		}
		if let Some(parent) = &parent {
			let text_node = Node::new(NodeType::Text, self.mem_position);
			let current_node = Rc::new(RefCell::new(text_node));
			let mut parent = parent.borrow_mut();
			let childs = parent.childs.get_or_insert(Vec::new());
			current_node.borrow_mut().index = childs.len();
			childs.push(Rc::clone(&current_node));
			// set current node as text node
			self.current_node = current_node;
			// set prev chars
			self.prev_chars = chars;
		}
		self.repeat_whitespace = false;
		self.set_code_in(CodeTypeIn::TextNode);
		// if is end of text<the tag left character>
		// should move back one character
		if is_end_text {
			// execute the text node handle
			// the position index will keep because it doesn't call 'next' but `handle`
			let handle = self.handle;
			handle(self, c, context)?;
		}
		Ok(())
	}

	// end of the doc
	fn eof(&mut self, context: &str) -> HResult {
		let cur_depth = self.chain_nodes.len();
		// check if all tags are closed correctly.
		if cur_depth > 1 {
			if !self.parse_options.auto_fix_unclosed_tag {
				let last_node = self.chain_nodes[cur_depth - 1].borrow();
				let begin_at = last_node.begin_at;
				let tag_name = chars_to_string(
					&last_node
						.meta
						.as_ref()
						.expect("tag node's meta must have")
						.borrow()
						.name,
				);
				return create_parse_error(ErrorKind::UnclosedTag(tag_name), begin_at, context);
			}
			// fix unclosed tags
			let unclosed = self.chain_nodes.split_off(1);
			self.fix_unclosed_tag(&unclosed);
		}
		// check and fix last node info.
		use CodeTypeIn::*;
		match self.code_in {
			TextNode => {
				let mut last_node = self.current_node.borrow_mut();
				last_node.content = Some(self.prev_chars.clone());
				if self.repeat_whitespace {
					last_node.node_type = NodeType::SpacesBetweenTag;
				}
				last_node.end_at = self.position;
			}
			Unkown | AbstractRoot => {
				// end ok
			}
			Tag => {
				if !self.parse_options.auto_fix_unclosed_tag {
					return create_parse_error(
						ErrorKind::UnclosedTag(format!("{:?}", self.code_in)),
						self.current_node.borrow().begin_at,
						context,
					);
				}
			}
			_ => {
				return create_parse_error(
					ErrorKind::UnclosedTag(format!("{:?}", self.code_in)),
					self.current_node.borrow().begin_at,
					context,
				);
			}
		}
		// set the root node's end position
		self.root.borrow_mut().end_at = self.position;
		Ok(())
	}
	// return error with doc.position
	fn error(&self, kind: ErrorKind, context: &str) -> HResult {
		create_parse_error(kind, self.position, context)
	}
	// set tag meta
	fn set_tag_meta(&mut self) -> bool {
		let name = self.clean_chars_to_vec();
		let lc_name = name
			.iter()
			.map(|ch| ch.to_ascii_lowercase())
			.collect::<Vec<char>>();
		let is_void = is_void_tag(&lc_name);
		let meta = TagMeta {
			name,
			attrs: Vec::with_capacity(5),
			lc_name_map: HashMap::with_capacity(5),
			is_void,
			..Default::default()
		};
		self.current_node.borrow_mut().meta = Some(RefCell::new(meta));
		is_void
	}
	// set tag code in
	fn set_tag_code_in(&mut self, code_in: TagCodeIn) {
		use TagCodeIn::*;
		match code_in {
			Wait | WaitValue | ValueEnd => self.handle = parse_tag_wait,
			Key => self.handle = parse_tag_attr_key,
			Value => self.handle = parse_tag_attr_value,
			KeyEnd => {}
		};
		if let Some(meta) = &self.current_node.borrow_mut().meta {
			meta.borrow_mut().code_in = code_in;
		}
	}
	// check tag code in
	fn is_tag_code_in(&self, code_in: &TagCodeIn) -> bool {
		let current_node = self.current_node.borrow();
		let tag_code_in = &current_node
			.meta
			.as_ref()
			.expect("Tag meta must set in parse_tag_name or parse_doctype_name")
			.borrow()
			.code_in;
		tag_code_in == code_in
	}
	// add attr key
	fn add_tag_attr_key(&mut self) {
		if let Some(meta) = &self.current_node.borrow_mut().meta {
			meta.borrow_mut().add_attr_key();
			meta.borrow_mut().code_in = TagCodeIn::Key;
		}
	}
	// set attr key
	fn set_tag_attr_key(&mut self) {
		let key = self.clean_chars_to_vec();
		if let Some(meta) = &self.current_node.borrow_mut().meta {
			let mut meta = meta.borrow_mut();
			let index = meta.attrs.len() - 1;
			let key_name = key
				.iter()
				.map(|ch| ch.to_ascii_lowercase())
				.collect::<String>();
			// end the attribute
			// insert lowercase attribute name to maps
			meta.lc_name_map.entry(key_name).or_insert(index);
			// set cur attr key
			meta.set_attr_key(key);
		}
	}
	// add a tag attribute value
	fn add_tag_attr_value(&mut self, quote: Option<char>) {
		let current_node = self.current_node.borrow_mut();
		let meta = current_node
			.meta
			.as_ref()
			.expect("The tag meta must not be empty");
		meta.borrow_mut().add_attr_value(quote);
		meta.borrow_mut().code_in = TagCodeIn::Value;
	}

	// set attr value
	fn set_tag_attr_value(&mut self, quote: Option<char>) {
		let value = self.clean_chars_to_vec();
		if let Some(meta) = &self.current_node.borrow_mut().meta {
			let mut meta = meta.borrow_mut();
			// set attribute value
			let attr = meta.set_attr_value(value, quote);
			// check the value if is a id value
			// store the id nodes to cache
			if let Some(id_name) = attr.check_if_id() {
				self
					.id_tags
					.borrow_mut()
					.insert(id_name, Rc::clone(&self.current_node));
			}
		}
	}

	// check tag type
	fn check_tag_type_do(&mut self) {
		use CodeTypeIn::*;
		let lc_tag_name = self
			.current_node
			.borrow()
			.meta
			.as_ref()
			.expect("")
			.borrow()
			.name
			.iter()
			.map(|ch| ch.to_ascii_lowercase())
			.collect::<Vec<char>>();
		if is_content_tag(&lc_tag_name, &None) {
			// svg tags allow script and style tag, but title and textarea will treat as normal tag
			self.mem_position = self.position;
			let code_in = if is_equal_chars(&lc_tag_name, &SCRIPT_TAG_NAME, &None) {
				HTMLScript
			} else if is_equal_chars(&lc_tag_name, &STYLE_TAG_NAME, &None) {
				HTMLStyle
			} else {
				EscapeableRawText
			};
			self.set_code_in(code_in);
			// set detect chars
			let mut next_chars = vec!['<', END_SLASH_CHAR];
			next_chars.extend_from_slice(
				&self
					.current_node
					.borrow()
					.meta
					.as_ref()
					.expect("")
					.borrow()
					.name,
			);
			self.detect = Some(next_chars);
		} else {
			if self.in_special.is_none() {
				// not void elements will check if special
				self.in_special = if let Some(&special) = SPECIAL_TAG_MAP.get(&lc_tag_name) {
					Some((
						special,
						self
							.current_node
							.borrow()
							.meta
							.as_ref()
							.expect("")
							.borrow()
							.name
							.clone(),
					))
				} else {
					None
				}
			}
			self.set_code_in(Unkown);
		}
	}
}

// get an element from string node map
fn get_element(map: &StringNodeMap, query: &str) -> Option<RefNode> {
	map.get(query).cloned()
}
/// Doc holder
pub struct DocHolder {
	doc: RefDoc,
}

impl DocHolder {
	// render
	pub fn render(&self, options: &RenderOptions) -> String {
		chars_to_string(&self.borrow().root.borrow().build(options, false))
	}

	// render text
	pub fn render_text(&self, options: &RenderOptions) -> String {
		chars_to_string(&self.borrow().root.borrow().build(options, true))
	}

	// borrow
	pub fn borrow(&self) -> Ref<Doc> {
		self.doc.borrow()
	}

	// borrow mut
	pub fn get_root_node(&self) -> RefNode {
		Rc::clone(&self.borrow().root)
	}

	// get element by id
	pub fn get_element_by_id(&self, id: &str) -> Option<RefNode> {
		get_element(&self.borrow().id_tags.borrow(), id)
	}
}

impl From<RefDoc> for DocHolder {
	fn from(doc: RefDoc) -> Self {
		DocHolder { doc }
	}
}