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 org.chromium.distiller; | 5 package org.chromium.distiller; |
6 | 6 |
7 import org.chromium.distiller.webdocument.WebTable; | 7 import org.chromium.distiller.webdocument.WebTable; |
8 | 8 |
9 import com.google.gwt.core.client.JsArray; | 9 import com.google.gwt.core.client.JsArray; |
10 import com.google.gwt.dom.client.Document; | 10 import com.google.gwt.dom.client.Document; |
11 import com.google.gwt.dom.client.Element; | 11 import com.google.gwt.dom.client.Element; |
12 import com.google.gwt.dom.client.Node; | 12 import com.google.gwt.dom.client.Node; |
13 import com.google.gwt.dom.client.NodeList; | |
13 | 14 |
14 import java.util.Map; | 15 import java.util.Map; |
15 import java.util.List; | 16 import java.util.List; |
16 | 17 |
17 public class DomUtilTest extends DomDistillerJsTestCase { | 18 public class DomUtilTest extends DomDistillerJsTestCase { |
19 private static final String CONTENT_TEXT = "Lorem Ipsum Lorem Ipsum Lorem Ip sum."; | |
20 | |
18 public void testGetAttributes() { | 21 public void testGetAttributes() { |
19 Element e = Document.get().createDivElement(); | 22 Element e = Document.get().createDivElement(); |
20 e.setInnerHTML("<div style=\"width:50px; height:100px\" id=\"f\" class=\ "sdf\"></div>"); | 23 e.setInnerHTML("<div style=\"width:50px; height:100px\" id=\"f\" class=\ "sdf\"></div>"); |
21 e = Element.as(e.getChildNodes().getItem(0)); | 24 e = Element.as(e.getChildNodes().getItem(0)); |
22 JsArray<Node> jsAttrs = DomUtil.getAttributes(e); | 25 JsArray<Node> jsAttrs = DomUtil.getAttributes(e); |
23 assertEquals(3, jsAttrs.length()); | 26 assertEquals(3, jsAttrs.length()); |
24 assertEquals("style", jsAttrs.get(0).getNodeName()); | 27 assertEquals("style", jsAttrs.get(0).getNodeName()); |
25 assertEquals("width:50px; height:100px", jsAttrs.get(0).getNodeValue()); | 28 assertEquals("width:50px; height:100px", jsAttrs.get(0).getNodeValue()); |
26 assertEquals("id", jsAttrs.get(1).getNodeName()); | 29 assertEquals("id", jsAttrs.get(1).getNodeName()); |
27 assertEquals("f", jsAttrs.get(1).getNodeValue()); | 30 assertEquals("f", jsAttrs.get(1).getNodeValue()); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 div.appendChild(div2); | 105 div.appendChild(div2); |
103 | 106 |
104 Element currDiv = TestUtil.createDiv(3); | 107 Element currDiv = TestUtil.createDiv(3); |
105 div2.appendChild(currDiv); | 108 div2.appendChild(currDiv); |
106 Element finalDiv1 = currDiv; | 109 Element finalDiv1 = currDiv; |
107 | 110 |
108 currDiv = TestUtil.createDiv(4); | 111 currDiv = TestUtil.createDiv(4); |
109 div2.appendChild(currDiv); | 112 div2.appendChild(currDiv); |
110 currDiv.appendChild(TestUtil.createDiv(5)); | 113 currDiv.appendChild(TestUtil.createDiv(5)); |
111 | 114 |
112 assertEquals(div2, DomUtil.getNearestCommonAncestor(finalDiv1, currDiv.g etChild(0))); | 115 assertEquals(div2, DomUtil.getNearestCommonAncestor(finalDiv1, |
116 currDiv.getChild(0))); | |
wychen
2016/06/03 16:49:53
nit: wrapping.
marcelorcorrea
2016/06/06 13:17:57
Done.
| |
117 | |
118 NodeList<Element> nodeList = DomUtil.querySelectorAll(mRoot, | |
119 "[id=\"3\"],[id=\"5\"]"); | |
120 | |
113 assertEquals(div2, DomUtil.getNearestCommonAncestor( | 121 assertEquals(div2, DomUtil.getNearestCommonAncestor( |
114 DomUtil.querySelectorAll(mRoot, "[id=\"3\"],[id=\"5\"]"))); | 122 TestUtil.nodeListToList(nodeList))); |
123 | |
115 } | 124 } |
116 | 125 |
117 /** | 126 /** |
118 * The tree graph is: | 127 * The tree graph is: |
119 * 1 - 2 - 3 | 128 * 1 - 2 - 3 |
120 */ | 129 */ |
121 public void testNearestCommonAncestorIsRoot() { | 130 public void testNearestCommonAncestorIsRoot() { |
122 Element div = TestUtil.createDiv(1); | 131 Element div = TestUtil.createDiv(1); |
123 mBody.appendChild(div); | 132 mBody.appendChild(div); |
124 | 133 |
125 Element div2 = TestUtil.createDiv(2); | 134 Element div2 = TestUtil.createDiv(2); |
126 div.appendChild(div2); | 135 div.appendChild(div2); |
127 | 136 |
128 Element div3 = TestUtil.createDiv(3); | 137 Element div3 = TestUtil.createDiv(3); |
129 div2.appendChild(div3); | 138 div2.appendChild(div3); |
130 | 139 |
131 assertEquals(div, DomUtil.getNearestCommonAncestor(div, div3)); | 140 assertEquals(div, DomUtil.getNearestCommonAncestor(div, div3)); |
141 | |
142 NodeList<Element> nodeList = DomUtil.querySelectorAll(mRoot, | |
143 "[id=\"1\"],[id=\"3\"]"); | |
144 | |
132 assertEquals(div, DomUtil.getNearestCommonAncestor( | 145 assertEquals(div, DomUtil.getNearestCommonAncestor( |
133 DomUtil.querySelectorAll(mRoot, "[id=\"1\"],[id=\"3\"]"))); | 146 TestUtil.nodeListToList(nodeList))); |
134 } | 147 } |
135 | 148 |
136 public void testNodeDepth() { | 149 public void testNodeDepth() { |
137 Element div = TestUtil.createDiv(1); | 150 Element div = TestUtil.createDiv(1); |
138 | 151 |
139 Element div2 = TestUtil.createDiv(2); | 152 Element div2 = TestUtil.createDiv(2); |
140 div.appendChild(div2); | 153 div.appendChild(div2); |
141 | 154 |
142 Element div3 = TestUtil.createDiv(3); | 155 Element div3 = TestUtil.createDiv(3); |
143 div2.appendChild(div3); | 156 div2.appendChild(div3); |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
371 for (int i = 0; i < mBody.getChildCount(); i++) { | 384 for (int i = 0; i < mBody.getChildCount(); i++) { |
372 DomUtil.stripImageElements(mBody.getChild(i)); | 385 DomUtil.stripImageElements(mBody.getChild(i)); |
373 } | 386 } |
374 assertEquals(expected, mBody.getInnerHTML()); | 387 assertEquals(expected, mBody.getInnerHTML()); |
375 | 388 |
376 mBody.setInnerHTML(html); | 389 mBody.setInnerHTML(html); |
377 DomUtil.stripImageElements(mBody); | 390 DomUtil.stripImageElements(mBody); |
378 assertEquals(expected, mBody.getInnerHTML()); | 391 assertEquals(expected, mBody.getInnerHTML()); |
379 } | 392 } |
380 | 393 |
394 public void testIsVisibleByOffsetParentDisplayNone() { | |
395 String html = | |
396 "<div style=\"display: none;\">" + | |
397 "<div>Some Text</div>" + | |
wychen
2016/06/03 16:49:53
"Some Text" can also be gone after using the new i
| |
398 "</div>"; | |
399 mBody.setInnerHTML(html); | |
400 Element child = mBody.getFirstChildElement().getFirstChildElement(); | |
401 assertFalse(DomUtil.isVisibleByOffset(child)); | |
402 } | |
403 | |
404 public void testIsVisibleByOffsetChildDisplayNone() { | |
405 String html = | |
406 "<div>" + | |
407 "<div style=\"display: none;\">Some Text</div>" + | |
408 "</div>"; | |
409 mBody.setInnerHTML(html); | |
410 Element child = mBody.getFirstChildElement().getFirstChildElement(); | |
411 assertFalse(DomUtil.isVisibleByOffset(child)); | |
412 } | |
413 | |
414 public void testIsVisibleByOffsetDisplayBlock() { | |
415 String html = | |
416 "<div>" + | |
417 "<div>Some Text</div>" + | |
418 "</div>"; | |
419 mBody.setInnerHTML(html); | |
420 Element child = mBody.getFirstChildElement().getFirstChildElement(); | |
421 assertTrue(DomUtil.isVisibleByOffset(child)); | |
422 } | |
423 | |
424 public void testOnlyProcessArticleElement() { | |
425 final String htmlArticle = | |
426 "<h1>" + CONTENT_TEXT + "</h1>" + | |
427 "<article>" + CONTENT_TEXT + "</article>"; | |
428 | |
429 String expected = "<article>" + CONTENT_TEXT + "</article>"; | |
430 | |
431 Element result = getArticleElement(htmlArticle); | |
432 assertEquals(expected, result.getString()); | |
433 } | |
434 | |
435 public void testOnlyProcessArticleElementWithHiddenArticleElement() { | |
436 final String paragraph = "<p>" + CONTENT_TEXT + "</p>" + | |
437 "<p>" + CONTENT_TEXT + "</p>"; | |
438 | |
439 final String htmlArticle = | |
440 "<h1>" + CONTENT_TEXT + "</h1>" + | |
441 "<article>" + paragraph + "</article>" + | |
442 "<article style=\"display:none\">" + paragraph +"</article>"; | |
443 | |
444 String expected = "<article>" + paragraph + "</article>"; | |
445 | |
446 Element result = getArticleElement(htmlArticle); | |
447 assertEquals(expected, result.getString()); | |
448 } | |
449 | |
450 public void testOnlyProcessArticleElementMultiple() { | |
451 final String htmlArticle = | |
452 "<h1>" + CONTENT_TEXT + "</h1>" + | |
453 "<article>" + CONTENT_TEXT + "</article>" + | |
454 "<article>" + CONTENT_TEXT + "</article>"; | |
455 | |
456 // The existence of multiple articles disables the fast path. | |
457 assertNull(getArticleElement(htmlArticle)); | |
458 } | |
459 | |
460 public void testOnlyProcessSchemaOrgArticle() { | |
461 final String paragraph = "<p>" + CONTENT_TEXT + "</p><p>" + | |
462 CONTENT_TEXT + "</p>"; | |
463 | |
464 final String htmlArticle = | |
465 "<h1>" + CONTENT_TEXT + "</h1>" + | |
466 "<div itemscope itemtype=\"http://schema.org/Article\">" + | |
467 paragraph + | |
468 "</div>"; | |
469 | |
470 final String expected = | |
471 "<div itemscope=\"\" " + | |
472 "itemtype=\"http://schema.org/Article\">" + paragraph + | |
473 "</div>"; | |
474 | |
475 Element result = getArticleElement(htmlArticle); | |
476 assertEquals(expected, result.getString()); | |
477 } | |
478 | |
479 public void testOnlyProcessSchemaOrgArticleWithHiddenArticleElement() { | |
480 final String paragraph = "<p>" + CONTENT_TEXT + "</p>" + | |
481 "<p>" + CONTENT_TEXT + "</p>"; | |
482 | |
483 final String htmlArticle = | |
484 "<h1>" + CONTENT_TEXT + "</h1>" + | |
485 "<div itemscope itemtype=\"http://schema.org/Article\">" + | |
486 paragraph + "</div>" + | |
487 "<div itemscope itemtype=\"http://schema.org/Article\" " + | |
488 "style=\"display:none\">" + paragraph + | |
489 "</div>"; | |
490 | |
491 String expected = | |
492 "<div itemscope=\"\" itemtype=\"http://schema.org/Article\">" + | |
493 paragraph + | |
494 "</div>"; | |
495 | |
496 Element result = getArticleElement(htmlArticle); | |
497 assertEquals(expected, result.getString()); | |
498 } | |
499 | |
500 public void testOnlyProcessSchemaOrgArticleNews() { | |
501 final String paragraph = "<p>" + CONTENT_TEXT + "</p>" + | |
502 "<p>" + CONTENT_TEXT + "</p>"; | |
503 | |
504 final String htmlArticle = | |
505 "<h1>" + CONTENT_TEXT + "</h1>" + | |
506 "<div itemscope itemtype=\"http://schema.org/NewsArticle\">" + | |
507 paragraph + | |
508 "</div>"; | |
509 | |
510 final String expected = | |
511 "<div itemscope=\"\" " + | |
512 "itemtype=\"http://schema.org/NewsArticle\">" + paragraph + | |
513 "</div>"; | |
514 | |
515 Element result = getArticleElement(htmlArticle); | |
516 assertEquals(expected, result.getString()); | |
517 } | |
518 | |
519 public void testOnlyProcessSchemaOrgArticleBlog() { | |
520 final String paragraph = "<p>" + CONTENT_TEXT + "</p>" + | |
521 "<p>" + CONTENT_TEXT + "</p>"; | |
522 | |
523 final String htmlArticle = | |
524 "<h1>" + CONTENT_TEXT + "</h1>" + | |
525 "<div itemscope itemtype=\"http://schema.org/BlogPosting\">" + | |
526 paragraph + | |
527 "</div>"; | |
528 | |
529 final String expected = | |
530 "<div itemscope=\"\" " + | |
531 "itemtype=\"http://schema.org/BlogPosting\">" + | |
532 paragraph + | |
533 "</div>"; | |
534 | |
535 Element result = getArticleElement(htmlArticle); | |
536 assertEquals(expected, result.getString()); | |
537 } | |
538 | |
539 public void testOnlyProcessSchemaOrgArticleNested() { | |
540 final String paragraph = "<p>" + CONTENT_TEXT + "</p>"; | |
541 | |
542 final String htmlArticle = | |
543 "<h1>" + CONTENT_TEXT + "</h1>" + | |
544 "<div itemscope itemtype=\"http://schema.org/Article\">" + | |
545 paragraph + | |
546 "<div itemscope itemtype=\"http://schema.org/Article\">" + | |
547 paragraph + | |
548 "</div>" + | |
549 "</div>"; | |
550 | |
551 final String expected = | |
552 "<div itemscope=\"\" itemtype=\"http://schema.org/Article\">" + | |
553 paragraph + | |
554 "<div itemscope=\"\" itemtype=\"http://schema.org/Article\">" + | |
555 paragraph + | |
556 "</div>" + | |
557 "</div>"; | |
558 | |
559 Element result = getArticleElement(htmlArticle); | |
560 assertEquals(expected, result.getString()); | |
561 } | |
562 | |
563 public void testOnlyProcessSchemaOrgArticleNestedWithNestedHiddenArticleElem ent() { | |
564 final String paragraph = "<p>" + CONTENT_TEXT + "</p>"; | |
565 | |
566 final String htmlArticle = | |
567 "<h1>" + CONTENT_TEXT + "</h1>" + | |
568 "<div itemscope itemtype=\"http://schema.org/Article\">" + | |
569 paragraph + | |
570 "<div itemscope itemtype=\"http://schema.org/Article\">" + | |
571 paragraph + | |
572 "</div>" + | |
573 "<div itemscope itemtype=\"http://schema.org/Article\" " + | |
574 "style=\"display:none\">" + paragraph + | |
575 "</div>" + | |
576 "</div>"; | |
577 | |
578 final String expected = | |
579 "<div itemscope=\"\" itemtype=\"http://schema.org/Article\">" + | |
580 paragraph + | |
581 "<div itemscope=\"\" itemtype=\"http://schema.org/Article\">" + | |
582 paragraph + "</div>" + | |
583 "<div itemscope=\"\" itemtype=\"http://schema.org/Article\" " + | |
584 "style=\"display:none\">" + paragraph + "</div>" + | |
585 "</div>"; | |
586 | |
587 Element result = getArticleElement(htmlArticle); | |
588 assertEquals(expected, result.getString()); | |
589 } | |
590 | |
591 public void testOnlyProcessSchemaOrgArticleNestedWithHiddenArticleElement() { | |
592 final String paragraph = "<p>" + CONTENT_TEXT + "</p>"; | |
593 | |
594 final String htmlArticle = | |
595 "<h1>" + CONTENT_TEXT + "</h1>" + | |
596 "<div itemscope itemtype=\"http://schema.org/Article\">" + | |
597 paragraph + | |
598 "<div itemscope itemtype=\"http://schema.org/Article\">" + | |
599 paragraph + | |
600 "</div>" + | |
601 "</div>" + | |
602 "<div itemscope itemtype=\"http://schema.org/Article\" " + | |
603 "style=\"display:none\">" + paragraph + "</div>"; | |
604 | |
605 final String expected = | |
606 "<div itemscope=\"\" itemtype=\"http://schema.org/Article\">" + | |
607 paragraph + | |
608 "<div itemscope=\"\" itemtype=\"http://schema.org/Article\">" + | |
609 paragraph + | |
610 "</div>" + | |
611 "</div>"; | |
612 | |
613 Element result = getArticleElement(htmlArticle); | |
614 assertEquals(expected, result.getString()); | |
615 } | |
616 | |
617 public void testOnlyProcessSchemaOrgNonArticleMovie() { | |
618 final String paragraph = "<p>" + CONTENT_TEXT + "</p>" + | |
619 "<p>" + CONTENT_TEXT + "</p>"; | |
620 | |
621 final String htmlArticle = | |
622 "<h1>" + CONTENT_TEXT + "</h1>" + | |
623 "<div itemscope itemtype=\"http://schema.org/Movie\">" + | |
624 paragraph + | |
625 "</div>"; | |
626 | |
627 // Non-article schema.org types should not use the fast path. | |
628 Element result = getArticleElement(htmlArticle); | |
629 assertNull(result); | |
630 } | |
631 | |
632 private Element getArticleElement(String html) { | |
633 mBody.setInnerHTML(html); | |
634 return DomUtil.getArticleElement(mRoot); | |
635 } | |
636 | |
381 public void testGetArea() { | 637 public void testGetArea() { |
382 String elements = | 638 String elements = |
383 "<div style=\"width: 200px; height: 100px\">w</div>" + | 639 "<div style=\"width: 200px; height: 100px\">w</div>" + |
384 "<div style=\"width: 300px;\">" + | 640 "<div style=\"width: 300px;\">" + |
385 "<div style=\"width: 300px; height: 200px\"></div>" + | 641 "<div style=\"width: 300px; height: 200px\"></div>" + |
386 "</div>" + | 642 "</div>" + |
387 "<div style=\"width: 400px; height: 100px\">" + | 643 "<div style=\"width: 400px; height: 100px\">" + |
388 "<div style=\"height: 100%\"></div>" + | 644 "<div style=\"height: 100%\"></div>" + |
389 "</div>"; | 645 "</div>"; |
390 mBody.setInnerHTML(elements); | 646 mBody.setInnerHTML(elements); |
391 | 647 |
392 Element element = mBody.getFirstChildElement(); | 648 Element element = mBody.getFirstChildElement(); |
393 assertEquals(200*100, DomUtil.getArea(element)); | 649 assertEquals(200*100, DomUtil.getArea(element)); |
394 | 650 |
395 element = element.getNextSiblingElement(); | 651 element = element.getNextSiblingElement(); |
396 assertEquals(300*200, DomUtil.getArea(element)); | 652 assertEquals(300*200, DomUtil.getArea(element)); |
397 | 653 |
398 element = element.getNextSiblingElement(); | 654 element = element.getNextSiblingElement(); |
399 assertEquals(400*100, DomUtil.getArea(element)); | 655 assertEquals(400*100, DomUtil.getArea(element)); |
400 | 656 |
401 element = element.getFirstChildElement(); | 657 element = element.getFirstChildElement(); |
402 assertEquals(400*100, DomUtil.getArea(element)); | 658 assertEquals(400*100, DomUtil.getArea(element)); |
403 } | 659 } |
404 } | 660 } |
OLD | NEW |