Chromium Code Reviews| 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 com.google.gwt.dom.client.Style; | 7 import com.google.gwt.dom.client.Style; |
| 8 import org.chromium.distiller.webdocument.WebElement; | |
| 8 import org.chromium.distiller.webdocument.WebEmbed; | 9 import org.chromium.distiller.webdocument.WebEmbed; |
| 10 import org.chromium.distiller.webdocument.WebFigure; | |
| 9 import org.chromium.distiller.webdocument.WebImage; | 11 import org.chromium.distiller.webdocument.WebImage; |
| 10 import org.chromium.distiller.extractors.embeds.EmbedExtractor; | 12 import org.chromium.distiller.extractors.embeds.EmbedExtractor; |
| 11 import org.chromium.distiller.extractors.embeds.TwitterExtractor; | 13 import org.chromium.distiller.extractors.embeds.TwitterExtractor; |
| 12 import org.chromium.distiller.extractors.embeds.VimeoExtractor; | 14 import org.chromium.distiller.extractors.embeds.VimeoExtractor; |
| 13 import org.chromium.distiller.extractors.embeds.YouTubeExtractor; | 15 import org.chromium.distiller.extractors.embeds.YouTubeExtractor; |
| 14 import org.chromium.distiller.extractors.embeds.ImageExtractor; | 16 import org.chromium.distiller.extractors.embeds.ImageExtractor; |
| 15 | 17 |
| 16 import com.google.gwt.dom.client.Document; | 18 import com.google.gwt.dom.client.Document; |
| 17 import com.google.gwt.dom.client.Element; | 19 import com.google.gwt.dom.client.Element; |
| 18 import com.google.gwt.dom.client.IFrameElement; | 20 import com.google.gwt.dom.client.IFrameElement; |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 203 result = (WebEmbed) extractor.extract(nontweet); | 205 result = (WebEmbed) extractor.extract(nontweet); |
| 204 assertNull(result); | 206 assertNull(result); |
| 205 } | 207 } |
| 206 | 208 |
| 207 public void testRenderedTwitterExtractor() { | 209 public void testRenderedTwitterExtractor() { |
| 208 IFrameElement twitter = TestUtil.createIframe(); | 210 IFrameElement twitter = TestUtil.createIframe(); |
| 209 // Add iframe to body so its document is generated. | 211 // Add iframe to body so its document is generated. |
| 210 mBody.appendChild(twitter); | 212 mBody.appendChild(twitter); |
| 211 | 213 |
| 212 // This string represents a very simplified version of the twitter ifram e embed structure. | 214 // This string represents a very simplified version of the twitter ifram e embed structure. |
| 213 String iframeStructure = | 215 String iframeStructure = |
| 214 "<div class=\"media-forward root standalone-tweet ltr\"" + | 216 "<div class=\"media-forward root standalone-tweet ltr\"" + |
| 215 "data-iframe-title=\"Embedded Tweet\"" + | 217 "data-iframe-title=\"Embedded Tweet\"" + |
| 216 "data-scribe=\"page:tweet\">" + | 218 "data-scribe=\"page:tweet\">" + |
| 217 "<blockquote data-tweet-id=\"1234567890\"" + | 219 "<blockquote data-tweet-id=\"1234567890\"" + |
| 218 "data-scribe=\"section:subject\">" + | 220 "data-scribe=\"section:subject\">" + |
| 219 "<div class=\"cards-base cards-multimedia customisable-b order\"" + | 221 "<div class=\"cards-base cards-multimedia customisable-b order\"" + |
| 220 "data-scribe=\"component:card\"" + | 222 "data-scribe=\"component:card\"" + |
| 221 "data-video-content-id=\"0987654321\">" + | 223 "data-video-content-id=\"0987654321\">" + |
| 222 "</div>" + | 224 "</div>" + |
| 223 "<div class=\"header\">" + | 225 "<div class=\"header\">" + |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 387 mBody.appendChild(image); | 389 mBody.appendChild(image); |
| 388 | 390 |
| 389 mHead.setInnerHTML("<base href=\"http://example.com/\">"); | 391 mHead.setInnerHTML("<base href=\"http://example.com/\">"); |
| 390 | 392 |
| 391 EmbedExtractor extractor = new ImageExtractor(); | 393 EmbedExtractor extractor = new ImageExtractor(); |
| 392 WebImage result = (WebImage) extractor.extract(image); | 394 WebImage result = (WebImage) extractor.extract(image); |
| 393 assertNotNull(result); | 395 assertNotNull(result); |
| 394 assertEquals("<img src=\"http://example.com/image.png\">", result.genera teOutput(false)); | 396 assertEquals("<img src=\"http://example.com/image.png\">", result.genera teOutput(false)); |
| 395 } | 397 } |
| 396 | 398 |
| 399 private void extractLazilyLoadedFigure(String attr) { | |
| 400 ImageElement image = TestUtil.createImage(); | |
| 401 image.setAttribute(attr, "image.png"); | |
| 402 Element figure = Document.get().createElement("FIGURE"); | |
| 403 figure.appendChild(image); | |
| 404 | |
| 405 mBody.appendChild(figure); | |
| 406 | |
| 407 mHead.setInnerHTML("<base href=\"http://example.com/\">"); | |
| 408 | |
| 409 EmbedExtractor extractor = new ImageExtractor(); | |
| 410 WebFigure result = (WebFigure) extractor.extract(figure); | |
| 411 assertNotNull(result); | |
| 412 assertEquals("<figure><img src=\"http://example.com/image.png\"></figure >", | |
| 413 result.generateOutput(false)); | |
| 414 } | |
| 415 | |
| 397 public void testImageExtractorLazy() { | 416 public void testImageExtractorLazy() { |
| 398 extractLazilyLoadedImage("data-src"); | 417 extractLazilyLoadedImage("data-src"); |
| 399 extractLazilyLoadedImage("datasrc"); | 418 extractLazilyLoadedImage("datasrc"); |
| 400 extractLazilyLoadedImage("data-original"); | 419 extractLazilyLoadedImage("data-original"); |
| 401 extractLazilyLoadedImage("data-url"); | 420 extractLazilyLoadedImage("data-url"); |
| 421 | |
| 422 extractLazilyLoadedFigure("data-src"); | |
| 423 extractLazilyLoadedFigure("datasrc"); | |
| 424 extractLazilyLoadedFigure("data-original"); | |
| 425 extractLazilyLoadedFigure("data-url"); | |
| 426 } | |
| 427 | |
| 428 public void testFigureWithoutCaption() { | |
| 429 ImageElement image = TestUtil.createImage(); | |
| 430 image.setSrc("http://wwww.example.com/image.jpeg"); | |
| 431 image.setAttribute("width", "100"); | |
| 432 image.setAttribute("height", "100"); | |
| 433 Element figure = Document.get().createElement("FIGURE"); | |
| 434 figure.appendChild(image); | |
| 435 mBody.appendChild(figure); | |
| 436 | |
| 437 EmbedExtractor extractor = new ImageExtractor(); | |
| 438 WebImage result = (WebImage) extractor.extract(figure); | |
| 439 String got = result.generateOutput(false); | |
| 440 String expected = | |
| 441 "<figure>" + | |
| 442 "<img src=\"http://wwww.example.com/image.jpeg\"" + | |
| 443 " width=\"100\" height=\"100\">" + | |
| 444 "</figure>"; | |
| 445 assertNotNull(result); | |
| 446 assertEquals(100, result.getHeight()); | |
| 447 assertEquals(100, result.getWidth()); | |
| 448 assertEquals(expected, got); | |
| 449 } | |
| 450 | |
| 451 public void testFigureWithoutImageAndCaption() { | |
| 452 Element figure = Document.get().createElement("FIGURE"); | |
| 453 mBody.appendChild(figure); | |
| 454 | |
| 455 EmbedExtractor extractor = new ImageExtractor(); | |
| 456 WebImage result = (WebImage) extractor.extract(figure); | |
| 457 assertNull(result); | |
| 458 } | |
| 459 | |
| 460 public void testFigureCaptionTextOnly() { | |
| 461 ImageElement image = TestUtil.createImage(); | |
| 462 image.setSrc("http://wwww.example.com/image.jpeg"); | |
| 463 image.setAttribute("width", "100"); | |
| 464 image.setAttribute("height", "100"); | |
| 465 | |
| 466 Element figure = Document.get().createElement("FIGURE"); | |
| 467 figure.appendChild(image); | |
| 468 Element figcaption = Document.get().createElement("FIGCAPTION"); | |
| 469 figcaption.setInnerHTML("This is a caption"); | |
| 470 figure.appendChild(figcaption); | |
| 471 mBody.appendChild(figure); | |
| 472 | |
| 473 EmbedExtractor extractor = new ImageExtractor(); | |
| 474 WebElement result = extractor.extract(figure); | |
| 475 assertEquals("This is a caption", result.generateOutput(true)); | |
| 476 } | |
| 477 | |
| 478 public void testFigureCaptionWithAnchor() { | |
| 479 ImageElement image = TestUtil.createImage(); | |
| 480 image.setSrc("http://wwww.example.com/image.jpeg"); | |
| 481 image.setAttribute("width", "100"); | |
| 482 image.setAttribute("height", "100"); | |
| 483 | |
| 484 Element figure = Document.get().createElement("FIGURE"); | |
| 485 figure.appendChild(image); | |
| 486 Element figcaption = Document.get().createElement("FIGCAPTION"); | |
| 487 Element anchor = Document.get().createAnchorElement(); | |
|
wychen
2016/06/07 17:39:19
Put an href and see if it gets converted.
Also, p
marcelorcorrea
2016/06/08 14:34:15
Done.
| |
| 488 anchor.setInnerHTML("This is a caption"); | |
| 489 figcaption.appendChild(anchor); | |
| 490 figure.appendChild(figcaption); | |
| 491 mBody.appendChild(figure); | |
| 492 String expected = | |
| 493 "<figure>" + | |
| 494 "<img src=\"http://wwww.example.com/image.jpeg\"" + | |
| 495 " width=\"100\" height=\"100\">" + | |
| 496 "<figcaption><a>This is a caption</a></figcaption>" + | |
| 497 "</figure>"; | |
| 498 EmbedExtractor extractor = new ImageExtractor(); | |
| 499 WebElement result = extractor.extract(figure); | |
| 500 assertEquals(expected, result.generateOutput(false)); | |
| 501 } | |
| 502 | |
| 503 public void testFigureWithCaptionWithoutAnchor() { | |
| 504 ImageElement image = TestUtil.createImage(); | |
| 505 image.setSrc("http://wwww.example.com/image.jpeg"); | |
| 506 image.setAttribute("width", "100"); | |
| 507 image.setAttribute("height", "100"); | |
| 508 Element figure = Document.get().createElement("FIGURE"); | |
| 509 figure.appendChild(image); | |
| 510 Element figcaption = Document.get().createElement("FIGCAPTION"); | |
| 511 figcaption.setInnerHTML("This is a caption"); | |
|
wychen
2016/06/07 17:39:19
Put this inside other elements, and make sure it's
marcelorcorrea
2016/06/08 14:34:15
Done.
| |
| 512 figure.appendChild(figcaption); | |
| 513 mBody.appendChild(figure); | |
| 514 | |
| 515 EmbedExtractor extractor = new ImageExtractor(); | |
| 516 WebImage result = (WebImage) extractor.extract(figure); | |
| 517 String got = result.generateOutput(false); | |
| 518 String expected = | |
| 519 "<figure>" + | |
| 520 "<img src=\"http://wwww.example.com/image.jpeg\"" + | |
| 521 " width=\"100\" height=\"100\">" + | |
| 522 "<figcaption>This is a caption</figcaption>" + | |
| 523 "</figure>"; | |
| 524 assertNotNull(result); | |
| 525 assertEquals(100, result.getHeight()); | |
| 526 assertEquals(100, result.getWidth()); | |
| 527 assertEquals(expected, got); | |
| 528 } | |
| 529 | |
| 530 public void testDivCaption() { | |
| 531 ImageElement image = TestUtil.createImage(); | |
| 532 image.setSrc("http://wwww.example.com/image.jpeg"); | |
| 533 image.setAttribute("width", "100"); | |
| 534 image.setAttribute("height", "100"); | |
| 535 | |
| 536 Element figure = Document.get().createElement("FIGURE"); | |
| 537 figure.appendChild(image); | |
| 538 Element div = Document.get().createElement("DIV"); | |
| 539 div.setInnerHTML("This is a caption"); | |
| 540 figure.appendChild(div); | |
| 541 mBody.appendChild(figure); | |
| 542 String expected = | |
| 543 "<figure>" + | |
| 544 "<img src=\"http://wwww.example.com/image.jpeg\"" + | |
| 545 " width=\"100\" height=\"100\">" + | |
| 546 "<figcaption>This is a caption</figcaption>" + | |
| 547 "</figure>"; | |
| 548 EmbedExtractor extractor = new ImageExtractor(); | |
| 549 WebElement result = extractor.extract(figure); | |
| 550 assertEquals(expected, result.generateOutput(false)); | |
| 402 } | 551 } |
| 403 } | 552 } |
| OLD | NEW |