compiler-dom.esm-bundler.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. import { registerRuntimeHelpers, isBuiltInType, createSimpleExpression, createCompilerError, createObjectProperty, getConstantType, createCallExpression, TO_DISPLAY_STRING, transformModel as transformModel$1, findProp, hasDynamicKeyVBind, transformOn as transformOn$1, isStaticExp, createCompoundExpression, checkCompatEnabled, noopDirectiveTransform, baseCompile, baseParse } from '@vue/compiler-core';
  2. export * from '@vue/compiler-core';
  3. import { isVoidTag, isHTMLTag, isSVGTag, makeMap, parseStringStyle, capitalize, extend } from '@vue/shared';
  4. const V_MODEL_RADIO = Symbol(!!(process.env.NODE_ENV !== "production") ? `vModelRadio` : ``);
  5. const V_MODEL_CHECKBOX = Symbol(!!(process.env.NODE_ENV !== "production") ? `vModelCheckbox` : ``);
  6. const V_MODEL_TEXT = Symbol(!!(process.env.NODE_ENV !== "production") ? `vModelText` : ``);
  7. const V_MODEL_SELECT = Symbol(!!(process.env.NODE_ENV !== "production") ? `vModelSelect` : ``);
  8. const V_MODEL_DYNAMIC = Symbol(!!(process.env.NODE_ENV !== "production") ? `vModelDynamic` : ``);
  9. const V_ON_WITH_MODIFIERS = Symbol(!!(process.env.NODE_ENV !== "production") ? `vOnModifiersGuard` : ``);
  10. const V_ON_WITH_KEYS = Symbol(!!(process.env.NODE_ENV !== "production") ? `vOnKeysGuard` : ``);
  11. const V_SHOW = Symbol(!!(process.env.NODE_ENV !== "production") ? `vShow` : ``);
  12. const TRANSITION = Symbol(!!(process.env.NODE_ENV !== "production") ? `Transition` : ``);
  13. const TRANSITION_GROUP = Symbol(!!(process.env.NODE_ENV !== "production") ? `TransitionGroup` : ``);
  14. registerRuntimeHelpers({
  15. [V_MODEL_RADIO]: `vModelRadio`,
  16. [V_MODEL_CHECKBOX]: `vModelCheckbox`,
  17. [V_MODEL_TEXT]: `vModelText`,
  18. [V_MODEL_SELECT]: `vModelSelect`,
  19. [V_MODEL_DYNAMIC]: `vModelDynamic`,
  20. [V_ON_WITH_MODIFIERS]: `withModifiers`,
  21. [V_ON_WITH_KEYS]: `withKeys`,
  22. [V_SHOW]: `vShow`,
  23. [TRANSITION]: `Transition`,
  24. [TRANSITION_GROUP]: `TransitionGroup`
  25. });
  26. let decoder;
  27. function decodeHtmlBrowser(raw, asAttr = false) {
  28. if (!decoder) {
  29. decoder = document.createElement("div");
  30. }
  31. if (asAttr) {
  32. decoder.innerHTML = `<div foo="${raw.replace(/"/g, "&quot;")}">`;
  33. return decoder.children[0].getAttribute("foo");
  34. } else {
  35. decoder.innerHTML = raw;
  36. return decoder.textContent;
  37. }
  38. }
  39. const isRawTextContainer = /* @__PURE__ */ makeMap(
  40. "style,iframe,script,noscript",
  41. true
  42. );
  43. const parserOptions = {
  44. isVoidTag,
  45. isNativeTag: (tag) => isHTMLTag(tag) || isSVGTag(tag),
  46. isPreTag: (tag) => tag === "pre",
  47. decodeEntities: decodeHtmlBrowser ,
  48. isBuiltInComponent: (tag) => {
  49. if (isBuiltInType(tag, `Transition`)) {
  50. return TRANSITION;
  51. } else if (isBuiltInType(tag, `TransitionGroup`)) {
  52. return TRANSITION_GROUP;
  53. }
  54. },
  55. // https://html.spec.whatwg.org/multipage/parsing.html#tree-construction-dispatcher
  56. getNamespace(tag, parent) {
  57. let ns = parent ? parent.ns : 0;
  58. if (parent && ns === 2) {
  59. if (parent.tag === "annotation-xml") {
  60. if (tag === "svg") {
  61. return 1;
  62. }
  63. if (parent.props.some(
  64. (a) => a.type === 6 && a.name === "encoding" && a.value != null && (a.value.content === "text/html" || a.value.content === "application/xhtml+xml")
  65. )) {
  66. ns = 0;
  67. }
  68. } else if (/^m(?:[ions]|text)$/.test(parent.tag) && tag !== "mglyph" && tag !== "malignmark") {
  69. ns = 0;
  70. }
  71. } else if (parent && ns === 1) {
  72. if (parent.tag === "foreignObject" || parent.tag === "desc" || parent.tag === "title") {
  73. ns = 0;
  74. }
  75. }
  76. if (ns === 0) {
  77. if (tag === "svg") {
  78. return 1;
  79. }
  80. if (tag === "math") {
  81. return 2;
  82. }
  83. }
  84. return ns;
  85. },
  86. // https://html.spec.whatwg.org/multipage/parsing.html#parsing-html-fragments
  87. getTextMode({ tag, ns }) {
  88. if (ns === 0) {
  89. if (tag === "textarea" || tag === "title") {
  90. return 1;
  91. }
  92. if (isRawTextContainer(tag)) {
  93. return 2;
  94. }
  95. }
  96. return 0;
  97. }
  98. };
  99. const transformStyle = (node) => {
  100. if (node.type === 1) {
  101. node.props.forEach((p, i) => {
  102. if (p.type === 6 && p.name === "style" && p.value) {
  103. node.props[i] = {
  104. type: 7,
  105. name: `bind`,
  106. arg: createSimpleExpression(`style`, true, p.loc),
  107. exp: parseInlineCSS(p.value.content, p.loc),
  108. modifiers: [],
  109. loc: p.loc
  110. };
  111. }
  112. });
  113. }
  114. };
  115. const parseInlineCSS = (cssText, loc) => {
  116. const normalized = parseStringStyle(cssText);
  117. return createSimpleExpression(
  118. JSON.stringify(normalized),
  119. false,
  120. loc,
  121. 3
  122. );
  123. };
  124. function createDOMCompilerError(code, loc) {
  125. return createCompilerError(
  126. code,
  127. loc,
  128. !!(process.env.NODE_ENV !== "production") || false ? DOMErrorMessages : void 0
  129. );
  130. }
  131. const DOMErrorMessages = {
  132. [53]: `v-html is missing expression.`,
  133. [54]: `v-html will override element children.`,
  134. [55]: `v-text is missing expression.`,
  135. [56]: `v-text will override element children.`,
  136. [57]: `v-model can only be used on <input>, <textarea> and <select> elements.`,
  137. [58]: `v-model argument is not supported on plain elements.`,
  138. [59]: `v-model cannot be used on file inputs since they are read-only. Use a v-on:change listener instead.`,
  139. [60]: `Unnecessary value binding used alongside v-model. It will interfere with v-model's behavior.`,
  140. [61]: `v-show is missing expression.`,
  141. [62]: `<Transition> expects exactly one child element or component.`,
  142. [63]: `Tags with side effect (<script> and <style>) are ignored in client component templates.`
  143. };
  144. const transformVHtml = (dir, node, context) => {
  145. const { exp, loc } = dir;
  146. if (!exp) {
  147. context.onError(
  148. createDOMCompilerError(53, loc)
  149. );
  150. }
  151. if (node.children.length) {
  152. context.onError(
  153. createDOMCompilerError(54, loc)
  154. );
  155. node.children.length = 0;
  156. }
  157. return {
  158. props: [
  159. createObjectProperty(
  160. createSimpleExpression(`innerHTML`, true, loc),
  161. exp || createSimpleExpression("", true)
  162. )
  163. ]
  164. };
  165. };
  166. const transformVText = (dir, node, context) => {
  167. const { exp, loc } = dir;
  168. if (!exp) {
  169. context.onError(
  170. createDOMCompilerError(55, loc)
  171. );
  172. }
  173. if (node.children.length) {
  174. context.onError(
  175. createDOMCompilerError(56, loc)
  176. );
  177. node.children.length = 0;
  178. }
  179. return {
  180. props: [
  181. createObjectProperty(
  182. createSimpleExpression(`textContent`, true),
  183. exp ? getConstantType(exp, context) > 0 ? exp : createCallExpression(
  184. context.helperString(TO_DISPLAY_STRING),
  185. [exp],
  186. loc
  187. ) : createSimpleExpression("", true)
  188. )
  189. ]
  190. };
  191. };
  192. const transformModel = (dir, node, context) => {
  193. const baseResult = transformModel$1(dir, node, context);
  194. if (!baseResult.props.length || node.tagType === 1) {
  195. return baseResult;
  196. }
  197. if (dir.arg) {
  198. context.onError(
  199. createDOMCompilerError(
  200. 58,
  201. dir.arg.loc
  202. )
  203. );
  204. }
  205. function checkDuplicatedValue() {
  206. const value = findProp(node, "value");
  207. if (value) {
  208. context.onError(
  209. createDOMCompilerError(
  210. 60,
  211. value.loc
  212. )
  213. );
  214. }
  215. }
  216. const { tag } = node;
  217. const isCustomElement = context.isCustomElement(tag);
  218. if (tag === "input" || tag === "textarea" || tag === "select" || isCustomElement) {
  219. let directiveToUse = V_MODEL_TEXT;
  220. let isInvalidType = false;
  221. if (tag === "input" || isCustomElement) {
  222. const type = findProp(node, `type`);
  223. if (type) {
  224. if (type.type === 7) {
  225. directiveToUse = V_MODEL_DYNAMIC;
  226. } else if (type.value) {
  227. switch (type.value.content) {
  228. case "radio":
  229. directiveToUse = V_MODEL_RADIO;
  230. break;
  231. case "checkbox":
  232. directiveToUse = V_MODEL_CHECKBOX;
  233. break;
  234. case "file":
  235. isInvalidType = true;
  236. context.onError(
  237. createDOMCompilerError(
  238. 59,
  239. dir.loc
  240. )
  241. );
  242. break;
  243. default:
  244. !!(process.env.NODE_ENV !== "production") && checkDuplicatedValue();
  245. break;
  246. }
  247. }
  248. } else if (hasDynamicKeyVBind(node)) {
  249. directiveToUse = V_MODEL_DYNAMIC;
  250. } else {
  251. !!(process.env.NODE_ENV !== "production") && checkDuplicatedValue();
  252. }
  253. } else if (tag === "select") {
  254. directiveToUse = V_MODEL_SELECT;
  255. } else {
  256. !!(process.env.NODE_ENV !== "production") && checkDuplicatedValue();
  257. }
  258. if (!isInvalidType) {
  259. baseResult.needRuntime = context.helper(directiveToUse);
  260. }
  261. } else {
  262. context.onError(
  263. createDOMCompilerError(
  264. 57,
  265. dir.loc
  266. )
  267. );
  268. }
  269. baseResult.props = baseResult.props.filter(
  270. (p) => !(p.key.type === 4 && p.key.content === "modelValue")
  271. );
  272. return baseResult;
  273. };
  274. const isEventOptionModifier = /* @__PURE__ */ makeMap(`passive,once,capture`);
  275. const isNonKeyModifier = /* @__PURE__ */ makeMap(
  276. // event propagation management
  277. `stop,prevent,self,ctrl,shift,alt,meta,exact,middle`
  278. );
  279. const maybeKeyModifier = /* @__PURE__ */ makeMap("left,right");
  280. const isKeyboardEvent = /* @__PURE__ */ makeMap(
  281. `onkeyup,onkeydown,onkeypress`,
  282. true
  283. );
  284. const resolveModifiers = (key, modifiers, context, loc) => {
  285. const keyModifiers = [];
  286. const nonKeyModifiers = [];
  287. const eventOptionModifiers = [];
  288. for (let i = 0; i < modifiers.length; i++) {
  289. const modifier = modifiers[i];
  290. if (modifier === "native" && checkCompatEnabled(
  291. "COMPILER_V_ON_NATIVE",
  292. context,
  293. loc
  294. )) {
  295. eventOptionModifiers.push(modifier);
  296. } else if (isEventOptionModifier(modifier)) {
  297. eventOptionModifiers.push(modifier);
  298. } else {
  299. if (maybeKeyModifier(modifier)) {
  300. if (isStaticExp(key)) {
  301. if (isKeyboardEvent(key.content)) {
  302. keyModifiers.push(modifier);
  303. } else {
  304. nonKeyModifiers.push(modifier);
  305. }
  306. } else {
  307. keyModifiers.push(modifier);
  308. nonKeyModifiers.push(modifier);
  309. }
  310. } else {
  311. if (isNonKeyModifier(modifier)) {
  312. nonKeyModifiers.push(modifier);
  313. } else {
  314. keyModifiers.push(modifier);
  315. }
  316. }
  317. }
  318. }
  319. return {
  320. keyModifiers,
  321. nonKeyModifiers,
  322. eventOptionModifiers
  323. };
  324. };
  325. const transformClick = (key, event) => {
  326. const isStaticClick = isStaticExp(key) && key.content.toLowerCase() === "onclick";
  327. return isStaticClick ? createSimpleExpression(event, true) : key.type !== 4 ? createCompoundExpression([
  328. `(`,
  329. key,
  330. `) === "onClick" ? "${event}" : (`,
  331. key,
  332. `)`
  333. ]) : key;
  334. };
  335. const transformOn = (dir, node, context) => {
  336. return transformOn$1(dir, node, context, (baseResult) => {
  337. const { modifiers } = dir;
  338. if (!modifiers.length)
  339. return baseResult;
  340. let { key, value: handlerExp } = baseResult.props[0];
  341. const { keyModifiers, nonKeyModifiers, eventOptionModifiers } = resolveModifiers(key, modifiers, context, dir.loc);
  342. if (nonKeyModifiers.includes("right")) {
  343. key = transformClick(key, `onContextmenu`);
  344. }
  345. if (nonKeyModifiers.includes("middle")) {
  346. key = transformClick(key, `onMouseup`);
  347. }
  348. if (nonKeyModifiers.length) {
  349. handlerExp = createCallExpression(context.helper(V_ON_WITH_MODIFIERS), [
  350. handlerExp,
  351. JSON.stringify(nonKeyModifiers)
  352. ]);
  353. }
  354. if (keyModifiers.length && // if event name is dynamic, always wrap with keys guard
  355. (!isStaticExp(key) || isKeyboardEvent(key.content))) {
  356. handlerExp = createCallExpression(context.helper(V_ON_WITH_KEYS), [
  357. handlerExp,
  358. JSON.stringify(keyModifiers)
  359. ]);
  360. }
  361. if (eventOptionModifiers.length) {
  362. const modifierPostfix = eventOptionModifiers.map(capitalize).join("");
  363. key = isStaticExp(key) ? createSimpleExpression(`${key.content}${modifierPostfix}`, true) : createCompoundExpression([`(`, key, `) + "${modifierPostfix}"`]);
  364. }
  365. return {
  366. props: [createObjectProperty(key, handlerExp)]
  367. };
  368. });
  369. };
  370. const transformShow = (dir, node, context) => {
  371. const { exp, loc } = dir;
  372. if (!exp) {
  373. context.onError(
  374. createDOMCompilerError(61, loc)
  375. );
  376. }
  377. return {
  378. props: [],
  379. needRuntime: context.helper(V_SHOW)
  380. };
  381. };
  382. const transformTransition = (node, context) => {
  383. if (node.type === 1 && node.tagType === 1) {
  384. const component = context.isBuiltInComponent(node.tag);
  385. if (component === TRANSITION) {
  386. return () => {
  387. if (!node.children.length) {
  388. return;
  389. }
  390. if (hasMultipleChildren(node)) {
  391. context.onError(
  392. createDOMCompilerError(
  393. 62,
  394. {
  395. start: node.children[0].loc.start,
  396. end: node.children[node.children.length - 1].loc.end,
  397. source: ""
  398. }
  399. )
  400. );
  401. }
  402. const child = node.children[0];
  403. if (child.type === 1) {
  404. for (const p of child.props) {
  405. if (p.type === 7 && p.name === "show") {
  406. node.props.push({
  407. type: 6,
  408. name: "persisted",
  409. value: void 0,
  410. loc: node.loc
  411. });
  412. }
  413. }
  414. }
  415. };
  416. }
  417. }
  418. };
  419. function hasMultipleChildren(node) {
  420. const children = node.children = node.children.filter(
  421. (c) => c.type !== 3 && !(c.type === 2 && !c.content.trim())
  422. );
  423. const child = children[0];
  424. return children.length !== 1 || child.type === 11 || child.type === 9 && child.branches.some(hasMultipleChildren);
  425. }
  426. const ignoreSideEffectTags = (node, context) => {
  427. if (node.type === 1 && node.tagType === 0 && (node.tag === "script" || node.tag === "style")) {
  428. !!(process.env.NODE_ENV !== "production") && context.onError(
  429. createDOMCompilerError(
  430. 63,
  431. node.loc
  432. )
  433. );
  434. context.removeNode();
  435. }
  436. };
  437. const DOMNodeTransforms = [
  438. transformStyle,
  439. ...!!(process.env.NODE_ENV !== "production") ? [transformTransition] : []
  440. ];
  441. const DOMDirectiveTransforms = {
  442. cloak: noopDirectiveTransform,
  443. html: transformVHtml,
  444. text: transformVText,
  445. model: transformModel,
  446. // override compiler-core
  447. on: transformOn,
  448. // override compiler-core
  449. show: transformShow
  450. };
  451. function compile(template, options = {}) {
  452. return baseCompile(
  453. template,
  454. extend({}, parserOptions, options, {
  455. nodeTransforms: [
  456. // ignore <script> and <tag>
  457. // this is not put inside DOMNodeTransforms because that list is used
  458. // by compiler-ssr to generate vnode fallback branches
  459. ignoreSideEffectTags,
  460. ...DOMNodeTransforms,
  461. ...options.nodeTransforms || []
  462. ],
  463. directiveTransforms: extend(
  464. {},
  465. DOMDirectiveTransforms,
  466. options.directiveTransforms || {}
  467. ),
  468. transformHoist: null
  469. })
  470. );
  471. }
  472. function parse(template, options = {}) {
  473. return baseParse(template, extend({}, parserOptions, options));
  474. }
  475. export { DOMDirectiveTransforms, DOMNodeTransforms, TRANSITION, TRANSITION_GROUP, V_MODEL_CHECKBOX, V_MODEL_DYNAMIC, V_MODEL_RADIO, V_MODEL_SELECT, V_MODEL_TEXT, V_ON_WITH_KEYS, V_ON_WITH_MODIFIERS, V_SHOW, compile, createDOMCompilerError, parse, parserOptions, transformStyle };