@Override public Void visitXmlTagNode(XmlTagNode node) { if (isScriptNode(node)) { Source htmlSource = htmlElement.getSource(); String scriptSourcePath = getScriptSourcePath(node); if (node.getAttributeEnd().getType() == TokenType.GT && scriptSourcePath == null) { EmbeddedHtmlScriptElementImpl script = new EmbeddedHtmlScriptElementImpl(node); String contents = node.getContent(); // TODO (danrubel): Move scanning embedded scripts into AnalysisContextImpl // so that clients such as builder can scan, parse, and get errors without resolving AnalysisErrorListener errorListener = new AnalysisErrorListener() { @Override public void onError(AnalysisError error) { // TODO (danrubel): make errors accessible once this is moved into // AnalysisContextImpl } }; StringScanner scanner = new StringScanner(null, contents, errorListener); com.google.dart.engine.scanner.Token firstToken = scanner.tokenize(); int[] lineStarts = scanner.getLineStarts(); // TODO (danrubel): Move parsing embedded scripts into AnalysisContextImpl // so that clients such as builder can scan, parse, and get errors without resolving Parser parser = new Parser(null, errorListener); CompilationUnit unit = parser.parseCompilationUnit(firstToken); try { CompilationUnitBuilder builder = new CompilationUnitBuilder(); CompilationUnitElementImpl elem = builder.buildCompilationUnit(htmlSource, unit); LibraryElementImpl library = new LibraryElementImpl(context, null); library.setDefiningCompilationUnit(elem); script.setScriptLibrary(library); } catch (AnalysisException e) { // TODO (danrubel): Handle or forward the exception e.printStackTrace(); } scripts.add(script); } else { ExternalHtmlScriptElementImpl script = new ExternalHtmlScriptElementImpl(node); if (scriptSourcePath != null) { script.setScriptSource( context.getSourceFactory().resolveUri(htmlSource, scriptSourcePath)); } scripts.add(script); } } else { node.visitChildren(this); } return null; }
/** * Determine if the specified node is a Dart script. * * @param node the node to be tested (not {@code null}) * @return {@code true} if the node is a Dart script */ private boolean isScriptNode(XmlTagNode node) { if (node.getTagNodes().size() != 0 || !node.getTag().getLexeme().equals(SCRIPT)) { return false; } for (XmlAttributeNode attribute : node.getAttributes()) { if (attribute.getName().getLexeme().equals(TYPE)) { Token valueToken = attribute.getValue(); if (valueToken != null) { String value = valueToken.getLexeme(); if (value.equals(APPLICATION_DART_IN_DOUBLE_QUOTES) || value.equals(APPLICATION_DART_IN_SINGLE_QUOTES)) { return true; } } } } return false; }
/** * Return the value of the source attribute if it exists. * * @param node the node containing attributes * @return the source path or {@code null} if not defined */ private String getScriptSourcePath(XmlTagNode node) { for (XmlAttributeNode attribute : node.getAttributes()) { if (attribute.getName().getLexeme().equals(SRC)) { String text = attribute.getText(); return text != null && text.length() > 0 ? text : null; } } return null; }