compiler-dom.cjs.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /**
  2. * @vue/compiler-dom v3.5.12
  3. * (c) 2018-present Yuxi (Evan) You and Vue contributors
  4. * @license MIT
  5. **/
  6. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. var compilerCore = require('@vue/compiler-core');
  9. var shared = require('@vue/shared');
  10. const V_MODEL_RADIO = Symbol(`vModelRadio` );
  11. const V_MODEL_CHECKBOX = Symbol(
  12. `vModelCheckbox`
  13. );
  14. const V_MODEL_TEXT = Symbol(`vModelText` );
  15. const V_MODEL_SELECT = Symbol(
  16. `vModelSelect`
  17. );
  18. const V_MODEL_DYNAMIC = Symbol(
  19. `vModelDynamic`
  20. );
  21. const V_ON_WITH_MODIFIERS = Symbol(
  22. `vOnModifiersGuard`
  23. );
  24. const V_ON_WITH_KEYS = Symbol(
  25. `vOnKeysGuard`
  26. );
  27. const V_SHOW = Symbol(`vShow` );
  28. const TRANSITION = Symbol(`Transition` );
  29. const TRANSITION_GROUP = Symbol(
  30. `TransitionGroup`
  31. );
  32. compilerCore.registerRuntimeHelpers({
  33. [V_MODEL_RADIO]: `vModelRadio`,
  34. [V_MODEL_CHECKBOX]: `vModelCheckbox`,
  35. [V_MODEL_TEXT]: `vModelText`,
  36. [V_MODEL_SELECT]: `vModelSelect`,
  37. [V_MODEL_DYNAMIC]: `vModelDynamic`,
  38. [V_ON_WITH_MODIFIERS]: `withModifiers`,
  39. [V_ON_WITH_KEYS]: `withKeys`,
  40. [V_SHOW]: `vShow`,
  41. [TRANSITION]: `Transition`,
  42. [TRANSITION_GROUP]: `TransitionGroup`
  43. });
  44. const parserOptions = {
  45. parseMode: "html",
  46. isVoidTag: shared.isVoidTag,
  47. isNativeTag: (tag) => shared.isHTMLTag(tag) || shared.isSVGTag(tag) || shared.isMathMLTag(tag),
  48. isPreTag: (tag) => tag === "pre",
  49. isIgnoreNewlineTag: (tag) => tag === "pre" || tag === "textarea",
  50. decodeEntities: void 0,
  51. isBuiltInComponent: (tag) => {
  52. if (tag === "Transition" || tag === "transition") {
  53. return TRANSITION;
  54. } else if (tag === "TransitionGroup" || tag === "transition-group") {
  55. return TRANSITION_GROUP;
  56. }
  57. },
  58. // https://html.spec.whatwg.org/multipage/parsing.html#tree-construction-dispatcher
  59. getNamespace(tag, parent, rootNamespace) {
  60. let ns = parent ? parent.ns : rootNamespace;
  61. if (parent && ns === 2) {
  62. if (parent.tag === "annotation-xml") {
  63. if (tag === "svg") {
  64. return 1;
  65. }
  66. if (parent.props.some(
  67. (a) => a.type === 6 && a.name === "encoding" && a.value != null && (a.value.content === "text/html" || a.value.content === "application/xhtml+xml")
  68. )) {
  69. ns = 0;
  70. }
  71. } else if (/^m(?:[ions]|text)$/.test(parent.tag) && tag !== "mglyph" && tag !== "malignmark") {
  72. ns = 0;
  73. }
  74. } else if (parent && ns === 1) {
  75. if (parent.tag === "foreignObject" || parent.tag === "desc" || parent.tag === "title") {
  76. ns = 0;
  77. }
  78. }
  79. if (ns === 0) {
  80. if (tag === "svg") {
  81. return 1;
  82. }
  83. if (tag === "math") {
  84. return 2;
  85. }
  86. }
  87. return ns;
  88. }
  89. };
  90. const transformStyle = (node) => {
  91. if (node.type === 1) {
  92. node.props.forEach((p, i) => {
  93. if (p.type === 6 && p.name === "style" && p.value) {
  94. node.props[i] = {
  95. type: 7,
  96. name: `bind`,
  97. arg: compilerCore.createSimpleExpression(`style`, true, p.loc),
  98. exp: parseInlineCSS(p.value.content, p.loc),
  99. modifiers: [],
  100. loc: p.loc
  101. };
  102. }
  103. });
  104. }
  105. };
  106. const parseInlineCSS = (cssText, loc) => {
  107. const normalized = shared.parseStringStyle(cssText);
  108. return compilerCore.createSimpleExpression(
  109. JSON.stringify(normalized),
  110. false,
  111. loc,
  112. 3
  113. );
  114. };
  115. function createDOMCompilerError(code, loc) {
  116. return compilerCore.createCompilerError(
  117. code,
  118. loc,
  119. DOMErrorMessages
  120. );
  121. }
  122. const DOMErrorCodes = {
  123. "X_V_HTML_NO_EXPRESSION": 53,
  124. "53": "X_V_HTML_NO_EXPRESSION",
  125. "X_V_HTML_WITH_CHILDREN": 54,
  126. "54": "X_V_HTML_WITH_CHILDREN",
  127. "X_V_TEXT_NO_EXPRESSION": 55,
  128. "55": "X_V_TEXT_NO_EXPRESSION",
  129. "X_V_TEXT_WITH_CHILDREN": 56,
  130. "56": "X_V_TEXT_WITH_CHILDREN",
  131. "X_V_MODEL_ON_INVALID_ELEMENT": 57,
  132. "57": "X_V_MODEL_ON_INVALID_ELEMENT",
  133. "X_V_MODEL_ARG_ON_ELEMENT": 58,
  134. "58": "X_V_MODEL_ARG_ON_ELEMENT",
  135. "X_V_MODEL_ON_FILE_INPUT_ELEMENT": 59,
  136. "59": "X_V_MODEL_ON_FILE_INPUT_ELEMENT",
  137. "X_V_MODEL_UNNECESSARY_VALUE": 60,
  138. "60": "X_V_MODEL_UNNECESSARY_VALUE",
  139. "X_V_SHOW_NO_EXPRESSION": 61,
  140. "61": "X_V_SHOW_NO_EXPRESSION",
  141. "X_TRANSITION_INVALID_CHILDREN": 62,
  142. "62": "X_TRANSITION_INVALID_CHILDREN",
  143. "X_IGNORED_SIDE_EFFECT_TAG": 63,
  144. "63": "X_IGNORED_SIDE_EFFECT_TAG",
  145. "__EXTEND_POINT__": 64,
  146. "64": "__EXTEND_POINT__"
  147. };
  148. const DOMErrorMessages = {
  149. [53]: `v-html is missing expression.`,
  150. [54]: `v-html will override element children.`,
  151. [55]: `v-text is missing expression.`,
  152. [56]: `v-text will override element children.`,
  153. [57]: `v-model can only be used on <input>, <textarea> and <select> elements.`,
  154. [58]: `v-model argument is not supported on plain elements.`,
  155. [59]: `v-model cannot be used on file inputs since they are read-only. Use a v-on:change listener instead.`,
  156. [60]: `Unnecessary value binding used alongside v-model. It will interfere with v-model's behavior.`,
  157. [61]: `v-show is missing expression.`,
  158. [62]: `<Transition> expects exactly one child element or component.`,
  159. [63]: `Tags with side effect (<script> and <style>) are ignored in client component templates.`
  160. };
  161. const transformVHtml = (dir, node, context) => {
  162. const { exp, loc } = dir;
  163. if (!exp) {
  164. context.onError(
  165. createDOMCompilerError(53, loc)
  166. );
  167. }
  168. if (node.children.length) {
  169. context.onError(
  170. createDOMCompilerError(54, loc)
  171. );
  172. node.children.length = 0;
  173. }
  174. return {
  175. props: [
  176. compilerCore.createObjectProperty(
  177. compilerCore.createSimpleExpression(`innerHTML`, true, loc),
  178. exp || compilerCore.createSimpleExpression("", true)
  179. )
  180. ]
  181. };
  182. };
  183. const transformVText = (dir, node, context) => {
  184. const { exp, loc } = dir;
  185. if (!exp) {
  186. context.onError(
  187. createDOMCompilerError(55, loc)
  188. );
  189. }
  190. if (node.children.length) {
  191. context.onError(
  192. createDOMCompilerError(56, loc)
  193. );
  194. node.children.length = 0;
  195. }
  196. return {
  197. props: [
  198. compilerCore.createObjectProperty(
  199. compilerCore.createSimpleExpression(`textContent`, true),
  200. exp ? compilerCore.getConstantType(exp, context) > 0 ? exp : compilerCore.createCallExpression(
  201. context.helperString(compilerCore.TO_DISPLAY_STRING),
  202. [exp],
  203. loc
  204. ) : compilerCore.createSimpleExpression("", true)
  205. )
  206. ]
  207. };
  208. };
  209. const transformModel = (dir, node, context) => {
  210. const baseResult = compilerCore.transformModel(dir, node, context);
  211. if (!baseResult.props.length || node.tagType === 1) {
  212. return baseResult;
  213. }
  214. if (dir.arg) {
  215. context.onError(
  216. createDOMCompilerError(
  217. 58,
  218. dir.arg.loc
  219. )
  220. );
  221. }
  222. function checkDuplicatedValue() {
  223. const value = compilerCore.findDir(node, "bind");
  224. if (value && compilerCore.isStaticArgOf(value.arg, "value")) {
  225. context.onError(
  226. createDOMCompilerError(
  227. 60,
  228. value.loc
  229. )
  230. );
  231. }
  232. }
  233. const { tag } = node;
  234. const isCustomElement = context.isCustomElement(tag);
  235. if (tag === "input" || tag === "textarea" || tag === "select" || isCustomElement) {
  236. let directiveToUse = V_MODEL_TEXT;
  237. let isInvalidType = false;
  238. if (tag === "input" || isCustomElement) {
  239. const type = compilerCore.findProp(node, `type`);
  240. if (type) {
  241. if (type.type === 7) {
  242. directiveToUse = V_MODEL_DYNAMIC;
  243. } else if (type.value) {
  244. switch (type.value.content) {
  245. case "radio":
  246. directiveToUse = V_MODEL_RADIO;
  247. break;
  248. case "checkbox":
  249. directiveToUse = V_MODEL_CHECKBOX;
  250. break;
  251. case "file":
  252. isInvalidType = true;
  253. context.onError(
  254. createDOMCompilerError(
  255. 59,
  256. dir.loc
  257. )
  258. );
  259. break;
  260. default:
  261. checkDuplicatedValue();
  262. break;
  263. }
  264. }
  265. } else if (compilerCore.hasDynamicKeyVBind(node)) {
  266. directiveToUse = V_MODEL_DYNAMIC;
  267. } else {
  268. checkDuplicatedValue();
  269. }
  270. } else if (tag === "select") {
  271. directiveToUse = V_MODEL_SELECT;
  272. } else {
  273. checkDuplicatedValue();
  274. }
  275. if (!isInvalidType) {
  276. baseResult.needRuntime = context.helper(directiveToUse);
  277. }
  278. } else {
  279. context.onError(
  280. createDOMCompilerError(
  281. 57,
  282. dir.loc
  283. )
  284. );
  285. }
  286. baseResult.props = baseResult.props.filter(
  287. (p) => !(p.key.type === 4 && p.key.content === "modelValue")
  288. );
  289. return baseResult;
  290. };
  291. const isEventOptionModifier = /* @__PURE__ */ shared.makeMap(`passive,once,capture`);
  292. const isNonKeyModifier = /* @__PURE__ */ shared.makeMap(
  293. // event propagation management
  294. `stop,prevent,self,ctrl,shift,alt,meta,exact,middle`
  295. );
  296. const maybeKeyModifier = /* @__PURE__ */ shared.makeMap("left,right");
  297. const isKeyboardEvent = /* @__PURE__ */ shared.makeMap(`onkeyup,onkeydown,onkeypress`);
  298. const resolveModifiers = (key, modifiers, context, loc) => {
  299. const keyModifiers = [];
  300. const nonKeyModifiers = [];
  301. const eventOptionModifiers = [];
  302. for (let i = 0; i < modifiers.length; i++) {
  303. const modifier = modifiers[i].content;
  304. if (modifier === "native" && compilerCore.checkCompatEnabled(
  305. "COMPILER_V_ON_NATIVE",
  306. context,
  307. loc
  308. )) {
  309. eventOptionModifiers.push(modifier);
  310. } else if (isEventOptionModifier(modifier)) {
  311. eventOptionModifiers.push(modifier);
  312. } else {
  313. if (maybeKeyModifier(modifier)) {
  314. if (compilerCore.isStaticExp(key)) {
  315. if (isKeyboardEvent(key.content.toLowerCase())) {
  316. keyModifiers.push(modifier);
  317. } else {
  318. nonKeyModifiers.push(modifier);
  319. }
  320. } else {
  321. keyModifiers.push(modifier);
  322. nonKeyModifiers.push(modifier);
  323. }
  324. } else {
  325. if (isNonKeyModifier(modifier)) {
  326. nonKeyModifiers.push(modifier);
  327. } else {
  328. keyModifiers.push(modifier);
  329. }
  330. }
  331. }
  332. }
  333. return {
  334. keyModifiers,
  335. nonKeyModifiers,
  336. eventOptionModifiers
  337. };
  338. };
  339. const transformClick = (key, event) => {
  340. const isStaticClick = compilerCore.isStaticExp(key) && key.content.toLowerCase() === "onclick";
  341. return isStaticClick ? compilerCore.createSimpleExpression(event, true) : key.type !== 4 ? compilerCore.createCompoundExpression([
  342. `(`,
  343. key,
  344. `) === "onClick" ? "${event}" : (`,
  345. key,
  346. `)`
  347. ]) : key;
  348. };
  349. const transformOn = (dir, node, context) => {
  350. return compilerCore.transformOn(dir, node, context, (baseResult) => {
  351. const { modifiers } = dir;
  352. if (!modifiers.length) return baseResult;
  353. let { key, value: handlerExp } = baseResult.props[0];
  354. const { keyModifiers, nonKeyModifiers, eventOptionModifiers } = resolveModifiers(key, modifiers, context, dir.loc);
  355. if (nonKeyModifiers.includes("right")) {
  356. key = transformClick(key, `onContextmenu`);
  357. }
  358. if (nonKeyModifiers.includes("middle")) {
  359. key = transformClick(key, `onMouseup`);
  360. }
  361. if (nonKeyModifiers.length) {
  362. handlerExp = compilerCore.createCallExpression(context.helper(V_ON_WITH_MODIFIERS), [
  363. handlerExp,
  364. JSON.stringify(nonKeyModifiers)
  365. ]);
  366. }
  367. if (keyModifiers.length && // if event name is dynamic, always wrap with keys guard
  368. (!compilerCore.isStaticExp(key) || isKeyboardEvent(key.content.toLowerCase()))) {
  369. handlerExp = compilerCore.createCallExpression(context.helper(V_ON_WITH_KEYS), [
  370. handlerExp,
  371. JSON.stringify(keyModifiers)
  372. ]);
  373. }
  374. if (eventOptionModifiers.length) {
  375. const modifierPostfix = eventOptionModifiers.map(shared.capitalize).join("");
  376. key = compilerCore.isStaticExp(key) ? compilerCore.createSimpleExpression(`${key.content}${modifierPostfix}`, true) : compilerCore.createCompoundExpression([`(`, key, `) + "${modifierPostfix}"`]);
  377. }
  378. return {
  379. props: [compilerCore.createObjectProperty(key, handlerExp)]
  380. };
  381. });
  382. };
  383. const transformShow = (dir, node, context) => {
  384. const { exp, loc } = dir;
  385. if (!exp) {
  386. context.onError(
  387. createDOMCompilerError(61, loc)
  388. );
  389. }
  390. return {
  391. props: [],
  392. needRuntime: context.helper(V_SHOW)
  393. };
  394. };
  395. const transformTransition = (node, context) => {
  396. if (node.type === 1 && node.tagType === 1) {
  397. const component = context.isBuiltInComponent(node.tag);
  398. if (component === TRANSITION) {
  399. return () => {
  400. if (!node.children.length) {
  401. return;
  402. }
  403. if (hasMultipleChildren(node)) {
  404. context.onError(
  405. createDOMCompilerError(
  406. 62,
  407. {
  408. start: node.children[0].loc.start,
  409. end: node.children[node.children.length - 1].loc.end,
  410. source: ""
  411. }
  412. )
  413. );
  414. }
  415. const child = node.children[0];
  416. if (child.type === 1) {
  417. for (const p of child.props) {
  418. if (p.type === 7 && p.name === "show") {
  419. node.props.push({
  420. type: 6,
  421. name: "persisted",
  422. nameLoc: node.loc,
  423. value: void 0,
  424. loc: node.loc
  425. });
  426. }
  427. }
  428. }
  429. };
  430. }
  431. }
  432. };
  433. function hasMultipleChildren(node) {
  434. const children = node.children = node.children.filter(
  435. (c) => c.type !== 3 && !(c.type === 2 && !c.content.trim())
  436. );
  437. const child = children[0];
  438. return children.length !== 1 || child.type === 11 || child.type === 9 && child.branches.some(hasMultipleChildren);
  439. }
  440. const expReplaceRE = /__VUE_EXP_START__(.*?)__VUE_EXP_END__/g;
  441. const stringifyStatic = (children, context, parent) => {
  442. if (context.scopes.vSlot > 0) {
  443. return;
  444. }
  445. const isParentCached = parent.type === 1 && parent.codegenNode && parent.codegenNode.type === 13 && parent.codegenNode.children && !shared.isArray(parent.codegenNode.children) && parent.codegenNode.children.type === 20;
  446. let nc = 0;
  447. let ec = 0;
  448. const currentChunk = [];
  449. const stringifyCurrentChunk = (currentIndex) => {
  450. if (nc >= 20 || ec >= 5) {
  451. const staticCall = compilerCore.createCallExpression(context.helper(compilerCore.CREATE_STATIC), [
  452. JSON.stringify(
  453. currentChunk.map((node) => stringifyNode(node, context)).join("")
  454. ).replace(expReplaceRE, `" + $1 + "`),
  455. // the 2nd argument indicates the number of DOM nodes this static vnode
  456. // will insert / hydrate
  457. String(currentChunk.length)
  458. ]);
  459. const deleteCount = currentChunk.length - 1;
  460. if (isParentCached) {
  461. children.splice(
  462. currentIndex - currentChunk.length,
  463. currentChunk.length,
  464. // @ts-expect-error
  465. staticCall
  466. );
  467. } else {
  468. currentChunk[0].codegenNode.value = staticCall;
  469. if (currentChunk.length > 1) {
  470. children.splice(currentIndex - currentChunk.length + 1, deleteCount);
  471. const cacheIndex = context.cached.indexOf(
  472. currentChunk[currentChunk.length - 1].codegenNode
  473. );
  474. if (cacheIndex > -1) {
  475. for (let i2 = cacheIndex; i2 < context.cached.length; i2++) {
  476. const c = context.cached[i2];
  477. if (c) c.index -= deleteCount;
  478. }
  479. context.cached.splice(cacheIndex - deleteCount + 1, deleteCount);
  480. }
  481. }
  482. }
  483. return deleteCount;
  484. }
  485. return 0;
  486. };
  487. let i = 0;
  488. for (; i < children.length; i++) {
  489. const child = children[i];
  490. const isCached = isParentCached || getCachedNode(child);
  491. if (isCached) {
  492. const result = analyzeNode(child);
  493. if (result) {
  494. nc += result[0];
  495. ec += result[1];
  496. currentChunk.push(child);
  497. continue;
  498. }
  499. }
  500. i -= stringifyCurrentChunk(i);
  501. nc = 0;
  502. ec = 0;
  503. currentChunk.length = 0;
  504. }
  505. stringifyCurrentChunk(i);
  506. };
  507. const getCachedNode = (node) => {
  508. if ((node.type === 1 && node.tagType === 0 || node.type === 12) && node.codegenNode && node.codegenNode.type === 20) {
  509. return node.codegenNode;
  510. }
  511. };
  512. const dataAriaRE = /^(data|aria)-/;
  513. const isStringifiableAttr = (name, ns) => {
  514. return (ns === 0 ? shared.isKnownHtmlAttr(name) : ns === 1 ? shared.isKnownSvgAttr(name) : ns === 2 ? shared.isKnownMathMLAttr(name) : false) || dataAriaRE.test(name);
  515. };
  516. const isNonStringifiable = /* @__PURE__ */ shared.makeMap(
  517. `caption,thead,tr,th,tbody,td,tfoot,colgroup,col`
  518. );
  519. function analyzeNode(node) {
  520. if (node.type === 1 && isNonStringifiable(node.tag)) {
  521. return false;
  522. }
  523. if (node.type === 12) {
  524. return [1, 0];
  525. }
  526. let nc = 1;
  527. let ec = node.props.length > 0 ? 1 : 0;
  528. let bailed = false;
  529. const bail = () => {
  530. bailed = true;
  531. return false;
  532. };
  533. function walk(node2) {
  534. const isOptionTag = node2.tag === "option" && node2.ns === 0;
  535. for (let i = 0; i < node2.props.length; i++) {
  536. const p = node2.props[i];
  537. if (p.type === 6 && !isStringifiableAttr(p.name, node2.ns)) {
  538. return bail();
  539. }
  540. if (p.type === 7 && p.name === "bind") {
  541. if (p.arg && (p.arg.type === 8 || p.arg.isStatic && !isStringifiableAttr(p.arg.content, node2.ns))) {
  542. return bail();
  543. }
  544. if (p.exp && (p.exp.type === 8 || p.exp.constType < 3)) {
  545. return bail();
  546. }
  547. if (isOptionTag && compilerCore.isStaticArgOf(p.arg, "value") && p.exp && !p.exp.isStatic) {
  548. return bail();
  549. }
  550. }
  551. }
  552. for (let i = 0; i < node2.children.length; i++) {
  553. nc++;
  554. const child = node2.children[i];
  555. if (child.type === 1) {
  556. if (child.props.length > 0) {
  557. ec++;
  558. }
  559. walk(child);
  560. if (bailed) {
  561. return false;
  562. }
  563. }
  564. }
  565. return true;
  566. }
  567. return walk(node) ? [nc, ec] : false;
  568. }
  569. function stringifyNode(node, context) {
  570. if (shared.isString(node)) {
  571. return node;
  572. }
  573. if (shared.isSymbol(node)) {
  574. return ``;
  575. }
  576. switch (node.type) {
  577. case 1:
  578. return stringifyElement(node, context);
  579. case 2:
  580. return shared.escapeHtml(node.content);
  581. case 3:
  582. return `<!--${shared.escapeHtml(node.content)}-->`;
  583. case 5:
  584. return shared.escapeHtml(shared.toDisplayString(evaluateConstant(node.content)));
  585. case 8:
  586. return shared.escapeHtml(evaluateConstant(node));
  587. case 12:
  588. return stringifyNode(node.content, context);
  589. default:
  590. return "";
  591. }
  592. }
  593. function stringifyElement(node, context) {
  594. let res = `<${node.tag}`;
  595. let innerHTML = "";
  596. for (let i = 0; i < node.props.length; i++) {
  597. const p = node.props[i];
  598. if (p.type === 6) {
  599. res += ` ${p.name}`;
  600. if (p.value) {
  601. res += `="${shared.escapeHtml(p.value.content)}"`;
  602. }
  603. } else if (p.type === 7) {
  604. if (p.name === "bind") {
  605. const exp = p.exp;
  606. if (exp.content[0] === "_") {
  607. res += ` ${p.arg.content}="__VUE_EXP_START__${exp.content}__VUE_EXP_END__"`;
  608. continue;
  609. }
  610. if (shared.isBooleanAttr(p.arg.content) && exp.content === "false") {
  611. continue;
  612. }
  613. let evaluated = evaluateConstant(exp);
  614. if (evaluated != null) {
  615. const arg = p.arg && p.arg.content;
  616. if (arg === "class") {
  617. evaluated = shared.normalizeClass(evaluated);
  618. } else if (arg === "style") {
  619. evaluated = shared.stringifyStyle(shared.normalizeStyle(evaluated));
  620. }
  621. res += ` ${p.arg.content}="${shared.escapeHtml(
  622. evaluated
  623. )}"`;
  624. }
  625. } else if (p.name === "html") {
  626. innerHTML = evaluateConstant(p.exp);
  627. } else if (p.name === "text") {
  628. innerHTML = shared.escapeHtml(
  629. shared.toDisplayString(evaluateConstant(p.exp))
  630. );
  631. }
  632. }
  633. }
  634. if (context.scopeId) {
  635. res += ` ${context.scopeId}`;
  636. }
  637. res += `>`;
  638. if (innerHTML) {
  639. res += innerHTML;
  640. } else {
  641. for (let i = 0; i < node.children.length; i++) {
  642. res += stringifyNode(node.children[i], context);
  643. }
  644. }
  645. if (!shared.isVoidTag(node.tag)) {
  646. res += `</${node.tag}>`;
  647. }
  648. return res;
  649. }
  650. function evaluateConstant(exp) {
  651. if (exp.type === 4) {
  652. return new Function(`return (${exp.content})`)();
  653. } else {
  654. let res = ``;
  655. exp.children.forEach((c) => {
  656. if (shared.isString(c) || shared.isSymbol(c)) {
  657. return;
  658. }
  659. if (c.type === 2) {
  660. res += c.content;
  661. } else if (c.type === 5) {
  662. res += shared.toDisplayString(evaluateConstant(c.content));
  663. } else {
  664. res += evaluateConstant(c);
  665. }
  666. });
  667. return res;
  668. }
  669. }
  670. const ignoreSideEffectTags = (node, context) => {
  671. if (node.type === 1 && node.tagType === 0 && (node.tag === "script" || node.tag === "style")) {
  672. context.onError(
  673. createDOMCompilerError(
  674. 63,
  675. node.loc
  676. )
  677. );
  678. context.removeNode();
  679. }
  680. };
  681. function isValidHTMLNesting(parent, child) {
  682. if (parent in onlyValidChildren) {
  683. return onlyValidChildren[parent].has(child);
  684. }
  685. if (child in onlyValidParents) {
  686. return onlyValidParents[child].has(parent);
  687. }
  688. if (parent in knownInvalidChildren) {
  689. if (knownInvalidChildren[parent].has(child)) return false;
  690. }
  691. if (child in knownInvalidParents) {
  692. if (knownInvalidParents[child].has(parent)) return false;
  693. }
  694. return true;
  695. }
  696. const headings = /* @__PURE__ */ new Set(["h1", "h2", "h3", "h4", "h5", "h6"]);
  697. const emptySet = /* @__PURE__ */ new Set([]);
  698. const onlyValidChildren = {
  699. head: /* @__PURE__ */ new Set([
  700. "base",
  701. "basefront",
  702. "bgsound",
  703. "link",
  704. "meta",
  705. "title",
  706. "noscript",
  707. "noframes",
  708. "style",
  709. "script",
  710. "template"
  711. ]),
  712. optgroup: /* @__PURE__ */ new Set(["option"]),
  713. select: /* @__PURE__ */ new Set(["optgroup", "option", "hr"]),
  714. // table
  715. table: /* @__PURE__ */ new Set(["caption", "colgroup", "tbody", "tfoot", "thead"]),
  716. tr: /* @__PURE__ */ new Set(["td", "th"]),
  717. colgroup: /* @__PURE__ */ new Set(["col"]),
  718. tbody: /* @__PURE__ */ new Set(["tr"]),
  719. thead: /* @__PURE__ */ new Set(["tr"]),
  720. tfoot: /* @__PURE__ */ new Set(["tr"]),
  721. // these elements can not have any children elements
  722. script: emptySet,
  723. iframe: emptySet,
  724. option: emptySet,
  725. textarea: emptySet,
  726. style: emptySet,
  727. title: emptySet
  728. };
  729. const onlyValidParents = {
  730. // sections
  731. html: emptySet,
  732. body: /* @__PURE__ */ new Set(["html"]),
  733. head: /* @__PURE__ */ new Set(["html"]),
  734. // table
  735. td: /* @__PURE__ */ new Set(["tr"]),
  736. colgroup: /* @__PURE__ */ new Set(["table"]),
  737. caption: /* @__PURE__ */ new Set(["table"]),
  738. tbody: /* @__PURE__ */ new Set(["table"]),
  739. tfoot: /* @__PURE__ */ new Set(["table"]),
  740. col: /* @__PURE__ */ new Set(["colgroup"]),
  741. th: /* @__PURE__ */ new Set(["tr"]),
  742. thead: /* @__PURE__ */ new Set(["table"]),
  743. tr: /* @__PURE__ */ new Set(["tbody", "thead", "tfoot"]),
  744. // data list
  745. dd: /* @__PURE__ */ new Set(["dl", "div"]),
  746. dt: /* @__PURE__ */ new Set(["dl", "div"]),
  747. // other
  748. figcaption: /* @__PURE__ */ new Set(["figure"]),
  749. // li: new Set(["ul", "ol"]),
  750. summary: /* @__PURE__ */ new Set(["details"]),
  751. area: /* @__PURE__ */ new Set(["map"])
  752. };
  753. const knownInvalidChildren = {
  754. p: /* @__PURE__ */ new Set([
  755. "address",
  756. "article",
  757. "aside",
  758. "blockquote",
  759. "center",
  760. "details",
  761. "dialog",
  762. "dir",
  763. "div",
  764. "dl",
  765. "fieldset",
  766. "figure",
  767. "footer",
  768. "form",
  769. "h1",
  770. "h2",
  771. "h3",
  772. "h4",
  773. "h5",
  774. "h6",
  775. "header",
  776. "hgroup",
  777. "hr",
  778. "li",
  779. "main",
  780. "nav",
  781. "menu",
  782. "ol",
  783. "p",
  784. "pre",
  785. "section",
  786. "table",
  787. "ul"
  788. ]),
  789. svg: /* @__PURE__ */ new Set([
  790. "b",
  791. "blockquote",
  792. "br",
  793. "code",
  794. "dd",
  795. "div",
  796. "dl",
  797. "dt",
  798. "em",
  799. "embed",
  800. "h1",
  801. "h2",
  802. "h3",
  803. "h4",
  804. "h5",
  805. "h6",
  806. "hr",
  807. "i",
  808. "img",
  809. "li",
  810. "menu",
  811. "meta",
  812. "ol",
  813. "p",
  814. "pre",
  815. "ruby",
  816. "s",
  817. "small",
  818. "span",
  819. "strong",
  820. "sub",
  821. "sup",
  822. "table",
  823. "u",
  824. "ul",
  825. "var"
  826. ])
  827. };
  828. const knownInvalidParents = {
  829. a: /* @__PURE__ */ new Set(["a"]),
  830. button: /* @__PURE__ */ new Set(["button"]),
  831. dd: /* @__PURE__ */ new Set(["dd", "dt"]),
  832. dt: /* @__PURE__ */ new Set(["dd", "dt"]),
  833. form: /* @__PURE__ */ new Set(["form"]),
  834. li: /* @__PURE__ */ new Set(["li"]),
  835. h1: headings,
  836. h2: headings,
  837. h3: headings,
  838. h4: headings,
  839. h5: headings,
  840. h6: headings
  841. };
  842. const validateHtmlNesting = (node, context) => {
  843. if (node.type === 1 && node.tagType === 0 && context.parent && context.parent.type === 1 && context.parent.tagType === 0 && !isValidHTMLNesting(context.parent.tag, node.tag)) {
  844. const error = new SyntaxError(
  845. `<${node.tag}> cannot be child of <${context.parent.tag}>, according to HTML specifications. This can cause hydration errors or potentially disrupt future functionality.`
  846. );
  847. error.loc = node.loc;
  848. context.onWarn(error);
  849. }
  850. };
  851. const DOMNodeTransforms = [
  852. transformStyle,
  853. ...[transformTransition, validateHtmlNesting]
  854. ];
  855. const DOMDirectiveTransforms = {
  856. cloak: compilerCore.noopDirectiveTransform,
  857. html: transformVHtml,
  858. text: transformVText,
  859. model: transformModel,
  860. // override compiler-core
  861. on: transformOn,
  862. // override compiler-core
  863. show: transformShow
  864. };
  865. function compile(src, options = {}) {
  866. return compilerCore.baseCompile(
  867. src,
  868. shared.extend({}, parserOptions, options, {
  869. nodeTransforms: [
  870. // ignore <script> and <tag>
  871. // this is not put inside DOMNodeTransforms because that list is used
  872. // by compiler-ssr to generate vnode fallback branches
  873. ignoreSideEffectTags,
  874. ...DOMNodeTransforms,
  875. ...options.nodeTransforms || []
  876. ],
  877. directiveTransforms: shared.extend(
  878. {},
  879. DOMDirectiveTransforms,
  880. options.directiveTransforms || {}
  881. ),
  882. transformHoist: stringifyStatic
  883. })
  884. );
  885. }
  886. function parse(template, options = {}) {
  887. return compilerCore.baseParse(template, shared.extend({}, parserOptions, options));
  888. }
  889. exports.DOMDirectiveTransforms = DOMDirectiveTransforms;
  890. exports.DOMErrorCodes = DOMErrorCodes;
  891. exports.DOMErrorMessages = DOMErrorMessages;
  892. exports.DOMNodeTransforms = DOMNodeTransforms;
  893. exports.TRANSITION = TRANSITION;
  894. exports.TRANSITION_GROUP = TRANSITION_GROUP;
  895. exports.V_MODEL_CHECKBOX = V_MODEL_CHECKBOX;
  896. exports.V_MODEL_DYNAMIC = V_MODEL_DYNAMIC;
  897. exports.V_MODEL_RADIO = V_MODEL_RADIO;
  898. exports.V_MODEL_SELECT = V_MODEL_SELECT;
  899. exports.V_MODEL_TEXT = V_MODEL_TEXT;
  900. exports.V_ON_WITH_KEYS = V_ON_WITH_KEYS;
  901. exports.V_ON_WITH_MODIFIERS = V_ON_WITH_MODIFIERS;
  902. exports.V_SHOW = V_SHOW;
  903. exports.compile = compile;
  904. exports.createDOMCompilerError = createDOMCompilerError;
  905. exports.parse = parse;
  906. exports.parserOptions = parserOptions;
  907. exports.transformStyle = transformStyle;
  908. Object.keys(compilerCore).forEach(function (k) {
  909. if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = compilerCore[k];
  910. });