Skip to content

Commit

Permalink
Change config to be an attribute getter.
Browse files Browse the repository at this point in the history
This changes config and defaultConfig to be attribute getters, rather than methods.
  • Loading branch information
otherdaniel authored Jul 6, 2021
1 parent e4b1cac commit f3f52ce
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -129,33 +129,33 @@ handle additional, application-specific use cases.
Exposed=(Window),
SecureContext
] interface Sanitizer {
constructor(optional SanitizerConfig config = {});
constructor(optional SanitizerConfig aConfig = {});

DocumentFragment sanitize(SanitizerInput input);
DOMString sanitizeToString(SanitizerInput input);

SanitizerConfig config();
static SanitizerConfig defaultConfig();
readonly attribute SanitizerConfig config;
static readonly attribute SanitizerConfig defaultConfig;
};
</pre>

* The <dfn constructor for=Sanitizer lt="Sanitizer(config)">
<code>new Sanitizer(<var>config</var>)</code></dfn> constructor steps
are to create a new Sanitizer instance, and to retain a copy of |config|
* The <dfn constructor for=Sanitizer lt="Sanitizer()">
<code>new Sanitizer(<var>aConfig</var>)</code></dfn> constructor steps
are to create a new Sanitizer instance, and to retain a copy of |aConfig|
as its [=configuration object=].
* The <dfn method for=Sanitizer><code>sanitize(<var>input</var>)</code></dfn>
method steps are to return the result of running the [=sanitize=]
algorithm on |input|,
* The <dfn method for=Sanitizer><code>sanitizeToString(<var>input</var>)</code></dfn>
method steps are to return the result of running [=sanitizeToString=]
algorithm on |input|.
* The <dfn method for=Sanitizer><code>config()</code></dfn> method steps are
to return the result of running the [=query the sanitizer config=]
algorithm. It essentially returns a copy of the Sanitizer's
* The <dfn attribute for=Sanitizer><code>config</code></dfn> attribute getter
steps steps are to return the result of running the [=query the sanitizer
config=] algorithm. It essentially returns a copy of the Sanitizer's
[=configuration object=], with some degree of normalization.
* The value of the static
<dfn method for=Sanitizer><code>defaultConfig()</code></dfn> method steps
are to return the value of the [=default configuration=] object.
* The <dfn attribute for=Sanitizer><code>defaultConfig</code></dfn> attribute
getter steps are to return the value of the [=default configuration=]
object.

Example:
```js
Expand Down Expand Up @@ -278,34 +278,36 @@ Examples:
```

A sanitizer's configuration can be queried using the
[=query the sanitizer config=] method.
{{Sanitizer}}'s {{config}} read-only attribute. The default configuration
can be quried using the {{Sanitizer}}'s {{defaultConfig}} static read-only
attribute.

Examples:
```js
// Does the default config allow script elements?
Sanitizer.defaultConfig().allowElements.includes("script") // false
Sanitizer.defaultConfig.allowElements.includes("script") // false

// We found a Sanitizer instance. Does it have an allow-list configured?
const a_sanitizer = ...;
!!a_sanitizer.config().allowElements // true, if an allowElements list is configured
!!a_sanitizer.config.allowElements // true, if an allowElements list is configured

// If it does have an allow elements list, does it include the <div> element?
a_sanitizer.config().allowElements.includes("div") // true, if "div" is in allowElements.
a_sanitizer.config.allowElements.includes("div") // true, if "div" is in allowElements.

// Note that the config attribute might do some normaliztion. E.g., it won't
// contain key/value pairs that are not declare in the IDL.
Object.keys(new Sanitizer({madeUpDictionaryKey: "Hello"}).config()) // []
Object.keys(new Sanitizer({madeUpDictionaryKey: "Hello"}).config) // []

// As a Sanitizer's config describes its operation, a new sanitizer with
// another instance's configuration should behave identically.
// (For illustration purposes only. It would make more sense to just use a directly.)
const a = /* ... a Sanitizer we found somewhere ... */;
const b = new Sanitizer(a.config()); // b should behave the same as a.
const b = new Sanitizer(a.config); // b should behave the same as a.

// defaultConfig() and new Sanitizer().config should be the same.
// defaultConfig and new Sanitizer().config should be the same.
// (For illustration purposes only. There are better ways of implementing
// object equality in JavaScript.)
JSON.stringify(Sanitizer.defaultConfig()) == JSON.stringify(new Sanitizer().config()); // true
JSON.stringify(Sanitizer.defaultConfig) == JSON.stringify(new Sanitizer().config); // true
```

### Attribute Match Lists ### {#attr-match-list}
Expand Down

0 comments on commit f3f52ce

Please sign in to comment.