Add a jsinterop example for interfacing with a Closure type.
@@ -10,27 +10,6 @@ For calling browser APIs you may want to consider
[Elemental](https://github.com/google/elemental2) which has autogenerated and
accurate JsInterop abstractions for (common) JavaScript APIs.
-### `@JsType` - Interface with an [extern type](https://github.com/google/closure-compiler/wiki/Externs-For-Common-Libraries).
-
-Note that `@JsType` is just syntactical sugar for applying `@JsConstructor`,
-`@JsMethod` or`@JsProperty` to all public constructors, methods and properties
-respectively.
-
-```java
-@JsType(isNative=true, namespace=JsPackage.GLOBAL)
-class RegExp {
- public RegExp(String pattern, String... flags) {}
- public native boolean test();
- public String flags;
-}
-
-// Usage
-static void main() {
- RegExp re = new RegExp("asdf", "g");
- String flags = re.flags; // "g"
- re.test("hello");
-}
-```
### `@JsType` - Interface with Closure utilities.
@@ -57,6 +36,54 @@ const Linkify = goog.require("goog.string.linkify");
Linkify.findFirstEmail("this is an email: aaa@gmail.com");
```
+### `@JsType` - Interface with a Closure type.
+Note that `@JsType` is just syntactical sugar for applying `@JsConstructor`,
+`@JsMethod` or`@JsProperty` to all public constructors, methods and properties
+respectively.
+
+
+```java
+@JsType(isNative=true, namespace="goog.html.sanitizer")
+public class HtmlSanitizer {
+ public HtmlSanitizer(Builder builder){};
+ public native SafeHtml sanitize(String unsanitizedHtml);
+ public native HTMLSpanElement sanitizeToDomNode(String unsanitizedHtml);
+
+ public static native SafeHtml sanitize(String unsanitizedHtml);
+
+ // Note: The inner class without namespace inherits the parent as namespace.
+ @JsType(isNative=true)
+ public static class Builder {
+ public native Builder allowCssStyles();
+ }
+}
+
+// Usage
+static void main() {
+ HtmlSanitizer.Builder builder = new HtmlSanitizer.Builder();
+ builder.allowCssStyles();
+ SafeHtml html = new HtmlSanitizer(builder).sanitize("<div />");
+}
+```
+
+### `@JsType` - Interface with an [extern type](https://github.com/google/closure-compiler/wiki/Externs-For-Common-Libraries).
+
+```java
+@JsType(isNative=true, namespace=JsPackage.GLOBAL)
+public class RegExp {
+ public RegExp(String pattern, String... flags) {}
+ public native boolean test();
+ public String flags;
+}
+
+// Usage
+static void main() {
+ RegExp re = new RegExp("asdf", "g");
+ String flags = re.flags; // "g"
+ re.test("hello");
+}
+```
+
### `@JsFunction` - Implement JavaScript callbacks in Java
```java
