取り消し

既定動作と取り消し (DOM)

[23] イベントは、既定動作 (default action) を持つことがあります。 既定動作イベントリスナーの実行後に処理される動作です。 イベント取り消し可能 (cancelable) (cancelable IDL属性) なら、 取り消し (cancel) (preventDefault メソッド) することができます。

仕様書

取り消し可能性

[7] イベントオブジェクトは、取り消し可能かどうかの状態を持ちます。 cancelable IDL属性は、初期化時に与えられた値を返さなければなりません >>6

初期化後にこれを変更することはできません。

[24] 利用者エージェント発火するイベントについては、 イベント型と文脈によって取り消し可能か否かが予め決められています。

取り消し

[10] イベントオブジェクトは、取り消し済みフラグ (canceled flag) なる状態を持ちます >>12。 初期値は未設定です >>12defaultPrevented IDL属性は、取り消し済みフラグが設定されているか否かを返さなければなりません >>11

[9] イベントオブジェクトpreventDefault メソッドは、 次のようにしなければなりません >>8

  1. [62] 文脈オブジェクトについて、 取り消し済みフラグを設定します。

[44] イベントオブジェクトreturnValue IDL属性取得器は、 次のようにしなければなりません >>11

  1. [64] 文脈オブジェクト取り消し済みフラグ否定を返します。

[65] イベントオブジェクトreturnValue IDL属性設定器は、 新しい値について、 次のようにしなければなりません >>11

  1. [67] 新しい値の場合、
    1. [66] 文脈オブジェクトについて取り消し済みフラグを設定します。

[63] イベントについて取り消し済みフラグを設定 (set the canceled flag) するには、 次のようにします。

  1. [42] イベント取り消し可能で、 イベント受動的リスナー内フラグの場合、
    1. [43] イベント取り消し済みフラグを、に設定します。
  2. [47] それ以外の場合、
    1. [48] 開発者コンソールに報告することが推奨 (encourage) されています。

[15] イベントハンドラーを返した時も、 取り消し済みフラグが設定されます。

[14] イベントdispatch は、イベントリスナーを実行し終えたら、 イベント取り消し済みフラグが設定されていれば、 設定されていなければを返さなければなりません >>13。 つまり、 dispatch は取り消されなかったかどうかを返します。

受動的フラグ

[35] イベントリスナーは、受動的 (passive) フラグを持ちます >>34。このフラグは、 preventDefault が効力を持つかどうかを表します。

[37] AddEventListenerOptionspassive は、イベントリスナー受動的フラグの値を boolean で指定します >>36addEventListener では追加するイベントリスナー受動的フラグを設定するか否かを指定します。 この値が明示されなければ、イベントリスナー受動的ではないとされます。

[39] removeEventListener では指定しても意味がありません。
[38] イベントハンドラーも、受動的ではありません。

[41] イベントは、受動的イベント内フラグ (in passive listener flag) を持ちます >>40。このフラグは初期状態では未設定ですが、受動的イベントリスナーコールバックの実行中のみ、 設定されます。

[45] このフラグは preventDefault メソッドで参照されます (>>44)。

[46] 受動的フラグは、利用者エージェントの処理を最適化できるようにするために導入されました。 例えば Touch Events の処理はスクロールのパフォーマンスに影響します。 イベント取消するとスクロールを抑制できるので、 イベントリスナーが登録されているとその結果を待たないとスクロール処理を実行できませんが、 実際ほとんどのイベントリスナー取消しないので、 待つだけ無駄です >>32。本フラグにより著者利用者エージェントヒントを与えることができます。

既定動作

[16] 既定動作 (default action) は、イベントの処理直後に実行される処理です。

[20] 既定動作の多くは、イベント取り消しされた場合には実行されません。 既定動作が規定されているイベントの多くは、取り消し可能です。

[19] この用語は W3C DOM 時代にはありましたが >>17, >>18、現在の DOM Standard では定義されていません。 90年代末から00年代前半には、どのようなきっかけであろうとイベント発火されると、 その結果として何らかの動作が発生する、 という解釈が存在していました。例えばクリック時の動作を起こしたければ、 実際にクリックせずとも、 click イベント発火することで代用させることができる実装や状況も存在していました。 00年代中頃からは、そうした解釈は誤り >>49 とする考え方が主流となり、 きっかけとなる出来事ごとの動作が明確を明確に規定し、 イベント発火はその途中の一段階に過ぎないものと扱うようになりました。 この現代的解釈のもとではイベントの結果生じた動作ではなく、 一連の動作の続きの段階ですから、イベント既定動作と説明するのは不正確になります。 (また click イベント発火したからといって、 クリック相当の動作が発生するわけではありません。)

[22] ただし HTML Standard など DOM Standard 以前から存在する仕様書には、 まだ既定動作という用語が残っています。そのような既定動作は、 イベントdispatch の後にイベント取り消しされていなければ、 同じタスクで引き続き実行することが求められているようです。

[21] 既定動作は色々なイベントに対して存在していますが、特に象徴的なのは click イベント既定動作といえる活性化動作でしょう。 例えば a 要素ハイパーリンククリックされ、 その click イベント取り消しされなければ、 そのハイパーリンクを辿ることになります。 もっとも現在活性化動作既定動作としてではなく、専用のアルゴリズムによって動作が規定されており、それは >>22 よりも複雑なものとなっています。

歴史

[5] Bug 127903 &#8211; <a> link tags intercept events meant for form submit buttons ( 版) https://bugzilla.mozilla.org/show_bug.cgi?id=127903 (名無しさん 2007-02-17 13:00:51 +00:00)

[4] WICD Core 1.0 ( ( 版)) http://www.w3.org/TR/WICD/#hyperlinking

[1] Safari の event.stopPropagation() や event.preventDefault() がウソ実装のままである件 http://www.remus.dti.ne.jp/~a-satomi/nikki/2005/06a.html#d27n03 (名無しさん [sage] 2005-06-26 23:06:31 +00:00)

[2] WinIE7preventDefault を実装していません。 代わりに returnValue があります。

[3] XML Events 2 ( ( 版)) http://www.w3.org/TR/xml-events2/#section-preventDefault-element

[25] Explain in many different ways that events cannot cause actions. Fixe… · whatwg/dom@24ab873 ( 版) https://github.com/whatwg/dom/commit/24ab873d17669b5c1994a3dd92a27d29b1dd03e4

[26] 12230 – Some synthetic events cause actions ( 版) https://www.w3.org/Bugs/Public/show_bug.cgi?id=12230

[27] 124990 – Event ordering rewrite tracking bug ( 版) https://bugzilla.mozilla.org/show_bug.cgi?id=124990

イベント群

[54] Document Object Model Events ( 版) http://www.w3.org/TR/2003/NOTE-DOM-Level-3-Events-20031107/events.html#Event-groups

[28] 12230 – Some synthetic events cause actions ( 版) https://www.w3.org/Bugs/Public/show_bug.cgi?id=12230#c57

System event group was added to Gecko 2002

https://bugzilla.mozilla.org/show_bug.cgi?id=124990#c0

(Though, before firefox 3, Gecko's event dispatching was really odd, but the system group certainly was there.)

[29] 12230 – Some synthetic events cause actions ( 版) https://www.w3.org/Bugs/Public/show_bug.cgi?id=12230#c60

(Gecko never has had more than default and system groups, so it implemented a variant of what the old D3E draft had.)

[30] 12230 – Some synthetic events cause actions ( 版) https://www.w3.org/Bugs/Public/show_bug.cgi?id=12230

受動的オプション

[51] Should the API be forwards compatible? · Issue #12 · WICG/EventListenerOptions ( 版) https://github.com/WICG/EventListenerOptions/issues/12

[52] Touch Events - Level 2 ( 版) https://w3c.github.io/touch-events/#cancelability

[53] Remove passive as event listener key · whatwg/dom@a13a3c7 ( 版) https://github.com/whatwg/dom/commit/a13a3c7fc14732691b50c51316b16de0915a61f2

[55] Forced passive touch listeners · Issue #18 · WICG/interventions () https://github.com/WICG/interventions/issues/18

[56] Editorial: major cleanup around firing and dispatching events (annevk著, ) https://github.com/whatwg/html/commit/8ffbd146d4709057e45dd6cfc81e7a70897050c5

[57] Remove mentions of "default action". Fix #14 (garykac著, ) https://github.com/w3c/clipboard-apis/commit/763deeaacc073057fae998a301bed0fdcd49e303

[58] Making touch scrolling fast by default  |  Web  |  Google Developers () https://developers.google.com/web/updates/2017/01/scrolling-intervention

[59] chrome, doesn't accept preventDefault and still cancelable is true · Issue #579 · whatwg/dom () https://github.com/whatwg/dom/issues/579

[60] chrome, doesn't accept preventDefault and still cancelable is true · Issue #63 · WICG/interventions () https://github.com/WICG/interventions/issues/63

[61] Define Event's srcElement and returnValue (miketaylr著, ) https://github.com/whatwg/dom/commit/e36b369a418663603e081d211dc844d197d97ecb

[68] Define Event's srcElement and returnValue (miketaylr著, ) https://github.com/whatwg/dom/commit/e36b369a418663603e081d211dc844d197d97ecb

[69] Define Event.srcElement and Event.returnValue · Issue #625 · whatwg/dom () https://github.com/whatwg/dom/issues/625

[70] Flag refs in preventDefault()'s description missing "context object’s" qualifier · Issue #627 · whatwg/dom () https://github.com/whatwg/dom/issues/627

[71] Define Event.srcElement and Event.returnValue by miketaylr · Pull Request #626 · whatwg/dom () https://github.com/whatwg/dom/pull/626

[72] New WebKit Features in Safari 11.1 | WebKit () https://webkit.org/blog/8216/new-webkit-features-in-safari-11-1/

[73] New WebKit Features in Safari 11.1 | WebKit () https://webkit.org/blog/8216/new-webkit-features-in-safari-11-1/

[74] Merge pull request #150 from w3c/tdresser-no-default-action (yoavweiss著, ) https://github.com/w3c/resource-timing/commit/0b6e7991107bcb5e17a98873f6a0ad160da073d1

[75] Remove mention of default actions. by tdresser · Pull Request #150 · w3c/resource-timing () https://github.com/w3c/resource-timing/pull/150

[76] Events don't have default actions · Issue #148 · w3c/resource-timing () https://github.com/w3c/resource-timing/issues/148

[77] Merge pull request #150 from w3c/tdresser-no-default-action (yoavweiss著, ) https://github.com/w3c/resource-timing/commit/0b6e7991107bcb5e17a98873f6a0ad160da073d1

[78] Remove mention of default actions. by tdresser · Pull Request #150 · w3c/resource-timing () https://github.com/w3c/resource-timing/pull/150