Commit 1f67fc8
authored
fix: accept a raw Element as the context option (#817)
* fix: accept a raw Element as the context option
`context` was normalised with `if (!o.context || !o.context.length)`, which
tested a shape that only jQuery objects and strings have. A raw DOM Element
has no `length` at all, so it was always dropped and the registration silently
fell back to `document`: the menu worked, but page-wide rather than scoped to
the element the caller passed. The same check misfired the other way for
`<form>` and `<select>` elements, whose `length` is their control/option count,
so an empty one was dropped and a non-empty one accepted.
Normalise by type instead: resolve the value once and keep it when it is an
element or the document, in the spirit of the existing isElementSelector()
helper used for `selector`. `o.context` stays a raw DOM node, which is what
every consumer expects (`$.contains(o.context, el)`, `o.context !== el`).
A context that resolves to no element now falls back to `document` as well.
Previously a selector string matching nothing left `o.context` undefined and
`$context` empty, so the registration was stored but bound to nothing at all
and could never fire.
* fix: narrow the context fix to newly-accepted Element shapes only
Backwards compatibility review of the previous commit. Registering with a raw
Element context is the fix #809 asks for, but the same normalisation also
decides several shapes that are ignored *today*, and quietly honouring those
would un-scope menus that currently work - with no error, the menu simply stops
appearing outside the context element.
So the change is now strictly additive. Only an Element, and only for 'create',
takes the new branch; everything else keeps the legacy length test verbatim:
* <form>/<select> (and anything else with a numeric `length`, such as `window`)
keep the old length-based decision, so an empty one is still ignored.
* A context selector matching nothing still registers nothing.
* 'destroy'/'update' still ignore an Element context. There `context` means the
trigger element, not a container, and scoping it would turn a
$.contextMenu('destroy', {context: element}) that tears everything down today
into a silent no-op.
An ignored Element context also meant the menu was tracked in `namespaces`,
which is what lets $.contextMenu('destroy', selector) find it. Keep tracking it
so that teardown keeps working, and unbind from the menu's own context rather
than always from `document`, which is where the handler now lives.
The legacy cases are pinned by tests, and documented as deprecated.1 parent 267c5b7 commit 1f67fc8
5 files changed
Lines changed: 321 additions & 13 deletions
File tree
- documentation
- src
- test/unit
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| 38 | + | |
37 | 39 | | |
38 | 40 | | |
39 | 41 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
69 | 106 | | |
70 | 107 | | |
71 | 108 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2747 | 2747 | | |
2748 | 2748 | | |
2749 | 2749 | | |
| 2750 | + | |
| 2751 | + | |
| 2752 | + | |
| 2753 | + | |
| 2754 | + | |
2750 | 2755 | | |
2751 | 2756 | | |
2752 | 2757 | | |
2753 | 2758 | | |
2754 | 2759 | | |
2755 | 2760 | | |
2756 | 2761 | | |
2757 | | - | |
| 2762 | + | |
| 2763 | + | |
| 2764 | + | |
| 2765 | + | |
| 2766 | + | |
| 2767 | + | |
| 2768 | + | |
| 2769 | + | |
| 2770 | + | |
| 2771 | + | |
| 2772 | + | |
| 2773 | + | |
| 2774 | + | |
| 2775 | + | |
| 2776 | + | |
| 2777 | + | |
| 2778 | + | |
| 2779 | + | |
| 2780 | + | |
| 2781 | + | |
| 2782 | + | |
| 2783 | + | |
| 2784 | + | |
| 2785 | + | |
| 2786 | + | |
| 2787 | + | |
| 2788 | + | |
| 2789 | + | |
| 2790 | + | |
| 2791 | + | |
| 2792 | + | |
| 2793 | + | |
| 2794 | + | |
2758 | 2795 | | |
2759 | 2796 | | |
2760 | 2797 | | |
| |||
2815 | 2852 | | |
2816 | 2853 | | |
2817 | 2854 | | |
2818 | | - | |
| 2855 | + | |
| 2856 | + | |
| 2857 | + | |
| 2858 | + | |
| 2859 | + | |
| 2860 | + | |
| 2861 | + | |
2819 | 2862 | | |
2820 | 2863 | | |
2821 | 2864 | | |
| |||
3005 | 3048 | | |
3006 | 3049 | | |
3007 | 3050 | | |
| 3051 | + | |
| 3052 | + | |
| 3053 | + | |
| 3054 | + | |
| 3055 | + | |
| 3056 | + | |
| 3057 | + | |
3008 | 3058 | | |
3009 | 3059 | | |
3010 | 3060 | | |
3011 | 3061 | | |
3012 | 3062 | | |
3013 | 3063 | | |
3014 | | - | |
3015 | | - | |
| 3064 | + | |
| 3065 | + | |
3016 | 3066 | | |
3017 | 3067 | | |
3018 | | - | |
3019 | | - | |
| 3068 | + | |
| 3069 | + | |
3020 | 3070 | | |
3021 | | - | |
| 3071 | + | |
3022 | 3072 | | |
3023 | 3073 | | |
3024 | | - | |
| 3074 | + | |
3025 | 3075 | | |
3026 | 3076 | | |
3027 | 3077 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
153 | 153 | | |
154 | 154 | | |
155 | 155 | | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
167 | 170 | | |
168 | 171 | | |
169 | 172 | | |
| |||
0 commit comments