OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package com.dom_distiller.client; | 5 package com.dom_distiller.client; |
6 | 6 |
7 import java.util.regex.Pattern; | 7 import java.util.regex.Pattern; |
8 import java.util.regex.Matcher; | 8 import java.util.regex.Matcher; |
9 | 9 |
10 import com.google.gwt.junit.client.GWTTestCase; | 10 import com.google.gwt.junit.client.GWTTestCase; |
11 | 11 |
12 import com.google.gwt.core.client.JsArray; | 12 import com.google.gwt.core.client.JsArray; |
13 import com.google.gwt.dom.client.Document; | 13 import com.google.gwt.dom.client.Document; |
14 import com.google.gwt.dom.client.Element; | 14 import com.google.gwt.dom.client.Element; |
15 import com.google.gwt.dom.client.Node; | 15 import com.google.gwt.dom.client.Node; |
16 import com.google.gwt.dom.client.NodeList; | 16 import com.google.gwt.dom.client.NodeList; |
17 import com.google.gwt.dom.client.Text; | 17 import com.google.gwt.dom.client.Text; |
18 import com.google.gwt.user.client.DOM; | 18 import com.google.gwt.user.client.DOM; |
19 | 19 |
20 import org.xml.sax.Attributes; | 20 import org.xml.sax.Attributes; |
21 | 21 |
22 public class DomToSaxParserTest extends GWTTestCase { | 22 public class DomToSaxVisitorTest extends GWTTestCase { |
23 @Override | 23 @Override |
24 public String getModuleName() { | 24 public String getModuleName() { |
25 return "com.dom_distiller.DomDistillerJUnit"; | 25 return "com.dom_distiller.DomDistillerJUnit"; |
26 } | 26 } |
27 | 27 |
28 public void testGetAttributes() { | 28 public void testGetAttributes() { |
29 Element e = Document.get().createDivElement(); | 29 Element e = Document.get().createDivElement(); |
30 e.setInnerHTML("<div style=\"width:50px; height:100px\" id=\"f\" class=\
"sdf\"></div>"); | 30 e.setInnerHTML("<div style=\"width:50px; height:100px\" id=\"f\" class=\
"sdf\"></div>"); |
31 e = Element.as(e.getChildNodes().getItem(0)); | 31 e = Element.as(e.getChildNodes().getItem(0)); |
32 JsArray<Node> jsAttrs = DomUtil.getAttributes(e); | 32 JsArray<Node> jsAttrs = DomUtil.getAttributes(e); |
33 assertEquals(3, jsAttrs.length()); | 33 assertEquals(3, jsAttrs.length()); |
34 } | 34 } |
35 | 35 |
36 public void testGetSaxAttributes() { | 36 public void testGetSaxAttributes() { |
37 Element e = Document.get().createDivElement(); | 37 Element e = Document.get().createDivElement(); |
38 e.setInnerHTML("<div style=\"width:50px; height:100px\" id=\"f\" class=\
"sdf\"></div>"); | 38 e.setInnerHTML("<div style=\"width:50px; height:100px\" id=\"f\" class=\
"sdf\"></div>"); |
39 e = Element.as(e.getChildNodes().getItem(0)); | 39 e = Element.as(e.getChildNodes().getItem(0)); |
40 Attributes attrs = DomToSaxParser.getSaxAttributes(e); | 40 Attributes attrs = DomToSaxVisitor.getSaxAttributes(e); |
41 assertEquals(3, attrs.getLength()); | 41 assertEquals(3, attrs.getLength()); |
42 assertEquals("f", attrs.getValue("id")); | 42 assertEquals("f", attrs.getValue("id")); |
43 assertEquals("sdf", attrs.getValue("class")); | 43 assertEquals("sdf", attrs.getValue("class")); |
44 } | 44 } |
45 | 45 |
46 private void runDomParserTest(String innerHtml) throws Throwable { | 46 private void runDomVisitorTest(String innerHtml) throws Throwable { |
47 Element container = Document.get().createDivElement(); | 47 Element container = Document.get().createDivElement(); |
48 container.setInnerHTML(innerHtml); | 48 container.setInnerHTML(innerHtml); |
49 SimpleContentHandler contentHandler = new SimpleContentHandler(); | 49 SimpleContentHandler contentHandler = new SimpleContentHandler(); |
50 DomToSaxParser.parse(container, contentHandler); | 50 DomToSaxVisitor visitor = new DomToSaxVisitor(contentHandler); |
| 51 new DomWalker(visitor).walk(container); |
51 String expectedDocument = "<div>" + innerHtml + "</div>"; | 52 String expectedDocument = "<div>" + innerHtml + "</div>"; |
52 assertEquals(expectedDocument, contentHandler.getDocumentString().toLowe
rCase()); | 53 assertEquals(expectedDocument, contentHandler.getDocumentString().toLowe
rCase()); |
53 } | 54 } |
54 | 55 |
55 public void testDomParserText() throws Throwable { | 56 public void testDomVisitorText() throws Throwable { |
56 runDomParserTest("foo"); | 57 runDomVisitorTest("foo"); |
57 } | 58 } |
58 | 59 |
59 public void testDomParserElement() throws Throwable { | 60 public void testDomVisitorElement() throws Throwable { |
60 runDomParserTest("<div></div>"); | 61 runDomVisitorTest("<div></div>"); |
61 } | 62 } |
62 | 63 |
63 public void testDomParserSiblings() throws Throwable { | 64 public void testDomVisitorSiblings() throws Throwable { |
64 runDomParserTest("<div></div><div></div>"); | 65 runDomVisitorTest("<div></div><div></div>"); |
65 } | 66 } |
66 | 67 |
67 public void testDomParserAttribute() throws Throwable { | 68 public void testDomVisitorAttribute() throws Throwable { |
68 runDomParserTest("<div style=\"width:50px; height:100px\" id=\"f\" class
=\"sdf\"></div>"); | 69 runDomVisitorTest("<div style=\"width:50px; height:100px\" id=\"f\" clas
s=\"sdf\"></div>"); |
69 } | 70 } |
70 | 71 |
71 public void testDomParserSimplePage() throws Throwable { | 72 public void testDomVisitorSimplePage() throws Throwable { |
72 String simpleHtml = | 73 String simpleHtml = |
73 "<div id=\"a\" class=\"foo\">foo<a href=\"x.com/#1\">x.com</a>ba
r</div>" + | 74 "<div id=\"a\" class=\"foo\">foo<a href=\"x.com/#1\">x.com</a>ba
r</div>" + |
74 "<div>baz</div>"; | 75 "<div>baz</div>"; |
75 runDomParserTest(simpleHtml); | 76 runDomVisitorTest(simpleHtml); |
76 } | 77 } |
77 } | 78 } |
OLD | NEW |