@vue/runtime-dom 3.5.0-alpha.5 → 3.5.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,12 +1,12 @@
1
1
  # @vue/runtime-dom
2
2
 
3
- ``` js
3
+ ```js
4
4
  import { h, createApp } from '@vue/runtime-dom'
5
5
 
6
6
  const RootComponent = {
7
7
  render() {
8
8
  return h('div', 'hello world')
9
- }
9
+ },
10
10
  }
11
11
 
12
12
  createApp(RootComponent).mount('#app')
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @vue/runtime-dom v3.5.0-alpha.5
2
+ * @vue/runtime-dom v3.5.0-beta.2
3
3
  * (c) 2018-present Yuxi (Evan) You and Vue contributors
4
4
  * @license MIT
5
5
  **/
@@ -10,6 +10,18 @@ Object.defineProperty(exports, '__esModule', { value: true });
10
10
  var runtimeCore = require('@vue/runtime-core');
11
11
  var shared = require('@vue/shared');
12
12
 
13
+ let policy = void 0;
14
+ const tt = typeof window !== "undefined" && window.trustedTypes;
15
+ if (tt) {
16
+ try {
17
+ policy = /* @__PURE__ */ tt.createPolicy("vue", {
18
+ createHTML: (val) => val
19
+ });
20
+ } catch (e) {
21
+ runtimeCore.warn(`Error creating trusted types policy: ${e}`);
22
+ }
23
+ }
24
+ const unsafeToTrustedHTML = policy ? (val) => policy.createHTML(val) : (val) => val;
13
25
  const svgNS = "http://www.w3.org/2000/svg";
14
26
  const mathmlNS = "http://www.w3.org/1998/Math/MathML";
15
27
  const doc = typeof document !== "undefined" ? document : null;
@@ -57,7 +69,9 @@ const nodeOps = {
57
69
  if (start === end || !(start = start.nextSibling)) break;
58
70
  }
59
71
  } else {
60
- templateContainer.innerHTML = namespace === "svg" ? `<svg>${content}</svg>` : namespace === "mathml" ? `<math>${content}</math>` : content;
72
+ templateContainer.innerHTML = unsafeToTrustedHTML(
73
+ namespace === "svg" ? `<svg>${content}</svg>` : namespace === "mathml" ? `<math>${content}</math>` : content
74
+ );
61
75
  const template = templateContainer.content;
62
76
  if (namespace === "svg" || namespace === "mathml") {
63
77
  const wrapper = template.firstChild;
@@ -724,16 +738,24 @@ function shouldSetAsProp(el, key, value, isSVG) {
724
738
  if (isNativeOn(key) && shared.isString(value)) {
725
739
  return false;
726
740
  }
727
- return key in el;
741
+ if (key in el) {
742
+ return true;
743
+ }
744
+ if (el._isVueCE && (/[A-Z]/.test(key) || !shared.isString(value))) {
745
+ return true;
746
+ }
747
+ return false;
728
748
  }
729
749
 
750
+ const REMOVAL = {};
730
751
  /*! #__NO_SIDE_EFFECTS__ */
731
752
  // @__NO_SIDE_EFFECTS__
732
- function defineCustomElement(options, extraOptions, hydrate2) {
753
+ function defineCustomElement(options, extraOptions, _createApp) {
733
754
  const Comp = runtimeCore.defineComponent(options, extraOptions);
755
+ if (shared.isPlainObject(Comp)) shared.extend(Comp, extraOptions);
734
756
  class VueCustomElement extends VueElement {
735
757
  constructor(initialProps) {
736
- super(Comp, initialProps, hydrate2);
758
+ super(Comp, initialProps, _createApp);
737
759
  }
738
760
  }
739
761
  VueCustomElement.def = Comp;
@@ -741,47 +763,87 @@ function defineCustomElement(options, extraOptions, hydrate2) {
741
763
  }
742
764
  /*! #__NO_SIDE_EFFECTS__ */
743
765
  const defineSSRCustomElement = /* @__NO_SIDE_EFFECTS__ */ (options, extraOptions) => {
744
- return /* @__PURE__ */ defineCustomElement(options, extraOptions, hydrate);
766
+ return /* @__PURE__ */ defineCustomElement(options, extraOptions, createSSRApp);
745
767
  };
746
768
  const BaseClass = typeof HTMLElement !== "undefined" ? HTMLElement : class {
747
769
  };
748
770
  class VueElement extends BaseClass {
749
- constructor(_def, _props = {}, hydrate2) {
771
+ constructor(_def, _props = {}, _createApp = createApp) {
750
772
  super();
751
773
  this._def = _def;
752
774
  this._props = _props;
775
+ this._createApp = _createApp;
776
+ this._isVueCE = true;
753
777
  /**
754
778
  * @internal
755
779
  */
756
780
  this._instance = null;
781
+ /**
782
+ * @internal
783
+ */
784
+ this._app = null;
785
+ /**
786
+ * @internal
787
+ */
788
+ this._nonce = this._def.nonce;
757
789
  this._connected = false;
758
790
  this._resolved = false;
759
791
  this._numberProps = null;
792
+ this._styleChildren = /* @__PURE__ */ new WeakSet();
760
793
  this._ob = null;
761
- if (this.shadowRoot && hydrate2) {
762
- hydrate2(this._createVNode(), this.shadowRoot);
794
+ if (this.shadowRoot && _createApp !== createApp) {
795
+ this._root = this.shadowRoot;
763
796
  } else {
764
797
  if (this.shadowRoot) {
765
798
  runtimeCore.warn(
766
799
  `Custom element has pre-rendered declarative shadow root but is not defined as hydratable. Use \`defineSSRCustomElement\`.`
767
800
  );
768
801
  }
769
- this.attachShadow({ mode: "open" });
770
- if (!this._def.__asyncLoader) {
771
- this._resolveProps(this._def);
802
+ if (_def.shadowRoot !== false) {
803
+ this.attachShadow({ mode: "open" });
804
+ this._root = this.shadowRoot;
805
+ } else {
806
+ this._root = this;
772
807
  }
773
808
  }
809
+ if (!this._def.__asyncLoader) {
810
+ this._resolveProps(this._def);
811
+ }
774
812
  }
775
813
  connectedCallback() {
814
+ if (!this.shadowRoot) {
815
+ this._parseSlots();
816
+ }
776
817
  this._connected = true;
818
+ let parent = this;
819
+ while (parent = parent && (parent.parentNode || parent.host)) {
820
+ if (parent instanceof VueElement) {
821
+ this._parent = parent;
822
+ break;
823
+ }
824
+ }
777
825
  if (!this._instance) {
778
826
  if (this._resolved) {
827
+ this._setParent();
779
828
  this._update();
780
829
  } else {
781
- this._resolveDef();
830
+ if (parent && parent._pendingResolve) {
831
+ this._pendingResolve = parent._pendingResolve.then(() => {
832
+ this._pendingResolve = void 0;
833
+ this._resolveDef();
834
+ });
835
+ } else {
836
+ this._resolveDef();
837
+ }
782
838
  }
783
839
  }
784
840
  }
841
+ _setParent(parent = this._parent) {
842
+ if (parent) {
843
+ this._instance.parent = parent._instance;
844
+ this._instance.provides = parent._instance.provides;
845
+ }
846
+ }
785
847
  disconnectedCallback() {
786
848
  this._connected = false;
787
849
  runtimeCore.nextTick(() => {
@@ -790,8 +852,9 @@ class VueElement extends BaseClass {
790
852
  this._ob.disconnect();
791
853
  this._ob = null;
792
854
  }
793
- render(null, this.shadowRoot);
794
- this._instance = null;
855
+ this._app && this._app.unmount();
856
+ this._instance.ce = void 0;
857
+ this._app = this._instance = null;
795
858
  }
796
859
  });
797
860
  }
@@ -799,7 +862,9 @@ class VueElement extends BaseClass {
799
862
  * resolve inner component definition (handle possible async component)
800
863
  */
801
864
  _resolveDef() {
802
- this._resolved = true;
865
+ if (this._pendingResolve) {
866
+ return;
867
+ }
803
868
  for (let i = 0; i < this.attributes.length; i++) {
804
869
  this._setAttr(this.attributes[i].name);
805
870
  }
@@ -810,6 +875,8 @@ class VueElement extends BaseClass {
810
875
  });
811
876
  this._ob.observe(this, { attributes: true });
812
877
  const resolve = (def, isAsync = false) => {
878
+ this._resolved = true;
879
+ this._pendingResolve = void 0;
813
880
  const { props, styles } = def;
814
881
  let numberProps;
815
882
  if (props && !shared.isArray(props)) {
@@ -827,22 +894,53 @@ class VueElement extends BaseClass {
827
894
  if (isAsync) {
828
895
  this._resolveProps(def);
829
896
  }
830
- this._applyStyles(styles);
831
- this._update();
897
+ if (this.shadowRoot) {
898
+ this._applyStyles(styles);
899
+ } else if (styles) {
900
+ runtimeCore.warn(
901
+ "Custom element style injection is not supported when using shadowRoot: false"
902
+ );
903
+ }
904
+ this._mount(def);
832
905
  };
833
906
  const asyncDef = this._def.__asyncLoader;
834
907
  if (asyncDef) {
835
- asyncDef().then((def) => resolve(def, true));
908
+ this._pendingResolve = asyncDef().then(
909
+ (def) => resolve(this._def = def, true)
910
+ );
836
911
  } else {
837
912
  resolve(this._def);
838
913
  }
839
914
  }
915
+ _mount(def) {
916
+ if (!def.name) {
917
+ def.name = "VueElement";
918
+ }
919
+ this._app = this._createApp(def);
920
+ if (def.configureApp) {
921
+ def.configureApp(this._app);
922
+ }
923
+ this._app._ceVNode = this._createVNode();
924
+ this._app.mount(this._root);
925
+ const exposed = this._instance && this._instance.exposed;
926
+ if (!exposed) return;
927
+ for (const key in exposed) {
928
+ if (!shared.hasOwn(this, key)) {
929
+ Object.defineProperty(this, key, {
930
+ // unwrap ref to be consistent with public instance behavior
931
+ get: () => runtimeCore.unref(exposed[key])
932
+ });
933
+ } else {
934
+ runtimeCore.warn(`Exposed property "${key}" already exists on custom element.`);
935
+ }
936
+ }
937
+ }
840
938
  _resolveProps(def) {
841
939
  const { props } = def;
842
940
  const declaredPropKeys = shared.isArray(props) ? props : Object.keys(props || {});
843
941
  for (const key of Object.keys(this)) {
844
942
  if (key[0] !== "_" && declaredPropKeys.includes(key)) {
845
- this._setProp(key, this[key], true, false);
943
+ this._setProp(key, this[key]);
846
944
  }
847
945
  }
848
946
  for (const key of declaredPropKeys.map(shared.camelize)) {
@@ -851,18 +949,20 @@ class VueElement extends BaseClass {
851
949
  return this._getProp(key);
852
950
  },
853
951
  set(val) {
854
- this._setProp(key, val);
952
+ this._setProp(key, val, true, true);
855
953
  }
856
954
  });
857
955
  }
858
956
  }
859
957
  _setAttr(key) {
860
- let value = this.hasAttribute(key) ? this.getAttribute(key) : void 0;
958
+ if (key.startsWith("data-v-")) return;
959
+ const has = this.hasAttribute(key);
960
+ let value = has ? this.getAttribute(key) : REMOVAL;
861
961
  const camelKey = shared.camelize(key);
862
- if (this._numberProps && this._numberProps[camelKey]) {
962
+ if (has && this._numberProps && this._numberProps[camelKey]) {
863
963
  value = shared.toNumber(value);
864
964
  }
865
- this._setProp(camelKey, value, false);
965
+ this._setProp(camelKey, value, false, true);
866
966
  }
867
967
  /**
868
968
  * @internal
@@ -873,9 +973,13 @@ class VueElement extends BaseClass {
873
973
  /**
874
974
  * @internal
875
975
  */
876
- _setProp(key, val, shouldReflect = true, shouldUpdate = true) {
976
+ _setProp(key, val, shouldReflect = true, shouldUpdate = false) {
877
977
  if (val !== this._props[key]) {
878
- this._props[key] = val;
978
+ if (val === REMOVAL) {
979
+ delete this._props[key];
980
+ } else {
981
+ this._props[key] = val;
982
+ }
879
983
  if (shouldUpdate && this._instance) {
880
984
  this._update();
881
985
  }
@@ -891,18 +995,23 @@ class VueElement extends BaseClass {
891
995
  }
892
996
  }
893
997
  _update() {
894
- render(this._createVNode(), this.shadowRoot);
998
+ render(this._createVNode(), this._root);
895
999
  }
896
1000
  _createVNode() {
897
- const vnode = runtimeCore.createVNode(this._def, shared.extend({}, this._props));
1001
+ const baseProps = {};
1002
+ if (!this.shadowRoot) {
1003
+ baseProps.onVnodeMounted = baseProps.onVnodeUpdated = this._renderSlots.bind(this);
1004
+ }
1005
+ const vnode = runtimeCore.createVNode(this._def, shared.extend(baseProps, this._props));
898
1006
  if (!this._instance) {
899
1007
  vnode.ce = (instance) => {
900
1008
  this._instance = instance;
1009
+ instance.ce = this;
901
1010
  instance.isCE = true;
902
1011
  {
903
1012
  instance.ceReload = (newStyles) => {
904
1013
  if (this._styles) {
905
- this._styles.forEach((s) => this.shadowRoot.removeChild(s));
1014
+ this._styles.forEach((s) => this._root.removeChild(s));
906
1015
  this._styles.length = 0;
907
1016
  }
908
1017
  this._applyStyles(newStyles);
@@ -912,9 +1021,10 @@ class VueElement extends BaseClass {
912
1021
  }
913
1022
  const dispatch = (event, args) => {
914
1023
  this.dispatchEvent(
915
- new CustomEvent(event, {
916
- detail: args
917
- })
1024
+ new CustomEvent(
1025
+ event,
1026
+ shared.isPlainObject(args[0]) ? shared.extend({ detail: args }, args[0]) : { detail: args }
1027
+ )
918
1028
  );
919
1029
  };
920
1030
  instance.emit = (event, ...args) => {
@@ -923,30 +1033,126 @@ class VueElement extends BaseClass {
923
1033
  dispatch(shared.hyphenate(event), args);
924
1034
  }
925
1035
  };
926
- let parent = this;
927
- while (parent = parent && (parent.parentNode || parent.host)) {
928
- if (parent instanceof VueElement) {
929
- instance.parent = parent._instance;
930
- instance.provides = parent._instance.provides;
931
- break;
932
- }
933
- }
1036
+ this._setParent();
934
1037
  };
935
1038
  }
936
1039
  return vnode;
937
1040
  }
938
- _applyStyles(styles) {
939
- if (styles) {
940
- styles.forEach((css) => {
941
- const s = document.createElement("style");
942
- s.textContent = css;
943
- this.shadowRoot.appendChild(s);
944
- {
1041
+ _applyStyles(styles, owner) {
1042
+ if (!styles) return;
1043
+ if (owner) {
1044
+ if (owner === this._def || this._styleChildren.has(owner)) {
1045
+ return;
1046
+ }
1047
+ this._styleChildren.add(owner);
1048
+ }
1049
+ const nonce = this._nonce;
1050
+ for (let i = styles.length - 1; i >= 0; i--) {
1051
+ const s = document.createElement("style");
1052
+ if (nonce) s.setAttribute("nonce", nonce);
1053
+ s.textContent = styles[i];
1054
+ this.shadowRoot.prepend(s);
1055
+ {
1056
+ if (owner) {
1057
+ if (owner.__hmrId) {
1058
+ if (!this._childStyles) this._childStyles = /* @__PURE__ */ new Map();
1059
+ let entry = this._childStyles.get(owner.__hmrId);
1060
+ if (!entry) {
1061
+ this._childStyles.set(owner.__hmrId, entry = []);
1062
+ }
1063
+ entry.push(s);
1064
+ }
1065
+ } else {
945
1066
  (this._styles || (this._styles = [])).push(s);
946
1067
  }
947
- });
1068
+ }
1069
+ }
1070
+ }
1071
+ /**
1072
+ * Only called when shaddowRoot is false
1073
+ */
1074
+ _parseSlots() {
1075
+ const slots = this._slots = {};
1076
+ let n;
1077
+ while (n = this.firstChild) {
1078
+ const slotName = n.nodeType === 1 && n.getAttribute("slot") || "default";
1079
+ (slots[slotName] || (slots[slotName] = [])).push(n);
1080
+ this.removeChild(n);
1081
+ }
1082
+ }
1083
+ /**
1084
+ * Only called when shaddowRoot is false
1085
+ */
1086
+ _renderSlots() {
1087
+ const outlets = this.querySelectorAll("slot");
1088
+ const scopeId = this._instance.type.__scopeId;
1089
+ for (let i = 0; i < outlets.length; i++) {
1090
+ const o = outlets[i];
1091
+ const slotName = o.getAttribute("name") || "default";
1092
+ const content = this._slots[slotName];
1093
+ const parent = o.parentNode;
1094
+ if (content) {
1095
+ for (const n of content) {
1096
+ if (scopeId && n.nodeType === 1) {
1097
+ const id = scopeId + "-s";
1098
+ const walker = document.createTreeWalker(n, 1);
1099
+ n.setAttribute(id, "");
1100
+ let child;
1101
+ while (child = walker.nextNode()) {
1102
+ child.setAttribute(id, "");
1103
+ }
1104
+ }
1105
+ parent.insertBefore(n, o);
1106
+ }
1107
+ } else {
1108
+ while (o.firstChild) parent.insertBefore(o.firstChild, o);
1109
+ }
1110
+ parent.removeChild(o);
1111
+ }
1112
+ }
1113
+ /**
1114
+ * @internal
1115
+ */
1116
+ _injectChildStyle(comp) {
1117
+ this._applyStyles(comp.styles, comp);
1118
+ }
1119
+ /**
1120
+ * @internal
1121
+ */
1122
+ _removeChildStyle(comp) {
1123
+ {
1124
+ this._styleChildren.delete(comp);
1125
+ if (this._childStyles && comp.__hmrId) {
1126
+ const oldStyles = this._childStyles.get(comp.__hmrId);
1127
+ if (oldStyles) {
1128
+ oldStyles.forEach((s) => this._root.removeChild(s));
1129
+ oldStyles.length = 0;
1130
+ }
1131
+ }
1132
+ }
1133
+ }
1134
+ }
1135
+ function useHost(caller) {
1136
+ const instance = runtimeCore.getCurrentInstance();
1137
+ const el = instance && instance.ce;
1138
+ if (el) {
1139
+ return el;
1140
+ } else {
1141
+ if (!instance) {
1142
+ runtimeCore.warn(
1143
+ `${caller || "useHost"} called without an active component instance.`
1144
+ );
1145
+ } else {
1146
+ runtimeCore.warn(
1147
+ `${caller || "useHost"} can only be used in components defined via defineCustomElement.`
1148
+ );
948
1149
  }
949
1150
  }
1151
+ return null;
1152
+ }
1153
+ function useShadowRoot() {
1154
+ const el = useHost("useShadowRoot") ;
1155
+ return el && el.shadowRoot;
950
1156
  }
951
1157
 
952
1158
  function useCssModule(name = "$style") {
@@ -1461,7 +1667,9 @@ const createApp = (...args) => {
1461
1667
  if (!shared.isFunction(component) && !component.render && !component.template) {
1462
1668
  component.template = container.innerHTML;
1463
1669
  }
1464
- container.innerHTML = "";
1670
+ if (container.nodeType === 1) {
1671
+ container.textContent = "";
1672
+ }
1465
1673
  const proxy = mount(container, false, resolveRootNamespace(container));
1466
1674
  if (container instanceof Element) {
1467
1675
  container.removeAttribute("v-cloak");
@@ -1567,6 +1775,8 @@ exports.initDirectivesForSSR = initDirectivesForSSR;
1567
1775
  exports.render = render;
1568
1776
  exports.useCssModule = useCssModule;
1569
1777
  exports.useCssVars = useCssVars;
1778
+ exports.useHost = useHost;
1779
+ exports.useShadowRoot = useShadowRoot;
1570
1780
  exports.vModelCheckbox = vModelCheckbox;
1571
1781
  exports.vModelDynamic = vModelDynamic;
1572
1782
  exports.vModelRadio = vModelRadio;