beautify-html.js 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242
  1. /* AUTO-GENERATED. DO NOT MODIFY. */
  2. /*
  3. The MIT License (MIT)
  4. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  5. Permission is hereby granted, free of charge, to any person
  6. obtaining a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. Style HTML
  23. ---------------
  24. Written by Nochum Sossonko, (nsossonko@hotmail.com)
  25. Based on code initially developed by: Einar Lielmanis, <einar@beautifier.io>
  26. https://beautifier.io/
  27. Usage:
  28. style_html(html_source);
  29. style_html(html_source, options);
  30. The options are:
  31. indent_inner_html (default false) — indent <head> and <body> sections,
  32. indent_size (default 4) — indentation size,
  33. indent_char (default space) — character to indent with,
  34. wrap_line_length (default 250) - maximum amount of characters per line (0 = disable)
  35. brace_style (default "collapse") - "collapse" | "expand" | "end-expand" | "none"
  36. put braces on the same line as control statements (default), or put braces on own line (Allman / ANSI style), or just put end braces on own line, or attempt to keep them where they are.
  37. inline (defaults to inline tags) - list of tags to be considered inline tags
  38. unformatted (defaults to inline tags) - list of tags, that shouldn't be reformatted
  39. content_unformatted (defaults to ["pre", "textarea"] tags) - list of tags, whose content shouldn't be reformatted
  40. indent_scripts (default normal) - "keep"|"separate"|"normal"
  41. preserve_newlines (default true) - whether existing line breaks before elements should be preserved
  42. Only works before elements, not inside tags or for text.
  43. max_preserve_newlines (default unlimited) - maximum number of line breaks to be preserved in one chunk
  44. indent_handlebars (default false) - format and indent {{#foo}} and {{/foo}}
  45. end_with_newline (false) - end with a newline
  46. extra_liners (default [head,body,/html]) -List of tags that should have an extra newline before them.
  47. e.g.
  48. style_html(html_source, {
  49. 'indent_inner_html': false,
  50. 'indent_size': 2,
  51. 'indent_char': ' ',
  52. 'wrap_line_length': 78,
  53. 'brace_style': 'expand',
  54. 'preserve_newlines': true,
  55. 'max_preserve_newlines': 5,
  56. 'indent_handlebars': false,
  57. 'extra_liners': ['/html']
  58. });
  59. */
  60. (function() {
  61. /* GENERATED_BUILD_OUTPUT */
  62. var legacy_beautify_html;
  63. /******/ (function() { // webpackBootstrap
  64. /******/ "use strict";
  65. /******/ var __webpack_modules__ = ([
  66. /* 0 */,
  67. /* 1 */,
  68. /* 2 */
  69. /***/ (function(module) {
  70. /*jshint node:true */
  71. /*
  72. The MIT License (MIT)
  73. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  74. Permission is hereby granted, free of charge, to any person
  75. obtaining a copy of this software and associated documentation files
  76. (the "Software"), to deal in the Software without restriction,
  77. including without limitation the rights to use, copy, modify, merge,
  78. publish, distribute, sublicense, and/or sell copies of the Software,
  79. and to permit persons to whom the Software is furnished to do so,
  80. subject to the following conditions:
  81. The above copyright notice and this permission notice shall be
  82. included in all copies or substantial portions of the Software.
  83. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  84. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  85. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  86. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  87. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  88. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  89. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  90. SOFTWARE.
  91. */
  92. function OutputLine(parent) {
  93. this.__parent = parent;
  94. this.__character_count = 0;
  95. // use indent_count as a marker for this.__lines that have preserved indentation
  96. this.__indent_count = -1;
  97. this.__alignment_count = 0;
  98. this.__wrap_point_index = 0;
  99. this.__wrap_point_character_count = 0;
  100. this.__wrap_point_indent_count = -1;
  101. this.__wrap_point_alignment_count = 0;
  102. this.__items = [];
  103. }
  104. OutputLine.prototype.clone_empty = function() {
  105. var line = new OutputLine(this.__parent);
  106. line.set_indent(this.__indent_count, this.__alignment_count);
  107. return line;
  108. };
  109. OutputLine.prototype.item = function(index) {
  110. if (index < 0) {
  111. return this.__items[this.__items.length + index];
  112. } else {
  113. return this.__items[index];
  114. }
  115. };
  116. OutputLine.prototype.has_match = function(pattern) {
  117. for (var lastCheckedOutput = this.__items.length - 1; lastCheckedOutput >= 0; lastCheckedOutput--) {
  118. if (this.__items[lastCheckedOutput].match(pattern)) {
  119. return true;
  120. }
  121. }
  122. return false;
  123. };
  124. OutputLine.prototype.set_indent = function(indent, alignment) {
  125. if (this.is_empty()) {
  126. this.__indent_count = indent || 0;
  127. this.__alignment_count = alignment || 0;
  128. this.__character_count = this.__parent.get_indent_size(this.__indent_count, this.__alignment_count);
  129. }
  130. };
  131. OutputLine.prototype._set_wrap_point = function() {
  132. if (this.__parent.wrap_line_length) {
  133. this.__wrap_point_index = this.__items.length;
  134. this.__wrap_point_character_count = this.__character_count;
  135. this.__wrap_point_indent_count = this.__parent.next_line.__indent_count;
  136. this.__wrap_point_alignment_count = this.__parent.next_line.__alignment_count;
  137. }
  138. };
  139. OutputLine.prototype._should_wrap = function() {
  140. return this.__wrap_point_index &&
  141. this.__character_count > this.__parent.wrap_line_length &&
  142. this.__wrap_point_character_count > this.__parent.next_line.__character_count;
  143. };
  144. OutputLine.prototype._allow_wrap = function() {
  145. if (this._should_wrap()) {
  146. this.__parent.add_new_line();
  147. var next = this.__parent.current_line;
  148. next.set_indent(this.__wrap_point_indent_count, this.__wrap_point_alignment_count);
  149. next.__items = this.__items.slice(this.__wrap_point_index);
  150. this.__items = this.__items.slice(0, this.__wrap_point_index);
  151. next.__character_count += this.__character_count - this.__wrap_point_character_count;
  152. this.__character_count = this.__wrap_point_character_count;
  153. if (next.__items[0] === " ") {
  154. next.__items.splice(0, 1);
  155. next.__character_count -= 1;
  156. }
  157. return true;
  158. }
  159. return false;
  160. };
  161. OutputLine.prototype.is_empty = function() {
  162. return this.__items.length === 0;
  163. };
  164. OutputLine.prototype.last = function() {
  165. if (!this.is_empty()) {
  166. return this.__items[this.__items.length - 1];
  167. } else {
  168. return null;
  169. }
  170. };
  171. OutputLine.prototype.push = function(item) {
  172. this.__items.push(item);
  173. var last_newline_index = item.lastIndexOf('\n');
  174. if (last_newline_index !== -1) {
  175. this.__character_count = item.length - last_newline_index;
  176. } else {
  177. this.__character_count += item.length;
  178. }
  179. };
  180. OutputLine.prototype.pop = function() {
  181. var item = null;
  182. if (!this.is_empty()) {
  183. item = this.__items.pop();
  184. this.__character_count -= item.length;
  185. }
  186. return item;
  187. };
  188. OutputLine.prototype._remove_indent = function() {
  189. if (this.__indent_count > 0) {
  190. this.__indent_count -= 1;
  191. this.__character_count -= this.__parent.indent_size;
  192. }
  193. };
  194. OutputLine.prototype._remove_wrap_indent = function() {
  195. if (this.__wrap_point_indent_count > 0) {
  196. this.__wrap_point_indent_count -= 1;
  197. }
  198. };
  199. OutputLine.prototype.trim = function() {
  200. while (this.last() === ' ') {
  201. this.__items.pop();
  202. this.__character_count -= 1;
  203. }
  204. };
  205. OutputLine.prototype.toString = function() {
  206. var result = '';
  207. if (this.is_empty()) {
  208. if (this.__parent.indent_empty_lines) {
  209. result = this.__parent.get_indent_string(this.__indent_count);
  210. }
  211. } else {
  212. result = this.__parent.get_indent_string(this.__indent_count, this.__alignment_count);
  213. result += this.__items.join('');
  214. }
  215. return result;
  216. };
  217. function IndentStringCache(options, baseIndentString) {
  218. this.__cache = [''];
  219. this.__indent_size = options.indent_size;
  220. this.__indent_string = options.indent_char;
  221. if (!options.indent_with_tabs) {
  222. this.__indent_string = new Array(options.indent_size + 1).join(options.indent_char);
  223. }
  224. // Set to null to continue support for auto detection of base indent
  225. baseIndentString = baseIndentString || '';
  226. if (options.indent_level > 0) {
  227. baseIndentString = new Array(options.indent_level + 1).join(this.__indent_string);
  228. }
  229. this.__base_string = baseIndentString;
  230. this.__base_string_length = baseIndentString.length;
  231. }
  232. IndentStringCache.prototype.get_indent_size = function(indent, column) {
  233. var result = this.__base_string_length;
  234. column = column || 0;
  235. if (indent < 0) {
  236. result = 0;
  237. }
  238. result += indent * this.__indent_size;
  239. result += column;
  240. return result;
  241. };
  242. IndentStringCache.prototype.get_indent_string = function(indent_level, column) {
  243. var result = this.__base_string;
  244. column = column || 0;
  245. if (indent_level < 0) {
  246. indent_level = 0;
  247. result = '';
  248. }
  249. column += indent_level * this.__indent_size;
  250. this.__ensure_cache(column);
  251. result += this.__cache[column];
  252. return result;
  253. };
  254. IndentStringCache.prototype.__ensure_cache = function(column) {
  255. while (column >= this.__cache.length) {
  256. this.__add_column();
  257. }
  258. };
  259. IndentStringCache.prototype.__add_column = function() {
  260. var column = this.__cache.length;
  261. var indent = 0;
  262. var result = '';
  263. if (this.__indent_size && column >= this.__indent_size) {
  264. indent = Math.floor(column / this.__indent_size);
  265. column -= indent * this.__indent_size;
  266. result = new Array(indent + 1).join(this.__indent_string);
  267. }
  268. if (column) {
  269. result += new Array(column + 1).join(' ');
  270. }
  271. this.__cache.push(result);
  272. };
  273. function Output(options, baseIndentString) {
  274. this.__indent_cache = new IndentStringCache(options, baseIndentString);
  275. this.raw = false;
  276. this._end_with_newline = options.end_with_newline;
  277. this.indent_size = options.indent_size;
  278. this.wrap_line_length = options.wrap_line_length;
  279. this.indent_empty_lines = options.indent_empty_lines;
  280. this.__lines = [];
  281. this.previous_line = null;
  282. this.current_line = null;
  283. this.next_line = new OutputLine(this);
  284. this.space_before_token = false;
  285. this.non_breaking_space = false;
  286. this.previous_token_wrapped = false;
  287. // initialize
  288. this.__add_outputline();
  289. }
  290. Output.prototype.__add_outputline = function() {
  291. this.previous_line = this.current_line;
  292. this.current_line = this.next_line.clone_empty();
  293. this.__lines.push(this.current_line);
  294. };
  295. Output.prototype.get_line_number = function() {
  296. return this.__lines.length;
  297. };
  298. Output.prototype.get_indent_string = function(indent, column) {
  299. return this.__indent_cache.get_indent_string(indent, column);
  300. };
  301. Output.prototype.get_indent_size = function(indent, column) {
  302. return this.__indent_cache.get_indent_size(indent, column);
  303. };
  304. Output.prototype.is_empty = function() {
  305. return !this.previous_line && this.current_line.is_empty();
  306. };
  307. Output.prototype.add_new_line = function(force_newline) {
  308. // never newline at the start of file
  309. // otherwise, newline only if we didn't just add one or we're forced
  310. if (this.is_empty() ||
  311. (!force_newline && this.just_added_newline())) {
  312. return false;
  313. }
  314. // if raw output is enabled, don't print additional newlines,
  315. // but still return True as though you had
  316. if (!this.raw) {
  317. this.__add_outputline();
  318. }
  319. return true;
  320. };
  321. Output.prototype.get_code = function(eol) {
  322. this.trim(true);
  323. // handle some edge cases where the last tokens
  324. // has text that ends with newline(s)
  325. var last_item = this.current_line.pop();
  326. if (last_item) {
  327. if (last_item[last_item.length - 1] === '\n') {
  328. last_item = last_item.replace(/\n+$/g, '');
  329. }
  330. this.current_line.push(last_item);
  331. }
  332. if (this._end_with_newline) {
  333. this.__add_outputline();
  334. }
  335. var sweet_code = this.__lines.join('\n');
  336. if (eol !== '\n') {
  337. sweet_code = sweet_code.replace(/[\n]/g, eol);
  338. }
  339. return sweet_code;
  340. };
  341. Output.prototype.set_wrap_point = function() {
  342. this.current_line._set_wrap_point();
  343. };
  344. Output.prototype.set_indent = function(indent, alignment) {
  345. indent = indent || 0;
  346. alignment = alignment || 0;
  347. // Next line stores alignment values
  348. this.next_line.set_indent(indent, alignment);
  349. // Never indent your first output indent at the start of the file
  350. if (this.__lines.length > 1) {
  351. this.current_line.set_indent(indent, alignment);
  352. return true;
  353. }
  354. this.current_line.set_indent();
  355. return false;
  356. };
  357. Output.prototype.add_raw_token = function(token) {
  358. for (var x = 0; x < token.newlines; x++) {
  359. this.__add_outputline();
  360. }
  361. this.current_line.set_indent(-1);
  362. this.current_line.push(token.whitespace_before);
  363. this.current_line.push(token.text);
  364. this.space_before_token = false;
  365. this.non_breaking_space = false;
  366. this.previous_token_wrapped = false;
  367. };
  368. Output.prototype.add_token = function(printable_token) {
  369. this.__add_space_before_token();
  370. this.current_line.push(printable_token);
  371. this.space_before_token = false;
  372. this.non_breaking_space = false;
  373. this.previous_token_wrapped = this.current_line._allow_wrap();
  374. };
  375. Output.prototype.__add_space_before_token = function() {
  376. if (this.space_before_token && !this.just_added_newline()) {
  377. if (!this.non_breaking_space) {
  378. this.set_wrap_point();
  379. }
  380. this.current_line.push(' ');
  381. }
  382. };
  383. Output.prototype.remove_indent = function(index) {
  384. var output_length = this.__lines.length;
  385. while (index < output_length) {
  386. this.__lines[index]._remove_indent();
  387. index++;
  388. }
  389. this.current_line._remove_wrap_indent();
  390. };
  391. Output.prototype.trim = function(eat_newlines) {
  392. eat_newlines = (eat_newlines === undefined) ? false : eat_newlines;
  393. this.current_line.trim();
  394. while (eat_newlines && this.__lines.length > 1 &&
  395. this.current_line.is_empty()) {
  396. this.__lines.pop();
  397. this.current_line = this.__lines[this.__lines.length - 1];
  398. this.current_line.trim();
  399. }
  400. this.previous_line = this.__lines.length > 1 ?
  401. this.__lines[this.__lines.length - 2] : null;
  402. };
  403. Output.prototype.just_added_newline = function() {
  404. return this.current_line.is_empty();
  405. };
  406. Output.prototype.just_added_blankline = function() {
  407. return this.is_empty() ||
  408. (this.current_line.is_empty() && this.previous_line.is_empty());
  409. };
  410. Output.prototype.ensure_empty_line_above = function(starts_with, ends_with) {
  411. var index = this.__lines.length - 2;
  412. while (index >= 0) {
  413. var potentialEmptyLine = this.__lines[index];
  414. if (potentialEmptyLine.is_empty()) {
  415. break;
  416. } else if (potentialEmptyLine.item(0).indexOf(starts_with) !== 0 &&
  417. potentialEmptyLine.item(-1) !== ends_with) {
  418. this.__lines.splice(index + 1, 0, new OutputLine(this));
  419. this.previous_line = this.__lines[this.__lines.length - 2];
  420. break;
  421. }
  422. index--;
  423. }
  424. };
  425. module.exports.Output = Output;
  426. /***/ }),
  427. /* 3 */
  428. /***/ (function(module) {
  429. /*jshint node:true */
  430. /*
  431. The MIT License (MIT)
  432. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  433. Permission is hereby granted, free of charge, to any person
  434. obtaining a copy of this software and associated documentation files
  435. (the "Software"), to deal in the Software without restriction,
  436. including without limitation the rights to use, copy, modify, merge,
  437. publish, distribute, sublicense, and/or sell copies of the Software,
  438. and to permit persons to whom the Software is furnished to do so,
  439. subject to the following conditions:
  440. The above copyright notice and this permission notice shall be
  441. included in all copies or substantial portions of the Software.
  442. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  443. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  444. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  445. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  446. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  447. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  448. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  449. SOFTWARE.
  450. */
  451. function Token(type, text, newlines, whitespace_before) {
  452. this.type = type;
  453. this.text = text;
  454. // comments_before are
  455. // comments that have a new line before them
  456. // and may or may not have a newline after
  457. // this is a set of comments before
  458. this.comments_before = null; /* inline comment*/
  459. // this.comments_after = new TokenStream(); // no new line before and newline after
  460. this.newlines = newlines || 0;
  461. this.whitespace_before = whitespace_before || '';
  462. this.parent = null;
  463. this.next = null;
  464. this.previous = null;
  465. this.opened = null;
  466. this.closed = null;
  467. this.directives = null;
  468. }
  469. module.exports.Token = Token;
  470. /***/ }),
  471. /* 4 */,
  472. /* 5 */,
  473. /* 6 */
  474. /***/ (function(module) {
  475. /*jshint node:true */
  476. /*
  477. The MIT License (MIT)
  478. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  479. Permission is hereby granted, free of charge, to any person
  480. obtaining a copy of this software and associated documentation files
  481. (the "Software"), to deal in the Software without restriction,
  482. including without limitation the rights to use, copy, modify, merge,
  483. publish, distribute, sublicense, and/or sell copies of the Software,
  484. and to permit persons to whom the Software is furnished to do so,
  485. subject to the following conditions:
  486. The above copyright notice and this permission notice shall be
  487. included in all copies or substantial portions of the Software.
  488. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  489. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  490. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  491. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  492. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  493. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  494. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  495. SOFTWARE.
  496. */
  497. function Options(options, merge_child_field) {
  498. this.raw_options = _mergeOpts(options, merge_child_field);
  499. // Support passing the source text back with no change
  500. this.disabled = this._get_boolean('disabled');
  501. this.eol = this._get_characters('eol', 'auto');
  502. this.end_with_newline = this._get_boolean('end_with_newline');
  503. this.indent_size = this._get_number('indent_size', 4);
  504. this.indent_char = this._get_characters('indent_char', ' ');
  505. this.indent_level = this._get_number('indent_level');
  506. this.preserve_newlines = this._get_boolean('preserve_newlines', true);
  507. this.max_preserve_newlines = this._get_number('max_preserve_newlines', 32786);
  508. if (!this.preserve_newlines) {
  509. this.max_preserve_newlines = 0;
  510. }
  511. this.indent_with_tabs = this._get_boolean('indent_with_tabs', this.indent_char === '\t');
  512. if (this.indent_with_tabs) {
  513. this.indent_char = '\t';
  514. // indent_size behavior changed after 1.8.6
  515. // It used to be that indent_size would be
  516. // set to 1 for indent_with_tabs. That is no longer needed and
  517. // actually doesn't make sense - why not use spaces? Further,
  518. // that might produce unexpected behavior - tabs being used
  519. // for single-column alignment. So, when indent_with_tabs is true
  520. // and indent_size is 1, reset indent_size to 4.
  521. if (this.indent_size === 1) {
  522. this.indent_size = 4;
  523. }
  524. }
  525. // Backwards compat with 1.3.x
  526. this.wrap_line_length = this._get_number('wrap_line_length', this._get_number('max_char'));
  527. this.indent_empty_lines = this._get_boolean('indent_empty_lines');
  528. // valid templating languages ['django', 'erb', 'handlebars', 'php', 'smarty', 'angular']
  529. // For now, 'auto' = all off for javascript, all except angular on for html (and inline javascript/css).
  530. // other values ignored
  531. this.templating = this._get_selection_list('templating', ['auto', 'none', 'angular', 'django', 'erb', 'handlebars', 'php', 'smarty'], ['auto']);
  532. }
  533. Options.prototype._get_array = function(name, default_value) {
  534. var option_value = this.raw_options[name];
  535. var result = default_value || [];
  536. if (typeof option_value === 'object') {
  537. if (option_value !== null && typeof option_value.concat === 'function') {
  538. result = option_value.concat();
  539. }
  540. } else if (typeof option_value === 'string') {
  541. result = option_value.split(/[^a-zA-Z0-9_\/\-]+/);
  542. }
  543. return result;
  544. };
  545. Options.prototype._get_boolean = function(name, default_value) {
  546. var option_value = this.raw_options[name];
  547. var result = option_value === undefined ? !!default_value : !!option_value;
  548. return result;
  549. };
  550. Options.prototype._get_characters = function(name, default_value) {
  551. var option_value = this.raw_options[name];
  552. var result = default_value || '';
  553. if (typeof option_value === 'string') {
  554. result = option_value.replace(/\\r/, '\r').replace(/\\n/, '\n').replace(/\\t/, '\t');
  555. }
  556. return result;
  557. };
  558. Options.prototype._get_number = function(name, default_value) {
  559. var option_value = this.raw_options[name];
  560. default_value = parseInt(default_value, 10);
  561. if (isNaN(default_value)) {
  562. default_value = 0;
  563. }
  564. var result = parseInt(option_value, 10);
  565. if (isNaN(result)) {
  566. result = default_value;
  567. }
  568. return result;
  569. };
  570. Options.prototype._get_selection = function(name, selection_list, default_value) {
  571. var result = this._get_selection_list(name, selection_list, default_value);
  572. if (result.length !== 1) {
  573. throw new Error(
  574. "Invalid Option Value: The option '" + name + "' can only be one of the following values:\n" +
  575. selection_list + "\nYou passed in: '" + this.raw_options[name] + "'");
  576. }
  577. return result[0];
  578. };
  579. Options.prototype._get_selection_list = function(name, selection_list, default_value) {
  580. if (!selection_list || selection_list.length === 0) {
  581. throw new Error("Selection list cannot be empty.");
  582. }
  583. default_value = default_value || [selection_list[0]];
  584. if (!this._is_valid_selection(default_value, selection_list)) {
  585. throw new Error("Invalid Default Value!");
  586. }
  587. var result = this._get_array(name, default_value);
  588. if (!this._is_valid_selection(result, selection_list)) {
  589. throw new Error(
  590. "Invalid Option Value: The option '" + name + "' can contain only the following values:\n" +
  591. selection_list + "\nYou passed in: '" + this.raw_options[name] + "'");
  592. }
  593. return result;
  594. };
  595. Options.prototype._is_valid_selection = function(result, selection_list) {
  596. return result.length && selection_list.length &&
  597. !result.some(function(item) { return selection_list.indexOf(item) === -1; });
  598. };
  599. // merges child options up with the parent options object
  600. // Example: obj = {a: 1, b: {a: 2}}
  601. // mergeOpts(obj, 'b')
  602. //
  603. // Returns: {a: 2}
  604. function _mergeOpts(allOptions, childFieldName) {
  605. var finalOpts = {};
  606. allOptions = _normalizeOpts(allOptions);
  607. var name;
  608. for (name in allOptions) {
  609. if (name !== childFieldName) {
  610. finalOpts[name] = allOptions[name];
  611. }
  612. }
  613. //merge in the per type settings for the childFieldName
  614. if (childFieldName && allOptions[childFieldName]) {
  615. for (name in allOptions[childFieldName]) {
  616. finalOpts[name] = allOptions[childFieldName][name];
  617. }
  618. }
  619. return finalOpts;
  620. }
  621. function _normalizeOpts(options) {
  622. var convertedOpts = {};
  623. var key;
  624. for (key in options) {
  625. var newKey = key.replace(/-/g, "_");
  626. convertedOpts[newKey] = options[key];
  627. }
  628. return convertedOpts;
  629. }
  630. module.exports.Options = Options;
  631. module.exports.normalizeOpts = _normalizeOpts;
  632. module.exports.mergeOpts = _mergeOpts;
  633. /***/ }),
  634. /* 7 */,
  635. /* 8 */
  636. /***/ (function(module) {
  637. /*jshint node:true */
  638. /*
  639. The MIT License (MIT)
  640. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  641. Permission is hereby granted, free of charge, to any person
  642. obtaining a copy of this software and associated documentation files
  643. (the "Software"), to deal in the Software without restriction,
  644. including without limitation the rights to use, copy, modify, merge,
  645. publish, distribute, sublicense, and/or sell copies of the Software,
  646. and to permit persons to whom the Software is furnished to do so,
  647. subject to the following conditions:
  648. The above copyright notice and this permission notice shall be
  649. included in all copies or substantial portions of the Software.
  650. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  651. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  652. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  653. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  654. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  655. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  656. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  657. SOFTWARE.
  658. */
  659. var regexp_has_sticky = RegExp.prototype.hasOwnProperty('sticky');
  660. function InputScanner(input_string) {
  661. this.__input = input_string || '';
  662. this.__input_length = this.__input.length;
  663. this.__position = 0;
  664. }
  665. InputScanner.prototype.restart = function() {
  666. this.__position = 0;
  667. };
  668. InputScanner.prototype.back = function() {
  669. if (this.__position > 0) {
  670. this.__position -= 1;
  671. }
  672. };
  673. InputScanner.prototype.hasNext = function() {
  674. return this.__position < this.__input_length;
  675. };
  676. InputScanner.prototype.next = function() {
  677. var val = null;
  678. if (this.hasNext()) {
  679. val = this.__input.charAt(this.__position);
  680. this.__position += 1;
  681. }
  682. return val;
  683. };
  684. InputScanner.prototype.peek = function(index) {
  685. var val = null;
  686. index = index || 0;
  687. index += this.__position;
  688. if (index >= 0 && index < this.__input_length) {
  689. val = this.__input.charAt(index);
  690. }
  691. return val;
  692. };
  693. // This is a JavaScript only helper function (not in python)
  694. // Javascript doesn't have a match method
  695. // and not all implementation support "sticky" flag.
  696. // If they do not support sticky then both this.match() and this.test() method
  697. // must get the match and check the index of the match.
  698. // If sticky is supported and set, this method will use it.
  699. // Otherwise it will check that global is set, and fall back to the slower method.
  700. InputScanner.prototype.__match = function(pattern, index) {
  701. pattern.lastIndex = index;
  702. var pattern_match = pattern.exec(this.__input);
  703. if (pattern_match && !(regexp_has_sticky && pattern.sticky)) {
  704. if (pattern_match.index !== index) {
  705. pattern_match = null;
  706. }
  707. }
  708. return pattern_match;
  709. };
  710. InputScanner.prototype.test = function(pattern, index) {
  711. index = index || 0;
  712. index += this.__position;
  713. if (index >= 0 && index < this.__input_length) {
  714. return !!this.__match(pattern, index);
  715. } else {
  716. return false;
  717. }
  718. };
  719. InputScanner.prototype.testChar = function(pattern, index) {
  720. // test one character regex match
  721. var val = this.peek(index);
  722. pattern.lastIndex = 0;
  723. return val !== null && pattern.test(val);
  724. };
  725. InputScanner.prototype.match = function(pattern) {
  726. var pattern_match = this.__match(pattern, this.__position);
  727. if (pattern_match) {
  728. this.__position += pattern_match[0].length;
  729. } else {
  730. pattern_match = null;
  731. }
  732. return pattern_match;
  733. };
  734. InputScanner.prototype.read = function(starting_pattern, until_pattern, until_after) {
  735. var val = '';
  736. var match;
  737. if (starting_pattern) {
  738. match = this.match(starting_pattern);
  739. if (match) {
  740. val += match[0];
  741. }
  742. }
  743. if (until_pattern && (match || !starting_pattern)) {
  744. val += this.readUntil(until_pattern, until_after);
  745. }
  746. return val;
  747. };
  748. InputScanner.prototype.readUntil = function(pattern, until_after) {
  749. var val = '';
  750. var match_index = this.__position;
  751. pattern.lastIndex = this.__position;
  752. var pattern_match = pattern.exec(this.__input);
  753. if (pattern_match) {
  754. match_index = pattern_match.index;
  755. if (until_after) {
  756. match_index += pattern_match[0].length;
  757. }
  758. } else {
  759. match_index = this.__input_length;
  760. }
  761. val = this.__input.substring(this.__position, match_index);
  762. this.__position = match_index;
  763. return val;
  764. };
  765. InputScanner.prototype.readUntilAfter = function(pattern) {
  766. return this.readUntil(pattern, true);
  767. };
  768. InputScanner.prototype.get_regexp = function(pattern, match_from) {
  769. var result = null;
  770. var flags = 'g';
  771. if (match_from && regexp_has_sticky) {
  772. flags = 'y';
  773. }
  774. // strings are converted to regexp
  775. if (typeof pattern === "string" && pattern !== '') {
  776. // result = new RegExp(pattern.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), flags);
  777. result = new RegExp(pattern, flags);
  778. } else if (pattern) {
  779. result = new RegExp(pattern.source, flags);
  780. }
  781. return result;
  782. };
  783. InputScanner.prototype.get_literal_regexp = function(literal_string) {
  784. return RegExp(literal_string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'));
  785. };
  786. /* css beautifier legacy helpers */
  787. InputScanner.prototype.peekUntilAfter = function(pattern) {
  788. var start = this.__position;
  789. var val = this.readUntilAfter(pattern);
  790. this.__position = start;
  791. return val;
  792. };
  793. InputScanner.prototype.lookBack = function(testVal) {
  794. var start = this.__position - 1;
  795. return start >= testVal.length && this.__input.substring(start - testVal.length, start)
  796. .toLowerCase() === testVal;
  797. };
  798. module.exports.InputScanner = InputScanner;
  799. /***/ }),
  800. /* 9 */
  801. /***/ (function(module, __unused_webpack_exports, __webpack_require__) {
  802. /*jshint node:true */
  803. /*
  804. The MIT License (MIT)
  805. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  806. Permission is hereby granted, free of charge, to any person
  807. obtaining a copy of this software and associated documentation files
  808. (the "Software"), to deal in the Software without restriction,
  809. including without limitation the rights to use, copy, modify, merge,
  810. publish, distribute, sublicense, and/or sell copies of the Software,
  811. and to permit persons to whom the Software is furnished to do so,
  812. subject to the following conditions:
  813. The above copyright notice and this permission notice shall be
  814. included in all copies or substantial portions of the Software.
  815. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  816. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  817. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  818. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  819. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  820. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  821. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  822. SOFTWARE.
  823. */
  824. var InputScanner = (__webpack_require__(8).InputScanner);
  825. var Token = (__webpack_require__(3).Token);
  826. var TokenStream = (__webpack_require__(10).TokenStream);
  827. var WhitespacePattern = (__webpack_require__(11).WhitespacePattern);
  828. var TOKEN = {
  829. START: 'TK_START',
  830. RAW: 'TK_RAW',
  831. EOF: 'TK_EOF'
  832. };
  833. var Tokenizer = function(input_string, options) {
  834. this._input = new InputScanner(input_string);
  835. this._options = options || {};
  836. this.__tokens = null;
  837. this._patterns = {};
  838. this._patterns.whitespace = new WhitespacePattern(this._input);
  839. };
  840. Tokenizer.prototype.tokenize = function() {
  841. this._input.restart();
  842. this.__tokens = new TokenStream();
  843. this._reset();
  844. var current;
  845. var previous = new Token(TOKEN.START, '');
  846. var open_token = null;
  847. var open_stack = [];
  848. var comments = new TokenStream();
  849. while (previous.type !== TOKEN.EOF) {
  850. current = this._get_next_token(previous, open_token);
  851. while (this._is_comment(current)) {
  852. comments.add(current);
  853. current = this._get_next_token(previous, open_token);
  854. }
  855. if (!comments.isEmpty()) {
  856. current.comments_before = comments;
  857. comments = new TokenStream();
  858. }
  859. current.parent = open_token;
  860. if (this._is_opening(current)) {
  861. open_stack.push(open_token);
  862. open_token = current;
  863. } else if (open_token && this._is_closing(current, open_token)) {
  864. current.opened = open_token;
  865. open_token.closed = current;
  866. open_token = open_stack.pop();
  867. current.parent = open_token;
  868. }
  869. current.previous = previous;
  870. previous.next = current;
  871. this.__tokens.add(current);
  872. previous = current;
  873. }
  874. return this.__tokens;
  875. };
  876. Tokenizer.prototype._is_first_token = function() {
  877. return this.__tokens.isEmpty();
  878. };
  879. Tokenizer.prototype._reset = function() {};
  880. Tokenizer.prototype._get_next_token = function(previous_token, open_token) { // jshint unused:false
  881. this._readWhitespace();
  882. var resulting_string = this._input.read(/.+/g);
  883. if (resulting_string) {
  884. return this._create_token(TOKEN.RAW, resulting_string);
  885. } else {
  886. return this._create_token(TOKEN.EOF, '');
  887. }
  888. };
  889. Tokenizer.prototype._is_comment = function(current_token) { // jshint unused:false
  890. return false;
  891. };
  892. Tokenizer.prototype._is_opening = function(current_token) { // jshint unused:false
  893. return false;
  894. };
  895. Tokenizer.prototype._is_closing = function(current_token, open_token) { // jshint unused:false
  896. return false;
  897. };
  898. Tokenizer.prototype._create_token = function(type, text) {
  899. var token = new Token(type, text,
  900. this._patterns.whitespace.newline_count,
  901. this._patterns.whitespace.whitespace_before_token);
  902. return token;
  903. };
  904. Tokenizer.prototype._readWhitespace = function() {
  905. return this._patterns.whitespace.read();
  906. };
  907. module.exports.Tokenizer = Tokenizer;
  908. module.exports.TOKEN = TOKEN;
  909. /***/ }),
  910. /* 10 */
  911. /***/ (function(module) {
  912. /*jshint node:true */
  913. /*
  914. The MIT License (MIT)
  915. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  916. Permission is hereby granted, free of charge, to any person
  917. obtaining a copy of this software and associated documentation files
  918. (the "Software"), to deal in the Software without restriction,
  919. including without limitation the rights to use, copy, modify, merge,
  920. publish, distribute, sublicense, and/or sell copies of the Software,
  921. and to permit persons to whom the Software is furnished to do so,
  922. subject to the following conditions:
  923. The above copyright notice and this permission notice shall be
  924. included in all copies or substantial portions of the Software.
  925. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  926. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  927. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  928. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  929. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  930. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  931. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  932. SOFTWARE.
  933. */
  934. function TokenStream(parent_token) {
  935. // private
  936. this.__tokens = [];
  937. this.__tokens_length = this.__tokens.length;
  938. this.__position = 0;
  939. this.__parent_token = parent_token;
  940. }
  941. TokenStream.prototype.restart = function() {
  942. this.__position = 0;
  943. };
  944. TokenStream.prototype.isEmpty = function() {
  945. return this.__tokens_length === 0;
  946. };
  947. TokenStream.prototype.hasNext = function() {
  948. return this.__position < this.__tokens_length;
  949. };
  950. TokenStream.prototype.next = function() {
  951. var val = null;
  952. if (this.hasNext()) {
  953. val = this.__tokens[this.__position];
  954. this.__position += 1;
  955. }
  956. return val;
  957. };
  958. TokenStream.prototype.peek = function(index) {
  959. var val = null;
  960. index = index || 0;
  961. index += this.__position;
  962. if (index >= 0 && index < this.__tokens_length) {
  963. val = this.__tokens[index];
  964. }
  965. return val;
  966. };
  967. TokenStream.prototype.add = function(token) {
  968. if (this.__parent_token) {
  969. token.parent = this.__parent_token;
  970. }
  971. this.__tokens.push(token);
  972. this.__tokens_length += 1;
  973. };
  974. module.exports.TokenStream = TokenStream;
  975. /***/ }),
  976. /* 11 */
  977. /***/ (function(module, __unused_webpack_exports, __webpack_require__) {
  978. /*jshint node:true */
  979. /*
  980. The MIT License (MIT)
  981. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  982. Permission is hereby granted, free of charge, to any person
  983. obtaining a copy of this software and associated documentation files
  984. (the "Software"), to deal in the Software without restriction,
  985. including without limitation the rights to use, copy, modify, merge,
  986. publish, distribute, sublicense, and/or sell copies of the Software,
  987. and to permit persons to whom the Software is furnished to do so,
  988. subject to the following conditions:
  989. The above copyright notice and this permission notice shall be
  990. included in all copies or substantial portions of the Software.
  991. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  992. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  993. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  994. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  995. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  996. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  997. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  998. SOFTWARE.
  999. */
  1000. var Pattern = (__webpack_require__(12).Pattern);
  1001. function WhitespacePattern(input_scanner, parent) {
  1002. Pattern.call(this, input_scanner, parent);
  1003. if (parent) {
  1004. this._line_regexp = this._input.get_regexp(parent._line_regexp);
  1005. } else {
  1006. this.__set_whitespace_patterns('', '');
  1007. }
  1008. this.newline_count = 0;
  1009. this.whitespace_before_token = '';
  1010. }
  1011. WhitespacePattern.prototype = new Pattern();
  1012. WhitespacePattern.prototype.__set_whitespace_patterns = function(whitespace_chars, newline_chars) {
  1013. whitespace_chars += '\\t ';
  1014. newline_chars += '\\n\\r';
  1015. this._match_pattern = this._input.get_regexp(
  1016. '[' + whitespace_chars + newline_chars + ']+', true);
  1017. this._newline_regexp = this._input.get_regexp(
  1018. '\\r\\n|[' + newline_chars + ']');
  1019. };
  1020. WhitespacePattern.prototype.read = function() {
  1021. this.newline_count = 0;
  1022. this.whitespace_before_token = '';
  1023. var resulting_string = this._input.read(this._match_pattern);
  1024. if (resulting_string === ' ') {
  1025. this.whitespace_before_token = ' ';
  1026. } else if (resulting_string) {
  1027. var matches = this.__split(this._newline_regexp, resulting_string);
  1028. this.newline_count = matches.length - 1;
  1029. this.whitespace_before_token = matches[this.newline_count];
  1030. }
  1031. return resulting_string;
  1032. };
  1033. WhitespacePattern.prototype.matching = function(whitespace_chars, newline_chars) {
  1034. var result = this._create();
  1035. result.__set_whitespace_patterns(whitespace_chars, newline_chars);
  1036. result._update();
  1037. return result;
  1038. };
  1039. WhitespacePattern.prototype._create = function() {
  1040. return new WhitespacePattern(this._input, this);
  1041. };
  1042. WhitespacePattern.prototype.__split = function(regexp, input_string) {
  1043. regexp.lastIndex = 0;
  1044. var start_index = 0;
  1045. var result = [];
  1046. var next_match = regexp.exec(input_string);
  1047. while (next_match) {
  1048. result.push(input_string.substring(start_index, next_match.index));
  1049. start_index = next_match.index + next_match[0].length;
  1050. next_match = regexp.exec(input_string);
  1051. }
  1052. if (start_index < input_string.length) {
  1053. result.push(input_string.substring(start_index, input_string.length));
  1054. } else {
  1055. result.push('');
  1056. }
  1057. return result;
  1058. };
  1059. module.exports.WhitespacePattern = WhitespacePattern;
  1060. /***/ }),
  1061. /* 12 */
  1062. /***/ (function(module) {
  1063. /*jshint node:true */
  1064. /*
  1065. The MIT License (MIT)
  1066. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  1067. Permission is hereby granted, free of charge, to any person
  1068. obtaining a copy of this software and associated documentation files
  1069. (the "Software"), to deal in the Software without restriction,
  1070. including without limitation the rights to use, copy, modify, merge,
  1071. publish, distribute, sublicense, and/or sell copies of the Software,
  1072. and to permit persons to whom the Software is furnished to do so,
  1073. subject to the following conditions:
  1074. The above copyright notice and this permission notice shall be
  1075. included in all copies or substantial portions of the Software.
  1076. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  1077. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1078. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  1079. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  1080. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  1081. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  1082. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1083. SOFTWARE.
  1084. */
  1085. function Pattern(input_scanner, parent) {
  1086. this._input = input_scanner;
  1087. this._starting_pattern = null;
  1088. this._match_pattern = null;
  1089. this._until_pattern = null;
  1090. this._until_after = false;
  1091. if (parent) {
  1092. this._starting_pattern = this._input.get_regexp(parent._starting_pattern, true);
  1093. this._match_pattern = this._input.get_regexp(parent._match_pattern, true);
  1094. this._until_pattern = this._input.get_regexp(parent._until_pattern);
  1095. this._until_after = parent._until_after;
  1096. }
  1097. }
  1098. Pattern.prototype.read = function() {
  1099. var result = this._input.read(this._starting_pattern);
  1100. if (!this._starting_pattern || result) {
  1101. result += this._input.read(this._match_pattern, this._until_pattern, this._until_after);
  1102. }
  1103. return result;
  1104. };
  1105. Pattern.prototype.read_match = function() {
  1106. return this._input.match(this._match_pattern);
  1107. };
  1108. Pattern.prototype.until_after = function(pattern) {
  1109. var result = this._create();
  1110. result._until_after = true;
  1111. result._until_pattern = this._input.get_regexp(pattern);
  1112. result._update();
  1113. return result;
  1114. };
  1115. Pattern.prototype.until = function(pattern) {
  1116. var result = this._create();
  1117. result._until_after = false;
  1118. result._until_pattern = this._input.get_regexp(pattern);
  1119. result._update();
  1120. return result;
  1121. };
  1122. Pattern.prototype.starting_with = function(pattern) {
  1123. var result = this._create();
  1124. result._starting_pattern = this._input.get_regexp(pattern, true);
  1125. result._update();
  1126. return result;
  1127. };
  1128. Pattern.prototype.matching = function(pattern) {
  1129. var result = this._create();
  1130. result._match_pattern = this._input.get_regexp(pattern, true);
  1131. result._update();
  1132. return result;
  1133. };
  1134. Pattern.prototype._create = function() {
  1135. return new Pattern(this._input, this);
  1136. };
  1137. Pattern.prototype._update = function() {};
  1138. module.exports.Pattern = Pattern;
  1139. /***/ }),
  1140. /* 13 */
  1141. /***/ (function(module) {
  1142. /*jshint node:true */
  1143. /*
  1144. The MIT License (MIT)
  1145. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  1146. Permission is hereby granted, free of charge, to any person
  1147. obtaining a copy of this software and associated documentation files
  1148. (the "Software"), to deal in the Software without restriction,
  1149. including without limitation the rights to use, copy, modify, merge,
  1150. publish, distribute, sublicense, and/or sell copies of the Software,
  1151. and to permit persons to whom the Software is furnished to do so,
  1152. subject to the following conditions:
  1153. The above copyright notice and this permission notice shall be
  1154. included in all copies or substantial portions of the Software.
  1155. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  1156. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1157. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  1158. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  1159. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  1160. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  1161. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1162. SOFTWARE.
  1163. */
  1164. function Directives(start_block_pattern, end_block_pattern) {
  1165. start_block_pattern = typeof start_block_pattern === 'string' ? start_block_pattern : start_block_pattern.source;
  1166. end_block_pattern = typeof end_block_pattern === 'string' ? end_block_pattern : end_block_pattern.source;
  1167. this.__directives_block_pattern = new RegExp(start_block_pattern + / beautify( \w+[:]\w+)+ /.source + end_block_pattern, 'g');
  1168. this.__directive_pattern = / (\w+)[:](\w+)/g;
  1169. this.__directives_end_ignore_pattern = new RegExp(start_block_pattern + /\sbeautify\signore:end\s/.source + end_block_pattern, 'g');
  1170. }
  1171. Directives.prototype.get_directives = function(text) {
  1172. if (!text.match(this.__directives_block_pattern)) {
  1173. return null;
  1174. }
  1175. var directives = {};
  1176. this.__directive_pattern.lastIndex = 0;
  1177. var directive_match = this.__directive_pattern.exec(text);
  1178. while (directive_match) {
  1179. directives[directive_match[1]] = directive_match[2];
  1180. directive_match = this.__directive_pattern.exec(text);
  1181. }
  1182. return directives;
  1183. };
  1184. Directives.prototype.readIgnored = function(input) {
  1185. return input.readUntilAfter(this.__directives_end_ignore_pattern);
  1186. };
  1187. module.exports.Directives = Directives;
  1188. /***/ }),
  1189. /* 14 */
  1190. /***/ (function(module, __unused_webpack_exports, __webpack_require__) {
  1191. /*jshint node:true */
  1192. /*
  1193. The MIT License (MIT)
  1194. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  1195. Permission is hereby granted, free of charge, to any person
  1196. obtaining a copy of this software and associated documentation files
  1197. (the "Software"), to deal in the Software without restriction,
  1198. including without limitation the rights to use, copy, modify, merge,
  1199. publish, distribute, sublicense, and/or sell copies of the Software,
  1200. and to permit persons to whom the Software is furnished to do so,
  1201. subject to the following conditions:
  1202. The above copyright notice and this permission notice shall be
  1203. included in all copies or substantial portions of the Software.
  1204. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  1205. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1206. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  1207. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  1208. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  1209. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  1210. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1211. SOFTWARE.
  1212. */
  1213. var Pattern = (__webpack_require__(12).Pattern);
  1214. var template_names = {
  1215. django: false,
  1216. erb: false,
  1217. handlebars: false,
  1218. php: false,
  1219. smarty: false,
  1220. angular: false
  1221. };
  1222. // This lets templates appear anywhere we would do a readUntil
  1223. // The cost is higher but it is pay to play.
  1224. function TemplatablePattern(input_scanner, parent) {
  1225. Pattern.call(this, input_scanner, parent);
  1226. this.__template_pattern = null;
  1227. this._disabled = Object.assign({}, template_names);
  1228. this._excluded = Object.assign({}, template_names);
  1229. if (parent) {
  1230. this.__template_pattern = this._input.get_regexp(parent.__template_pattern);
  1231. this._excluded = Object.assign(this._excluded, parent._excluded);
  1232. this._disabled = Object.assign(this._disabled, parent._disabled);
  1233. }
  1234. var pattern = new Pattern(input_scanner);
  1235. this.__patterns = {
  1236. handlebars_comment: pattern.starting_with(/{{!--/).until_after(/--}}/),
  1237. handlebars_unescaped: pattern.starting_with(/{{{/).until_after(/}}}/),
  1238. handlebars: pattern.starting_with(/{{/).until_after(/}}/),
  1239. php: pattern.starting_with(/<\?(?:[= ]|php)/).until_after(/\?>/),
  1240. erb: pattern.starting_with(/<%[^%]/).until_after(/[^%]%>/),
  1241. // django coflicts with handlebars a bit.
  1242. django: pattern.starting_with(/{%/).until_after(/%}/),
  1243. django_value: pattern.starting_with(/{{/).until_after(/}}/),
  1244. django_comment: pattern.starting_with(/{#/).until_after(/#}/),
  1245. smarty: pattern.starting_with(/{(?=[^}{\s\n])/).until_after(/[^\s\n]}/),
  1246. smarty_comment: pattern.starting_with(/{\*/).until_after(/\*}/),
  1247. smarty_literal: pattern.starting_with(/{literal}/).until_after(/{\/literal}/)
  1248. };
  1249. }
  1250. TemplatablePattern.prototype = new Pattern();
  1251. TemplatablePattern.prototype._create = function() {
  1252. return new TemplatablePattern(this._input, this);
  1253. };
  1254. TemplatablePattern.prototype._update = function() {
  1255. this.__set_templated_pattern();
  1256. };
  1257. TemplatablePattern.prototype.disable = function(language) {
  1258. var result = this._create();
  1259. result._disabled[language] = true;
  1260. result._update();
  1261. return result;
  1262. };
  1263. TemplatablePattern.prototype.read_options = function(options) {
  1264. var result = this._create();
  1265. for (var language in template_names) {
  1266. result._disabled[language] = options.templating.indexOf(language) === -1;
  1267. }
  1268. result._update();
  1269. return result;
  1270. };
  1271. TemplatablePattern.prototype.exclude = function(language) {
  1272. var result = this._create();
  1273. result._excluded[language] = true;
  1274. result._update();
  1275. return result;
  1276. };
  1277. TemplatablePattern.prototype.read = function() {
  1278. var result = '';
  1279. if (this._match_pattern) {
  1280. result = this._input.read(this._starting_pattern);
  1281. } else {
  1282. result = this._input.read(this._starting_pattern, this.__template_pattern);
  1283. }
  1284. var next = this._read_template();
  1285. while (next) {
  1286. if (this._match_pattern) {
  1287. next += this._input.read(this._match_pattern);
  1288. } else {
  1289. next += this._input.readUntil(this.__template_pattern);
  1290. }
  1291. result += next;
  1292. next = this._read_template();
  1293. }
  1294. if (this._until_after) {
  1295. result += this._input.readUntilAfter(this._until_pattern);
  1296. }
  1297. return result;
  1298. };
  1299. TemplatablePattern.prototype.__set_templated_pattern = function() {
  1300. var items = [];
  1301. if (!this._disabled.php) {
  1302. items.push(this.__patterns.php._starting_pattern.source);
  1303. }
  1304. if (!this._disabled.handlebars) {
  1305. items.push(this.__patterns.handlebars._starting_pattern.source);
  1306. }
  1307. if (!this._disabled.angular) {
  1308. // Handlebars ('{{' and '}}') are also special tokens in Angular)
  1309. items.push(this.__patterns.handlebars._starting_pattern.source);
  1310. }
  1311. if (!this._disabled.erb) {
  1312. items.push(this.__patterns.erb._starting_pattern.source);
  1313. }
  1314. if (!this._disabled.django) {
  1315. items.push(this.__patterns.django._starting_pattern.source);
  1316. // The starting pattern for django is more complex because it has different
  1317. // patterns for value, comment, and other sections
  1318. items.push(this.__patterns.django_value._starting_pattern.source);
  1319. items.push(this.__patterns.django_comment._starting_pattern.source);
  1320. }
  1321. if (!this._disabled.smarty) {
  1322. items.push(this.__patterns.smarty._starting_pattern.source);
  1323. }
  1324. if (this._until_pattern) {
  1325. items.push(this._until_pattern.source);
  1326. }
  1327. this.__template_pattern = this._input.get_regexp('(?:' + items.join('|') + ')');
  1328. };
  1329. TemplatablePattern.prototype._read_template = function() {
  1330. var resulting_string = '';
  1331. var c = this._input.peek();
  1332. if (c === '<') {
  1333. var peek1 = this._input.peek(1);
  1334. //if we're in a comment, do something special
  1335. // We treat all comments as literals, even more than preformatted tags
  1336. // we just look for the appropriate close tag
  1337. if (!this._disabled.php && !this._excluded.php && peek1 === '?') {
  1338. resulting_string = resulting_string ||
  1339. this.__patterns.php.read();
  1340. }
  1341. if (!this._disabled.erb && !this._excluded.erb && peek1 === '%') {
  1342. resulting_string = resulting_string ||
  1343. this.__patterns.erb.read();
  1344. }
  1345. } else if (c === '{') {
  1346. if (!this._disabled.handlebars && !this._excluded.handlebars) {
  1347. resulting_string = resulting_string ||
  1348. this.__patterns.handlebars_comment.read();
  1349. resulting_string = resulting_string ||
  1350. this.__patterns.handlebars_unescaped.read();
  1351. resulting_string = resulting_string ||
  1352. this.__patterns.handlebars.read();
  1353. }
  1354. if (!this._disabled.django) {
  1355. // django coflicts with handlebars a bit.
  1356. if (!this._excluded.django && !this._excluded.handlebars) {
  1357. resulting_string = resulting_string ||
  1358. this.__patterns.django_value.read();
  1359. }
  1360. if (!this._excluded.django) {
  1361. resulting_string = resulting_string ||
  1362. this.__patterns.django_comment.read();
  1363. resulting_string = resulting_string ||
  1364. this.__patterns.django.read();
  1365. }
  1366. }
  1367. if (!this._disabled.smarty) {
  1368. // smarty cannot be enabled with django or handlebars enabled
  1369. if (this._disabled.django && this._disabled.handlebars) {
  1370. resulting_string = resulting_string ||
  1371. this.__patterns.smarty_comment.read();
  1372. resulting_string = resulting_string ||
  1373. this.__patterns.smarty_literal.read();
  1374. resulting_string = resulting_string ||
  1375. this.__patterns.smarty.read();
  1376. }
  1377. }
  1378. }
  1379. return resulting_string;
  1380. };
  1381. module.exports.TemplatablePattern = TemplatablePattern;
  1382. /***/ }),
  1383. /* 15 */,
  1384. /* 16 */,
  1385. /* 17 */,
  1386. /* 18 */
  1387. /***/ (function(module, __unused_webpack_exports, __webpack_require__) {
  1388. /*jshint node:true */
  1389. /*
  1390. The MIT License (MIT)
  1391. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  1392. Permission is hereby granted, free of charge, to any person
  1393. obtaining a copy of this software and associated documentation files
  1394. (the "Software"), to deal in the Software without restriction,
  1395. including without limitation the rights to use, copy, modify, merge,
  1396. publish, distribute, sublicense, and/or sell copies of the Software,
  1397. and to permit persons to whom the Software is furnished to do so,
  1398. subject to the following conditions:
  1399. The above copyright notice and this permission notice shall be
  1400. included in all copies or substantial portions of the Software.
  1401. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  1402. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1403. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  1404. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  1405. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  1406. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  1407. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1408. SOFTWARE.
  1409. */
  1410. var Beautifier = (__webpack_require__(19).Beautifier),
  1411. Options = (__webpack_require__(20).Options);
  1412. function style_html(html_source, options, js_beautify, css_beautify) {
  1413. var beautifier = new Beautifier(html_source, options, js_beautify, css_beautify);
  1414. return beautifier.beautify();
  1415. }
  1416. module.exports = style_html;
  1417. module.exports.defaultOptions = function() {
  1418. return new Options();
  1419. };
  1420. /***/ }),
  1421. /* 19 */
  1422. /***/ (function(module, __unused_webpack_exports, __webpack_require__) {
  1423. /*jshint node:true */
  1424. /*
  1425. The MIT License (MIT)
  1426. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  1427. Permission is hereby granted, free of charge, to any person
  1428. obtaining a copy of this software and associated documentation files
  1429. (the "Software"), to deal in the Software without restriction,
  1430. including without limitation the rights to use, copy, modify, merge,
  1431. publish, distribute, sublicense, and/or sell copies of the Software,
  1432. and to permit persons to whom the Software is furnished to do so,
  1433. subject to the following conditions:
  1434. The above copyright notice and this permission notice shall be
  1435. included in all copies or substantial portions of the Software.
  1436. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  1437. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1438. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  1439. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  1440. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  1441. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  1442. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1443. SOFTWARE.
  1444. */
  1445. var Options = (__webpack_require__(20).Options);
  1446. var Output = (__webpack_require__(2).Output);
  1447. var Tokenizer = (__webpack_require__(21).Tokenizer);
  1448. var TOKEN = (__webpack_require__(21).TOKEN);
  1449. var lineBreak = /\r\n|[\r\n]/;
  1450. var allLineBreaks = /\r\n|[\r\n]/g;
  1451. var Printer = function(options, base_indent_string) { //handles input/output and some other printing functions
  1452. this.indent_level = 0;
  1453. this.alignment_size = 0;
  1454. this.max_preserve_newlines = options.max_preserve_newlines;
  1455. this.preserve_newlines = options.preserve_newlines;
  1456. this._output = new Output(options, base_indent_string);
  1457. };
  1458. Printer.prototype.current_line_has_match = function(pattern) {
  1459. return this._output.current_line.has_match(pattern);
  1460. };
  1461. Printer.prototype.set_space_before_token = function(value, non_breaking) {
  1462. this._output.space_before_token = value;
  1463. this._output.non_breaking_space = non_breaking;
  1464. };
  1465. Printer.prototype.set_wrap_point = function() {
  1466. this._output.set_indent(this.indent_level, this.alignment_size);
  1467. this._output.set_wrap_point();
  1468. };
  1469. Printer.prototype.add_raw_token = function(token) {
  1470. this._output.add_raw_token(token);
  1471. };
  1472. Printer.prototype.print_preserved_newlines = function(raw_token) {
  1473. var newlines = 0;
  1474. if (raw_token.type !== TOKEN.TEXT && raw_token.previous.type !== TOKEN.TEXT) {
  1475. newlines = raw_token.newlines ? 1 : 0;
  1476. }
  1477. if (this.preserve_newlines) {
  1478. newlines = raw_token.newlines < this.max_preserve_newlines + 1 ? raw_token.newlines : this.max_preserve_newlines + 1;
  1479. }
  1480. for (var n = 0; n < newlines; n++) {
  1481. this.print_newline(n > 0);
  1482. }
  1483. return newlines !== 0;
  1484. };
  1485. Printer.prototype.traverse_whitespace = function(raw_token) {
  1486. if (raw_token.whitespace_before || raw_token.newlines) {
  1487. if (!this.print_preserved_newlines(raw_token)) {
  1488. this._output.space_before_token = true;
  1489. }
  1490. return true;
  1491. }
  1492. return false;
  1493. };
  1494. Printer.prototype.previous_token_wrapped = function() {
  1495. return this._output.previous_token_wrapped;
  1496. };
  1497. Printer.prototype.print_newline = function(force) {
  1498. this._output.add_new_line(force);
  1499. };
  1500. Printer.prototype.print_token = function(token) {
  1501. if (token.text) {
  1502. this._output.set_indent(this.indent_level, this.alignment_size);
  1503. this._output.add_token(token.text);
  1504. }
  1505. };
  1506. Printer.prototype.indent = function() {
  1507. this.indent_level++;
  1508. };
  1509. Printer.prototype.deindent = function() {
  1510. if (this.indent_level > 0) {
  1511. this.indent_level--;
  1512. this._output.set_indent(this.indent_level, this.alignment_size);
  1513. }
  1514. };
  1515. Printer.prototype.get_full_indent = function(level) {
  1516. level = this.indent_level + (level || 0);
  1517. if (level < 1) {
  1518. return '';
  1519. }
  1520. return this._output.get_indent_string(level);
  1521. };
  1522. var get_type_attribute = function(start_token) {
  1523. var result = null;
  1524. var raw_token = start_token.next;
  1525. // Search attributes for a type attribute
  1526. while (raw_token.type !== TOKEN.EOF && start_token.closed !== raw_token) {
  1527. if (raw_token.type === TOKEN.ATTRIBUTE && raw_token.text === 'type') {
  1528. if (raw_token.next && raw_token.next.type === TOKEN.EQUALS &&
  1529. raw_token.next.next && raw_token.next.next.type === TOKEN.VALUE) {
  1530. result = raw_token.next.next.text;
  1531. }
  1532. break;
  1533. }
  1534. raw_token = raw_token.next;
  1535. }
  1536. return result;
  1537. };
  1538. var get_custom_beautifier_name = function(tag_check, raw_token) {
  1539. var typeAttribute = null;
  1540. var result = null;
  1541. if (!raw_token.closed) {
  1542. return null;
  1543. }
  1544. if (tag_check === 'script') {
  1545. typeAttribute = 'text/javascript';
  1546. } else if (tag_check === 'style') {
  1547. typeAttribute = 'text/css';
  1548. }
  1549. typeAttribute = get_type_attribute(raw_token) || typeAttribute;
  1550. // For script and style tags that have a type attribute, only enable custom beautifiers for matching values
  1551. // For those without a type attribute use default;
  1552. if (typeAttribute.search('text/css') > -1) {
  1553. result = 'css';
  1554. } else if (typeAttribute.search(/module|((text|application|dojo)\/(x-)?(javascript|ecmascript|jscript|livescript|(ld\+)?json|method|aspect))/) > -1) {
  1555. result = 'javascript';
  1556. } else if (typeAttribute.search(/(text|application|dojo)\/(x-)?(html)/) > -1) {
  1557. result = 'html';
  1558. } else if (typeAttribute.search(/test\/null/) > -1) {
  1559. // Test only mime-type for testing the beautifier when null is passed as beautifing function
  1560. result = 'null';
  1561. }
  1562. return result;
  1563. };
  1564. function in_array(what, arr) {
  1565. return arr.indexOf(what) !== -1;
  1566. }
  1567. function TagFrame(parent, parser_token, indent_level) {
  1568. this.parent = parent || null;
  1569. this.tag = parser_token ? parser_token.tag_name : '';
  1570. this.indent_level = indent_level || 0;
  1571. this.parser_token = parser_token || null;
  1572. }
  1573. function TagStack(printer) {
  1574. this._printer = printer;
  1575. this._current_frame = null;
  1576. }
  1577. TagStack.prototype.get_parser_token = function() {
  1578. return this._current_frame ? this._current_frame.parser_token : null;
  1579. };
  1580. TagStack.prototype.record_tag = function(parser_token) { //function to record a tag and its parent in this.tags Object
  1581. var new_frame = new TagFrame(this._current_frame, parser_token, this._printer.indent_level);
  1582. this._current_frame = new_frame;
  1583. };
  1584. TagStack.prototype._try_pop_frame = function(frame) { //function to retrieve the opening tag to the corresponding closer
  1585. var parser_token = null;
  1586. if (frame) {
  1587. parser_token = frame.parser_token;
  1588. this._printer.indent_level = frame.indent_level;
  1589. this._current_frame = frame.parent;
  1590. }
  1591. return parser_token;
  1592. };
  1593. TagStack.prototype._get_frame = function(tag_list, stop_list) { //function to retrieve the opening tag to the corresponding closer
  1594. var frame = this._current_frame;
  1595. while (frame) { //till we reach '' (the initial value);
  1596. if (tag_list.indexOf(frame.tag) !== -1) { //if this is it use it
  1597. break;
  1598. } else if (stop_list && stop_list.indexOf(frame.tag) !== -1) {
  1599. frame = null;
  1600. break;
  1601. }
  1602. frame = frame.parent;
  1603. }
  1604. return frame;
  1605. };
  1606. TagStack.prototype.try_pop = function(tag, stop_list) { //function to retrieve the opening tag to the corresponding closer
  1607. var frame = this._get_frame([tag], stop_list);
  1608. return this._try_pop_frame(frame);
  1609. };
  1610. TagStack.prototype.indent_to_tag = function(tag_list) {
  1611. var frame = this._get_frame(tag_list);
  1612. if (frame) {
  1613. this._printer.indent_level = frame.indent_level;
  1614. }
  1615. };
  1616. function Beautifier(source_text, options, js_beautify, css_beautify) {
  1617. //Wrapper function to invoke all the necessary constructors and deal with the output.
  1618. this._source_text = source_text || '';
  1619. options = options || {};
  1620. this._js_beautify = js_beautify;
  1621. this._css_beautify = css_beautify;
  1622. this._tag_stack = null;
  1623. // Allow the setting of language/file-type specific options
  1624. // with inheritance of overall settings
  1625. var optionHtml = new Options(options, 'html');
  1626. this._options = optionHtml;
  1627. this._is_wrap_attributes_force = this._options.wrap_attributes.substr(0, 'force'.length) === 'force';
  1628. this._is_wrap_attributes_force_expand_multiline = (this._options.wrap_attributes === 'force-expand-multiline');
  1629. this._is_wrap_attributes_force_aligned = (this._options.wrap_attributes === 'force-aligned');
  1630. this._is_wrap_attributes_aligned_multiple = (this._options.wrap_attributes === 'aligned-multiple');
  1631. this._is_wrap_attributes_preserve = this._options.wrap_attributes.substr(0, 'preserve'.length) === 'preserve';
  1632. this._is_wrap_attributes_preserve_aligned = (this._options.wrap_attributes === 'preserve-aligned');
  1633. }
  1634. Beautifier.prototype.beautify = function() {
  1635. // if disabled, return the input unchanged.
  1636. if (this._options.disabled) {
  1637. return this._source_text;
  1638. }
  1639. var source_text = this._source_text;
  1640. var eol = this._options.eol;
  1641. if (this._options.eol === 'auto') {
  1642. eol = '\n';
  1643. if (source_text && lineBreak.test(source_text)) {
  1644. eol = source_text.match(lineBreak)[0];
  1645. }
  1646. }
  1647. // HACK: newline parsing inconsistent. This brute force normalizes the input.
  1648. source_text = source_text.replace(allLineBreaks, '\n');
  1649. var baseIndentString = source_text.match(/^[\t ]*/)[0];
  1650. var last_token = {
  1651. text: '',
  1652. type: ''
  1653. };
  1654. var last_tag_token = new TagOpenParserToken(this._options);
  1655. var printer = new Printer(this._options, baseIndentString);
  1656. var tokens = new Tokenizer(source_text, this._options).tokenize();
  1657. this._tag_stack = new TagStack(printer);
  1658. var parser_token = null;
  1659. var raw_token = tokens.next();
  1660. while (raw_token.type !== TOKEN.EOF) {
  1661. if (raw_token.type === TOKEN.TAG_OPEN || raw_token.type === TOKEN.COMMENT) {
  1662. parser_token = this._handle_tag_open(printer, raw_token, last_tag_token, last_token, tokens);
  1663. last_tag_token = parser_token;
  1664. } else if ((raw_token.type === TOKEN.ATTRIBUTE || raw_token.type === TOKEN.EQUALS || raw_token.type === TOKEN.VALUE) ||
  1665. (raw_token.type === TOKEN.TEXT && !last_tag_token.tag_complete)) {
  1666. parser_token = this._handle_inside_tag(printer, raw_token, last_tag_token, last_token);
  1667. } else if (raw_token.type === TOKEN.TAG_CLOSE) {
  1668. parser_token = this._handle_tag_close(printer, raw_token, last_tag_token);
  1669. } else if (raw_token.type === TOKEN.TEXT) {
  1670. parser_token = this._handle_text(printer, raw_token, last_tag_token);
  1671. } else if (raw_token.type === TOKEN.CONTROL_FLOW_OPEN) {
  1672. parser_token = this._handle_control_flow_open(printer, raw_token);
  1673. } else if (raw_token.type === TOKEN.CONTROL_FLOW_CLOSE) {
  1674. parser_token = this._handle_control_flow_close(printer, raw_token);
  1675. } else {
  1676. // This should never happen, but if it does. Print the raw token
  1677. printer.add_raw_token(raw_token);
  1678. }
  1679. last_token = parser_token;
  1680. raw_token = tokens.next();
  1681. }
  1682. var sweet_code = printer._output.get_code(eol);
  1683. return sweet_code;
  1684. };
  1685. Beautifier.prototype._handle_control_flow_open = function(printer, raw_token) {
  1686. var parser_token = {
  1687. text: raw_token.text,
  1688. type: raw_token.type
  1689. };
  1690. printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true);
  1691. if (raw_token.newlines) {
  1692. printer.print_preserved_newlines(raw_token);
  1693. } else {
  1694. printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true);
  1695. }
  1696. printer.print_token(raw_token);
  1697. printer.indent();
  1698. return parser_token;
  1699. };
  1700. Beautifier.prototype._handle_control_flow_close = function(printer, raw_token) {
  1701. var parser_token = {
  1702. text: raw_token.text,
  1703. type: raw_token.type
  1704. };
  1705. printer.deindent();
  1706. if (raw_token.newlines) {
  1707. printer.print_preserved_newlines(raw_token);
  1708. } else {
  1709. printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true);
  1710. }
  1711. printer.print_token(raw_token);
  1712. return parser_token;
  1713. };
  1714. Beautifier.prototype._handle_tag_close = function(printer, raw_token, last_tag_token) {
  1715. var parser_token = {
  1716. text: raw_token.text,
  1717. type: raw_token.type
  1718. };
  1719. printer.alignment_size = 0;
  1720. last_tag_token.tag_complete = true;
  1721. printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true);
  1722. if (last_tag_token.is_unformatted) {
  1723. printer.add_raw_token(raw_token);
  1724. } else {
  1725. if (last_tag_token.tag_start_char === '<') {
  1726. printer.set_space_before_token(raw_token.text[0] === '/', true); // space before />, no space before >
  1727. if (this._is_wrap_attributes_force_expand_multiline && last_tag_token.has_wrapped_attrs) {
  1728. printer.print_newline(false);
  1729. }
  1730. }
  1731. printer.print_token(raw_token);
  1732. }
  1733. if (last_tag_token.indent_content &&
  1734. !(last_tag_token.is_unformatted || last_tag_token.is_content_unformatted)) {
  1735. printer.indent();
  1736. // only indent once per opened tag
  1737. last_tag_token.indent_content = false;
  1738. }
  1739. if (!last_tag_token.is_inline_element &&
  1740. !(last_tag_token.is_unformatted || last_tag_token.is_content_unformatted)) {
  1741. printer.set_wrap_point();
  1742. }
  1743. return parser_token;
  1744. };
  1745. Beautifier.prototype._handle_inside_tag = function(printer, raw_token, last_tag_token, last_token) {
  1746. var wrapped = last_tag_token.has_wrapped_attrs;
  1747. var parser_token = {
  1748. text: raw_token.text,
  1749. type: raw_token.type
  1750. };
  1751. printer.set_space_before_token(raw_token.newlines || raw_token.whitespace_before !== '', true);
  1752. if (last_tag_token.is_unformatted) {
  1753. printer.add_raw_token(raw_token);
  1754. } else if (last_tag_token.tag_start_char === '{' && raw_token.type === TOKEN.TEXT) {
  1755. // For the insides of handlebars allow newlines or a single space between open and contents
  1756. if (printer.print_preserved_newlines(raw_token)) {
  1757. raw_token.newlines = 0;
  1758. printer.add_raw_token(raw_token);
  1759. } else {
  1760. printer.print_token(raw_token);
  1761. }
  1762. } else {
  1763. if (raw_token.type === TOKEN.ATTRIBUTE) {
  1764. printer.set_space_before_token(true);
  1765. } else if (raw_token.type === TOKEN.EQUALS) { //no space before =
  1766. printer.set_space_before_token(false);
  1767. } else if (raw_token.type === TOKEN.VALUE && raw_token.previous.type === TOKEN.EQUALS) { //no space before value
  1768. printer.set_space_before_token(false);
  1769. }
  1770. if (raw_token.type === TOKEN.ATTRIBUTE && last_tag_token.tag_start_char === '<') {
  1771. if (this._is_wrap_attributes_preserve || this._is_wrap_attributes_preserve_aligned) {
  1772. printer.traverse_whitespace(raw_token);
  1773. wrapped = wrapped || raw_token.newlines !== 0;
  1774. }
  1775. // Wrap for 'force' options, and if the number of attributes is at least that specified in 'wrap_attributes_min_attrs':
  1776. // 1. always wrap the second and beyond attributes
  1777. // 2. wrap the first attribute only if 'force-expand-multiline' is specified
  1778. if (this._is_wrap_attributes_force &&
  1779. last_tag_token.attr_count >= this._options.wrap_attributes_min_attrs &&
  1780. (last_token.type !== TOKEN.TAG_OPEN || // ie. second attribute and beyond
  1781. this._is_wrap_attributes_force_expand_multiline)) {
  1782. printer.print_newline(false);
  1783. wrapped = true;
  1784. }
  1785. }
  1786. printer.print_token(raw_token);
  1787. wrapped = wrapped || printer.previous_token_wrapped();
  1788. last_tag_token.has_wrapped_attrs = wrapped;
  1789. }
  1790. return parser_token;
  1791. };
  1792. Beautifier.prototype._handle_text = function(printer, raw_token, last_tag_token) {
  1793. var parser_token = {
  1794. text: raw_token.text,
  1795. type: 'TK_CONTENT'
  1796. };
  1797. if (last_tag_token.custom_beautifier_name) { //check if we need to format javascript
  1798. this._print_custom_beatifier_text(printer, raw_token, last_tag_token);
  1799. } else if (last_tag_token.is_unformatted || last_tag_token.is_content_unformatted) {
  1800. printer.add_raw_token(raw_token);
  1801. } else {
  1802. printer.traverse_whitespace(raw_token);
  1803. printer.print_token(raw_token);
  1804. }
  1805. return parser_token;
  1806. };
  1807. Beautifier.prototype._print_custom_beatifier_text = function(printer, raw_token, last_tag_token) {
  1808. var local = this;
  1809. if (raw_token.text !== '') {
  1810. var text = raw_token.text,
  1811. _beautifier,
  1812. script_indent_level = 1,
  1813. pre = '',
  1814. post = '';
  1815. if (last_tag_token.custom_beautifier_name === 'javascript' && typeof this._js_beautify === 'function') {
  1816. _beautifier = this._js_beautify;
  1817. } else if (last_tag_token.custom_beautifier_name === 'css' && typeof this._css_beautify === 'function') {
  1818. _beautifier = this._css_beautify;
  1819. } else if (last_tag_token.custom_beautifier_name === 'html') {
  1820. _beautifier = function(html_source, options) {
  1821. var beautifier = new Beautifier(html_source, options, local._js_beautify, local._css_beautify);
  1822. return beautifier.beautify();
  1823. };
  1824. }
  1825. if (this._options.indent_scripts === "keep") {
  1826. script_indent_level = 0;
  1827. } else if (this._options.indent_scripts === "separate") {
  1828. script_indent_level = -printer.indent_level;
  1829. }
  1830. var indentation = printer.get_full_indent(script_indent_level);
  1831. // if there is at least one empty line at the end of this text, strip it
  1832. // we'll be adding one back after the text but before the containing tag.
  1833. text = text.replace(/\n[ \t]*$/, '');
  1834. // Handle the case where content is wrapped in a comment or cdata.
  1835. if (last_tag_token.custom_beautifier_name !== 'html' &&
  1836. text[0] === '<' && text.match(/^(<!--|<!\[CDATA\[)/)) {
  1837. var matched = /^(<!--[^\n]*|<!\[CDATA\[)(\n?)([ \t\n]*)([\s\S]*)(-->|]]>)$/.exec(text);
  1838. // if we start to wrap but don't finish, print raw
  1839. if (!matched) {
  1840. printer.add_raw_token(raw_token);
  1841. return;
  1842. }
  1843. pre = indentation + matched[1] + '\n';
  1844. text = matched[4];
  1845. if (matched[5]) {
  1846. post = indentation + matched[5];
  1847. }
  1848. // if there is at least one empty line at the end of this text, strip it
  1849. // we'll be adding one back after the text but before the containing tag.
  1850. text = text.replace(/\n[ \t]*$/, '');
  1851. if (matched[2] || matched[3].indexOf('\n') !== -1) {
  1852. // if the first line of the non-comment text has spaces
  1853. // use that as the basis for indenting in null case.
  1854. matched = matched[3].match(/[ \t]+$/);
  1855. if (matched) {
  1856. raw_token.whitespace_before = matched[0];
  1857. }
  1858. }
  1859. }
  1860. if (text) {
  1861. if (_beautifier) {
  1862. // call the Beautifier if avaliable
  1863. var Child_options = function() {
  1864. this.eol = '\n';
  1865. };
  1866. Child_options.prototype = this._options.raw_options;
  1867. var child_options = new Child_options();
  1868. text = _beautifier(indentation + text, child_options);
  1869. } else {
  1870. // simply indent the string otherwise
  1871. var white = raw_token.whitespace_before;
  1872. if (white) {
  1873. text = text.replace(new RegExp('\n(' + white + ')?', 'g'), '\n');
  1874. }
  1875. text = indentation + text.replace(/\n/g, '\n' + indentation);
  1876. }
  1877. }
  1878. if (pre) {
  1879. if (!text) {
  1880. text = pre + post;
  1881. } else {
  1882. text = pre + text + '\n' + post;
  1883. }
  1884. }
  1885. printer.print_newline(false);
  1886. if (text) {
  1887. raw_token.text = text;
  1888. raw_token.whitespace_before = '';
  1889. raw_token.newlines = 0;
  1890. printer.add_raw_token(raw_token);
  1891. printer.print_newline(true);
  1892. }
  1893. }
  1894. };
  1895. Beautifier.prototype._handle_tag_open = function(printer, raw_token, last_tag_token, last_token, tokens) {
  1896. var parser_token = this._get_tag_open_token(raw_token);
  1897. if ((last_tag_token.is_unformatted || last_tag_token.is_content_unformatted) &&
  1898. !last_tag_token.is_empty_element &&
  1899. raw_token.type === TOKEN.TAG_OPEN && !parser_token.is_start_tag) {
  1900. // End element tags for unformatted or content_unformatted elements
  1901. // are printed raw to keep any newlines inside them exactly the same.
  1902. printer.add_raw_token(raw_token);
  1903. parser_token.start_tag_token = this._tag_stack.try_pop(parser_token.tag_name);
  1904. } else {
  1905. printer.traverse_whitespace(raw_token);
  1906. this._set_tag_position(printer, raw_token, parser_token, last_tag_token, last_token);
  1907. if (!parser_token.is_inline_element) {
  1908. printer.set_wrap_point();
  1909. }
  1910. printer.print_token(raw_token);
  1911. }
  1912. // count the number of attributes
  1913. if (parser_token.is_start_tag && this._is_wrap_attributes_force) {
  1914. var peek_index = 0;
  1915. var peek_token;
  1916. do {
  1917. peek_token = tokens.peek(peek_index);
  1918. if (peek_token.type === TOKEN.ATTRIBUTE) {
  1919. parser_token.attr_count += 1;
  1920. }
  1921. peek_index += 1;
  1922. } while (peek_token.type !== TOKEN.EOF && peek_token.type !== TOKEN.TAG_CLOSE);
  1923. }
  1924. //indent attributes an auto, forced, aligned or forced-align line-wrap
  1925. if (this._is_wrap_attributes_force_aligned || this._is_wrap_attributes_aligned_multiple || this._is_wrap_attributes_preserve_aligned) {
  1926. parser_token.alignment_size = raw_token.text.length + 1;
  1927. }
  1928. if (!parser_token.tag_complete && !parser_token.is_unformatted) {
  1929. printer.alignment_size = parser_token.alignment_size;
  1930. }
  1931. return parser_token;
  1932. };
  1933. var TagOpenParserToken = function(options, parent, raw_token) {
  1934. this.parent = parent || null;
  1935. this.text = '';
  1936. this.type = 'TK_TAG_OPEN';
  1937. this.tag_name = '';
  1938. this.is_inline_element = false;
  1939. this.is_unformatted = false;
  1940. this.is_content_unformatted = false;
  1941. this.is_empty_element = false;
  1942. this.is_start_tag = false;
  1943. this.is_end_tag = false;
  1944. this.indent_content = false;
  1945. this.multiline_content = false;
  1946. this.custom_beautifier_name = null;
  1947. this.start_tag_token = null;
  1948. this.attr_count = 0;
  1949. this.has_wrapped_attrs = false;
  1950. this.alignment_size = 0;
  1951. this.tag_complete = false;
  1952. this.tag_start_char = '';
  1953. this.tag_check = '';
  1954. if (!raw_token) {
  1955. this.tag_complete = true;
  1956. } else {
  1957. var tag_check_match;
  1958. this.tag_start_char = raw_token.text[0];
  1959. this.text = raw_token.text;
  1960. if (this.tag_start_char === '<') {
  1961. tag_check_match = raw_token.text.match(/^<([^\s>]*)/);
  1962. this.tag_check = tag_check_match ? tag_check_match[1] : '';
  1963. } else {
  1964. tag_check_match = raw_token.text.match(/^{{~?(?:[\^]|#\*?)?([^\s}]+)/);
  1965. this.tag_check = tag_check_match ? tag_check_match[1] : '';
  1966. // handle "{{#> myPartial}}" or "{{~#> myPartial}}"
  1967. if ((raw_token.text.startsWith('{{#>') || raw_token.text.startsWith('{{~#>')) && this.tag_check[0] === '>') {
  1968. if (this.tag_check === '>' && raw_token.next !== null) {
  1969. this.tag_check = raw_token.next.text.split(' ')[0];
  1970. } else {
  1971. this.tag_check = raw_token.text.split('>')[1];
  1972. }
  1973. }
  1974. }
  1975. this.tag_check = this.tag_check.toLowerCase();
  1976. if (raw_token.type === TOKEN.COMMENT) {
  1977. this.tag_complete = true;
  1978. }
  1979. this.is_start_tag = this.tag_check.charAt(0) !== '/';
  1980. this.tag_name = !this.is_start_tag ? this.tag_check.substr(1) : this.tag_check;
  1981. this.is_end_tag = !this.is_start_tag ||
  1982. (raw_token.closed && raw_token.closed.text === '/>');
  1983. // if whitespace handler ~ included (i.e. {{~#if true}}), handlebars tags start at pos 3 not pos 2
  1984. var handlebar_starts = 2;
  1985. if (this.tag_start_char === '{' && this.text.length >= 3) {
  1986. if (this.text.charAt(2) === '~') {
  1987. handlebar_starts = 3;
  1988. }
  1989. }
  1990. // handlebars tags that don't start with # or ^ are single_tags, and so also start and end.
  1991. // if they start with # or ^, they are still considered single tags if indenting of handlebars is set to false
  1992. this.is_end_tag = this.is_end_tag ||
  1993. (this.tag_start_char === '{' && (!options.indent_handlebars || this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(handlebar_starts)))));
  1994. }
  1995. };
  1996. Beautifier.prototype._get_tag_open_token = function(raw_token) { //function to get a full tag and parse its type
  1997. var parser_token = new TagOpenParserToken(this._options, this._tag_stack.get_parser_token(), raw_token);
  1998. parser_token.alignment_size = this._options.wrap_attributes_indent_size;
  1999. parser_token.is_end_tag = parser_token.is_end_tag ||
  2000. in_array(parser_token.tag_check, this._options.void_elements);
  2001. parser_token.is_empty_element = parser_token.tag_complete ||
  2002. (parser_token.is_start_tag && parser_token.is_end_tag);
  2003. parser_token.is_unformatted = !parser_token.tag_complete && in_array(parser_token.tag_check, this._options.unformatted);
  2004. parser_token.is_content_unformatted = !parser_token.is_empty_element && in_array(parser_token.tag_check, this._options.content_unformatted);
  2005. parser_token.is_inline_element = in_array(parser_token.tag_name, this._options.inline) || (this._options.inline_custom_elements && parser_token.tag_name.includes("-")) || parser_token.tag_start_char === '{';
  2006. return parser_token;
  2007. };
  2008. Beautifier.prototype._set_tag_position = function(printer, raw_token, parser_token, last_tag_token, last_token) {
  2009. if (!parser_token.is_empty_element) {
  2010. if (parser_token.is_end_tag) { //this tag is a double tag so check for tag-ending
  2011. parser_token.start_tag_token = this._tag_stack.try_pop(parser_token.tag_name); //remove it and all ancestors
  2012. } else { // it's a start-tag
  2013. // check if this tag is starting an element that has optional end element
  2014. // and do an ending needed
  2015. if (this._do_optional_end_element(parser_token)) {
  2016. if (!parser_token.is_inline_element) {
  2017. printer.print_newline(false);
  2018. }
  2019. }
  2020. this._tag_stack.record_tag(parser_token); //push it on the tag stack
  2021. if ((parser_token.tag_name === 'script' || parser_token.tag_name === 'style') &&
  2022. !(parser_token.is_unformatted || parser_token.is_content_unformatted)) {
  2023. parser_token.custom_beautifier_name = get_custom_beautifier_name(parser_token.tag_check, raw_token);
  2024. }
  2025. }
  2026. }
  2027. if (in_array(parser_token.tag_check, this._options.extra_liners)) { //check if this double needs an extra line
  2028. printer.print_newline(false);
  2029. if (!printer._output.just_added_blankline()) {
  2030. printer.print_newline(true);
  2031. }
  2032. }
  2033. if (parser_token.is_empty_element) { //if this tag name is a single tag type (either in the list or has a closing /)
  2034. // if you hit an else case, reset the indent level if you are inside an:
  2035. // 'if', 'unless', or 'each' block.
  2036. if (parser_token.tag_start_char === '{' && parser_token.tag_check === 'else') {
  2037. this._tag_stack.indent_to_tag(['if', 'unless', 'each']);
  2038. parser_token.indent_content = true;
  2039. // Don't add a newline if opening {{#if}} tag is on the current line
  2040. var foundIfOnCurrentLine = printer.current_line_has_match(/{{#if/);
  2041. if (!foundIfOnCurrentLine) {
  2042. printer.print_newline(false);
  2043. }
  2044. }
  2045. // Don't add a newline before elements that should remain where they are.
  2046. if (parser_token.tag_name === '!--' && last_token.type === TOKEN.TAG_CLOSE &&
  2047. last_tag_token.is_end_tag && parser_token.text.indexOf('\n') === -1) {
  2048. //Do nothing. Leave comments on same line.
  2049. } else {
  2050. if (!(parser_token.is_inline_element || parser_token.is_unformatted)) {
  2051. printer.print_newline(false);
  2052. }
  2053. this._calcluate_parent_multiline(printer, parser_token);
  2054. }
  2055. } else if (parser_token.is_end_tag) { //this tag is a double tag so check for tag-ending
  2056. var do_end_expand = false;
  2057. // deciding whether a block is multiline should not be this hard
  2058. do_end_expand = parser_token.start_tag_token && parser_token.start_tag_token.multiline_content;
  2059. do_end_expand = do_end_expand || (!parser_token.is_inline_element &&
  2060. !(last_tag_token.is_inline_element || last_tag_token.is_unformatted) &&
  2061. !(last_token.type === TOKEN.TAG_CLOSE && parser_token.start_tag_token === last_tag_token) &&
  2062. last_token.type !== 'TK_CONTENT'
  2063. );
  2064. if (parser_token.is_content_unformatted || parser_token.is_unformatted) {
  2065. do_end_expand = false;
  2066. }
  2067. if (do_end_expand) {
  2068. printer.print_newline(false);
  2069. }
  2070. } else { // it's a start-tag
  2071. parser_token.indent_content = !parser_token.custom_beautifier_name;
  2072. if (parser_token.tag_start_char === '<') {
  2073. if (parser_token.tag_name === 'html') {
  2074. parser_token.indent_content = this._options.indent_inner_html;
  2075. } else if (parser_token.tag_name === 'head') {
  2076. parser_token.indent_content = this._options.indent_head_inner_html;
  2077. } else if (parser_token.tag_name === 'body') {
  2078. parser_token.indent_content = this._options.indent_body_inner_html;
  2079. }
  2080. }
  2081. if (!(parser_token.is_inline_element || parser_token.is_unformatted) &&
  2082. (last_token.type !== 'TK_CONTENT' || parser_token.is_content_unformatted)) {
  2083. printer.print_newline(false);
  2084. }
  2085. this._calcluate_parent_multiline(printer, parser_token);
  2086. }
  2087. };
  2088. Beautifier.prototype._calcluate_parent_multiline = function(printer, parser_token) {
  2089. if (parser_token.parent && printer._output.just_added_newline() &&
  2090. !((parser_token.is_inline_element || parser_token.is_unformatted) && parser_token.parent.is_inline_element)) {
  2091. parser_token.parent.multiline_content = true;
  2092. }
  2093. };
  2094. //To be used for <p> tag special case:
  2095. var p_closers = ['address', 'article', 'aside', 'blockquote', 'details', 'div', 'dl', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hr', 'main', 'menu', 'nav', 'ol', 'p', 'pre', 'section', 'table', 'ul'];
  2096. var p_parent_excludes = ['a', 'audio', 'del', 'ins', 'map', 'noscript', 'video'];
  2097. Beautifier.prototype._do_optional_end_element = function(parser_token) {
  2098. var result = null;
  2099. // NOTE: cases of "if there is no more content in the parent element"
  2100. // are handled automatically by the beautifier.
  2101. // It assumes parent or ancestor close tag closes all children.
  2102. // https://www.w3.org/TR/html5/syntax.html#optional-tags
  2103. if (parser_token.is_empty_element || !parser_token.is_start_tag || !parser_token.parent) {
  2104. return;
  2105. }
  2106. if (parser_token.tag_name === 'body') {
  2107. // A head element’s end tag may be omitted if the head element is not immediately followed by a space character or a comment.
  2108. result = result || this._tag_stack.try_pop('head');
  2109. //} else if (parser_token.tag_name === 'body') {
  2110. // DONE: A body element’s end tag may be omitted if the body element is not immediately followed by a comment.
  2111. } else if (parser_token.tag_name === 'li') {
  2112. // An li element’s end tag may be omitted if the li element is immediately followed by another li element or if there is no more content in the parent element.
  2113. result = result || this._tag_stack.try_pop('li', ['ol', 'ul', 'menu']);
  2114. } else if (parser_token.tag_name === 'dd' || parser_token.tag_name === 'dt') {
  2115. // A dd element’s end tag may be omitted if the dd element is immediately followed by another dd element or a dt element, or if there is no more content in the parent element.
  2116. // A dt element’s end tag may be omitted if the dt element is immediately followed by another dt element or a dd element.
  2117. result = result || this._tag_stack.try_pop('dt', ['dl']);
  2118. result = result || this._tag_stack.try_pop('dd', ['dl']);
  2119. } else if (parser_token.parent.tag_name === 'p' && p_closers.indexOf(parser_token.tag_name) !== -1) {
  2120. // IMPORTANT: this else-if works because p_closers has no overlap with any other element we look for in this method
  2121. // check for the parent element is an HTML element that is not an <a>, <audio>, <del>, <ins>, <map>, <noscript>, or <video> element, or an autonomous custom element.
  2122. // To do this right, this needs to be coded as an inclusion of the inverse of the exclusion above.
  2123. // But to start with (if we ignore "autonomous custom elements") the exclusion would be fine.
  2124. var p_parent = parser_token.parent.parent;
  2125. if (!p_parent || p_parent_excludes.indexOf(p_parent.tag_name) === -1) {
  2126. result = result || this._tag_stack.try_pop('p');
  2127. }
  2128. } else if (parser_token.tag_name === 'rp' || parser_token.tag_name === 'rt') {
  2129. // An rt element’s end tag may be omitted if the rt element is immediately followed by an rt or rp element, or if there is no more content in the parent element.
  2130. // An rp element’s end tag may be omitted if the rp element is immediately followed by an rt or rp element, or if there is no more content in the parent element.
  2131. result = result || this._tag_stack.try_pop('rt', ['ruby', 'rtc']);
  2132. result = result || this._tag_stack.try_pop('rp', ['ruby', 'rtc']);
  2133. } else if (parser_token.tag_name === 'optgroup') {
  2134. // An optgroup element’s end tag may be omitted if the optgroup element is immediately followed by another optgroup element, or if there is no more content in the parent element.
  2135. // An option element’s end tag may be omitted if the option element is immediately followed by another option element, or if it is immediately followed by an optgroup element, or if there is no more content in the parent element.
  2136. result = result || this._tag_stack.try_pop('optgroup', ['select']);
  2137. //result = result || this._tag_stack.try_pop('option', ['select']);
  2138. } else if (parser_token.tag_name === 'option') {
  2139. // An option element’s end tag may be omitted if the option element is immediately followed by another option element, or if it is immediately followed by an optgroup element, or if there is no more content in the parent element.
  2140. result = result || this._tag_stack.try_pop('option', ['select', 'datalist', 'optgroup']);
  2141. } else if (parser_token.tag_name === 'colgroup') {
  2142. // DONE: A colgroup element’s end tag may be omitted if the colgroup element is not immediately followed by a space character or a comment.
  2143. // A caption element's end tag may be ommitted if a colgroup, thead, tfoot, tbody, or tr element is started.
  2144. result = result || this._tag_stack.try_pop('caption', ['table']);
  2145. } else if (parser_token.tag_name === 'thead') {
  2146. // A colgroup element's end tag may be ommitted if a thead, tfoot, tbody, or tr element is started.
  2147. // A caption element's end tag may be ommitted if a colgroup, thead, tfoot, tbody, or tr element is started.
  2148. result = result || this._tag_stack.try_pop('caption', ['table']);
  2149. result = result || this._tag_stack.try_pop('colgroup', ['table']);
  2150. //} else if (parser_token.tag_name === 'caption') {
  2151. // DONE: A caption element’s end tag may be omitted if the caption element is not immediately followed by a space character or a comment.
  2152. } else if (parser_token.tag_name === 'tbody' || parser_token.tag_name === 'tfoot') {
  2153. // A thead element’s end tag may be omitted if the thead element is immediately followed by a tbody or tfoot element.
  2154. // A tbody element’s end tag may be omitted if the tbody element is immediately followed by a tbody or tfoot element, or if there is no more content in the parent element.
  2155. // A colgroup element's end tag may be ommitted if a thead, tfoot, tbody, or tr element is started.
  2156. // A caption element's end tag may be ommitted if a colgroup, thead, tfoot, tbody, or tr element is started.
  2157. result = result || this._tag_stack.try_pop('caption', ['table']);
  2158. result = result || this._tag_stack.try_pop('colgroup', ['table']);
  2159. result = result || this._tag_stack.try_pop('thead', ['table']);
  2160. result = result || this._tag_stack.try_pop('tbody', ['table']);
  2161. //} else if (parser_token.tag_name === 'tfoot') {
  2162. // DONE: A tfoot element’s end tag may be omitted if there is no more content in the parent element.
  2163. } else if (parser_token.tag_name === 'tr') {
  2164. // A tr element’s end tag may be omitted if the tr element is immediately followed by another tr element, or if there is no more content in the parent element.
  2165. // A colgroup element's end tag may be ommitted if a thead, tfoot, tbody, or tr element is started.
  2166. // A caption element's end tag may be ommitted if a colgroup, thead, tfoot, tbody, or tr element is started.
  2167. result = result || this._tag_stack.try_pop('caption', ['table']);
  2168. result = result || this._tag_stack.try_pop('colgroup', ['table']);
  2169. result = result || this._tag_stack.try_pop('tr', ['table', 'thead', 'tbody', 'tfoot']);
  2170. } else if (parser_token.tag_name === 'th' || parser_token.tag_name === 'td') {
  2171. // A td element’s end tag may be omitted if the td element is immediately followed by a td or th element, or if there is no more content in the parent element.
  2172. // A th element’s end tag may be omitted if the th element is immediately followed by a td or th element, or if there is no more content in the parent element.
  2173. result = result || this._tag_stack.try_pop('td', ['table', 'thead', 'tbody', 'tfoot', 'tr']);
  2174. result = result || this._tag_stack.try_pop('th', ['table', 'thead', 'tbody', 'tfoot', 'tr']);
  2175. }
  2176. // Start element omission not handled currently
  2177. // A head element’s start tag may be omitted if the element is empty, or if the first thing inside the head element is an element.
  2178. // A tbody element’s start tag may be omitted if the first thing inside the tbody element is a tr element, and if the element is not immediately preceded by a tbody, thead, or tfoot element whose end tag has been omitted. (It can’t be omitted if the element is empty.)
  2179. // A colgroup element’s start tag may be omitted if the first thing inside the colgroup element is a col element, and if the element is not immediately preceded by another colgroup element whose end tag has been omitted. (It can’t be omitted if the element is empty.)
  2180. // Fix up the parent of the parser token
  2181. parser_token.parent = this._tag_stack.get_parser_token();
  2182. return result;
  2183. };
  2184. module.exports.Beautifier = Beautifier;
  2185. /***/ }),
  2186. /* 20 */
  2187. /***/ (function(module, __unused_webpack_exports, __webpack_require__) {
  2188. /*jshint node:true */
  2189. /*
  2190. The MIT License (MIT)
  2191. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  2192. Permission is hereby granted, free of charge, to any person
  2193. obtaining a copy of this software and associated documentation files
  2194. (the "Software"), to deal in the Software without restriction,
  2195. including without limitation the rights to use, copy, modify, merge,
  2196. publish, distribute, sublicense, and/or sell copies of the Software,
  2197. and to permit persons to whom the Software is furnished to do so,
  2198. subject to the following conditions:
  2199. The above copyright notice and this permission notice shall be
  2200. included in all copies or substantial portions of the Software.
  2201. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  2202. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2203. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  2204. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  2205. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  2206. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  2207. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  2208. SOFTWARE.
  2209. */
  2210. var BaseOptions = (__webpack_require__(6).Options);
  2211. function Options(options) {
  2212. BaseOptions.call(this, options, 'html');
  2213. if (this.templating.length === 1 && this.templating[0] === 'auto') {
  2214. this.templating = ['django', 'erb', 'handlebars', 'php'];
  2215. }
  2216. this.indent_inner_html = this._get_boolean('indent_inner_html');
  2217. this.indent_body_inner_html = this._get_boolean('indent_body_inner_html', true);
  2218. this.indent_head_inner_html = this._get_boolean('indent_head_inner_html', true);
  2219. this.indent_handlebars = this._get_boolean('indent_handlebars', true);
  2220. this.wrap_attributes = this._get_selection('wrap_attributes',
  2221. ['auto', 'force', 'force-aligned', 'force-expand-multiline', 'aligned-multiple', 'preserve', 'preserve-aligned']);
  2222. this.wrap_attributes_min_attrs = this._get_number('wrap_attributes_min_attrs', 2);
  2223. this.wrap_attributes_indent_size = this._get_number('wrap_attributes_indent_size', this.indent_size);
  2224. this.extra_liners = this._get_array('extra_liners', ['head', 'body', '/html']);
  2225. // Block vs inline elements
  2226. // https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements
  2227. // https://developer.mozilla.org/en-US/docs/Web/HTML/Inline_elements
  2228. // https://www.w3.org/TR/html5/dom.html#phrasing-content
  2229. this.inline = this._get_array('inline', [
  2230. 'a', 'abbr', 'area', 'audio', 'b', 'bdi', 'bdo', 'br', 'button', 'canvas', 'cite',
  2231. 'code', 'data', 'datalist', 'del', 'dfn', 'em', 'embed', 'i', 'iframe', 'img',
  2232. 'input', 'ins', 'kbd', 'keygen', 'label', 'map', 'mark', 'math', 'meter', 'noscript',
  2233. 'object', 'output', 'progress', 'q', 'ruby', 's', 'samp', /* 'script', */ 'select', 'small',
  2234. 'span', 'strong', 'sub', 'sup', 'svg', 'template', 'textarea', 'time', 'u', 'var',
  2235. 'video', 'wbr', 'text',
  2236. // obsolete inline tags
  2237. 'acronym', 'big', 'strike', 'tt'
  2238. ]);
  2239. this.inline_custom_elements = this._get_boolean('inline_custom_elements', true);
  2240. this.void_elements = this._get_array('void_elements', [
  2241. // HTLM void elements - aka self-closing tags - aka singletons
  2242. // https://www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements
  2243. 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen',
  2244. 'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr',
  2245. // NOTE: Optional tags are too complex for a simple list
  2246. // they are hard coded in _do_optional_end_element
  2247. // Doctype and xml elements
  2248. '!doctype', '?xml',
  2249. // obsolete tags
  2250. // basefont: https://www.computerhope.com/jargon/h/html-basefont-tag.htm
  2251. // isndex: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/isindex
  2252. 'basefont', 'isindex'
  2253. ]);
  2254. this.unformatted = this._get_array('unformatted', []);
  2255. this.content_unformatted = this._get_array('content_unformatted', [
  2256. 'pre', 'textarea'
  2257. ]);
  2258. this.unformatted_content_delimiter = this._get_characters('unformatted_content_delimiter');
  2259. this.indent_scripts = this._get_selection('indent_scripts', ['normal', 'keep', 'separate']);
  2260. }
  2261. Options.prototype = new BaseOptions();
  2262. module.exports.Options = Options;
  2263. /***/ }),
  2264. /* 21 */
  2265. /***/ (function(module, __unused_webpack_exports, __webpack_require__) {
  2266. /*jshint node:true */
  2267. /*
  2268. The MIT License (MIT)
  2269. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  2270. Permission is hereby granted, free of charge, to any person
  2271. obtaining a copy of this software and associated documentation files
  2272. (the "Software"), to deal in the Software without restriction,
  2273. including without limitation the rights to use, copy, modify, merge,
  2274. publish, distribute, sublicense, and/or sell copies of the Software,
  2275. and to permit persons to whom the Software is furnished to do so,
  2276. subject to the following conditions:
  2277. The above copyright notice and this permission notice shall be
  2278. included in all copies or substantial portions of the Software.
  2279. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  2280. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2281. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  2282. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  2283. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  2284. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  2285. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  2286. SOFTWARE.
  2287. */
  2288. var BaseTokenizer = (__webpack_require__(9).Tokenizer);
  2289. var BASETOKEN = (__webpack_require__(9).TOKEN);
  2290. var Directives = (__webpack_require__(13).Directives);
  2291. var TemplatablePattern = (__webpack_require__(14).TemplatablePattern);
  2292. var Pattern = (__webpack_require__(12).Pattern);
  2293. var TOKEN = {
  2294. TAG_OPEN: 'TK_TAG_OPEN',
  2295. TAG_CLOSE: 'TK_TAG_CLOSE',
  2296. CONTROL_FLOW_OPEN: 'TK_CONTROL_FLOW_OPEN',
  2297. CONTROL_FLOW_CLOSE: 'TK_CONTROL_FLOW_CLOSE',
  2298. ATTRIBUTE: 'TK_ATTRIBUTE',
  2299. EQUALS: 'TK_EQUALS',
  2300. VALUE: 'TK_VALUE',
  2301. COMMENT: 'TK_COMMENT',
  2302. TEXT: 'TK_TEXT',
  2303. UNKNOWN: 'TK_UNKNOWN',
  2304. START: BASETOKEN.START,
  2305. RAW: BASETOKEN.RAW,
  2306. EOF: BASETOKEN.EOF
  2307. };
  2308. var directives_core = new Directives(/<\!--/, /-->/);
  2309. var Tokenizer = function(input_string, options) {
  2310. BaseTokenizer.call(this, input_string, options);
  2311. this._current_tag_name = '';
  2312. // Words end at whitespace or when a tag starts
  2313. // if we are indenting handlebars, they are considered tags
  2314. var templatable_reader = new TemplatablePattern(this._input).read_options(this._options);
  2315. var pattern_reader = new Pattern(this._input);
  2316. this.__patterns = {
  2317. word: templatable_reader.until(/[\n\r\t <]/),
  2318. word_control_flow_close_excluded: templatable_reader.until(/[\n\r\t <}]/),
  2319. single_quote: templatable_reader.until_after(/'/),
  2320. double_quote: templatable_reader.until_after(/"/),
  2321. attribute: templatable_reader.until(/[\n\r\t =>]|\/>/),
  2322. element_name: templatable_reader.until(/[\n\r\t >\/]/),
  2323. angular_control_flow_start: pattern_reader.matching(/\@[a-zA-Z]+[^({]*[({]/),
  2324. handlebars_comment: pattern_reader.starting_with(/{{!--/).until_after(/--}}/),
  2325. handlebars: pattern_reader.starting_with(/{{/).until_after(/}}/),
  2326. handlebars_open: pattern_reader.until(/[\n\r\t }]/),
  2327. handlebars_raw_close: pattern_reader.until(/}}/),
  2328. comment: pattern_reader.starting_with(/<!--/).until_after(/-->/),
  2329. cdata: pattern_reader.starting_with(/<!\[CDATA\[/).until_after(/]]>/),
  2330. // https://en.wikipedia.org/wiki/Conditional_comment
  2331. conditional_comment: pattern_reader.starting_with(/<!\[/).until_after(/]>/),
  2332. processing: pattern_reader.starting_with(/<\?/).until_after(/\?>/)
  2333. };
  2334. if (this._options.indent_handlebars) {
  2335. this.__patterns.word = this.__patterns.word.exclude('handlebars');
  2336. this.__patterns.word_control_flow_close_excluded = this.__patterns.word_control_flow_close_excluded.exclude('handlebars');
  2337. }
  2338. this._unformatted_content_delimiter = null;
  2339. if (this._options.unformatted_content_delimiter) {
  2340. var literal_regexp = this._input.get_literal_regexp(this._options.unformatted_content_delimiter);
  2341. this.__patterns.unformatted_content_delimiter =
  2342. pattern_reader.matching(literal_regexp)
  2343. .until_after(literal_regexp);
  2344. }
  2345. };
  2346. Tokenizer.prototype = new BaseTokenizer();
  2347. Tokenizer.prototype._is_comment = function(current_token) { // jshint unused:false
  2348. return false; //current_token.type === TOKEN.COMMENT || current_token.type === TOKEN.UNKNOWN;
  2349. };
  2350. Tokenizer.prototype._is_opening = function(current_token) {
  2351. return current_token.type === TOKEN.TAG_OPEN || current_token.type === TOKEN.CONTROL_FLOW_OPEN;
  2352. };
  2353. Tokenizer.prototype._is_closing = function(current_token, open_token) {
  2354. return (current_token.type === TOKEN.TAG_CLOSE &&
  2355. (open_token && (
  2356. ((current_token.text === '>' || current_token.text === '/>') && open_token.text[0] === '<') ||
  2357. (current_token.text === '}}' && open_token.text[0] === '{' && open_token.text[1] === '{')))
  2358. ) || (current_token.type === TOKEN.CONTROL_FLOW_CLOSE &&
  2359. (current_token.text === '}' && open_token.text.endsWith('{')));
  2360. };
  2361. Tokenizer.prototype._reset = function() {
  2362. this._current_tag_name = '';
  2363. };
  2364. Tokenizer.prototype._get_next_token = function(previous_token, open_token) { // jshint unused:false
  2365. var token = null;
  2366. this._readWhitespace();
  2367. var c = this._input.peek();
  2368. if (c === null) {
  2369. return this._create_token(TOKEN.EOF, '');
  2370. }
  2371. token = token || this._read_open_handlebars(c, open_token);
  2372. token = token || this._read_attribute(c, previous_token, open_token);
  2373. token = token || this._read_close(c, open_token);
  2374. token = token || this._read_script_and_style(c, previous_token);
  2375. token = token || this._read_control_flows(c, open_token);
  2376. token = token || this._read_raw_content(c, previous_token, open_token);
  2377. token = token || this._read_content_word(c, open_token);
  2378. token = token || this._read_comment_or_cdata(c);
  2379. token = token || this._read_processing(c);
  2380. token = token || this._read_open(c, open_token);
  2381. token = token || this._create_token(TOKEN.UNKNOWN, this._input.next());
  2382. return token;
  2383. };
  2384. Tokenizer.prototype._read_comment_or_cdata = function(c) { // jshint unused:false
  2385. var token = null;
  2386. var resulting_string = null;
  2387. var directives = null;
  2388. if (c === '<') {
  2389. var peek1 = this._input.peek(1);
  2390. // We treat all comments as literals, even more than preformatted tags
  2391. // we only look for the appropriate closing marker
  2392. if (peek1 === '!') {
  2393. resulting_string = this.__patterns.comment.read();
  2394. // only process directive on html comments
  2395. if (resulting_string) {
  2396. directives = directives_core.get_directives(resulting_string);
  2397. if (directives && directives.ignore === 'start') {
  2398. resulting_string += directives_core.readIgnored(this._input);
  2399. }
  2400. } else {
  2401. resulting_string = this.__patterns.cdata.read();
  2402. }
  2403. }
  2404. if (resulting_string) {
  2405. token = this._create_token(TOKEN.COMMENT, resulting_string);
  2406. token.directives = directives;
  2407. }
  2408. }
  2409. return token;
  2410. };
  2411. Tokenizer.prototype._read_processing = function(c) { // jshint unused:false
  2412. var token = null;
  2413. var resulting_string = null;
  2414. var directives = null;
  2415. if (c === '<') {
  2416. var peek1 = this._input.peek(1);
  2417. if (peek1 === '!' || peek1 === '?') {
  2418. resulting_string = this.__patterns.conditional_comment.read();
  2419. resulting_string = resulting_string || this.__patterns.processing.read();
  2420. }
  2421. if (resulting_string) {
  2422. token = this._create_token(TOKEN.COMMENT, resulting_string);
  2423. token.directives = directives;
  2424. }
  2425. }
  2426. return token;
  2427. };
  2428. Tokenizer.prototype._read_open = function(c, open_token) {
  2429. var resulting_string = null;
  2430. var token = null;
  2431. if (!open_token || open_token.type === TOKEN.CONTROL_FLOW_OPEN) {
  2432. if (c === '<') {
  2433. resulting_string = this._input.next();
  2434. if (this._input.peek() === '/') {
  2435. resulting_string += this._input.next();
  2436. }
  2437. resulting_string += this.__patterns.element_name.read();
  2438. token = this._create_token(TOKEN.TAG_OPEN, resulting_string);
  2439. }
  2440. }
  2441. return token;
  2442. };
  2443. Tokenizer.prototype._read_open_handlebars = function(c, open_token) {
  2444. var resulting_string = null;
  2445. var token = null;
  2446. if (!open_token || open_token.type === TOKEN.CONTROL_FLOW_OPEN) {
  2447. if ((this._options.templating.includes('angular') || this._options.indent_handlebars) && c === '{' && this._input.peek(1) === '{') {
  2448. if (this._options.indent_handlebars && this._input.peek(2) === '!') {
  2449. resulting_string = this.__patterns.handlebars_comment.read();
  2450. resulting_string = resulting_string || this.__patterns.handlebars.read();
  2451. token = this._create_token(TOKEN.COMMENT, resulting_string);
  2452. } else {
  2453. resulting_string = this.__patterns.handlebars_open.read();
  2454. token = this._create_token(TOKEN.TAG_OPEN, resulting_string);
  2455. }
  2456. }
  2457. }
  2458. return token;
  2459. };
  2460. Tokenizer.prototype._read_control_flows = function(c, open_token) {
  2461. var resulting_string = '';
  2462. var token = null;
  2463. // Only check for control flows if angular templating is set
  2464. if (!this._options.templating.includes('angular')) {
  2465. return token;
  2466. }
  2467. if (c === '@') {
  2468. resulting_string = this.__patterns.angular_control_flow_start.read();
  2469. if (resulting_string === '') {
  2470. return token;
  2471. }
  2472. var opening_parentheses_count = resulting_string.endsWith('(') ? 1 : 0;
  2473. var closing_parentheses_count = 0;
  2474. // The opening brace of the control flow is where the number of opening and closing parentheses equal
  2475. // e.g. @if({value: true} !== null) {
  2476. while (!(resulting_string.endsWith('{') && opening_parentheses_count === closing_parentheses_count)) {
  2477. var next_char = this._input.next();
  2478. if (next_char === null) {
  2479. break;
  2480. } else if (next_char === '(') {
  2481. opening_parentheses_count++;
  2482. } else if (next_char === ')') {
  2483. closing_parentheses_count++;
  2484. }
  2485. resulting_string += next_char;
  2486. }
  2487. token = this._create_token(TOKEN.CONTROL_FLOW_OPEN, resulting_string);
  2488. } else if (c === '}' && open_token && open_token.type === TOKEN.CONTROL_FLOW_OPEN) {
  2489. resulting_string = this._input.next();
  2490. token = this._create_token(TOKEN.CONTROL_FLOW_CLOSE, resulting_string);
  2491. }
  2492. return token;
  2493. };
  2494. Tokenizer.prototype._read_close = function(c, open_token) {
  2495. var resulting_string = null;
  2496. var token = null;
  2497. if (open_token && open_token.type === TOKEN.TAG_OPEN) {
  2498. if (open_token.text[0] === '<' && (c === '>' || (c === '/' && this._input.peek(1) === '>'))) {
  2499. resulting_string = this._input.next();
  2500. if (c === '/') { // for close tag "/>"
  2501. resulting_string += this._input.next();
  2502. }
  2503. token = this._create_token(TOKEN.TAG_CLOSE, resulting_string);
  2504. } else if (open_token.text[0] === '{' && c === '}' && this._input.peek(1) === '}') {
  2505. this._input.next();
  2506. this._input.next();
  2507. token = this._create_token(TOKEN.TAG_CLOSE, '}}');
  2508. }
  2509. }
  2510. return token;
  2511. };
  2512. Tokenizer.prototype._read_attribute = function(c, previous_token, open_token) {
  2513. var token = null;
  2514. var resulting_string = '';
  2515. if (open_token && open_token.text[0] === '<') {
  2516. if (c === '=') {
  2517. token = this._create_token(TOKEN.EQUALS, this._input.next());
  2518. } else if (c === '"' || c === "'") {
  2519. var content = this._input.next();
  2520. if (c === '"') {
  2521. content += this.__patterns.double_quote.read();
  2522. } else {
  2523. content += this.__patterns.single_quote.read();
  2524. }
  2525. token = this._create_token(TOKEN.VALUE, content);
  2526. } else {
  2527. resulting_string = this.__patterns.attribute.read();
  2528. if (resulting_string) {
  2529. if (previous_token.type === TOKEN.EQUALS) {
  2530. token = this._create_token(TOKEN.VALUE, resulting_string);
  2531. } else {
  2532. token = this._create_token(TOKEN.ATTRIBUTE, resulting_string);
  2533. }
  2534. }
  2535. }
  2536. }
  2537. return token;
  2538. };
  2539. Tokenizer.prototype._is_content_unformatted = function(tag_name) {
  2540. // void_elements have no content and so cannot have unformatted content
  2541. // script and style tags should always be read as unformatted content
  2542. // finally content_unformatted and unformatted element contents are unformatted
  2543. return this._options.void_elements.indexOf(tag_name) === -1 &&
  2544. (this._options.content_unformatted.indexOf(tag_name) !== -1 ||
  2545. this._options.unformatted.indexOf(tag_name) !== -1);
  2546. };
  2547. Tokenizer.prototype._read_raw_content = function(c, previous_token, open_token) { // jshint unused:false
  2548. var resulting_string = '';
  2549. if (open_token && open_token.text[0] === '{') {
  2550. resulting_string = this.__patterns.handlebars_raw_close.read();
  2551. } else if (previous_token.type === TOKEN.TAG_CLOSE &&
  2552. previous_token.opened.text[0] === '<' && previous_token.text[0] !== '/') {
  2553. // ^^ empty tag has no content
  2554. var tag_name = previous_token.opened.text.substr(1).toLowerCase();
  2555. if (this._is_content_unformatted(tag_name)) {
  2556. resulting_string = this._input.readUntil(new RegExp('</' + tag_name + '[\\n\\r\\t ]*?>', 'ig'));
  2557. }
  2558. }
  2559. if (resulting_string) {
  2560. return this._create_token(TOKEN.TEXT, resulting_string);
  2561. }
  2562. return null;
  2563. };
  2564. Tokenizer.prototype._read_script_and_style = function(c, previous_token) { // jshint unused:false
  2565. if (previous_token.type === TOKEN.TAG_CLOSE && previous_token.opened.text[0] === '<' && previous_token.text[0] !== '/') {
  2566. var tag_name = previous_token.opened.text.substr(1).toLowerCase();
  2567. if (tag_name === 'script' || tag_name === 'style') {
  2568. // Script and style tags are allowed to have comments wrapping their content
  2569. // or just have regular content.
  2570. var token = this._read_comment_or_cdata(c);
  2571. if (token) {
  2572. token.type = TOKEN.TEXT;
  2573. return token;
  2574. }
  2575. var resulting_string = this._input.readUntil(new RegExp('</' + tag_name + '[\\n\\r\\t ]*?>', 'ig'));
  2576. if (resulting_string) {
  2577. return this._create_token(TOKEN.TEXT, resulting_string);
  2578. }
  2579. }
  2580. }
  2581. return null;
  2582. };
  2583. Tokenizer.prototype._read_content_word = function(c, open_token) {
  2584. var resulting_string = '';
  2585. if (this._options.unformatted_content_delimiter) {
  2586. if (c === this._options.unformatted_content_delimiter[0]) {
  2587. resulting_string = this.__patterns.unformatted_content_delimiter.read();
  2588. }
  2589. }
  2590. if (!resulting_string) {
  2591. resulting_string = (open_token && open_token.type === TOKEN.CONTROL_FLOW_OPEN) ? this.__patterns.word_control_flow_close_excluded.read() : this.__patterns.word.read();
  2592. }
  2593. if (resulting_string) {
  2594. return this._create_token(TOKEN.TEXT, resulting_string);
  2595. }
  2596. return null;
  2597. };
  2598. module.exports.Tokenizer = Tokenizer;
  2599. module.exports.TOKEN = TOKEN;
  2600. /***/ })
  2601. /******/ ]);
  2602. /************************************************************************/
  2603. /******/ // The module cache
  2604. /******/ var __webpack_module_cache__ = {};
  2605. /******/
  2606. /******/ // The require function
  2607. /******/ function __webpack_require__(moduleId) {
  2608. /******/ // Check if module is in cache
  2609. /******/ var cachedModule = __webpack_module_cache__[moduleId];
  2610. /******/ if (cachedModule !== undefined) {
  2611. /******/ return cachedModule.exports;
  2612. /******/ }
  2613. /******/ // Create a new module (and put it into the cache)
  2614. /******/ var module = __webpack_module_cache__[moduleId] = {
  2615. /******/ // no module.id needed
  2616. /******/ // no module.loaded needed
  2617. /******/ exports: {}
  2618. /******/ };
  2619. /******/
  2620. /******/ // Execute the module function
  2621. /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
  2622. /******/
  2623. /******/ // Return the exports of the module
  2624. /******/ return module.exports;
  2625. /******/ }
  2626. /******/
  2627. /************************************************************************/
  2628. /******/
  2629. /******/ // startup
  2630. /******/ // Load entry module and return exports
  2631. /******/ // This entry module is referenced by other modules so it can't be inlined
  2632. /******/ var __webpack_exports__ = __webpack_require__(18);
  2633. /******/ legacy_beautify_html = __webpack_exports__;
  2634. /******/
  2635. /******/ })()
  2636. ;
  2637. var style_html = legacy_beautify_html;
  2638. /* Footer */
  2639. if (typeof define === "function" && define.amd) {
  2640. // Add support for AMD ( https://github.com/amdjs/amdjs-api/wiki/AMD#defineamd-property- )
  2641. define(["require", "./beautify", "./beautify-css"], function(requireamd) {
  2642. var js_beautify = requireamd("./beautify");
  2643. var css_beautify = requireamd("./beautify-css");
  2644. return {
  2645. html_beautify: function(html_source, options) {
  2646. return style_html(html_source, options, js_beautify.js_beautify, css_beautify.css_beautify);
  2647. }
  2648. };
  2649. });
  2650. } else if (typeof exports !== "undefined") {
  2651. // Add support for CommonJS. Just put this file somewhere on your require.paths
  2652. // and you will be able to `var html_beautify = require("beautify").html_beautify`.
  2653. var js_beautify = require('./beautify.js');
  2654. var css_beautify = require('./beautify-css.js');
  2655. exports.html_beautify = function(html_source, options) {
  2656. return style_html(html_source, options, js_beautify.js_beautify, css_beautify.css_beautify);
  2657. };
  2658. } else if (typeof window !== "undefined") {
  2659. // If we're running a web page and don't have either of the above, add our one global
  2660. window.html_beautify = function(html_source, options) {
  2661. return style_html(html_source, options, window.js_beautify, window.css_beautify);
  2662. };
  2663. } else if (typeof global !== "undefined") {
  2664. // If we don't even have window, try global.
  2665. global.html_beautify = function(html_source, options) {
  2666. return style_html(html_source, options, global.js_beautify, global.css_beautify);
  2667. };
  2668. }
  2669. }());