Re: [whatwg/url] [Proposal] Specify a fragment-directive meant for UA instructions (#445)

There are some additional details to consider. I have some initial thoughts on how to handle them as a starting point but this isn't yet fully baked so am open to alternatives:

#### 'hashchange' event

When script does something like:

```
window.location.hash = 'foo##bar`
```
We should process the hash and strip off the `##bar` part, applying any specified UA instructions. We should only fire 'hashchange' if the location.hash changes. i.e. 'hashchange' is fired only if the part before `##` changes.

A counter-argument to this is that the behavior will be different based on whether or not the UA implements `##`; however, I think this is a more intuitive behavior.

#### URL objects

How does `new URL()` work with `##`? I would suggest:

```
let url = new URL('#foo##bar', 'https://example.com/');
url.toString() === 'https://example.com/#foo##bar'
url.href === 'https://example.com/#foo##bar'
url.hash === '#foo';
```

When a URL is navigated to, document.URL will have the `##` part stripped during loading so that after navigating to the above URL:

```
// navigated to https://example.com/#foo##bar
url.toString() === 'https://example.com/#foo'
url.href === 'https://example.com/#foo'
url.hash === '#foo';
```

In other words: the `##` part is never visible in `hash` for `URL` or `Location` objects; however, it is visible in the full representation of the URL until it is loaded.

The reason to strip it from href or toString() after loading is to prevent pages from relying on the content inside (thus replicating the existing situation with `hash`). I don't feel especially strongly about this though; if consistency were preferable we could make `document.URL.href` keep the `##` part.

Combining base URLs:

```
let url = new URL('https://example.com#foo')

let directive = new URL('##bar', url);
directive.href === 'https://example.com#foo##bar';

let hash = new URL('#boo', directive)
hash.href === 'https://example.com#boo##bar';

let page = new URL('index.html', directive);
page.href === 'https://example.com/index.html';
```

In other words: the fragment directive is treated as separate sub-resource identifiers. A changed resource clears the fragment directive.

#### Anchors

Since the href attribute of an anchor isn't yet loaded:

```
<a href="https://example.com/#foo##bar">
a.href === 'https://example.com/#foo##bar'
```

This is consistent with the `URL` handling above.

#### Reloads

Should a fragment-directive be reapplied when a page is reloaded? Intuitively, I'd say yes. e.g. If we used the fragment-directive to translate the page, a reload should keep the page in the desired language. This means the UA would need to keep the fragment-directive internally as a reload will occur when the `document.URL` does not contain the directive.

#### Multiple directives

If adopted, it's possible in the future there could be several possible uses of the fragment directive (e.g. targetText and htmlTranslate). It should be possible to specify more than one instruction in the fragment directive.

We could adopt the [media fragments](https://www.w3.org/TR/media-frags/) syntax. Example:

https://example.com##htmlLanguage=es&targetText=esempio

would first translate the page to spanis, then scroll into view the text snippet "esempio". 

#### Feature detection

Pages can detect if the fragment-directive is supported:

```
let is_supported = new URL('https://test.com##test').hash === ''
```

Are there any other aspects, particularly around URL handling, we should be thinking about?

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/url/issues/445#issuecomment-522106441

Received on Friday, 16 August 2019 18:26:18 UTC