| Index: pkg/analyzer/lib/src/generated/html.dart
|
| diff --git a/pkg/analyzer/lib/src/generated/html.dart b/pkg/analyzer/lib/src/generated/html.dart
|
| index 2f1e762721903bb704c15f3f6bfab3acb022ca71..1da03c471fcd5a9152ebe47ca60f5b4db1526580 100644
|
| --- a/pkg/analyzer/lib/src/generated/html.dart
|
| +++ b/pkg/analyzer/lib/src/generated/html.dart
|
| @@ -19,85 +19,6 @@ import 'element.dart';
|
| import 'engine.dart' show AnalysisEngine, AngularHtmlUnitResolver, ExpressionVisitor;
|
|
|
| /**
|
| - * Instances of the class `XmlExpression` represent an abstract expression embedded into
|
| - * [XmlNode].
|
| - */
|
| -abstract class XmlExpression {
|
| - /**
|
| - * An empty array of expressions.
|
| - */
|
| - static List<XmlExpression> EMPTY_ARRAY = new List<XmlExpression>(0);
|
| -
|
| - /**
|
| - * Check if the given offset belongs to the expression's source range.
|
| - */
|
| - bool contains(int offset) => this.offset <= offset && offset < end;
|
| -
|
| - /**
|
| - * Return the offset of the character immediately following the last character of this
|
| - * expression's source range. This is equivalent to `getOffset() + getLength()`.
|
| - *
|
| - * @return the offset of the character just past the expression's source range
|
| - */
|
| - int get end;
|
| -
|
| - /**
|
| - * Return the number of characters in the expression's source range.
|
| - */
|
| - int get length;
|
| -
|
| - /**
|
| - * Return the offset of the first character in the expression's source range.
|
| - */
|
| - int get offset;
|
| -
|
| - /**
|
| - * Return the [Reference] at the given offset.
|
| - *
|
| - * @param offset the offset from the beginning of the file
|
| - * @return the [Reference] at the given offset, maybe `null`
|
| - */
|
| - XmlExpression_Reference getReference(int offset);
|
| -}
|
| -
|
| -/**
|
| - * The reference to the [Element].
|
| - */
|
| -class XmlExpression_Reference {
|
| - Element element;
|
| -
|
| - int offset = 0;
|
| -
|
| - int length = 0;
|
| -
|
| - XmlExpression_Reference(Element element, int offset, int length) {
|
| - this.element = element;
|
| - this.offset = offset;
|
| - this.length = length;
|
| - }
|
| -}
|
| -
|
| -/**
|
| - * Instances of the class `SimpleXmlVisitor` implement an AST visitor that will do nothing
|
| - * when visiting an AST node. It is intended to be a superclass for classes that use the visitor
|
| - * pattern primarily as a dispatch mechanism (and hence don't need to recursively visit a whole
|
| - * structure) and that only need to visit a small number of node types.
|
| - */
|
| -class SimpleXmlVisitor<R> implements XmlVisitor<R> {
|
| - @override
|
| - R visitHtmlScriptTagNode(HtmlScriptTagNode node) => null;
|
| -
|
| - @override
|
| - R visitHtmlUnit(HtmlUnit htmlUnit) => null;
|
| -
|
| - @override
|
| - R visitXmlAttributeNode(XmlAttributeNode xmlAttributeNode) => null;
|
| -
|
| - @override
|
| - R visitXmlTagNode(XmlTagNode xmlTagNode) => null;
|
| -}
|
| -
|
| -/**
|
| * The abstract class `AbstractScanner` implements a scanner for HTML code. Subclasses are
|
| * required to implement the interface used to access the characters being scanned.
|
| */
|
| @@ -416,306 +337,257 @@ abstract class AbstractScanner {
|
| }
|
| }
|
|
|
| +class ExpressionVisitor_HtmlUnitUtils_getExpression extends ExpressionVisitor {
|
| + int offset = 0;
|
| +
|
| + List<Expression> result;
|
| +
|
| + ExpressionVisitor_HtmlUnitUtils_getExpression(this.offset, this.result) : super();
|
| +
|
| + @override
|
| + void visitExpression(Expression expression) {
|
| + Expression at = HtmlUnitUtils._getExpressionAt(expression, offset);
|
| + if (at != null) {
|
| + result[0] = at;
|
| + throw new HtmlUnitUtils_FoundExpressionError();
|
| + }
|
| + }
|
| +}
|
| +
|
| /**
|
| - * Instances of the class `Token` represent a token that was scanned from the input. Each
|
| - * token knows which token follows it, acting as the head of a linked list of tokens.
|
| + * Instances of the class `HtmlParser` are used to parse tokens into a AST structure comprised
|
| + * of [XmlNode]s.
|
| */
|
| -class Token {
|
| +class HtmlParser extends XmlParser {
|
| /**
|
| - * The offset from the beginning of the file to the first character in the token.
|
| + * The line information associated with the source being parsed.
|
| */
|
| - final int offset;
|
| + LineInfo _lineInfo;
|
|
|
| /**
|
| - * The previous token in the token stream.
|
| + * The error listener to which errors will be reported.
|
| */
|
| - Token previous;
|
| + final AnalysisErrorListener _errorListener;
|
|
|
| - /**
|
| - * The next token in the token stream.
|
| - */
|
| - Token _next;
|
| + static String _APPLICATION_DART_IN_DOUBLE_QUOTES = "\"application/dart\"";
|
|
|
| - /**
|
| - * The type of the token.
|
| - */
|
| - final TokenType type;
|
| + static String _APPLICATION_DART_IN_SINGLE_QUOTES = "'application/dart'";
|
|
|
| - /**
|
| - * The lexeme represented by this token.
|
| - */
|
| - String _value;
|
| + static String _SCRIPT = "script";
|
|
|
| - /**
|
| - * Initialize a newly created token.
|
| - *
|
| - * @param type the token type (not `null`)
|
| - * @param offset the offset from the beginning of the file to the first character in the token
|
| - */
|
| - Token.con1(TokenType type, int offset) : this.con2(type, offset, type.lexeme);
|
| + static String _TYPE = "type";
|
|
|
| /**
|
| - * Initialize a newly created token.
|
| - *
|
| - * @param type the token type (not `null`)
|
| - * @param offset the offset from the beginning of the file to the first character in the token
|
| - * @param value the lexeme represented by this token (not `null`)
|
| + * A set containing the names of tags that do not have a closing tag.
|
| */
|
| - Token.con2(this.type, this.offset, String value) {
|
| - this._value = StringUtilities.intern(value);
|
| - }
|
| + static Set<String> SELF_CLOSING = new Set<String>.from(JavaArrays.asList(<String> [
|
| + "area",
|
| + "base",
|
| + "basefont",
|
| + "br",
|
| + "col",
|
| + "frame",
|
| + "hr",
|
| + "img",
|
| + "input",
|
| + "link",
|
| + "meta",
|
| + "param",
|
| + "!"]));
|
|
|
| /**
|
| - * Return the offset from the beginning of the file to the character after last character of the
|
| - * token.
|
| + * Given the contents of an embedded expression that occurs at the given offset, parse it as a
|
| + * Dart expression. The contents should not include the expression's delimiters.
|
| *
|
| - * @return the offset from the beginning of the file to the first character after last character
|
| - * of the token
|
| + * @param source the source that contains that given token
|
| + * @param token the token to start parsing from
|
| + * @return the Dart expression that was parsed
|
| */
|
| - int get end => offset + length;
|
| + static Expression parseEmbeddedExpression(Source source, sc.Token token, AnalysisErrorListener errorListener) {
|
| + Parser parser = new Parser(source, errorListener);
|
| + return parser.parseExpression(token);
|
| + }
|
|
|
| /**
|
| - * Return the number of characters in the node's source range.
|
| + * Given the contents of an embedded expression that occurs at the given offset, scans it as a
|
| + * Dart code.
|
| *
|
| - * @return the number of characters in the node's source range
|
| + * @param source the source of that contains the given contents
|
| + * @param contents the contents to scan
|
| + * @param contentOffset the offset of the contents in the larger file
|
| + * @return the first Dart token
|
| */
|
| - int get length => lexeme.length;
|
| + static sc.Token scanDartSource(Source source, LineInfo lineInfo, String contents, int contentOffset, AnalysisErrorListener errorListener) {
|
| + LineInfo_Location location = lineInfo.getLocation(contentOffset);
|
| + sc.Scanner scanner = new sc.Scanner(source, new sc.SubSequenceReader(contents, contentOffset), errorListener);
|
| + scanner.setSourceStart(location.lineNumber, location.columnNumber);
|
| + return scanner.tokenize();
|
| + }
|
|
|
| /**
|
| - * Return the lexeme that represents this token.
|
| + * Construct a parser for the specified source.
|
| *
|
| - * @return the lexeme (not `null`)
|
| + * @param source the source being parsed
|
| + * @param errorListener the error listener to which errors will be reported
|
| */
|
| - String get lexeme => _value;
|
| + HtmlParser(Source source, this._errorListener) : super(source);
|
|
|
| /**
|
| - * Return the next token in the token stream.
|
| + * Parse the given tokens.
|
| *
|
| - * @return the next token in the token stream
|
| + * @param token the first token in the stream of tokens to be parsed
|
| + * @param lineInfo the line information created by the scanner
|
| + * @return the parse result (not `null`)
|
| */
|
| - Token get next => _next;
|
| + HtmlUnit parse(Token token, LineInfo lineInfo) {
|
| + this._lineInfo = lineInfo;
|
| + List<XmlTagNode> tagNodes = parseTopTagNodes(token);
|
| + return new HtmlUnit(token, tagNodes, currentToken);
|
| + }
|
|
|
| - /**
|
| - * Return `true` if this token is a synthetic token. A synthetic token is a token that was
|
| - * introduced by the parser in order to recover from an error in the code. Synthetic tokens always
|
| - * have a length of zero (`0`).
|
| - *
|
| - * @return `true` if this token is a synthetic token
|
| - */
|
| - bool get isSynthetic => length == 0;
|
| + @override
|
| + XmlAttributeNode createAttributeNode(Token name, Token equals, Token value) => new XmlAttributeNode(name, equals, value);
|
|
|
| - /**
|
| - * Set the next token in the token stream to the given token. This has the side-effect of setting
|
| - * this token to be the previous token for the given token.
|
| - *
|
| - * @param token the next token in the token stream
|
| - * @return the token that was passed in
|
| - */
|
| - Token setNext(Token token) {
|
| - _next = token;
|
| - token.previous = this;
|
| - return token;
|
| + @override
|
| + XmlTagNode createTagNode(Token nodeStart, Token tag, List<XmlAttributeNode> attributes, Token attributeEnd, List<XmlTagNode> tagNodes, Token contentEnd, Token closingTag, Token nodeEnd) {
|
| + if (_isScriptNode(tag, attributes, tagNodes)) {
|
| + HtmlScriptTagNode tagNode = new HtmlScriptTagNode(nodeStart, tag, attributes, attributeEnd, tagNodes, contentEnd, closingTag, nodeEnd);
|
| + String contents = tagNode.content;
|
| + int contentOffset = attributeEnd.end;
|
| + LineInfo_Location location = _lineInfo.getLocation(contentOffset);
|
| + sc.Scanner scanner = new sc.Scanner(source, new sc.SubSequenceReader(contents, contentOffset), _errorListener);
|
| + scanner.setSourceStart(location.lineNumber, location.columnNumber);
|
| + sc.Token firstToken = scanner.tokenize();
|
| + Parser parser = new Parser(source, _errorListener);
|
| + CompilationUnit unit = parser.parseCompilationUnit(firstToken);
|
| + unit.lineInfo = _lineInfo;
|
| + tagNode.script = unit;
|
| + return tagNode;
|
| + }
|
| + return new XmlTagNode(nodeStart, tag, attributes, attributeEnd, tagNodes, contentEnd, closingTag, nodeEnd);
|
| }
|
|
|
| @override
|
| - String toString() => lexeme;
|
| -}
|
| + bool isSelfClosing(Token tag) => SELF_CLOSING.contains(tag.lexeme);
|
|
|
| -/**
|
| - * Instances of `XmlTagNode` represent XML or HTML elements such as `` and
|
| - * `<body foo="bar"> ... </body>`.
|
| - */
|
| -class XmlTagNode extends XmlNode {
|
| /**
|
| - * Constant representing empty list of attributes.
|
| + * Determine if the specified node is a Dart script.
|
| + *
|
| + * @param node the node to be tested (not `null`)
|
| + * @return `true` if the node is a Dart script
|
| */
|
| - static List<XmlAttributeNode> NO_ATTRIBUTES = new UnmodifiableListView(new List<XmlAttributeNode>());
|
| -
|
| - /**
|
| - * Constant representing empty list of tag nodes.
|
| - */
|
| - static List<XmlTagNode> NO_TAG_NODES = new UnmodifiableListView(new List<XmlTagNode>());
|
| -
|
| - /**
|
| - * The starting [TokenType#LT] token (not `null`).
|
| - */
|
| - final Token nodeStart;
|
| -
|
| - /**
|
| - * The [TokenType#TAG] token after the starting '<' (not `null`).
|
| - */
|
| - final Token _tag;
|
| -
|
| - /**
|
| - * The attributes contained by the receiver (not `null`, contains no `null`s).
|
| - */
|
| - List<XmlAttributeNode> _attributes;
|
| -
|
| - /**
|
| - * The [TokenType#GT] or [TokenType#SLASH_GT] token after the attributes (not
|
| - * `null`). The token may be the same token as [nodeEnd] if there are no child
|
| - * [tagNodes].
|
| - */
|
| - final Token attributeEnd;
|
| + bool _isScriptNode(Token tag, List<XmlAttributeNode> attributes, List<XmlTagNode> tagNodes) {
|
| + if (tagNodes.length != 0 || tag.lexeme != _SCRIPT) {
|
| + return false;
|
| + }
|
| + for (XmlAttributeNode attribute in attributes) {
|
| + if (attribute.name == _TYPE) {
|
| + Token valueToken = attribute.valueToken;
|
| + if (valueToken != null) {
|
| + String value = valueToken.lexeme;
|
| + if (value == _APPLICATION_DART_IN_DOUBLE_QUOTES || value == _APPLICATION_DART_IN_SINGLE_QUOTES) {
|
| + return true;
|
| + }
|
| + }
|
| + }
|
| + }
|
| + return false;
|
| + }
|
| +}
|
|
|
| +/**
|
| + * Instances of the class `HtmlScriptTagNode` represent a script tag within an HTML file that
|
| + * references a Dart script.
|
| + */
|
| +class HtmlScriptTagNode extends XmlTagNode {
|
| /**
|
| - * The tag nodes contained in the receiver (not `null`, contains no `null`s).
|
| + * The AST structure representing the Dart code within this tag.
|
| */
|
| - List<XmlTagNode> _tagNodes;
|
| + CompilationUnit _script;
|
|
|
| /**
|
| - * The token (not `null`) after the content, which may be
|
| - * * (1) [TokenType#LT_SLASH] for nodes with open and close tags, or
|
| - * * (2) the [TokenType#LT] nodeStart of the next sibling node if this node is self
|
| - * closing or the attributeEnd is [TokenType#SLASH_GT], or
|
| - * * (3) [TokenType#EOF] if the node does not have a closing tag and is the last node in
|
| - * the stream [TokenType#LT_SLASH] token after the content, or `null` if there is no
|
| - * content and the attributes ended with [TokenType#SLASH_GT].
|
| + * The element representing this script.
|
| */
|
| - final Token contentEnd;
|
| + HtmlScriptElement scriptElement;
|
|
|
| /**
|
| - * The closing [TokenType#TAG] after the child elements or `null` if there is no
|
| - * content and the attributes ended with [TokenType#SLASH_GT]
|
| + * Initialize a newly created node to represent a script tag within an HTML file that references a
|
| + * Dart script.
|
| + *
|
| + * @param nodeStart the token marking the beginning of the tag
|
| + * @param tag the name of the tag
|
| + * @param attributes the attributes in the tag
|
| + * @param attributeEnd the token terminating the region where attributes can be
|
| + * @param tagNodes the children of the tag
|
| + * @param contentEnd the token that starts the closing tag
|
| + * @param closingTag the name of the tag that occurs in the closing tag
|
| + * @param nodeEnd the last token in the tag
|
| */
|
| - final Token closingTag;
|
| + HtmlScriptTagNode(Token nodeStart, Token tag, List<XmlAttributeNode> attributes, Token attributeEnd, List<XmlTagNode> tagNodes, Token contentEnd, Token closingTag, Token nodeEnd) : super(nodeStart, tag, attributes, attributeEnd, tagNodes, contentEnd, closingTag, nodeEnd);
|
|
|
| - /**
|
| - * The ending [TokenType#GT] or [TokenType#SLASH_GT] token (not `null`).
|
| - */
|
| - final Token nodeEnd;
|
| + @override
|
| + accept(XmlVisitor visitor) => visitor.visitHtmlScriptTagNode(this);
|
|
|
| /**
|
| - * The expressions that are embedded in the tag's content.
|
| + * Return the AST structure representing the Dart code within this tag, or `null` if this
|
| + * tag references an external script.
|
| + *
|
| + * @return the AST structure representing the Dart code within this tag
|
| */
|
| - List<XmlExpression> expressions = XmlExpression.EMPTY_ARRAY;
|
| + CompilationUnit get script => _script;
|
|
|
| /**
|
| - * Construct a new instance representing an XML or HTML element
|
| + * Set the AST structure representing the Dart code within this tag to the given compilation unit.
|
| *
|
| - * @param nodeStart the starting [TokenType#LT] token (not `null`)
|
| - * @param tag the [TokenType#TAG] token after the starting '<' (not `null`).
|
| - * @param attributes the attributes associated with this element or [NO_ATTRIBUTES] (not
|
| - * `null`, contains no `null`s)
|
| - * @param attributeEnd The [TokenType#GT] or [TokenType#SLASH_GT] token after the
|
| - * attributes (not `null`). The token may be the same token as [nodeEnd] if
|
| - * there are no child [tagNodes].
|
| - * @param tagNodes child tag nodes of the receiver or [NO_TAG_NODES] (not `null`,
|
| - * contains no `null`s)
|
| - * @param contentEnd the token (not `null`) after the content, which may be
|
| - * * (1) [TokenType#LT_SLASH] for nodes with open and close tags, or
|
| - * * (2) the [TokenType#LT] nodeStart of the next sibling node if this node is
|
| - * self closing or the attributeEnd is [TokenType#SLASH_GT], or
|
| - * * (3) [TokenType#EOF] if the node does not have a closing tag and is the last
|
| - * node in the stream [TokenType#LT_SLASH] token after the content, or `null`
|
| - * if there is no content and the attributes ended with [TokenType#SLASH_GT].
|
| - * @param closingTag the closing [TokenType#TAG] after the child elements or `null` if
|
| - * there is no content and the attributes ended with [TokenType#SLASH_GT]
|
| - * @param nodeEnd the ending [TokenType#GT] or [TokenType#SLASH_GT] token (not
|
| - * `null`)
|
| + * @param unit the AST structure representing the Dart code within this tag
|
| */
|
| - XmlTagNode(this.nodeStart, this._tag, List<XmlAttributeNode> attributes, this.attributeEnd, List<XmlTagNode> tagNodes, this.contentEnd, this.closingTag, this.nodeEnd) {
|
| - this._attributes = becomeParentOfAll(attributes, ifEmpty: NO_ATTRIBUTES);
|
| - this._tagNodes = becomeParentOfAll(tagNodes, ifEmpty: NO_TAG_NODES);
|
| + void set script(CompilationUnit unit) {
|
| + _script = unit;
|
| }
|
| +}
|
|
|
| - @override
|
| - accept(XmlVisitor visitor) => visitor.visitXmlTagNode(this);
|
| -
|
| +/**
|
| + * Instances of the class `HtmlUnit` represent the contents of an HTML file.
|
| + */
|
| +class HtmlUnit extends XmlNode {
|
| /**
|
| - * Answer the attribute with the specified name.
|
| - *
|
| - * @param name the attribute name
|
| - * @return the attribute or `null` if no matching attribute is found
|
| + * The first token in the token stream that was parsed to form this HTML unit.
|
| */
|
| - XmlAttributeNode getAttribute(String name) {
|
| - for (XmlAttributeNode attribute in _attributes) {
|
| - if (attribute.name == name) {
|
| - return attribute;
|
| - }
|
| - }
|
| - return null;
|
| - }
|
| + final Token beginToken;
|
|
|
| /**
|
| - * Answer the receiver's attributes. Callers should not manipulate the returned list to edit the
|
| - * AST structure.
|
| - *
|
| - * @return the attributes (not `null`, contains no `null`s)
|
| + * The last token in the token stream that was parsed to form this compilation unit. This token
|
| + * should always have a type of [TokenType.EOF].
|
| */
|
| - List<XmlAttributeNode> get attributes => _attributes;
|
| + final Token endToken;
|
|
|
| /**
|
| - * Find the attribute with the given name (see [getAttribute] and answer the lexeme
|
| - * for the attribute's value token without the leading and trailing quotes (see
|
| - * [XmlAttributeNode#getText]).
|
| - *
|
| - * @param name the attribute name
|
| - * @return the attribute text or `null` if no matching attribute is found
|
| + * The tag nodes contained in the receiver (not `null`, contains no `null`s).
|
| */
|
| - String getAttributeText(String name) {
|
| - XmlAttributeNode attribute = getAttribute(name);
|
| - return attribute != null ? attribute.text : null;
|
| - }
|
| -
|
| - @override
|
| - Token get beginToken => nodeStart;
|
| + List<XmlTagNode> _tagNodes;
|
|
|
| /**
|
| - * Answer a string representing the content contained in the receiver. This includes the textual
|
| - * representation of any child tag nodes ([getTagNodes]). Whitespace between '<',
|
| - * '</', and '>', '/>' is discarded, but all other whitespace is preserved.
|
| + * Construct a new instance representing the content of an HTML file.
|
| *
|
| - * @return the content (not `null`)
|
| + * @param beginToken the first token in the file (not `null`)
|
| + * @param tagNodes child tag nodes of the receiver (not `null`, contains no `null`s)
|
| + * @param endToken the last token in the token stream which should be of type
|
| + * [TokenType.EOF]
|
| */
|
| - String get content {
|
| - Token token = attributeEnd.next;
|
| - if (identical(token, contentEnd)) {
|
| - return "";
|
| - }
|
| - //TODO (danrubel): handle CDATA and replace HTML character encodings with the actual characters
|
| - String content = token.lexeme;
|
| - token = token.next;
|
| - if (identical(token, contentEnd)) {
|
| - return content;
|
| - }
|
| - JavaStringBuilder buffer = new JavaStringBuilder();
|
| - while (!identical(token, contentEnd)) {
|
| - buffer.append(token.lexeme);
|
| - token = token.next;
|
| - }
|
| - return buffer.toString();
|
| + HtmlUnit(this.beginToken, List<XmlTagNode> tagNodes, this.endToken) {
|
| + this._tagNodes = becomeParentOfAll(tagNodes);
|
| }
|
|
|
| @override
|
| - Token get endToken {
|
| - if (nodeEnd != null) {
|
| - return nodeEnd;
|
| - }
|
| - if (closingTag != null) {
|
| - return closingTag;
|
| - }
|
| - if (contentEnd != null) {
|
| - return contentEnd;
|
| - }
|
| - if (!_tagNodes.isEmpty) {
|
| - return _tagNodes[_tagNodes.length - 1].endToken;
|
| - }
|
| - if (attributeEnd != null) {
|
| - return attributeEnd;
|
| - }
|
| - if (!_attributes.isEmpty) {
|
| - return _attributes[_attributes.length - 1].endToken;
|
| - }
|
| - return _tag;
|
| - }
|
| + accept(XmlVisitor visitor) => visitor.visitHtmlUnit(this);
|
|
|
| /**
|
| - * Answer the tag name after the starting '<'.
|
| + * Return the element associated with this HTML unit.
|
| *
|
| - * @return the tag name (not `null`)
|
| + * @return the element or `null` if the receiver is not resolved
|
| */
|
| - String get tag => _tag.lexeme;
|
| + @override
|
| + HtmlElement get element => super.element as HtmlElement;
|
|
|
| /**
|
| * Answer the tag nodes contained in the receiver. Callers should not manipulate the returned list
|
| @@ -725,18 +597,16 @@ class XmlTagNode extends XmlNode {
|
| */
|
| List<XmlTagNode> get tagNodes => _tagNodes;
|
|
|
| - /**
|
| - * Answer the [TokenType#TAG] token after the starting '<'.
|
| - *
|
| - * @return the token (not `null`)
|
| - */
|
| - Token get tagToken => _tag;
|
| + @override
|
| + void set element(Element element) {
|
| + if (element != null && element is! HtmlElement) {
|
| + throw new IllegalArgumentException("HtmlElement expected, but ${element.runtimeType} given");
|
| + }
|
| + super.element = element;
|
| + }
|
|
|
| @override
|
| void visitChildren(XmlVisitor visitor) {
|
| - for (XmlAttributeNode node in _attributes) {
|
| - node.accept(visitor);
|
| - }
|
| for (XmlTagNode node in _tagNodes) {
|
| node.accept(visitor);
|
| }
|
| @@ -744,468 +614,140 @@ class XmlTagNode extends XmlNode {
|
| }
|
|
|
| /**
|
| - * Instances of the class `XmlParser` are used to parse tokens into a AST structure comprised
|
| - * of [XmlNode]s.
|
| + * Utilities locating [Expression]s and [Element]s in [HtmlUnit].
|
| */
|
| -class XmlParser {
|
| - /**
|
| - * The source being parsed.
|
| - */
|
| - final Source source;
|
| -
|
| +class HtmlUnitUtils {
|
| /**
|
| - * The next token to be parsed.
|
| + * Returns the [XmlAttributeNode] that is part of the given [HtmlUnit] and encloses
|
| + * the given offset.
|
| */
|
| - Token _currentToken;
|
| -
|
| - /**
|
| - * Construct a parser for the specified source.
|
| - *
|
| - * @param source the source being parsed
|
| - */
|
| - XmlParser(this.source);
|
| -
|
| - /**
|
| - * Create a node representing an attribute.
|
| - *
|
| - * @param name the name of the attribute
|
| - * @param equals the equals sign, or `null` if there is no value
|
| - * @param value the value of the attribute
|
| - * @return the node that was created
|
| - */
|
| - XmlAttributeNode createAttributeNode(Token name, Token equals, Token value) => new XmlAttributeNode(name, equals, value);
|
| + static XmlAttributeNode getAttributeNode(HtmlUnit htmlUnit, int offset) {
|
| + if (htmlUnit == null) {
|
| + return null;
|
| + }
|
| + List<XmlAttributeNode> result = [null];
|
| + try {
|
| + htmlUnit.accept(new RecursiveXmlVisitor_HtmlUnitUtils_getAttributeNode(offset, result));
|
| + } on HtmlUnitUtils_FoundAttributeNodeError catch (e) {
|
| + return result[0];
|
| + }
|
| + return null;
|
| + }
|
|
|
| /**
|
| - * Create a node representing a tag.
|
| - *
|
| - * @param nodeStart the token marking the beginning of the tag
|
| - * @param tag the name of the tag
|
| - * @param attributes the attributes in the tag
|
| - * @param attributeEnd the token terminating the region where attributes can be
|
| - * @param tagNodes the children of the tag
|
| - * @param contentEnd the token that starts the closing tag
|
| - * @param closingTag the name of the tag that occurs in the closing tag
|
| - * @param nodeEnd the last token in the tag
|
| - * @return the node that was created
|
| + * Returns the best [Element] of the given [Expression].
|
| */
|
| - XmlTagNode createTagNode(Token nodeStart, Token tag, List<XmlAttributeNode> attributes, Token attributeEnd, List<XmlTagNode> tagNodes, Token contentEnd, Token closingTag, Token nodeEnd) => new XmlTagNode(nodeStart, tag, attributes, attributeEnd, tagNodes, contentEnd, closingTag, nodeEnd);
|
| + static Element getElement(Expression expression) {
|
| + if (expression == null) {
|
| + return null;
|
| + }
|
| + return ElementLocator.locate(expression);
|
| + }
|
|
|
| /**
|
| - * Answer `true` if the specified tag is self closing and thus should never have content or
|
| - * child tag nodes.
|
| - *
|
| - * @param tag the tag (not `null`)
|
| - * @return `true` if self closing
|
| + * Returns the [Element] of the [Expression] in the given [HtmlUnit], enclosing
|
| + * the given offset.
|
| */
|
| - bool isSelfClosing(Token tag) => false;
|
| + static Element getElementAtOffset(HtmlUnit htmlUnit, int offset) {
|
| + Expression expression = getExpression(htmlUnit, offset);
|
| + return getElement(expression);
|
| + }
|
|
|
| /**
|
| - * Parse the entire token stream and in the process, advance the current token to the end of the
|
| - * token stream.
|
| - *
|
| - * @return the list of tag nodes found (not `null`, contains no `null`)
|
| + * Returns the [Element] to open when requested at the given [Expression].
|
| */
|
| - List<XmlTagNode> parseTopTagNodes(Token firstToken) {
|
| - _currentToken = firstToken;
|
| - List<XmlTagNode> tagNodes = new List<XmlTagNode>();
|
| - TokenType type = _currentToken.type;
|
| - while (type != TokenType.EOF) {
|
| - if (type == TokenType.LT) {
|
| - tagNodes.add(_parseTagNode());
|
| - } else if (type == TokenType.DECLARATION || type == TokenType.DIRECTIVE || type == TokenType.COMMENT) {
|
| - // ignored tokens
|
| - _currentToken = _currentToken.next;
|
| - } else {
|
| - _reportUnexpectedToken();
|
| - _currentToken = _currentToken.next;
|
| + static Element getElementToOpen(HtmlUnit htmlUnit, Expression expression) {
|
| + Element element = getElement(expression);
|
| + {
|
| + AngularElement angularElement = AngularHtmlUnitResolver.getAngularElement(element);
|
| + if (angularElement != null) {
|
| + return angularElement;
|
| }
|
| - type = _currentToken.type;
|
| }
|
| - return tagNodes;
|
| + return element;
|
| }
|
|
|
| /**
|
| - * Answer the current token.
|
| - *
|
| - * @return the current token
|
| - */
|
| - Token get currentToken => _currentToken;
|
| -
|
| - /**
|
| - * Insert a synthetic token of the specified type before the current token
|
| - *
|
| - * @param type the type of token to be inserted (not `null`)
|
| - * @return the synthetic token that was inserted (not `null`)
|
| + * Returns the [XmlTagNode] that is part of the given [HtmlUnit] and encloses the
|
| + * given offset.
|
| */
|
| - Token _insertSyntheticToken(TokenType type) {
|
| - Token token = new Token.con2(type, _currentToken.offset, "");
|
| - _currentToken.previous.setNext(token);
|
| - token.setNext(_currentToken);
|
| - return token;
|
| + static XmlTagNode getEnclosingTagNode(HtmlUnit htmlUnit, int offset) {
|
| + if (htmlUnit == null) {
|
| + return null;
|
| + }
|
| + List<XmlTagNode> result = [null];
|
| + try {
|
| + htmlUnit.accept(new RecursiveXmlVisitor_HtmlUnitUtils_getEnclosingTagNode(offset, result));
|
| + } on HtmlUnitUtils_FoundTagNodeError catch (e) {
|
| + return result[0];
|
| + }
|
| + return null;
|
| }
|
|
|
| /**
|
| - * Parse the token stream for an attribute. This method advances the current token over the
|
| - * attribute, but should not be called if the [currentToken] is not [TokenType#TAG].
|
| - *
|
| - * @return the attribute (not `null`)
|
| + * Returns the [Expression] that is part of the given [HtmlUnit] and encloses the
|
| + * given offset.
|
| */
|
| - XmlAttributeNode _parseAttribute() {
|
| - // Assume the current token is a tag
|
| - Token name = _currentToken;
|
| - _currentToken = _currentToken.next;
|
| - // Equals sign
|
| - Token equals;
|
| - if (_currentToken.type == TokenType.EQ) {
|
| - equals = _currentToken;
|
| - _currentToken = _currentToken.next;
|
| - } else {
|
| - _reportUnexpectedToken();
|
| - equals = _insertSyntheticToken(TokenType.EQ);
|
| + static Expression getExpression(HtmlUnit htmlUnit, int offset) {
|
| + if (htmlUnit == null) {
|
| + return null;
|
| }
|
| - // String value
|
| - Token value;
|
| - if (_currentToken.type == TokenType.STRING) {
|
| - value = _currentToken;
|
| - _currentToken = _currentToken.next;
|
| - } else {
|
| - _reportUnexpectedToken();
|
| - value = _insertSyntheticToken(TokenType.STRING);
|
| + List<Expression> result = [null];
|
| + try {
|
| + // TODO(scheglov) this code is very Angular specific
|
| + htmlUnit.accept(new ExpressionVisitor_HtmlUnitUtils_getExpression(offset, result));
|
| + } on HtmlUnitUtils_FoundExpressionError catch (e) {
|
| + return result[0];
|
| }
|
| - return createAttributeNode(name, equals, value);
|
| + return null;
|
| }
|
|
|
| /**
|
| - * Parse the stream for a sequence of attributes. This method advances the current token to the
|
| - * next [TokenType#GT], [TokenType#SLASH_GT], or [TokenType#EOF].
|
| - *
|
| - * @return a collection of zero or more attributes (not `null`, contains no `null`s)
|
| + * Returns the [XmlTagNode] that is part of the given [HtmlUnit] and its open or
|
| + * closing tag name encloses the given offset.
|
| */
|
| - List<XmlAttributeNode> _parseAttributes() {
|
| - TokenType type = _currentToken.type;
|
| - if (type == TokenType.GT || type == TokenType.SLASH_GT || type == TokenType.EOF) {
|
| - return XmlTagNode.NO_ATTRIBUTES;
|
| + static XmlTagNode getTagNode(HtmlUnit htmlUnit, int offset) {
|
| + XmlTagNode node = getEnclosingTagNode(htmlUnit, offset);
|
| + // do we have an enclosing tag at all?
|
| + if (node == null) {
|
| + return null;
|
| }
|
| - List<XmlAttributeNode> attributes = new List<XmlAttributeNode>();
|
| - while (type != TokenType.GT && type != TokenType.SLASH_GT && type != TokenType.EOF) {
|
| - if (type == TokenType.TAG) {
|
| - attributes.add(_parseAttribute());
|
| - } else {
|
| - _reportUnexpectedToken();
|
| - _currentToken = _currentToken.next;
|
| - }
|
| - type = _currentToken.type;
|
| + // is "offset" in the open tag?
|
| + Token openTag = node.tagToken;
|
| + if (openTag.offset <= offset && offset <= openTag.end) {
|
| + return node;
|
| }
|
| - return attributes;
|
| + // is "offset" in the open tag?
|
| + Token closeTag = node.closingTag;
|
| + if (closeTag != null && closeTag.offset <= offset && offset <= closeTag.end) {
|
| + return node;
|
| + }
|
| + // not on a tag name
|
| + return null;
|
| }
|
|
|
| /**
|
| - * Parse the stream for a sequence of tag nodes existing within a parent tag node. This method
|
| - * advances the current token to the next [TokenType#LT_SLASH] or [TokenType#EOF].
|
| - *
|
| - * @return a list of nodes (not `null`, contains no `null`s)
|
| + * Returns the [Expression] that is part of the given root [AstNode] and encloses the
|
| + * given offset.
|
| */
|
| - List<XmlTagNode> _parseChildTagNodes() {
|
| - TokenType type = _currentToken.type;
|
| - if (type == TokenType.LT_SLASH || type == TokenType.EOF) {
|
| - return XmlTagNode.NO_TAG_NODES;
|
| - }
|
| - List<XmlTagNode> nodes = new List<XmlTagNode>();
|
| - while (type != TokenType.LT_SLASH && type != TokenType.EOF) {
|
| - if (type == TokenType.LT) {
|
| - nodes.add(_parseTagNode());
|
| - } else if (type == TokenType.COMMENT) {
|
| - // ignored token
|
| - _currentToken = _currentToken.next;
|
| - } else {
|
| - _reportUnexpectedToken();
|
| - _currentToken = _currentToken.next;
|
| + static Expression _getExpressionAt(AstNode root, int offset) {
|
| + if (root.offset <= offset && offset <= root.end) {
|
| + AstNode dartNode = new NodeLocator.con1(offset).searchWithin(root);
|
| + if (dartNode is Expression) {
|
| + return dartNode;
|
| }
|
| - type = _currentToken.type;
|
| }
|
| - return nodes;
|
| + return null;
|
| }
|
| +}
|
|
|
| - /**
|
| - * Parse the token stream for the next tag node. This method advances current token over the
|
| - * parsed tag node, but should only be called if the current token is [TokenType#LT]
|
| - *
|
| - * @return the tag node or `null` if none found
|
| - */
|
| - XmlTagNode _parseTagNode() {
|
| - // Assume that the current node is a tag node start TokenType#LT
|
| - Token nodeStart = _currentToken;
|
| - _currentToken = _currentToken.next;
|
| - // Get the tag or create a synthetic tag and report an error
|
| - Token tag;
|
| - if (_currentToken.type == TokenType.TAG) {
|
| - tag = _currentToken;
|
| - _currentToken = _currentToken.next;
|
| - } else {
|
| - _reportUnexpectedToken();
|
| - tag = _insertSyntheticToken(TokenType.TAG);
|
| - }
|
| - // Parse the attributes
|
| - List<XmlAttributeNode> attributes = _parseAttributes();
|
| - // Token ending attribute list
|
| - Token attributeEnd;
|
| - if (_currentToken.type == TokenType.GT || _currentToken.type == TokenType.SLASH_GT) {
|
| - attributeEnd = _currentToken;
|
| - _currentToken = _currentToken.next;
|
| - } else {
|
| - _reportUnexpectedToken();
|
| - attributeEnd = _insertSyntheticToken(TokenType.SLASH_GT);
|
| - }
|
| - // If the node has no children, then return the node
|
| - if (attributeEnd.type == TokenType.SLASH_GT || isSelfClosing(tag)) {
|
| - return createTagNode(nodeStart, tag, attributes, attributeEnd, XmlTagNode.NO_TAG_NODES, _currentToken, null, attributeEnd);
|
| - }
|
| - // Parse the child tag nodes
|
| - List<XmlTagNode> tagNodes = _parseChildTagNodes();
|
| - // Token ending child tag nodes
|
| - Token contentEnd;
|
| - if (_currentToken.type == TokenType.LT_SLASH) {
|
| - contentEnd = _currentToken;
|
| - _currentToken = _currentToken.next;
|
| - } else {
|
| - // TODO (danrubel): handle self closing HTML elements by inserting synthetic tokens
|
| - // but not reporting an error
|
| - _reportUnexpectedToken();
|
| - contentEnd = _insertSyntheticToken(TokenType.LT_SLASH);
|
| - }
|
| - // Closing tag
|
| - Token closingTag;
|
| - if (_currentToken.type == TokenType.TAG) {
|
| - closingTag = _currentToken;
|
| - _currentToken = _currentToken.next;
|
| - } else {
|
| - _reportUnexpectedToken();
|
| - closingTag = _insertSyntheticToken(TokenType.TAG);
|
| - }
|
| - // Token ending node
|
| - Token nodeEnd;
|
| - if (_currentToken.type == TokenType.GT) {
|
| - nodeEnd = _currentToken;
|
| - _currentToken = _currentToken.next;
|
| - } else {
|
| - _reportUnexpectedToken();
|
| - nodeEnd = _insertSyntheticToken(TokenType.GT);
|
| - }
|
| - return createTagNode(nodeStart, tag, attributes, attributeEnd, tagNodes, contentEnd, closingTag, nodeEnd);
|
| - }
|
| -
|
| - /**
|
| - * Report the current token as unexpected
|
| - */
|
| - void _reportUnexpectedToken() {
|
| - }
|
| +class HtmlUnitUtils_FoundAttributeNodeError extends Error {
|
| }
|
|
|
| -/**
|
| - * The abstract class `XmlNode` defines behavior common to all XML/HTML nodes.
|
| - */
|
| -abstract class XmlNode {
|
| - /**
|
| - * The parent of the node, or `null` if the node is the root of an AST structure.
|
| - */
|
| - XmlNode _parent;
|
| -
|
| - /**
|
| - * The element associated with this node or `null` if the receiver is not resolved.
|
| - */
|
| - Element _element;
|
| -
|
| - /**
|
| - * Use the given visitor to visit this node.
|
| - *
|
| - * @param visitor the visitor that will visit this node
|
| - * @return the value returned by the visitor as a result of visiting this node
|
| - */
|
| - accept(XmlVisitor visitor);
|
| -
|
| - /**
|
| - * Return the first token included in this node's source range.
|
| - *
|
| - * @return the first token or `null` if none
|
| - */
|
| - Token get beginToken;
|
| -
|
| - /**
|
| - * Return the element associated with this node.
|
| - *
|
| - * @return the element or `null` if the receiver is not resolved
|
| - */
|
| - Element get element => _element;
|
| -
|
| - /**
|
| - * Return the offset of the character immediately following the last character of this node's
|
| - * source range. This is equivalent to `node.getOffset() + node.getLength()`. For an html
|
| - * unit this will be equal to the length of the unit's source.
|
| - *
|
| - * @return the offset of the character just past the node's source range
|
| - */
|
| - int get end => offset + length;
|
| -
|
| - /**
|
| - * Return the last token included in this node's source range.
|
| - *
|
| - * @return the last token or `null` if none
|
| - */
|
| - Token get endToken;
|
| -
|
| - /**
|
| - * Return the number of characters in the node's source range.
|
| - *
|
| - * @return the number of characters in the node's source range
|
| - */
|
| - int get length {
|
| - Token beginToken = this.beginToken;
|
| - Token endToken = this.endToken;
|
| - if (beginToken == null || endToken == null) {
|
| - return -1;
|
| - }
|
| - return endToken.offset + endToken.length - beginToken.offset;
|
| - }
|
| -
|
| - /**
|
| - * Return the offset from the beginning of the file to the first character in the node's source
|
| - * range.
|
| - *
|
| - * @return the offset from the beginning of the file to the first character in the node's source
|
| - * range
|
| - */
|
| - int get offset {
|
| - Token beginToken = this.beginToken;
|
| - if (beginToken == null) {
|
| - return -1;
|
| - }
|
| - return this.beginToken.offset;
|
| - }
|
| -
|
| - /**
|
| - * Return this node's parent node, or `null` if this node is the root of an AST structure.
|
| - *
|
| - * Note that the relationship between an AST node and its parent node may change over the lifetime
|
| - * of a node.
|
| - *
|
| - * @return the parent of this node, or `null` if none
|
| - */
|
| - XmlNode get parent => _parent;
|
| -
|
| - /**
|
| - * Set the element associated with this node.
|
| - *
|
| - * @param element the element
|
| - */
|
| - void set element(Element element) {
|
| - this._element = element;
|
| - }
|
| -
|
| - @override
|
| - String toString() {
|
| - PrintStringWriter writer = new PrintStringWriter();
|
| - accept(new ToSourceVisitor(writer));
|
| - return writer.toString();
|
| - }
|
| -
|
| - /**
|
| - * Use the given visitor to visit all of the children of this node. The children will be visited
|
| - * in source order.
|
| - *
|
| - * @param visitor the visitor that will be used to visit the children of this node
|
| - */
|
| - void visitChildren(XmlVisitor visitor);
|
| -
|
| - /**
|
| - * Make this node the parent of the given child node.
|
| - *
|
| - * @param child the node that will become a child of this node
|
| - * @return the node that was made a child of this node
|
| - */
|
| - XmlNode becomeParentOf(XmlNode child) {
|
| - if (child != null) {
|
| - XmlNode node = child;
|
| - node.parent = this;
|
| - }
|
| - return child;
|
| - }
|
| -
|
| - /**
|
| - * Make this node the parent of the given child nodes.
|
| - *
|
| - * @param children the nodes that will become the children of this node
|
| - * @param ifEmpty the (empty) nodes to return if "children" is empty
|
| - * @return the nodes that were made children of this node
|
| - */
|
| - List becomeParentOfAll(List children, {List ifEmpty}) {
|
| - if (children == null || children.isEmpty) {
|
| - if (ifEmpty != null) {
|
| - return ifEmpty;
|
| - }
|
| - }
|
| - if (children != null) {
|
| - for (JavaIterator iter = new JavaIterator(children); iter.hasNext;) {
|
| - XmlNode node = iter.next();
|
| - node.parent = this;
|
| - }
|
| - // This will create ArrayList for exactly given number of elements.
|
| - return new List.from(children);
|
| - }
|
| - return children;
|
| - }
|
| -
|
| - /**
|
| - * This method exists for debugging purposes only.
|
| - */
|
| - void _appendIdentifier(JavaStringBuilder builder, XmlNode node) {
|
| - if (node is XmlTagNode) {
|
| - builder.append(node.tag);
|
| - } else if (node is XmlAttributeNode) {
|
| - builder.append(node.name);
|
| - } else {
|
| - builder.append("htmlUnit");
|
| - }
|
| - }
|
| -
|
| - /**
|
| - * This method exists for debugging purposes only.
|
| - */
|
| - String _buildRecursiveStructureMessage(XmlNode newParent) {
|
| - JavaStringBuilder builder = new JavaStringBuilder();
|
| - builder.append("Attempt to create recursive structure: ");
|
| - XmlNode current = newParent;
|
| - while (current != null) {
|
| - if (!identical(current, newParent)) {
|
| - builder.append(" -> ");
|
| - }
|
| - if (identical(current, this)) {
|
| - builder.appendChar(0x2A);
|
| - _appendIdentifier(builder, current);
|
| - builder.appendChar(0x2A);
|
| - } else {
|
| - _appendIdentifier(builder, current);
|
| - }
|
| - current = current.parent;
|
| - }
|
| - return builder.toString();
|
| - }
|
| +class HtmlUnitUtils_FoundExpressionError extends Error {
|
| +}
|
|
|
| - /**
|
| - * Set the parent of this node to the given node.
|
| - *
|
| - * @param newParent the node that is to be made the parent of this node
|
| - */
|
| - void set parent(XmlNode newParent) {
|
| - XmlNode current = newParent;
|
| - while (current != null) {
|
| - if (identical(current, this)) {
|
| - AnalysisEngine.instance.logger.logError2("Circular structure while setting an XML node's parent", new IllegalArgumentException(_buildRecursiveStructureMessage(newParent)));
|
| - return;
|
| - }
|
| - current = current.parent;
|
| - }
|
| - _parent = newParent;
|
| - }
|
| +class HtmlUnitUtils_FoundTagNodeError extends Error {
|
| }
|
|
|
| /**
|
| @@ -1238,142 +780,41 @@ class RawXmlExpression extends XmlExpression {
|
| }
|
|
|
| /**
|
| - * Utilities locating [Expression]s and [Element]s in [HtmlUnit].
|
| + * Instances of the class `RecursiveXmlVisitor` implement an XML visitor that will recursively
|
| + * visit all of the nodes in an XML structure. For example, using an instance of this class to visit
|
| + * a [XmlTagNode] will also cause all of the contained [XmlAttributeNode]s and
|
| + * [XmlTagNode]s to be visited.
|
| + *
|
| + * Subclasses that override a visit method must either invoke the overridden visit method or must
|
| + * explicitly ask the visited node to visit its children. Failure to do so will cause the children
|
| + * of the visited node to not be visited.
|
| */
|
| -class HtmlUnitUtils {
|
| - /**
|
| - * Returns the [XmlAttributeNode] that is part of the given [HtmlUnit] and encloses
|
| - * the given offset.
|
| - */
|
| - static XmlAttributeNode getAttributeNode(HtmlUnit htmlUnit, int offset) {
|
| - if (htmlUnit == null) {
|
| - return null;
|
| - }
|
| - List<XmlAttributeNode> result = [null];
|
| - try {
|
| - htmlUnit.accept(new RecursiveXmlVisitor_HtmlUnitUtils_getAttributeNode(offset, result));
|
| - } on HtmlUnitUtils_FoundAttributeNodeError catch (e) {
|
| - return result[0];
|
| - }
|
| - return null;
|
| - }
|
| -
|
| - /**
|
| - * Returns the best [Element] of the given [Expression].
|
| - */
|
| - static Element getElement(Expression expression) {
|
| - if (expression == null) {
|
| - return null;
|
| - }
|
| - return ElementLocator.locate(expression);
|
| - }
|
| -
|
| - /**
|
| - * Returns the [Element] of the [Expression] in the given [HtmlUnit], enclosing
|
| - * the given offset.
|
| - */
|
| - static Element getElementAtOffset(HtmlUnit htmlUnit, int offset) {
|
| - Expression expression = getExpression(htmlUnit, offset);
|
| - return getElement(expression);
|
| - }
|
| -
|
| - /**
|
| - * Returns the [Element] to open when requested at the given [Expression].
|
| - */
|
| - static Element getElementToOpen(HtmlUnit htmlUnit, Expression expression) {
|
| - Element element = getElement(expression);
|
| - {
|
| - AngularElement angularElement = AngularHtmlUnitResolver.getAngularElement(element);
|
| - if (angularElement != null) {
|
| - return angularElement;
|
| - }
|
| - }
|
| - return element;
|
| - }
|
| -
|
| - /**
|
| - * Returns the [XmlTagNode] that is part of the given [HtmlUnit] and encloses the
|
| - * given offset.
|
| - */
|
| - static XmlTagNode getEnclosingTagNode(HtmlUnit htmlUnit, int offset) {
|
| - if (htmlUnit == null) {
|
| - return null;
|
| - }
|
| - List<XmlTagNode> result = [null];
|
| - try {
|
| - htmlUnit.accept(new RecursiveXmlVisitor_HtmlUnitUtils_getEnclosingTagNode(offset, result));
|
| - } on HtmlUnitUtils_FoundTagNodeError catch (e) {
|
| - return result[0];
|
| - }
|
| +class RecursiveXmlVisitor<R> implements XmlVisitor<R> {
|
| + @override
|
| + R visitHtmlScriptTagNode(HtmlScriptTagNode node) {
|
| + node.visitChildren(this);
|
| return null;
|
| }
|
|
|
| - /**
|
| - * Returns the [Expression] that is part of the given [HtmlUnit] and encloses the
|
| - * given offset.
|
| - */
|
| - static Expression getExpression(HtmlUnit htmlUnit, int offset) {
|
| - if (htmlUnit == null) {
|
| - return null;
|
| - }
|
| - List<Expression> result = [null];
|
| - try {
|
| - // TODO(scheglov) this code is very Angular specific
|
| - htmlUnit.accept(new ExpressionVisitor_HtmlUnitUtils_getExpression(offset, result));
|
| - } on HtmlUnitUtils_FoundExpressionError catch (e) {
|
| - return result[0];
|
| - }
|
| + @override
|
| + R visitHtmlUnit(HtmlUnit node) {
|
| + node.visitChildren(this);
|
| return null;
|
| }
|
|
|
| - /**
|
| - * Returns the [XmlTagNode] that is part of the given [HtmlUnit] and its open or
|
| - * closing tag name encloses the given offset.
|
| - */
|
| - static XmlTagNode getTagNode(HtmlUnit htmlUnit, int offset) {
|
| - XmlTagNode node = getEnclosingTagNode(htmlUnit, offset);
|
| - // do we have an enclosing tag at all?
|
| - if (node == null) {
|
| - return null;
|
| - }
|
| - // is "offset" in the open tag?
|
| - Token openTag = node.tagToken;
|
| - if (openTag.offset <= offset && offset <= openTag.end) {
|
| - return node;
|
| - }
|
| - // is "offset" in the open tag?
|
| - Token closeTag = node.closingTag;
|
| - if (closeTag != null && closeTag.offset <= offset && offset <= closeTag.end) {
|
| - return node;
|
| - }
|
| - // not on a tag name
|
| + @override
|
| + R visitXmlAttributeNode(XmlAttributeNode node) {
|
| + node.visitChildren(this);
|
| return null;
|
| }
|
|
|
| - /**
|
| - * Returns the [Expression] that is part of the given root [AstNode] and encloses the
|
| - * given offset.
|
| - */
|
| - static Expression _getExpressionAt(AstNode root, int offset) {
|
| - if (root.offset <= offset && offset <= root.end) {
|
| - AstNode dartNode = new NodeLocator.con1(offset).searchWithin(root);
|
| - if (dartNode is Expression) {
|
| - return dartNode;
|
| - }
|
| - }
|
| + @override
|
| + R visitXmlTagNode(XmlTagNode node) {
|
| + node.visitChildren(this);
|
| return null;
|
| }
|
| }
|
|
|
| -class HtmlUnitUtils_FoundAttributeNodeError extends Error {
|
| -}
|
| -
|
| -class HtmlUnitUtils_FoundExpressionError extends Error {
|
| -}
|
| -
|
| -class HtmlUnitUtils_FoundTagNodeError extends Error {
|
| -}
|
| -
|
| class RecursiveXmlVisitor_HtmlUnitUtils_getAttributeNode extends RecursiveXmlVisitor<Object> {
|
| int offset = 0;
|
|
|
| @@ -1410,35 +851,83 @@ class RecursiveXmlVisitor_HtmlUnitUtils_getEnclosingTagNode extends RecursiveXml
|
| }
|
| }
|
|
|
| -class ExpressionVisitor_HtmlUnitUtils_getExpression extends ExpressionVisitor {
|
| - int offset = 0;
|
| +/**
|
| + * Instances of the class `SimpleXmlVisitor` implement an AST visitor that will do nothing
|
| + * when visiting an AST node. It is intended to be a superclass for classes that use the visitor
|
| + * pattern primarily as a dispatch mechanism (and hence don't need to recursively visit a whole
|
| + * structure) and that only need to visit a small number of node types.
|
| + */
|
| +class SimpleXmlVisitor<R> implements XmlVisitor<R> {
|
| + @override
|
| + R visitHtmlScriptTagNode(HtmlScriptTagNode node) => null;
|
|
|
| - List<Expression> result;
|
| + @override
|
| + R visitHtmlUnit(HtmlUnit htmlUnit) => null;
|
|
|
| - ExpressionVisitor_HtmlUnitUtils_getExpression(this.offset, this.result) : super();
|
| + @override
|
| + R visitXmlAttributeNode(XmlAttributeNode xmlAttributeNode) => null;
|
|
|
| @override
|
| - void visitExpression(Expression expression) {
|
| - Expression at = HtmlUnitUtils._getExpressionAt(expression, offset);
|
| - if (at != null) {
|
| - result[0] = at;
|
| - throw new HtmlUnitUtils_FoundExpressionError();
|
| - }
|
| - }
|
| + R visitXmlTagNode(XmlTagNode xmlTagNode) => null;
|
| }
|
|
|
| /**
|
| - * The interface `XmlVisitor` defines the behavior of objects that can be used to visit an
|
| - * [XmlNode] structure.
|
| + * Instances of the class `StringScanner` implement a scanner that reads from a string. The
|
| + * scanning logic is in the superclass.
|
| */
|
| -abstract class XmlVisitor<R> {
|
| - R visitHtmlScriptTagNode(HtmlScriptTagNode node);
|
| +class StringScanner extends AbstractScanner {
|
| + /**
|
| + * The string from which characters will be read.
|
| + */
|
| + final String _string;
|
|
|
| - R visitHtmlUnit(HtmlUnit htmlUnit);
|
| + /**
|
| + * The number of characters in the string.
|
| + */
|
| + int _stringLength = 0;
|
|
|
| - R visitXmlAttributeNode(XmlAttributeNode xmlAttributeNode);
|
| + /**
|
| + * The index, relative to the string, of the last character that was read.
|
| + */
|
| + int _charOffset = 0;
|
|
|
| - R visitXmlTagNode(XmlTagNode xmlTagNode);
|
| + /**
|
| + * Initialize a newly created scanner to scan the characters in the given string.
|
| + *
|
| + * @param source the source being scanned
|
| + * @param string the string from which characters will be read
|
| + */
|
| + StringScanner(Source source, this._string) : super(source) {
|
| + this._stringLength = _string.length;
|
| + this._charOffset = -1;
|
| + }
|
| +
|
| + @override
|
| + int get offset => _charOffset;
|
| +
|
| + void set offset(int offset) {
|
| + _charOffset = offset;
|
| + }
|
| +
|
| + @override
|
| + int advance() {
|
| + if (++_charOffset < _stringLength) {
|
| + return _string.codeUnitAt(_charOffset);
|
| + }
|
| + _charOffset = _stringLength;
|
| + return -1;
|
| + }
|
| +
|
| + @override
|
| + String getString(int start, int endDelta) => _string.substring(start, _charOffset + 1 + endDelta).toString();
|
| +
|
| + @override
|
| + int peek() {
|
| + if (_charOffset + 1 < _stringLength) {
|
| + return _string.codeUnitAt(_charOffset + 1);
|
| + }
|
| + return -1;
|
| + }
|
| }
|
|
|
| /**
|
| @@ -1522,522 +1011,1033 @@ class ToSourceVisitor implements XmlVisitor<Object> {
|
| }
|
|
|
| /**
|
| - * Instances of the class `HtmlUnit` represent the contents of an HTML file.
|
| + * Instances of the class `Token` represent a token that was scanned from the input. Each
|
| + * token knows which token follows it, acting as the head of a linked list of tokens.
|
| */
|
| -class HtmlUnit extends XmlNode {
|
| +class Token {
|
| /**
|
| - * The first token in the token stream that was parsed to form this HTML unit.
|
| + * The offset from the beginning of the file to the first character in the token.
|
| */
|
| - final Token beginToken;
|
| + final int offset;
|
|
|
| /**
|
| - * The last token in the token stream that was parsed to form this compilation unit. This token
|
| - * should always have a type of [TokenType.EOF].
|
| + * The previous token in the token stream.
|
| */
|
| - final Token endToken;
|
| + Token previous;
|
|
|
| /**
|
| - * The tag nodes contained in the receiver (not `null`, contains no `null`s).
|
| + * The next token in the token stream.
|
| */
|
| - List<XmlTagNode> _tagNodes;
|
| + Token _next;
|
|
|
| /**
|
| - * Construct a new instance representing the content of an HTML file.
|
| - *
|
| - * @param beginToken the first token in the file (not `null`)
|
| - * @param tagNodes child tag nodes of the receiver (not `null`, contains no `null`s)
|
| - * @param endToken the last token in the token stream which should be of type
|
| - * [TokenType.EOF]
|
| + * The type of the token.
|
| */
|
| - HtmlUnit(this.beginToken, List<XmlTagNode> tagNodes, this.endToken) {
|
| - this._tagNodes = becomeParentOfAll(tagNodes);
|
| - }
|
| -
|
| - @override
|
| - accept(XmlVisitor visitor) => visitor.visitHtmlUnit(this);
|
| + final TokenType type;
|
|
|
| /**
|
| - * Return the element associated with this HTML unit.
|
| - *
|
| - * @return the element or `null` if the receiver is not resolved
|
| + * The lexeme represented by this token.
|
| */
|
| - @override
|
| - HtmlElement get element => super.element as HtmlElement;
|
| + String _value;
|
|
|
| /**
|
| - * Answer the tag nodes contained in the receiver. Callers should not manipulate the returned list
|
| - * to edit the AST structure.
|
| + * Initialize a newly created token.
|
| *
|
| - * @return the children (not `null`, contains no `null`s)
|
| + * @param type the token type (not `null`)
|
| + * @param offset the offset from the beginning of the file to the first character in the token
|
| */
|
| - List<XmlTagNode> get tagNodes => _tagNodes;
|
| -
|
| - @override
|
| - void set element(Element element) {
|
| - if (element != null && element is! HtmlElement) {
|
| - throw new IllegalArgumentException("HtmlElement expected, but ${element.runtimeType} given");
|
| - }
|
| - super.element = element;
|
| - }
|
| + Token.con1(TokenType type, int offset) : this.con2(type, offset, type.lexeme);
|
|
|
| - @override
|
| - void visitChildren(XmlVisitor visitor) {
|
| - for (XmlTagNode node in _tagNodes) {
|
| - node.accept(visitor);
|
| - }
|
| + /**
|
| + * Initialize a newly created token.
|
| + *
|
| + * @param type the token type (not `null`)
|
| + * @param offset the offset from the beginning of the file to the first character in the token
|
| + * @param value the lexeme represented by this token (not `null`)
|
| + */
|
| + Token.con2(this.type, this.offset, String value) {
|
| + this._value = StringUtilities.intern(value);
|
| }
|
| -}
|
|
|
| -/**
|
| - * Instances of the class `StringScanner` implement a scanner that reads from a string. The
|
| - * scanning logic is in the superclass.
|
| - */
|
| -class StringScanner extends AbstractScanner {
|
| /**
|
| - * The string from which characters will be read.
|
| + * Return the offset from the beginning of the file to the character after last character of the
|
| + * token.
|
| + *
|
| + * @return the offset from the beginning of the file to the first character after last character
|
| + * of the token
|
| */
|
| - final String _string;
|
| + int get end => offset + length;
|
|
|
| /**
|
| - * The number of characters in the string.
|
| + * Return the number of characters in the node's source range.
|
| + *
|
| + * @return the number of characters in the node's source range
|
| */
|
| - int _stringLength = 0;
|
| + int get length => lexeme.length;
|
|
|
| /**
|
| - * The index, relative to the string, of the last character that was read.
|
| + * Return the lexeme that represents this token.
|
| + *
|
| + * @return the lexeme (not `null`)
|
| */
|
| - int _charOffset = 0;
|
| + String get lexeme => _value;
|
|
|
| /**
|
| - * Initialize a newly created scanner to scan the characters in the given string.
|
| + * Return the next token in the token stream.
|
| *
|
| - * @param source the source being scanned
|
| - * @param string the string from which characters will be read
|
| + * @return the next token in the token stream
|
| */
|
| - StringScanner(Source source, this._string) : super(source) {
|
| - this._stringLength = _string.length;
|
| - this._charOffset = -1;
|
| - }
|
| -
|
| - @override
|
| - int get offset => _charOffset;
|
| + Token get next => _next;
|
|
|
| - void set offset(int offset) {
|
| - _charOffset = offset;
|
| - }
|
| + /**
|
| + * Return `true` if this token is a synthetic token. A synthetic token is a token that was
|
| + * introduced by the parser in order to recover from an error in the code. Synthetic tokens always
|
| + * have a length of zero (`0`).
|
| + *
|
| + * @return `true` if this token is a synthetic token
|
| + */
|
| + bool get isSynthetic => length == 0;
|
|
|
| - @override
|
| - int advance() {
|
| - if (++_charOffset < _stringLength) {
|
| - return _string.codeUnitAt(_charOffset);
|
| - }
|
| - _charOffset = _stringLength;
|
| - return -1;
|
| + /**
|
| + * Set the next token in the token stream to the given token. This has the side-effect of setting
|
| + * this token to be the previous token for the given token.
|
| + *
|
| + * @param token the next token in the token stream
|
| + * @return the token that was passed in
|
| + */
|
| + Token setNext(Token token) {
|
| + _next = token;
|
| + token.previous = this;
|
| + return token;
|
| }
|
|
|
| @override
|
| - String getString(int start, int endDelta) => _string.substring(start, _charOffset + 1 + endDelta).toString();
|
| -
|
| - @override
|
| - int peek() {
|
| - if (_charOffset + 1 < _stringLength) {
|
| - return _string.codeUnitAt(_charOffset + 1);
|
| - }
|
| - return -1;
|
| - }
|
| + String toString() => lexeme;
|
| }
|
|
|
| /**
|
| - * Instances of the class `HtmlParser` are used to parse tokens into a AST structure comprised
|
| - * of [XmlNode]s.
|
| + * The enumeration `TokenType` defines the types of tokens that can be returned by the
|
| + * scanner.
|
| */
|
| -class HtmlParser extends XmlParser {
|
| +class TokenType extends Enum<TokenType> {
|
| /**
|
| - * The line information associated with the source being parsed.
|
| + * The type of the token that marks the end of the input.
|
| */
|
| - LineInfo _lineInfo;
|
| + static const TokenType EOF = const TokenType_EOF('EOF', 0, "");
|
|
|
| - /**
|
| - * The error listener to which errors will be reported.
|
| - */
|
| - final AnalysisErrorListener _errorListener;
|
| + static const TokenType EQ = const TokenType('EQ', 1, "=");
|
|
|
| - static String _APPLICATION_DART_IN_DOUBLE_QUOTES = "\"application/dart\"";
|
| + static const TokenType GT = const TokenType('GT', 2, ">");
|
|
|
| - static String _APPLICATION_DART_IN_SINGLE_QUOTES = "'application/dart'";
|
| + static const TokenType LT_SLASH = const TokenType('LT_SLASH', 3, "</");
|
|
|
| - static String _SCRIPT = "script";
|
| + static const TokenType LT = const TokenType('LT', 4, "<");
|
|
|
| - static String _TYPE = "type";
|
| + static const TokenType SLASH_GT = const TokenType('SLASH_GT', 5, "/>");
|
| +
|
| + static const TokenType COMMENT = const TokenType('COMMENT', 6, null);
|
| +
|
| + static const TokenType DECLARATION = const TokenType('DECLARATION', 7, null);
|
| +
|
| + static const TokenType DIRECTIVE = const TokenType('DIRECTIVE', 8, null);
|
| +
|
| + static const TokenType STRING = const TokenType('STRING', 9, null);
|
| +
|
| + static const TokenType TAG = const TokenType('TAG', 10, null);
|
| +
|
| + static const TokenType TEXT = const TokenType('TEXT', 11, null);
|
| +
|
| + static const List<TokenType> values = const [
|
| + EOF,
|
| + EQ,
|
| + GT,
|
| + LT_SLASH,
|
| + LT,
|
| + SLASH_GT,
|
| + COMMENT,
|
| + DECLARATION,
|
| + DIRECTIVE,
|
| + STRING,
|
| + TAG,
|
| + TEXT];
|
|
|
| /**
|
| - * A set containing the names of tags that do not have a closing tag.
|
| + * The lexeme that defines this type of token, or `null` if there is more than one possible
|
| + * lexeme for this type of token.
|
| */
|
| - static Set<String> SELF_CLOSING = new Set<String>.from(JavaArrays.asList(<String> [
|
| - "area",
|
| - "base",
|
| - "basefont",
|
| - "br",
|
| - "col",
|
| - "frame",
|
| - "hr",
|
| - "img",
|
| - "input",
|
| - "link",
|
| - "meta",
|
| - "param",
|
| - "!"]));
|
| + final String lexeme;
|
| +
|
| + const TokenType(String name, int ordinal, this.lexeme) : super(name, ordinal);
|
| +}
|
| +
|
| +class TokenType_EOF extends TokenType {
|
| + const TokenType_EOF(String name, int ordinal, String arg0) : super(name, ordinal, arg0);
|
| +
|
| + @override
|
| + String toString() => "-eof-";
|
| +}
|
| +
|
| +/**
|
| + * Instances of `XmlAttributeNode` represent name/value pairs owned by an [XmlTagNode].
|
| + */
|
| +class XmlAttributeNode extends XmlNode {
|
| + final Token _name;
|
| +
|
| + final Token equals;
|
| +
|
| + final Token _value;
|
| +
|
| + List<XmlExpression> expressions = XmlExpression.EMPTY_ARRAY;
|
|
|
| /**
|
| - * Given the contents of an embedded expression that occurs at the given offset, parse it as a
|
| - * Dart expression. The contents should not include the expression's delimiters.
|
| + * Construct a new instance representing an XML attribute.
|
| *
|
| - * @param source the source that contains that given token
|
| - * @param token the token to start parsing from
|
| - * @return the Dart expression that was parsed
|
| + * @param name the name token (not `null`). This may be a zero length token if the attribute
|
| + * is badly formed.
|
| + * @param equals the equals sign or `null` if none
|
| + * @param value the value token (not `null`)
|
| */
|
| - static Expression parseEmbeddedExpression(Source source, sc.Token token, AnalysisErrorListener errorListener) {
|
| - Parser parser = new Parser(source, errorListener);
|
| - return parser.parseExpression(token);
|
| - }
|
| + XmlAttributeNode(this._name, this.equals, this._value);
|
| +
|
| + @override
|
| + accept(XmlVisitor visitor) => visitor.visitXmlAttributeNode(this);
|
| +
|
| + @override
|
| + Token get beginToken => _name;
|
| +
|
| + @override
|
| + Token get endToken => _value;
|
|
|
| /**
|
| - * Given the contents of an embedded expression that occurs at the given offset, scans it as a
|
| - * Dart code.
|
| + * Answer the attribute name. This may be a zero length string if the attribute is badly formed.
|
| *
|
| - * @param source the source of that contains the given contents
|
| - * @param contents the contents to scan
|
| - * @param contentOffset the offset of the contents in the larger file
|
| - * @return the first Dart token
|
| + * @return the name (not `null`)
|
| */
|
| - static sc.Token scanDartSource(Source source, LineInfo lineInfo, String contents, int contentOffset, AnalysisErrorListener errorListener) {
|
| - LineInfo_Location location = lineInfo.getLocation(contentOffset);
|
| - sc.Scanner scanner = new sc.Scanner(source, new sc.SubSequenceReader(contents, contentOffset), errorListener);
|
| - scanner.setSourceStart(location.lineNumber, location.columnNumber);
|
| - return scanner.tokenize();
|
| - }
|
| + String get name => _name.lexeme;
|
|
|
| /**
|
| - * Construct a parser for the specified source.
|
| + * Answer the attribute name token. This may be a zero length token if the attribute is badly
|
| + * formed.
|
| *
|
| - * @param source the source being parsed
|
| - * @param errorListener the error listener to which errors will be reported
|
| + * @return the name token (not `null`)
|
| */
|
| - HtmlParser(Source source, this._errorListener) : super(source);
|
| + Token get nameToken => _name;
|
|
|
| /**
|
| - * Parse the given tokens.
|
| + * Answer the lexeme for the value token without the leading and trailing quotes.
|
| *
|
| - * @param token the first token in the stream of tokens to be parsed
|
| - * @param lineInfo the line information created by the scanner
|
| - * @return the parse result (not `null`)
|
| + * @return the text or `null` if the value is not specified
|
| */
|
| - HtmlUnit parse(Token token, LineInfo lineInfo) {
|
| - this._lineInfo = lineInfo;
|
| - List<XmlTagNode> tagNodes = parseTopTagNodes(token);
|
| - return new HtmlUnit(token, tagNodes, currentToken);
|
| - }
|
| -
|
| - @override
|
| - XmlAttributeNode createAttributeNode(Token name, Token equals, Token value) => new XmlAttributeNode(name, equals, value);
|
| -
|
| - @override
|
| - XmlTagNode createTagNode(Token nodeStart, Token tag, List<XmlAttributeNode> attributes, Token attributeEnd, List<XmlTagNode> tagNodes, Token contentEnd, Token closingTag, Token nodeEnd) {
|
| - if (_isScriptNode(tag, attributes, tagNodes)) {
|
| - HtmlScriptTagNode tagNode = new HtmlScriptTagNode(nodeStart, tag, attributes, attributeEnd, tagNodes, contentEnd, closingTag, nodeEnd);
|
| - String contents = tagNode.content;
|
| - int contentOffset = attributeEnd.end;
|
| - LineInfo_Location location = _lineInfo.getLocation(contentOffset);
|
| - sc.Scanner scanner = new sc.Scanner(source, new sc.SubSequenceReader(contents, contentOffset), _errorListener);
|
| - scanner.setSourceStart(location.lineNumber, location.columnNumber);
|
| - sc.Token firstToken = scanner.tokenize();
|
| - Parser parser = new Parser(source, _errorListener);
|
| - CompilationUnit unit = parser.parseCompilationUnit(firstToken);
|
| - unit.lineInfo = _lineInfo;
|
| - tagNode.script = unit;
|
| - return tagNode;
|
| + String get text {
|
| + if (_value == null) {
|
| + return null;
|
| }
|
| - return new XmlTagNode(nodeStart, tag, attributes, attributeEnd, tagNodes, contentEnd, closingTag, nodeEnd);
|
| + //TODO (danrubel): replace HTML character encodings with the actual characters
|
| + String text = _value.lexeme;
|
| + int len = text.length;
|
| + if (len > 0) {
|
| + if (text.codeUnitAt(0) == 0x22) {
|
| + if (len > 1 && text.codeUnitAt(len - 1) == 0x22) {
|
| + return text.substring(1, len - 1);
|
| + } else {
|
| + return text.substring(1);
|
| + }
|
| + } else if (text.codeUnitAt(0) == 0x27) {
|
| + if (len > 1 && text.codeUnitAt(len - 1) == 0x27) {
|
| + return text.substring(1, len - 1);
|
| + } else {
|
| + return text.substring(1);
|
| + }
|
| + }
|
| + }
|
| + return text;
|
| }
|
|
|
| - @override
|
| - bool isSelfClosing(Token tag) => SELF_CLOSING.contains(tag.lexeme);
|
| -
|
| /**
|
| - * Determine if the specified node is a Dart script.
|
| + * Answer the offset of the value after the leading quote.
|
| *
|
| - * @param node the node to be tested (not `null`)
|
| - * @return `true` if the node is a Dart script
|
| + * @return the offset of the value, or `-1` if the value is not specified
|
| */
|
| - bool _isScriptNode(Token tag, List<XmlAttributeNode> attributes, List<XmlTagNode> tagNodes) {
|
| - if (tagNodes.length != 0 || tag.lexeme != _SCRIPT) {
|
| - return false;
|
| + int get textOffset {
|
| + if (_value == null) {
|
| + return -1;
|
| }
|
| - for (XmlAttributeNode attribute in attributes) {
|
| - if (attribute.name == _TYPE) {
|
| - Token valueToken = attribute.valueToken;
|
| - if (valueToken != null) {
|
| - String value = valueToken.lexeme;
|
| - if (value == _APPLICATION_DART_IN_DOUBLE_QUOTES || value == _APPLICATION_DART_IN_SINGLE_QUOTES) {
|
| - return true;
|
| - }
|
| - }
|
| - }
|
| + String text = _value.lexeme;
|
| + if (StringUtilities.startsWithChar(text, 0x22) || StringUtilities.startsWithChar(text, 0x27)) {
|
| + return _value.offset + 1;
|
| }
|
| - return false;
|
| - }
|
| -}
|
| -
|
| -/**
|
| - * Instances of the class `RecursiveXmlVisitor` implement an XML visitor that will recursively
|
| - * visit all of the nodes in an XML structure. For example, using an instance of this class to visit
|
| - * a [XmlTagNode] will also cause all of the contained [XmlAttributeNode]s and
|
| - * [XmlTagNode]s to be visited.
|
| - *
|
| - * Subclasses that override a visit method must either invoke the overridden visit method or must
|
| - * explicitly ask the visited node to visit its children. Failure to do so will cause the children
|
| - * of the visited node to not be visited.
|
| - */
|
| -class RecursiveXmlVisitor<R> implements XmlVisitor<R> {
|
| - @override
|
| - R visitHtmlScriptTagNode(HtmlScriptTagNode node) {
|
| - node.visitChildren(this);
|
| - return null;
|
| - }
|
| -
|
| - @override
|
| - R visitHtmlUnit(HtmlUnit node) {
|
| - node.visitChildren(this);
|
| - return null;
|
| + return _value.offset;
|
| }
|
|
|
| - @override
|
| - R visitXmlAttributeNode(XmlAttributeNode node) {
|
| - node.visitChildren(this);
|
| - return null;
|
| - }
|
| + /**
|
| + * Answer the attribute value token. A properly formed value will start and end with matching
|
| + * quote characters, but the value returned may not be properly formed.
|
| + *
|
| + * @return the value token or `null` if this represents a badly formed attribute
|
| + */
|
| + Token get valueToken => _value;
|
|
|
| @override
|
| - R visitXmlTagNode(XmlTagNode node) {
|
| - node.visitChildren(this);
|
| - return null;
|
| + void visitChildren(XmlVisitor visitor) {
|
| }
|
| }
|
|
|
| /**
|
| - * Instances of the class `HtmlScriptTagNode` represent a script tag within an HTML file that
|
| - * references a Dart script.
|
| + * Instances of the class `XmlExpression` represent an abstract expression embedded into
|
| + * [XmlNode].
|
| */
|
| -class HtmlScriptTagNode extends XmlTagNode {
|
| +abstract class XmlExpression {
|
| /**
|
| - * The AST structure representing the Dart code within this tag.
|
| + * An empty array of expressions.
|
| */
|
| - CompilationUnit _script;
|
| + static List<XmlExpression> EMPTY_ARRAY = new List<XmlExpression>(0);
|
|
|
| /**
|
| - * The element representing this script.
|
| + * Check if the given offset belongs to the expression's source range.
|
| */
|
| - HtmlScriptElement scriptElement;
|
| + bool contains(int offset) => this.offset <= offset && offset < end;
|
|
|
| /**
|
| - * Initialize a newly created node to represent a script tag within an HTML file that references a
|
| - * Dart script.
|
| + * Return the offset of the character immediately following the last character of this
|
| + * expression's source range. This is equivalent to `getOffset() + getLength()`.
|
| *
|
| - * @param nodeStart the token marking the beginning of the tag
|
| - * @param tag the name of the tag
|
| - * @param attributes the attributes in the tag
|
| - * @param attributeEnd the token terminating the region where attributes can be
|
| - * @param tagNodes the children of the tag
|
| - * @param contentEnd the token that starts the closing tag
|
| - * @param closingTag the name of the tag that occurs in the closing tag
|
| - * @param nodeEnd the last token in the tag
|
| + * @return the offset of the character just past the expression's source range
|
| */
|
| - HtmlScriptTagNode(Token nodeStart, Token tag, List<XmlAttributeNode> attributes, Token attributeEnd, List<XmlTagNode> tagNodes, Token contentEnd, Token closingTag, Token nodeEnd) : super(nodeStart, tag, attributes, attributeEnd, tagNodes, contentEnd, closingTag, nodeEnd);
|
| + int get end;
|
|
|
| - @override
|
| - accept(XmlVisitor visitor) => visitor.visitHtmlScriptTagNode(this);
|
| + /**
|
| + * Return the number of characters in the expression's source range.
|
| + */
|
| + int get length;
|
|
|
| /**
|
| - * Return the AST structure representing the Dart code within this tag, or `null` if this
|
| - * tag references an external script.
|
| - *
|
| - * @return the AST structure representing the Dart code within this tag
|
| + * Return the offset of the first character in the expression's source range.
|
| */
|
| - CompilationUnit get script => _script;
|
| + int get offset;
|
|
|
| /**
|
| - * Set the AST structure representing the Dart code within this tag to the given compilation unit.
|
| + * Return the [Reference] at the given offset.
|
| *
|
| - * @param unit the AST structure representing the Dart code within this tag
|
| + * @param offset the offset from the beginning of the file
|
| + * @return the [Reference] at the given offset, maybe `null`
|
| */
|
| - void set script(CompilationUnit unit) {
|
| - _script = unit;
|
| - }
|
| + XmlExpression_Reference getReference(int offset);
|
| }
|
|
|
| /**
|
| - * The enumeration `TokenType` defines the types of tokens that can be returned by the
|
| - * scanner.
|
| + * The reference to the [Element].
|
| */
|
| -class TokenType extends Enum<TokenType> {
|
| - /**
|
| - * The type of the token that marks the end of the input.
|
| - */
|
| - static const TokenType EOF = const TokenType_EOF('EOF', 0, "");
|
| +class XmlExpression_Reference {
|
| + Element element;
|
|
|
| - static const TokenType EQ = const TokenType('EQ', 1, "=");
|
| + int offset = 0;
|
|
|
| - static const TokenType GT = const TokenType('GT', 2, ">");
|
| + int length = 0;
|
|
|
| - static const TokenType LT_SLASH = const TokenType('LT_SLASH', 3, "</");
|
| + XmlExpression_Reference(Element element, int offset, int length) {
|
| + this.element = element;
|
| + this.offset = offset;
|
| + this.length = length;
|
| + }
|
| +}
|
|
|
| - static const TokenType LT = const TokenType('LT', 4, "<");
|
| +/**
|
| + * The abstract class `XmlNode` defines behavior common to all XML/HTML nodes.
|
| + */
|
| +abstract class XmlNode {
|
| + /**
|
| + * The parent of the node, or `null` if the node is the root of an AST structure.
|
| + */
|
| + XmlNode _parent;
|
|
|
| - static const TokenType SLASH_GT = const TokenType('SLASH_GT', 5, "/>");
|
| + /**
|
| + * The element associated with this node or `null` if the receiver is not resolved.
|
| + */
|
| + Element _element;
|
|
|
| - static const TokenType COMMENT = const TokenType('COMMENT', 6, null);
|
| + /**
|
| + * Use the given visitor to visit this node.
|
| + *
|
| + * @param visitor the visitor that will visit this node
|
| + * @return the value returned by the visitor as a result of visiting this node
|
| + */
|
| + accept(XmlVisitor visitor);
|
|
|
| - static const TokenType DECLARATION = const TokenType('DECLARATION', 7, null);
|
| + /**
|
| + * Return the first token included in this node's source range.
|
| + *
|
| + * @return the first token or `null` if none
|
| + */
|
| + Token get beginToken;
|
|
|
| - static const TokenType DIRECTIVE = const TokenType('DIRECTIVE', 8, null);
|
| + /**
|
| + * Return the element associated with this node.
|
| + *
|
| + * @return the element or `null` if the receiver is not resolved
|
| + */
|
| + Element get element => _element;
|
|
|
| - static const TokenType STRING = const TokenType('STRING', 9, null);
|
| + /**
|
| + * Return the offset of the character immediately following the last character of this node's
|
| + * source range. This is equivalent to `node.getOffset() + node.getLength()`. For an html
|
| + * unit this will be equal to the length of the unit's source.
|
| + *
|
| + * @return the offset of the character just past the node's source range
|
| + */
|
| + int get end => offset + length;
|
|
|
| - static const TokenType TAG = const TokenType('TAG', 10, null);
|
| + /**
|
| + * Return the last token included in this node's source range.
|
| + *
|
| + * @return the last token or `null` if none
|
| + */
|
| + Token get endToken;
|
|
|
| - static const TokenType TEXT = const TokenType('TEXT', 11, null);
|
| + /**
|
| + * Return the number of characters in the node's source range.
|
| + *
|
| + * @return the number of characters in the node's source range
|
| + */
|
| + int get length {
|
| + Token beginToken = this.beginToken;
|
| + Token endToken = this.endToken;
|
| + if (beginToken == null || endToken == null) {
|
| + return -1;
|
| + }
|
| + return endToken.offset + endToken.length - beginToken.offset;
|
| + }
|
|
|
| - static const List<TokenType> values = const [
|
| - EOF,
|
| - EQ,
|
| - GT,
|
| - LT_SLASH,
|
| - LT,
|
| - SLASH_GT,
|
| - COMMENT,
|
| - DECLARATION,
|
| - DIRECTIVE,
|
| - STRING,
|
| - TAG,
|
| - TEXT];
|
| + /**
|
| + * Return the offset from the beginning of the file to the first character in the node's source
|
| + * range.
|
| + *
|
| + * @return the offset from the beginning of the file to the first character in the node's source
|
| + * range
|
| + */
|
| + int get offset {
|
| + Token beginToken = this.beginToken;
|
| + if (beginToken == null) {
|
| + return -1;
|
| + }
|
| + return this.beginToken.offset;
|
| + }
|
| +
|
| + /**
|
| + * Return this node's parent node, or `null` if this node is the root of an AST structure.
|
| + *
|
| + * Note that the relationship between an AST node and its parent node may change over the lifetime
|
| + * of a node.
|
| + *
|
| + * @return the parent of this node, or `null` if none
|
| + */
|
| + XmlNode get parent => _parent;
|
| +
|
| + /**
|
| + * Set the element associated with this node.
|
| + *
|
| + * @param element the element
|
| + */
|
| + void set element(Element element) {
|
| + this._element = element;
|
| + }
|
| +
|
| + @override
|
| + String toString() {
|
| + PrintStringWriter writer = new PrintStringWriter();
|
| + accept(new ToSourceVisitor(writer));
|
| + return writer.toString();
|
| + }
|
| +
|
| + /**
|
| + * Use the given visitor to visit all of the children of this node. The children will be visited
|
| + * in source order.
|
| + *
|
| + * @param visitor the visitor that will be used to visit the children of this node
|
| + */
|
| + void visitChildren(XmlVisitor visitor);
|
| +
|
| + /**
|
| + * Make this node the parent of the given child node.
|
| + *
|
| + * @param child the node that will become a child of this node
|
| + * @return the node that was made a child of this node
|
| + */
|
| + XmlNode becomeParentOf(XmlNode child) {
|
| + if (child != null) {
|
| + XmlNode node = child;
|
| + node.parent = this;
|
| + }
|
| + return child;
|
| + }
|
| +
|
| + /**
|
| + * Make this node the parent of the given child nodes.
|
| + *
|
| + * @param children the nodes that will become the children of this node
|
| + * @param ifEmpty the (empty) nodes to return if "children" is empty
|
| + * @return the nodes that were made children of this node
|
| + */
|
| + List becomeParentOfAll(List children, {List ifEmpty}) {
|
| + if (children == null || children.isEmpty) {
|
| + if (ifEmpty != null) {
|
| + return ifEmpty;
|
| + }
|
| + }
|
| + if (children != null) {
|
| + for (JavaIterator iter = new JavaIterator(children); iter.hasNext;) {
|
| + XmlNode node = iter.next();
|
| + node.parent = this;
|
| + }
|
| + // This will create ArrayList for exactly given number of elements.
|
| + return new List.from(children);
|
| + }
|
| + return children;
|
| + }
|
| +
|
| + /**
|
| + * This method exists for debugging purposes only.
|
| + */
|
| + void _appendIdentifier(JavaStringBuilder builder, XmlNode node) {
|
| + if (node is XmlTagNode) {
|
| + builder.append(node.tag);
|
| + } else if (node is XmlAttributeNode) {
|
| + builder.append(node.name);
|
| + } else {
|
| + builder.append("htmlUnit");
|
| + }
|
| + }
|
| +
|
| + /**
|
| + * This method exists for debugging purposes only.
|
| + */
|
| + String _buildRecursiveStructureMessage(XmlNode newParent) {
|
| + JavaStringBuilder builder = new JavaStringBuilder();
|
| + builder.append("Attempt to create recursive structure: ");
|
| + XmlNode current = newParent;
|
| + while (current != null) {
|
| + if (!identical(current, newParent)) {
|
| + builder.append(" -> ");
|
| + }
|
| + if (identical(current, this)) {
|
| + builder.appendChar(0x2A);
|
| + _appendIdentifier(builder, current);
|
| + builder.appendChar(0x2A);
|
| + } else {
|
| + _appendIdentifier(builder, current);
|
| + }
|
| + current = current.parent;
|
| + }
|
| + return builder.toString();
|
| + }
|
| +
|
| + /**
|
| + * Set the parent of this node to the given node.
|
| + *
|
| + * @param newParent the node that is to be made the parent of this node
|
| + */
|
| + void set parent(XmlNode newParent) {
|
| + XmlNode current = newParent;
|
| + while (current != null) {
|
| + if (identical(current, this)) {
|
| + AnalysisEngine.instance.logger.logError2("Circular structure while setting an XML node's parent", new IllegalArgumentException(_buildRecursiveStructureMessage(newParent)));
|
| + return;
|
| + }
|
| + current = current.parent;
|
| + }
|
| + _parent = newParent;
|
| + }
|
| +}
|
| +
|
| +/**
|
| + * Instances of the class `XmlParser` are used to parse tokens into a AST structure comprised
|
| + * of [XmlNode]s.
|
| + */
|
| +class XmlParser {
|
| + /**
|
| + * The source being parsed.
|
| + */
|
| + final Source source;
|
| +
|
| + /**
|
| + * The next token to be parsed.
|
| + */
|
| + Token _currentToken;
|
| +
|
| + /**
|
| + * Construct a parser for the specified source.
|
| + *
|
| + * @param source the source being parsed
|
| + */
|
| + XmlParser(this.source);
|
| +
|
| + /**
|
| + * Create a node representing an attribute.
|
| + *
|
| + * @param name the name of the attribute
|
| + * @param equals the equals sign, or `null` if there is no value
|
| + * @param value the value of the attribute
|
| + * @return the node that was created
|
| + */
|
| + XmlAttributeNode createAttributeNode(Token name, Token equals, Token value) => new XmlAttributeNode(name, equals, value);
|
| +
|
| + /**
|
| + * Create a node representing a tag.
|
| + *
|
| + * @param nodeStart the token marking the beginning of the tag
|
| + * @param tag the name of the tag
|
| + * @param attributes the attributes in the tag
|
| + * @param attributeEnd the token terminating the region where attributes can be
|
| + * @param tagNodes the children of the tag
|
| + * @param contentEnd the token that starts the closing tag
|
| + * @param closingTag the name of the tag that occurs in the closing tag
|
| + * @param nodeEnd the last token in the tag
|
| + * @return the node that was created
|
| + */
|
| + XmlTagNode createTagNode(Token nodeStart, Token tag, List<XmlAttributeNode> attributes, Token attributeEnd, List<XmlTagNode> tagNodes, Token contentEnd, Token closingTag, Token nodeEnd) => new XmlTagNode(nodeStart, tag, attributes, attributeEnd, tagNodes, contentEnd, closingTag, nodeEnd);
|
| +
|
| + /**
|
| + * Answer `true` if the specified tag is self closing and thus should never have content or
|
| + * child tag nodes.
|
| + *
|
| + * @param tag the tag (not `null`)
|
| + * @return `true` if self closing
|
| + */
|
| + bool isSelfClosing(Token tag) => false;
|
| +
|
| + /**
|
| + * Parse the entire token stream and in the process, advance the current token to the end of the
|
| + * token stream.
|
| + *
|
| + * @return the list of tag nodes found (not `null`, contains no `null`)
|
| + */
|
| + List<XmlTagNode> parseTopTagNodes(Token firstToken) {
|
| + _currentToken = firstToken;
|
| + List<XmlTagNode> tagNodes = new List<XmlTagNode>();
|
| + TokenType type = _currentToken.type;
|
| + while (type != TokenType.EOF) {
|
| + if (type == TokenType.LT) {
|
| + tagNodes.add(_parseTagNode());
|
| + } else if (type == TokenType.DECLARATION || type == TokenType.DIRECTIVE || type == TokenType.COMMENT) {
|
| + // ignored tokens
|
| + _currentToken = _currentToken.next;
|
| + } else {
|
| + _reportUnexpectedToken();
|
| + _currentToken = _currentToken.next;
|
| + }
|
| + type = _currentToken.type;
|
| + }
|
| + return tagNodes;
|
| + }
|
| +
|
| + /**
|
| + * Answer the current token.
|
| + *
|
| + * @return the current token
|
| + */
|
| + Token get currentToken => _currentToken;
|
| +
|
| + /**
|
| + * Insert a synthetic token of the specified type before the current token
|
| + *
|
| + * @param type the type of token to be inserted (not `null`)
|
| + * @return the synthetic token that was inserted (not `null`)
|
| + */
|
| + Token _insertSyntheticToken(TokenType type) {
|
| + Token token = new Token.con2(type, _currentToken.offset, "");
|
| + _currentToken.previous.setNext(token);
|
| + token.setNext(_currentToken);
|
| + return token;
|
| + }
|
| +
|
| + /**
|
| + * Parse the token stream for an attribute. This method advances the current token over the
|
| + * attribute, but should not be called if the [currentToken] is not [TokenType#TAG].
|
| + *
|
| + * @return the attribute (not `null`)
|
| + */
|
| + XmlAttributeNode _parseAttribute() {
|
| + // Assume the current token is a tag
|
| + Token name = _currentToken;
|
| + _currentToken = _currentToken.next;
|
| + // Equals sign
|
| + Token equals;
|
| + if (_currentToken.type == TokenType.EQ) {
|
| + equals = _currentToken;
|
| + _currentToken = _currentToken.next;
|
| + } else {
|
| + _reportUnexpectedToken();
|
| + equals = _insertSyntheticToken(TokenType.EQ);
|
| + }
|
| + // String value
|
| + Token value;
|
| + if (_currentToken.type == TokenType.STRING) {
|
| + value = _currentToken;
|
| + _currentToken = _currentToken.next;
|
| + } else {
|
| + _reportUnexpectedToken();
|
| + value = _insertSyntheticToken(TokenType.STRING);
|
| + }
|
| + return createAttributeNode(name, equals, value);
|
| + }
|
| +
|
| + /**
|
| + * Parse the stream for a sequence of attributes. This method advances the current token to the
|
| + * next [TokenType#GT], [TokenType#SLASH_GT], or [TokenType#EOF].
|
| + *
|
| + * @return a collection of zero or more attributes (not `null`, contains no `null`s)
|
| + */
|
| + List<XmlAttributeNode> _parseAttributes() {
|
| + TokenType type = _currentToken.type;
|
| + if (type == TokenType.GT || type == TokenType.SLASH_GT || type == TokenType.EOF) {
|
| + return XmlTagNode.NO_ATTRIBUTES;
|
| + }
|
| + List<XmlAttributeNode> attributes = new List<XmlAttributeNode>();
|
| + while (type != TokenType.GT && type != TokenType.SLASH_GT && type != TokenType.EOF) {
|
| + if (type == TokenType.TAG) {
|
| + attributes.add(_parseAttribute());
|
| + } else {
|
| + _reportUnexpectedToken();
|
| + _currentToken = _currentToken.next;
|
| + }
|
| + type = _currentToken.type;
|
| + }
|
| + return attributes;
|
| + }
|
| +
|
| + /**
|
| + * Parse the stream for a sequence of tag nodes existing within a parent tag node. This method
|
| + * advances the current token to the next [TokenType#LT_SLASH] or [TokenType#EOF].
|
| + *
|
| + * @return a list of nodes (not `null`, contains no `null`s)
|
| + */
|
| + List<XmlTagNode> _parseChildTagNodes() {
|
| + TokenType type = _currentToken.type;
|
| + if (type == TokenType.LT_SLASH || type == TokenType.EOF) {
|
| + return XmlTagNode.NO_TAG_NODES;
|
| + }
|
| + List<XmlTagNode> nodes = new List<XmlTagNode>();
|
| + while (type != TokenType.LT_SLASH && type != TokenType.EOF) {
|
| + if (type == TokenType.LT) {
|
| + nodes.add(_parseTagNode());
|
| + } else if (type == TokenType.COMMENT) {
|
| + // ignored token
|
| + _currentToken = _currentToken.next;
|
| + } else {
|
| + _reportUnexpectedToken();
|
| + _currentToken = _currentToken.next;
|
| + }
|
| + type = _currentToken.type;
|
| + }
|
| + return nodes;
|
| + }
|
| +
|
| + /**
|
| + * Parse the token stream for the next tag node. This method advances current token over the
|
| + * parsed tag node, but should only be called if the current token is [TokenType#LT]
|
| + *
|
| + * @return the tag node or `null` if none found
|
| + */
|
| + XmlTagNode _parseTagNode() {
|
| + // Assume that the current node is a tag node start TokenType#LT
|
| + Token nodeStart = _currentToken;
|
| + _currentToken = _currentToken.next;
|
| + // Get the tag or create a synthetic tag and report an error
|
| + Token tag;
|
| + if (_currentToken.type == TokenType.TAG) {
|
| + tag = _currentToken;
|
| + _currentToken = _currentToken.next;
|
| + } else {
|
| + _reportUnexpectedToken();
|
| + tag = _insertSyntheticToken(TokenType.TAG);
|
| + }
|
| + // Parse the attributes
|
| + List<XmlAttributeNode> attributes = _parseAttributes();
|
| + // Token ending attribute list
|
| + Token attributeEnd;
|
| + if (_currentToken.type == TokenType.GT || _currentToken.type == TokenType.SLASH_GT) {
|
| + attributeEnd = _currentToken;
|
| + _currentToken = _currentToken.next;
|
| + } else {
|
| + _reportUnexpectedToken();
|
| + attributeEnd = _insertSyntheticToken(TokenType.SLASH_GT);
|
| + }
|
| + // If the node has no children, then return the node
|
| + if (attributeEnd.type == TokenType.SLASH_GT || isSelfClosing(tag)) {
|
| + return createTagNode(nodeStart, tag, attributes, attributeEnd, XmlTagNode.NO_TAG_NODES, _currentToken, null, attributeEnd);
|
| + }
|
| + // Parse the child tag nodes
|
| + List<XmlTagNode> tagNodes = _parseChildTagNodes();
|
| + // Token ending child tag nodes
|
| + Token contentEnd;
|
| + if (_currentToken.type == TokenType.LT_SLASH) {
|
| + contentEnd = _currentToken;
|
| + _currentToken = _currentToken.next;
|
| + } else {
|
| + // TODO (danrubel): handle self closing HTML elements by inserting synthetic tokens
|
| + // but not reporting an error
|
| + _reportUnexpectedToken();
|
| + contentEnd = _insertSyntheticToken(TokenType.LT_SLASH);
|
| + }
|
| + // Closing tag
|
| + Token closingTag;
|
| + if (_currentToken.type == TokenType.TAG) {
|
| + closingTag = _currentToken;
|
| + _currentToken = _currentToken.next;
|
| + } else {
|
| + _reportUnexpectedToken();
|
| + closingTag = _insertSyntheticToken(TokenType.TAG);
|
| + }
|
| + // Token ending node
|
| + Token nodeEnd;
|
| + if (_currentToken.type == TokenType.GT) {
|
| + nodeEnd = _currentToken;
|
| + _currentToken = _currentToken.next;
|
| + } else {
|
| + _reportUnexpectedToken();
|
| + nodeEnd = _insertSyntheticToken(TokenType.GT);
|
| + }
|
| + return createTagNode(nodeStart, tag, attributes, attributeEnd, tagNodes, contentEnd, closingTag, nodeEnd);
|
| + }
|
| +
|
| + /**
|
| + * Report the current token as unexpected
|
| + */
|
| + void _reportUnexpectedToken() {
|
| + }
|
| +}
|
| +
|
| +/**
|
| + * Instances of `XmlTagNode` represent XML or HTML elements such as `` and
|
| + * `<body foo="bar"> ... </body>`.
|
| + */
|
| +class XmlTagNode extends XmlNode {
|
| + /**
|
| + * Constant representing empty list of attributes.
|
| + */
|
| + static List<XmlAttributeNode> NO_ATTRIBUTES = new UnmodifiableListView(new List<XmlAttributeNode>());
|
|
|
| /**
|
| - * The lexeme that defines this type of token, or `null` if there is more than one possible
|
| - * lexeme for this type of token.
|
| + * Constant representing empty list of tag nodes.
|
| */
|
| - final String lexeme;
|
| + static List<XmlTagNode> NO_TAG_NODES = new UnmodifiableListView(new List<XmlTagNode>());
|
|
|
| - const TokenType(String name, int ordinal, this.lexeme) : super(name, ordinal);
|
| -}
|
| + /**
|
| + * The starting [TokenType#LT] token (not `null`).
|
| + */
|
| + final Token nodeStart;
|
|
|
| -class TokenType_EOF extends TokenType {
|
| - const TokenType_EOF(String name, int ordinal, String arg0) : super(name, ordinal, arg0);
|
| + /**
|
| + * The [TokenType#TAG] token after the starting '<' (not `null`).
|
| + */
|
| + final Token _tag;
|
|
|
| - @override
|
| - String toString() => "-eof-";
|
| -}
|
| + /**
|
| + * The attributes contained by the receiver (not `null`, contains no `null`s).
|
| + */
|
| + List<XmlAttributeNode> _attributes;
|
|
|
| -/**
|
| - * Instances of `XmlAttributeNode` represent name/value pairs owned by an [XmlTagNode].
|
| - */
|
| -class XmlAttributeNode extends XmlNode {
|
| - final Token _name;
|
| + /**
|
| + * The [TokenType#GT] or [TokenType#SLASH_GT] token after the attributes (not
|
| + * `null`). The token may be the same token as [nodeEnd] if there are no child
|
| + * [tagNodes].
|
| + */
|
| + final Token attributeEnd;
|
|
|
| - final Token equals;
|
| + /**
|
| + * The tag nodes contained in the receiver (not `null`, contains no `null`s).
|
| + */
|
| + List<XmlTagNode> _tagNodes;
|
|
|
| - final Token _value;
|
| + /**
|
| + * The token (not `null`) after the content, which may be
|
| + * * (1) [TokenType#LT_SLASH] for nodes with open and close tags, or
|
| + * * (2) the [TokenType#LT] nodeStart of the next sibling node if this node is self
|
| + * closing or the attributeEnd is [TokenType#SLASH_GT], or
|
| + * * (3) [TokenType#EOF] if the node does not have a closing tag and is the last node in
|
| + * the stream [TokenType#LT_SLASH] token after the content, or `null` if there is no
|
| + * content and the attributes ended with [TokenType#SLASH_GT].
|
| + */
|
| + final Token contentEnd;
|
|
|
| - List<XmlExpression> expressions = XmlExpression.EMPTY_ARRAY;
|
| + /**
|
| + * The closing [TokenType#TAG] after the child elements or `null` if there is no
|
| + * content and the attributes ended with [TokenType#SLASH_GT]
|
| + */
|
| + final Token closingTag;
|
|
|
| /**
|
| - * Construct a new instance representing an XML attribute.
|
| - *
|
| - * @param name the name token (not `null`). This may be a zero length token if the attribute
|
| - * is badly formed.
|
| - * @param equals the equals sign or `null` if none
|
| - * @param value the value token (not `null`)
|
| + * The ending [TokenType#GT] or [TokenType#SLASH_GT] token (not `null`).
|
| */
|
| - XmlAttributeNode(this._name, this.equals, this._value);
|
| + final Token nodeEnd;
|
|
|
| - @override
|
| - accept(XmlVisitor visitor) => visitor.visitXmlAttributeNode(this);
|
| + /**
|
| + * The expressions that are embedded in the tag's content.
|
| + */
|
| + List<XmlExpression> expressions = XmlExpression.EMPTY_ARRAY;
|
|
|
| - @override
|
| - Token get beginToken => _name;
|
| + /**
|
| + * Construct a new instance representing an XML or HTML element
|
| + *
|
| + * @param nodeStart the starting [TokenType#LT] token (not `null`)
|
| + * @param tag the [TokenType#TAG] token after the starting '<' (not `null`).
|
| + * @param attributes the attributes associated with this element or [NO_ATTRIBUTES] (not
|
| + * `null`, contains no `null`s)
|
| + * @param attributeEnd The [TokenType#GT] or [TokenType#SLASH_GT] token after the
|
| + * attributes (not `null`). The token may be the same token as [nodeEnd] if
|
| + * there are no child [tagNodes].
|
| + * @param tagNodes child tag nodes of the receiver or [NO_TAG_NODES] (not `null`,
|
| + * contains no `null`s)
|
| + * @param contentEnd the token (not `null`) after the content, which may be
|
| + * * (1) [TokenType#LT_SLASH] for nodes with open and close tags, or
|
| + * * (2) the [TokenType#LT] nodeStart of the next sibling node if this node is
|
| + * self closing or the attributeEnd is [TokenType#SLASH_GT], or
|
| + * * (3) [TokenType#EOF] if the node does not have a closing tag and is the last
|
| + * node in the stream [TokenType#LT_SLASH] token after the content, or `null`
|
| + * if there is no content and the attributes ended with [TokenType#SLASH_GT].
|
| + * @param closingTag the closing [TokenType#TAG] after the child elements or `null` if
|
| + * there is no content and the attributes ended with [TokenType#SLASH_GT]
|
| + * @param nodeEnd the ending [TokenType#GT] or [TokenType#SLASH_GT] token (not
|
| + * `null`)
|
| + */
|
| + XmlTagNode(this.nodeStart, this._tag, List<XmlAttributeNode> attributes, this.attributeEnd, List<XmlTagNode> tagNodes, this.contentEnd, this.closingTag, this.nodeEnd) {
|
| + this._attributes = becomeParentOfAll(attributes, ifEmpty: NO_ATTRIBUTES);
|
| + this._tagNodes = becomeParentOfAll(tagNodes, ifEmpty: NO_TAG_NODES);
|
| + }
|
|
|
| @override
|
| - Token get endToken => _value;
|
| + accept(XmlVisitor visitor) => visitor.visitXmlTagNode(this);
|
|
|
| /**
|
| - * Answer the attribute name. This may be a zero length string if the attribute is badly formed.
|
| + * Answer the attribute with the specified name.
|
| *
|
| - * @return the name (not `null`)
|
| + * @param name the attribute name
|
| + * @return the attribute or `null` if no matching attribute is found
|
| */
|
| - String get name => _name.lexeme;
|
| + XmlAttributeNode getAttribute(String name) {
|
| + for (XmlAttributeNode attribute in _attributes) {
|
| + if (attribute.name == name) {
|
| + return attribute;
|
| + }
|
| + }
|
| + return null;
|
| + }
|
|
|
| /**
|
| - * Answer the attribute name token. This may be a zero length token if the attribute is badly
|
| - * formed.
|
| + * Answer the receiver's attributes. Callers should not manipulate the returned list to edit the
|
| + * AST structure.
|
| *
|
| - * @return the name token (not `null`)
|
| + * @return the attributes (not `null`, contains no `null`s)
|
| */
|
| - Token get nameToken => _name;
|
| + List<XmlAttributeNode> get attributes => _attributes;
|
|
|
| /**
|
| - * Answer the lexeme for the value token without the leading and trailing quotes.
|
| + * Find the attribute with the given name (see [getAttribute] and answer the lexeme
|
| + * for the attribute's value token without the leading and trailing quotes (see
|
| + * [XmlAttributeNode#getText]).
|
| *
|
| - * @return the text or `null` if the value is not specified
|
| + * @param name the attribute name
|
| + * @return the attribute text or `null` if no matching attribute is found
|
| */
|
| - String get text {
|
| - if (_value == null) {
|
| - return null;
|
| - }
|
| - //TODO (danrubel): replace HTML character encodings with the actual characters
|
| - String text = _value.lexeme;
|
| - int len = text.length;
|
| - if (len > 0) {
|
| - if (text.codeUnitAt(0) == 0x22) {
|
| - if (len > 1 && text.codeUnitAt(len - 1) == 0x22) {
|
| - return text.substring(1, len - 1);
|
| - } else {
|
| - return text.substring(1);
|
| - }
|
| - } else if (text.codeUnitAt(0) == 0x27) {
|
| - if (len > 1 && text.codeUnitAt(len - 1) == 0x27) {
|
| - return text.substring(1, len - 1);
|
| - } else {
|
| - return text.substring(1);
|
| - }
|
| - }
|
| - }
|
| - return text;
|
| + String getAttributeText(String name) {
|
| + XmlAttributeNode attribute = getAttribute(name);
|
| + return attribute != null ? attribute.text : null;
|
| }
|
|
|
| + @override
|
| + Token get beginToken => nodeStart;
|
| +
|
| /**
|
| - * Answer the offset of the value after the leading quote.
|
| + * Answer a string representing the content contained in the receiver. This includes the textual
|
| + * representation of any child tag nodes ([getTagNodes]). Whitespace between '<',
|
| + * '</', and '>', '/>' is discarded, but all other whitespace is preserved.
|
| *
|
| - * @return the offset of the value, or `-1` if the value is not specified
|
| + * @return the content (not `null`)
|
| */
|
| - int get textOffset {
|
| - if (_value == null) {
|
| - return -1;
|
| + String get content {
|
| + Token token = attributeEnd.next;
|
| + if (identical(token, contentEnd)) {
|
| + return "";
|
| }
|
| - String text = _value.lexeme;
|
| - if (StringUtilities.startsWithChar(text, 0x22) || StringUtilities.startsWithChar(text, 0x27)) {
|
| - return _value.offset + 1;
|
| + //TODO (danrubel): handle CDATA and replace HTML character encodings with the actual characters
|
| + String content = token.lexeme;
|
| + token = token.next;
|
| + if (identical(token, contentEnd)) {
|
| + return content;
|
| }
|
| - return _value.offset;
|
| + JavaStringBuilder buffer = new JavaStringBuilder();
|
| + while (!identical(token, contentEnd)) {
|
| + buffer.append(token.lexeme);
|
| + token = token.next;
|
| + }
|
| + return buffer.toString();
|
| + }
|
| +
|
| + @override
|
| + Token get endToken {
|
| + if (nodeEnd != null) {
|
| + return nodeEnd;
|
| + }
|
| + if (closingTag != null) {
|
| + return closingTag;
|
| + }
|
| + if (contentEnd != null) {
|
| + return contentEnd;
|
| + }
|
| + if (!_tagNodes.isEmpty) {
|
| + return _tagNodes[_tagNodes.length - 1].endToken;
|
| + }
|
| + if (attributeEnd != null) {
|
| + return attributeEnd;
|
| + }
|
| + if (!_attributes.isEmpty) {
|
| + return _attributes[_attributes.length - 1].endToken;
|
| + }
|
| + return _tag;
|
| }
|
|
|
| /**
|
| - * Answer the attribute value token. A properly formed value will start and end with matching
|
| - * quote characters, but the value returned may not be properly formed.
|
| + * Answer the tag name after the starting '<'.
|
| *
|
| - * @return the value token or `null` if this represents a badly formed attribute
|
| + * @return the tag name (not `null`)
|
| */
|
| - Token get valueToken => _value;
|
| + String get tag => _tag.lexeme;
|
| +
|
| + /**
|
| + * Answer the tag nodes contained in the receiver. Callers should not manipulate the returned list
|
| + * to edit the AST structure.
|
| + *
|
| + * @return the children (not `null`, contains no `null`s)
|
| + */
|
| + List<XmlTagNode> get tagNodes => _tagNodes;
|
| +
|
| + /**
|
| + * Answer the [TokenType#TAG] token after the starting '<'.
|
| + *
|
| + * @return the token (not `null`)
|
| + */
|
| + Token get tagToken => _tag;
|
|
|
| @override
|
| void visitChildren(XmlVisitor visitor) {
|
| + for (XmlAttributeNode node in _attributes) {
|
| + node.accept(visitor);
|
| + }
|
| + for (XmlTagNode node in _tagNodes) {
|
| + node.accept(visitor);
|
| + }
|
| }
|
| +}
|
| +
|
| +/**
|
| + * The interface `XmlVisitor` defines the behavior of objects that can be used to visit an
|
| + * [XmlNode] structure.
|
| + */
|
| +abstract class XmlVisitor<R> {
|
| + R visitHtmlScriptTagNode(HtmlScriptTagNode node);
|
| +
|
| + R visitHtmlUnit(HtmlUnit htmlUnit);
|
| +
|
| + R visitXmlAttributeNode(XmlAttributeNode xmlAttributeNode);
|
| +
|
| + R visitXmlTagNode(XmlTagNode xmlTagNode);
|
| }
|
|
|