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.core.client.JsArray; | 7 import com.google.gwt.core.client.JsArray; |
| 8 import com.google.gwt.core.client.JsArrayString; | 8 import com.google.gwt.core.client.JsArrayString; |
| 9 import com.google.gwt.dom.client.AnchorElement; | 9 import com.google.gwt.dom.client.AnchorElement; |
| 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.ImageElement; | 12 import com.google.gwt.dom.client.ImageElement; |
| 13 import com.google.gwt.dom.client.Node; | 13 import com.google.gwt.dom.client.Node; |
| 14 import com.google.gwt.dom.client.NodeList; | 14 import com.google.gwt.dom.client.NodeList; |
| 15 import com.google.gwt.dom.client.Style; | 15 import com.google.gwt.dom.client.Style; |
| 16 import com.google.gwt.dom.client.VideoElement; | 16 import com.google.gwt.dom.client.VideoElement; |
| 17 import com.google.gwt.http.client.URL; | 17 import com.google.gwt.http.client.URL; |
| 18 import com.google.gwt.regexp.shared.MatchResult; | |
| 19 import com.google.gwt.regexp.shared.RegExp; | |
| 18 | 20 |
| 19 import java.util.ArrayList; | 21 import java.util.ArrayList; |
| 20 import java.util.HashMap; | 22 import java.util.HashMap; |
| 21 import java.util.List; | 23 import java.util.List; |
| 22 import java.util.Map; | 24 import java.util.Map; |
| 23 | 25 |
| 24 public class DomUtil { | 26 public class DomUtil { |
| 25 /** | 27 /** |
| 26 * GWT does not provide a way to get a list of all attributes that have been explicitly set on a | 28 * GWT does not provide a way to get a list of all attributes that have been explicitly set on a |
| 27 * DOM element (only a way to query the value of a particular attribute). In javascript, this | 29 * DOM element (only a way to query the value of a particular attribute). In javascript, this |
| (...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 488 public void exit(Node n) { | 490 public void exit(Node n) { |
| 489 } | 491 } |
| 490 | 492 |
| 491 @Override | 493 @Override |
| 492 public void skip(Element e) { | 494 public void skip(Element e) { |
| 493 } | 495 } |
| 494 }).walk(root); | 496 }).walk(root); |
| 495 return nodes; | 497 return nodes; |
| 496 } | 498 } |
| 497 | 499 |
| 500 public static String formatDuration(String duration) { | |
| 501 RegExp pattern = RegExp.compile("^P(?:([0-9]+)Y)?" + | |
| 502 "(?:([0-9]+)M)?(?:([0-9]+)W)?(?:([0-9]+)D)?" + | |
| 503 "(T(?:([0-9]+)H)?(?:([0-9]+)M)?(?:([0-9]+)S)?)?$", "i"); | |
| 504 MatchResult matchResult = pattern.exec(duration); | |
| 505 List<String> result = new ArrayList<>(); | |
| 506 if (matchResult != null) { | |
| 507 if (matchResult.getGroup(1) != null) { | |
| 508 result.add(matchResult.getGroup(1) + " year(s)"); | |
|
wychen
2016/07/24 23:06:34
Can we handle plural forms?
| |
| 509 } | |
| 510 if (matchResult.getGroup(2) != null) { | |
| 511 result.add(matchResult.getGroup(2) + " month(s)"); | |
| 512 } | |
| 513 if (matchResult.getGroup(3) != null) { | |
| 514 result.add(matchResult.getGroup(3) + " week(s)"); | |
| 515 } | |
| 516 if (matchResult.getGroup(4) != null) { | |
| 517 result.add(matchResult.getGroup(4) + " day(s)"); | |
| 518 } | |
| 519 if (matchResult.getGroup(6) != null) { | |
| 520 result.add(matchResult.getGroup(6) + " hour(s)"); | |
| 521 } | |
| 522 if (matchResult.getGroup(7) != null) { | |
| 523 result.add(matchResult.getGroup(7) + " minute(s)"); | |
| 524 } | |
| 525 if (matchResult.getGroup(8) != null) { | |
| 526 result.add(matchResult.getGroup(8) + " second(s)"); | |
| 527 } | |
| 528 } | |
| 529 return join(result.toArray(), " "); | |
| 530 } | |
| 531 | |
| 532 /** | |
| 533 * Tries to get the language of the web page. It looks for | |
| 534 * the 'lang' attribute in the HTML tag, if it doesn't find it looks | |
| 535 * the meta tags for Content-Language or Language properties. | |
| 536 * | |
| 537 * @param root The root element. | |
| 538 * @return A string containing the language(s) or empty. | |
| 539 */ | |
| 540 public static String getLanguage(Element root) { | |
| 541 String language = root.getLang(); | |
| 542 if (language.isEmpty()) { | |
| 543 String query = "META[HTTP-EQUIV=\"content-language\" i][CONTENT]," + | |
| 544 "META[NAME=\"language\" i][CONTENT]"; | |
| 545 NodeList<Element> languages = DomUtil.querySelectorAll(root, query); | |
|
wychen
2016/07/24 23:06:34
Would it be faster if we only handle <head> instea
| |
| 546 if (languages.getLength() > 0) { | |
| 547 language = languages.getItem(0).getAttribute("CONTENT"); | |
| 548 } | |
| 549 } | |
| 550 return language; | |
| 551 } | |
| 552 | |
| 498 public static int getArea(Element e) { | 553 public static int getArea(Element e) { |
| 499 if (e != null) { | 554 if (e != null) { |
| 500 return e.getOffsetHeight() * e.getOffsetWidth(); | 555 return e.getOffsetHeight() * e.getOffsetWidth(); |
| 501 } | 556 } |
| 502 return 0; | 557 return 0; |
| 503 } | 558 } |
| 504 | 559 |
| 505 /** | 560 /** |
| 506 * Generate HTML/text output for a given node tree/subtree. This will ignore hidden | 561 * Generate HTML/text output for a given node tree/subtree. This will ignore hidden |
| 507 * elements. | 562 * elements. |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 523 return l.querySelectorAll(selectors); | 578 return l.querySelectorAll(selectors); |
| 524 }-*/; | 579 }-*/; |
| 525 | 580 |
| 526 public static native Document createHTMLDocument(Document doc) /*-{ | 581 public static native Document createHTMLDocument(Document doc) /*-{ |
| 527 return doc.implementation.createHTMLDocument(); | 582 return doc.implementation.createHTMLDocument(); |
| 528 }-*/; | 583 }-*/; |
| 529 | 584 |
| 530 public static native Element getFirstElementChild(Document document) /*-{ | 585 public static native Element getFirstElementChild(Document document) /*-{ |
| 531 return document.firstElementChild; | 586 return document.firstElementChild; |
| 532 }-*/; | 587 }-*/; |
| 588 | |
| 589 public static native String join(Object[] list, String conjunction) /*-{ | |
| 590 return list.join(conjunction); | |
| 591 }-*/; | |
| 533 } | 592 } |
| OLD | NEW |