MCPcopy Create free account
hub / github.com/CanadaHonk/shadow / parse

Method parse

engine/htmlparser.js:60–325  ·  view source on GitHub ↗
(input, autoParent = true)

Source from the content-addressed store, hash-verified

58 constructor() {}
59
60 parse(input, autoParent = true) {
61 const checkToken = (a, b) => !this.escaping && a === b;
62
63 const checkSES = c => {
64 if (this.escaping) {
65 return false;
66 }
67
68 if (c != '"' && c != '\'') {
69 return false;
70 }
71
72 const state = c == '"' ? StringState.Double : StringState.Single;
73
74 // start
75 if (this.stringState == StringState.None) {
76 this.stringState = state;
77 return true;
78 }
79
80 // end
81 if (this.stringState == state) {
82 this.stringState = StringState.None;
83 return true;
84 }
85
86 return false;
87 };
88
89 const isspace = c => c === ' ' || c === '\n';
90 const isclosing = c => c === '>' || c === '/';
91
92 const isVoidEl = (name = this.currentName) => VOID_ELEMENTS.includes(name);
93
94 input = input.replace(/<!DOCTYPE .*?>/i, '').replace(/<!--[\w\W]*?-->/g, '')
95 .replaceAll('\r\n', '\n').replaceAll('\r', '\n'); // normalize newlines
96
97 for (let i = 0; i < input.length; i++) {
98 const peek = () => input[i + 1];
99
100 const c = input[i];
101 if (c == '\\') {
102 this.escaping = true;
103 continue;
104 }
105
106 // console.log(`${c} | parent: ${this.parent.tagName} | attr state: ${this.attrState}. tag state: ${this.tagState}`);
107
108 switch (this.attrState) {
109 case AttrState.Name: {
110 if (checkToken(c, '=')) {
111 this.attrState = AttrState.Value;
112 continue;
113 }
114
115 if (isclosing(c)) {
116 this.currentNode.setAttr(this.currentAttrName, true);
117

Callers 2

innerHTMLMethod · 0.95
_loadFunction · 0.95

Calls 3

isspaceFunction · 0.85
setAttrMethod · 0.80
appendChildMethod · 0.45

Tested by

no test coverage detected