Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: java/org/chromium/distiller/DomUtil.java

Issue 1705123002: Add support for Schema.org/Recipe Base URL: https://github.com/chromium/dom-distiller.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 public void exit(Node n) { 428 public void exit(Node n) {
427 } 429 }
428 430
429 @Override 431 @Override
430 public void skip(Element e) { 432 public void skip(Element e) {
431 } 433 }
432 }).walk(root); 434 }).walk(root);
433 return nodes; 435 return nodes;
434 } 436 }
435 437
438 public static String formatDuration(String duration) {
wychen 2016/03/14 22:58:42 I18n and l10n might be difficult. Singular/plural
439 RegExp pattern = RegExp.compile("^P(?:([0-9]+)Y)?" +
440 "(?:([0-9]+)M)?(?:([0-9]+)W)?(?:([0-9]+)D)?" +
441 "(T(?:([0-9]+)H)?(?:([0-9]+)M)?(?:([0-9]+)S)?)?$", "i");
442 MatchResult matchResult = pattern.exec(duration);
443 List<String> result = new ArrayList<>();
444 if (matchResult != null) {
445 if (matchResult.getGroup(1) != null) {
446 result.add(matchResult.getGroup(1) + " year(s)");
447 }
448 if (matchResult.getGroup(2) != null) {
449 result.add(matchResult.getGroup(2) + " month(s)");
450 }
451 if (matchResult.getGroup(3) != null) {
452 result.add(matchResult.getGroup(3) + " week(s)");
453 }
454 if (matchResult.getGroup(4) != null) {
455 result.add(matchResult.getGroup(4) + " day(s)");
456 }
457 if (matchResult.getGroup(6) != null) {
458 result.add(matchResult.getGroup(6) + " hour(s)");
459 }
460 if (matchResult.getGroup(7) != null) {
461 result.add(matchResult.getGroup(7) + " minute(s)");
462 }
463 if (matchResult.getGroup(8) != null) {
464 result.add(matchResult.getGroup(8) + " second(s)");
465 }
466 }
467 return join(result.toArray(), " ");
468 }
469
436 /** 470 /**
437 * Generate HTML/text output for a given node tree/subree. This will ignore hidden 471 * Generate HTML/text output for a given node tree/subree. This will ignore hidden
438 * elements. 472 * elements.
439 * @param subtree The root of the subtree. 473 * @param subtree The root of the subtree.
440 * @param textOnly If this function should return text only and not HTML. 474 * @param textOnly If this function should return text only and not HTML.
441 * @return The output for the provided subtree. 475 * @return The output for the provided subtree.
442 */ 476 */
443 public static String generateOutputFromTree(Node subtree, boolean textOnly) { 477 public static String generateOutputFromTree(Node subtree, boolean textOnly) {
444 return generateOutputFromList(getOutputNodes(subtree), textOnly); 478 return generateOutputFromList(getOutputNodes(subtree), textOnly);
445 } 479 }
446 480
447 // Returns whether querySelectorAll is available 481 // Returns whether querySelectorAll is available
448 public static native boolean supportQuerySelectorAll(Element root) /*-{ 482 public static native boolean supportQuerySelectorAll(Element root) /*-{
449 return (typeof(root.querySelectorAll) == 'function'); 483 return (typeof(root.querySelectorAll) == 'function');
450 }-*/; 484 }-*/;
451 485
452 // GWT doesn't support querySelectorAll, so testing the caller could be hard er. 486 // GWT doesn't support querySelectorAll, so testing the caller could be hard er.
453 public static native NodeList<Element> querySelectorAll(Node l, String selec tors) /*-{ 487 public static native NodeList<Element> querySelectorAll(Node l, String selec tors) /*-{
454 return l.querySelectorAll(selectors); 488 return l.querySelectorAll(selectors);
455 }-*/; 489 }-*/;
456 490
457 public static native Document createHTMLDocument(Document doc) /*-{ 491 public static native Document createHTMLDocument(Document doc) /*-{
458 return doc.implementation.createHTMLDocument(); 492 return doc.implementation.createHTMLDocument();
459 }-*/; 493 }-*/;
460 494
461 public static native Element getFirstElementChild(Document document) /*-{ 495 public static native Element getFirstElementChild(Document document) /*-{
462 return document.firstElementChild; 496 return document.firstElementChild;
463 }-*/; 497 }-*/;
498
499 public static native String join(Object[] list, String conjunction) /*-{
500 return list.join(conjunction);
501 }-*/;
464 } 502 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698