List <JsToken> BuildTokenList(ContainerNode node) { IEnumerable <JsToken> tokens = new JsLexer().GetTokens( _source , node.Header.End // begin , node.Footer.Begin // end ); // simple way to make list but it crashes if we hit invalid token // var tokenList = tokens.ToList(); // more complicated but it survives invalid token exceptions List <JsToken> tokenList = new List <JsToken>(); try{ foreach (var token in tokens) { tokenList.Add(token); } }catch {} // incase we hit invalid javascript, we don't want the whole document to die return(tokenList); }
int DoKeyword(int i) { JsToken next = (i + 1) < _tokenList.Count ? _tokenList[i + 1] : null; // create node for it ConsumeText(_currentToken.Begin); var keywordHeader = new HeaderFooter(_source, _currentToken.Begin, _currentToken.End, _currentToken.Text, HeaderFooterType.Single); var keywordNode = new ContainerNode(_source, keywordHeader, new Dictionary <string, string>()); _currentNode.AddChild(keywordNode); _beginOfTextNode = _currentToken.End; // name function if (_currentToken.Text == "function" && next != null && next.Type == JsTokenType.Identifier) { IdentifyNode(keywordNode, next.Text); ++i; // skip id token _beginOfTextNode = next.End; } return(i); }
// returns the new current node void DoStarterTag(HeaderFooter tag) { // Head & Single // new node ContainerNode n = new ContainerNode(_source, tag); // create parent-child relationship _curNode.AddChild(n); // force some node types to be single bool isSingle = tag.Type == HeaderFooterType.Single || _emptyTags.Contains(tag.Name); // if forced-empty node has a tailtag, it will be ignored // !! Not doing anything special about illegally embeded nodes such as // <p>first paragraph <p> new paragraph, close previous <p> another paragraph if (!isSingle) { _curNode = n; } }
void MoveDownIntoSubSection(int i) { _openTokensIndexStack.Push(i); // add previous text node ConsumeText(_currentToken.Begin); // add new node var name = BracketName(_currentToken.Text); var header = new HeaderFooter(_currentNode.Source, _currentToken.Begin, _currentToken.End, "block", HeaderFooterType.Head); var blockNode = new ContainerNode(_source, header, new Dictionary <string, string> { { "class", name } }); _currentNode.AddChild(blockNode); IdentifyContainerNode(i, blockNode); // set new text-node start _beginOfTextNode = _currentToken.End; // move down to new node _currentNode = blockNode; }