OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2013, the Dart project authors. | 2 * Copyright (c) 2013, the Dart project authors. |
3 * | 3 * |
4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u se this file except | 4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u se this file except |
5 * in compliance with the License. You may obtain a copy of the License at | 5 * in compliance with the License. You may obtain a copy of the License at |
6 * | 6 * |
7 * http://www.eclipse.org/legal/epl-v10.html | 7 * http://www.eclipse.org/legal/epl-v10.html |
8 * | 8 * |
9 * Unless required by applicable law or agreed to in writing, software distribut ed under the License | 9 * Unless required by applicable law or agreed to in writing, software distribut ed under the License |
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K IND, either express | 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K IND, either express |
11 * or implied. See the License for the specific language governing permissions a nd limitations under | 11 * or implied. See the License for the specific language governing permissions a nd limitations under |
12 * the License. | 12 * the License. |
13 */ | 13 */ |
14 package com.google.dart.engine.html.parser; | 14 package com.google.dart.engine.html.parser; |
15 | 15 |
16 import com.google.dart.engine.EngineTestCase; | 16 import com.google.dart.engine.EngineTestCase; |
17 import com.google.dart.engine.ast.Expression; | |
18 import com.google.dart.engine.ast.SimpleIdentifier; | |
19 import com.google.dart.engine.error.GatheringErrorListener; | |
20 import com.google.dart.engine.html.ast.EmbeddedExpression; | |
17 import com.google.dart.engine.html.ast.HtmlUnit; | 21 import com.google.dart.engine.html.ast.HtmlUnit; |
22 import com.google.dart.engine.html.ast.XmlAttributeNode; | |
18 import com.google.dart.engine.html.ast.XmlTagNode; | 23 import com.google.dart.engine.html.ast.XmlTagNode; |
19 import com.google.dart.engine.html.parser.XmlValidator.Attributes; | 24 import com.google.dart.engine.html.parser.XmlValidator.Attributes; |
20 import com.google.dart.engine.html.parser.XmlValidator.Tag; | 25 import com.google.dart.engine.html.parser.XmlValidator.Tag; |
21 import com.google.dart.engine.html.scanner.HtmlScanResult; | 26 import com.google.dart.engine.html.scanner.HtmlScanResult; |
22 import com.google.dart.engine.html.scanner.HtmlScanner; | 27 import com.google.dart.engine.html.scanner.HtmlScanner; |
23 import com.google.dart.engine.source.SourceFactory; | 28 import com.google.dart.engine.source.SourceFactory; |
24 import com.google.dart.engine.source.TestSource; | 29 import com.google.dart.engine.source.TestSource; |
25 | 30 |
26 import static com.google.dart.engine.utilities.io.FileUtilities2.createFile; | 31 import static com.google.dart.engine.utilities.io.FileUtilities2.createFile; |
27 | 32 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 | 83 |
79 public void test_parse_attribute_single_quote() throws Exception { | 84 public void test_parse_attribute_single_quote() throws Exception { |
80 HtmlUnit htmlUnit = parse(// | 85 HtmlUnit htmlUnit = parse(// |
81 "<html><body foo='sdfsdf'></body></html>").getHtmlUnit(); | 86 "<html><body foo='sdfsdf'></body></html>").getHtmlUnit(); |
82 validate(htmlUnit, t("html", t("body", a("foo", "'sdfsdf'"), ""))); | 87 validate(htmlUnit, t("html", t("body", a("foo", "'sdfsdf'"), ""))); |
83 XmlTagNode htmlNode = htmlUnit.getTagNodes().get(0); | 88 XmlTagNode htmlNode = htmlUnit.getTagNodes().get(0); |
84 XmlTagNode bodyNode = htmlNode.getTagNodes().get(0); | 89 XmlTagNode bodyNode = htmlNode.getTagNodes().get(0); |
85 assertEquals("sdfsdf", bodyNode.getAttributes().get(0).getText()); | 90 assertEquals("sdfsdf", bodyNode.getAttributes().get(0).getText()); |
86 } | 91 } |
87 | 92 |
93 public void test_parse_attribute_withEmbeddedExpression() throws Exception { | |
94 HtmlUnit htmlUnit = parse("<html><body foo='{{bar}}'></body></html>").getHtm lUnit(); | |
danrubel
2013/11/25 02:26:12
TODO add test with missing }}
Brian Wilkerson
2013/11/25 19:04:36
TODO added. Not sure what to test, though, since w
| |
95 XmlTagNode htmlNode = htmlUnit.getTagNodes().get(0); | |
96 XmlTagNode bodyNode = htmlNode.getTagNodes().get(0); | |
97 XmlAttributeNode attribute = bodyNode.getAttributes().get(0); | |
98 EmbeddedExpression[] expressions = attribute.getExpressions(); | |
99 assertLength(1, expressions); | |
100 Expression expression = expressions[0].getExpression(); | |
101 assertInstanceOf(SimpleIdentifier.class, expression); | |
102 assertEquals("bar", ((SimpleIdentifier) expression).getName()); | |
103 } | |
104 | |
88 public void test_parse_comment_embedded() throws Exception { | 105 public void test_parse_comment_embedded() throws Exception { |
89 HtmlUnit htmlUnit = parse(// | 106 HtmlUnit htmlUnit = parse(// |
90 "<html <!-- comment -->></html>").getHtmlUnit(); | 107 "<html <!-- comment -->></html>").getHtmlUnit(); |
91 validate(htmlUnit, t("html", "")); | 108 validate(htmlUnit, t("html", "")); |
92 } | 109 } |
93 | 110 |
94 public void test_parse_comment_first() throws Exception { | 111 public void test_parse_comment_first() throws Exception { |
95 HtmlUnit htmlUnit = parse(// | 112 HtmlUnit htmlUnit = parse(// |
96 "<!-- comment --><html></html>").getHtmlUnit(); | 113 "<!-- comment --><html></html>").getHtmlUnit(); |
97 validate(htmlUnit, t("html", "")); | 114 validate(htmlUnit, t("html", "")); |
(...skipping 11 matching lines...) Expand all Loading... | |
109 // XmlTagNode.getContent() does not include whitespace between '<' and '>' a t this time | 126 // XmlTagNode.getContent() does not include whitespace between '<' and '>' a t this time |
110 validate(htmlUnit, t("html", "\n<pa=\"b\">blat \n </p>\n", t("p", a("a", "\" b\""), "blat \n "))); | 127 validate(htmlUnit, t("html", "\n<pa=\"b\">blat \n </p>\n", t("p", a("a", "\" b\""), "blat \n "))); |
111 } | 128 } |
112 | 129 |
113 public void test_parse_content_none() throws Exception { | 130 public void test_parse_content_none() throws Exception { |
114 HtmlUnit htmlUnit = parse(// | 131 HtmlUnit htmlUnit = parse(// |
115 "<html><p/>blat<p/></html>").getHtmlUnit(); | 132 "<html><p/>blat<p/></html>").getHtmlUnit(); |
116 validate(htmlUnit, t("html", "<p/>blat<p/>", t("p", ""), t("p", ""))); | 133 validate(htmlUnit, t("html", "<p/>blat<p/>", t("p", ""), t("p", ""))); |
117 } | 134 } |
118 | 135 |
136 public void test_parse_content_withEmbeddedExpression() throws Exception { | |
137 HtmlUnit htmlUnit = parse("<html><body><p>abc {{elipsis}}} xyz</p></body></h tml>").getHtmlUnit(); | |
danrubel
2013/11/25 02:26:12
Three } ... intentional?
TODO test with missing }
Brian Wilkerson
2013/11/25 19:04:36
Nope. Fixed.
| |
138 XmlTagNode htmlNode = htmlUnit.getTagNodes().get(0); | |
139 XmlTagNode bodyNode = htmlNode.getTagNodes().get(0); | |
140 XmlTagNode pNode = bodyNode.getTagNodes().get(0); | |
141 EmbeddedExpression[] expressions = pNode.getExpressions(); | |
142 assertLength(1, expressions); | |
143 Expression expression = expressions[0].getExpression(); | |
144 assertInstanceOf(SimpleIdentifier.class, expression); | |
145 assertEquals("elipsis", ((SimpleIdentifier) expression).getName()); | |
146 } | |
147 | |
119 public void test_parse_declaration() throws Exception { | 148 public void test_parse_declaration() throws Exception { |
120 HtmlUnit htmlUnit = parse(// | 149 HtmlUnit htmlUnit = parse(// |
121 "<!DOCTYPE html>\n\n<html><p></p></html>").getHtmlUnit(); | 150 "<!DOCTYPE html>\n\n<html><p></p></html>").getHtmlUnit(); |
122 validate(htmlUnit, t("html", t("p", ""))); | 151 validate(htmlUnit, t("html", t("p", ""))); |
123 } | 152 } |
124 | 153 |
125 public void test_parse_directive() throws Exception { | 154 public void test_parse_directive() throws Exception { |
126 HtmlUnit htmlUnit = parse(// | 155 HtmlUnit htmlUnit = parse(// |
127 "<?xml ?>\n\n<html><p></p></html>").getHtmlUnit(); | 156 "<?xml ?>\n\n<html><p></p></html>").getHtmlUnit(); |
128 validate(htmlUnit, t("html", t("p", ""))); | 157 validate(htmlUnit, t("html", t("p", ""))); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
188 | 217 |
189 private HtmlParseResult parse(String contents) throws Exception { | 218 private HtmlParseResult parse(String contents) throws Exception { |
190 SourceFactory factory = new SourceFactory(); | 219 SourceFactory factory = new SourceFactory(); |
191 TestSource source = new TestSource( | 220 TestSource source = new TestSource( |
192 factory.getContentCache(), | 221 factory.getContentCache(), |
193 createFile("/test.dart"), | 222 createFile("/test.dart"), |
194 contents); | 223 contents); |
195 HtmlScanner scanner = new HtmlScanner(source); | 224 HtmlScanner scanner = new HtmlScanner(source); |
196 source.getContents(scanner); | 225 source.getContents(scanner); |
197 HtmlScanResult scanResult = scanner.getResult(); | 226 HtmlScanResult scanResult = scanner.getResult(); |
198 HtmlParseResult result = new HtmlParser(source).parse(scanResult); | 227 GatheringErrorListener errorListener = new GatheringErrorListener(); |
228 HtmlParseResult result = new HtmlParser(source, errorListener).parse(scanRes ult); | |
229 errorListener.assertNoErrors(); | |
199 return result; | 230 return result; |
200 } | 231 } |
201 | 232 |
202 private void validate(HtmlUnit htmlUnit, Tag... expectedTags) { | 233 private void validate(HtmlUnit htmlUnit, Tag... expectedTags) { |
203 XmlValidator validator = new XmlValidator(); | 234 XmlValidator validator = new XmlValidator(); |
204 validator.expectTags(expectedTags); | 235 validator.expectTags(expectedTags); |
205 htmlUnit.accept(validator); | 236 htmlUnit.accept(validator); |
206 validator.assertValid(); | 237 validator.assertValid(); |
207 } | 238 } |
208 } | 239 } |
OLD | NEW |