1
0

Scopes.swift 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. //
  2. // HttpHandlers+Scopes.swift
  3. // Swifter
  4. //
  5. // Copyright © 2014-2016 Damian Kołakowski. All rights reserved.
  6. //
  7. // swiftlint:disable file_length
  8. import Foundation
  9. public func scopes(_ scope: @escaping Closure) -> ((HttpRequest) -> HttpResponse) {
  10. return { _ in
  11. scopesBuffer[Process.tid] = ""
  12. scope()
  13. return .raw(200, "OK", ["Content-Type": "text/html"], {
  14. try? $0.write([UInt8](("<!DOCTYPE html>" + (scopesBuffer[Process.tid] ?? "")).utf8))
  15. })
  16. }
  17. }
  18. public typealias Closure = () -> Void
  19. public var idd: String?
  20. public var dir: String?
  21. public var rel: String?
  22. public var rev: String?
  23. public var alt: String?
  24. public var forr: String?
  25. public var src: String?
  26. public var type: String?
  27. public var href: String?
  28. public var text: String?
  29. public var abbr: String?
  30. public var size: String?
  31. public var face: String?
  32. public var char: String?
  33. public var cite: String?
  34. public var span: String?
  35. public var data: String?
  36. public var axis: String?
  37. public var Name: String?
  38. public var name: String?
  39. public var code: String?
  40. public var link: String?
  41. public var lang: String?
  42. public var cols: String?
  43. public var rows: String?
  44. public var ismap: String?
  45. public var shape: String?
  46. public var style: String?
  47. public var alink: String?
  48. public var width: String?
  49. public var rules: String?
  50. public var align: String?
  51. public var frame: String?
  52. public var vlink: String?
  53. public var deferr: String?
  54. public var color: String?
  55. public var media: String?
  56. public var title: String?
  57. public var scope: String?
  58. public var classs: String?
  59. public var manifest: String?
  60. public var value: String?
  61. public var clear: String?
  62. public var start: String?
  63. public var label: String?
  64. public var action: String?
  65. public var height: String?
  66. public var method: String?
  67. public var acceptt: String?
  68. public var object: String?
  69. public var scheme: String?
  70. public var coords: String?
  71. public var usemap: String?
  72. public var onblur: String?
  73. public var nohref: String?
  74. public var nowrap: String?
  75. public var hspace: String?
  76. public var border: String?
  77. public var valign: String?
  78. public var vspace: String?
  79. public var onload: String?
  80. public var target: String?
  81. public var prompt: String?
  82. public var onfocus: String?
  83. public var enctype: String?
  84. public var onclick: String?
  85. public var ontouchstart: String?
  86. public var onkeyup: String?
  87. public var profile: String?
  88. public var version: String?
  89. public var onreset: String?
  90. public var charset: String?
  91. public var standby: String?
  92. public var colspan: String?
  93. public var charoff: String?
  94. public var classid: String?
  95. public var compact: String?
  96. public var declare: String?
  97. public var rowspan: String?
  98. public var checked: String?
  99. public var archive: String?
  100. public var bgcolor: String?
  101. public var content: String?
  102. public var noshade: String?
  103. public var summary: String?
  104. public var headers: String?
  105. public var onselect: String?
  106. public var readonly: String?
  107. public var tabindex: String?
  108. public var onchange: String?
  109. public var noresize: String?
  110. public var disabled: String?
  111. public var longdesc: String?
  112. public var codebase: String?
  113. public var language: String?
  114. public var datetime: String?
  115. public var selected: String?
  116. public var hreflang: String?
  117. public var onsubmit: String?
  118. public var multiple: String?
  119. public var onunload: String?
  120. public var codetype: String?
  121. public var scrolling: String?
  122. public var onkeydown: String?
  123. public var maxlength: String?
  124. public var valuetype: String?
  125. public var accesskey: String?
  126. public var onmouseup: String?
  127. public var autofocus: String?
  128. public var onkeypress: String?
  129. public var ondblclick: String?
  130. public var onmouseout: String?
  131. public var httpEquiv: String?
  132. public var dataText: String?
  133. public var background: String?
  134. public var onmousemove: String?
  135. public var onmouseover: String?
  136. public var cellpadding: String?
  137. public var onmousedown: String?
  138. public var frameborder: String?
  139. public var marginwidth: String?
  140. public var cellspacing: String?
  141. public var placeholder: String?
  142. public var marginheight: String?
  143. public var acceptCharset: String?
  144. public var inner: String?
  145. public func a(_ closure: Closure) { element("a", closure) }
  146. public func b(_ closure: Closure) { element("b", closure) }
  147. public func i(_ closure: Closure) { element("i", closure) }
  148. public func p(_ closure: Closure) { element("p", closure) }
  149. public func q(_ closure: Closure) { element("q", closure) }
  150. public func s(_ closure: Closure) { element("s", closure) }
  151. public func u(_ closure: Closure) { element("u", closure) }
  152. public func br(_ closure: Closure) { element("br", closure) }
  153. public func dd(_ closure: Closure) { element("dd", closure) }
  154. public func dl(_ closure: Closure) { element("dl", closure) }
  155. public func dt(_ closure: Closure) { element("dt", closure) }
  156. public func em(_ closure: Closure) { element("em", closure) }
  157. public func hr(_ closure: Closure) { element("hr", closure) }
  158. public func li(_ closure: Closure) { element("li", closure) }
  159. public func ol(_ closure: Closure) { element("ol", closure) }
  160. public func rp(_ closure: Closure) { element("rp", closure) }
  161. public func rt(_ closure: Closure) { element("rt", closure) }
  162. public func td(_ closure: Closure) { element("td", closure) }
  163. public func th(_ closure: Closure) { element("th", closure) }
  164. public func tr(_ closure: Closure) { element("tr", closure) }
  165. public func tt(_ closure: Closure) { element("tt", closure) }
  166. public func ul(_ closure: Closure) { element("ul", closure) }
  167. public func ul<T: Sequence>(_ collection: T, _ closure: @escaping (T.Iterator.Element) -> Void) {
  168. element("ul", {
  169. for item in collection {
  170. closure(item)
  171. }
  172. })
  173. }
  174. public func h1(_ closure: Closure) { element("h1", closure) }
  175. public func h2(_ closure: Closure) { element("h2", closure) }
  176. public func h3(_ closure: Closure) { element("h3", closure) }
  177. public func h4(_ closure: Closure) { element("h4", closure) }
  178. public func h5(_ closure: Closure) { element("h5", closure) }
  179. public func h6(_ closure: Closure) { element("h6", closure) }
  180. public func bdi(_ closure: Closure) { element("bdi", closure) }
  181. public func bdo(_ closure: Closure) { element("bdo", closure) }
  182. public func big(_ closure: Closure) { element("big", closure) }
  183. public func col(_ closure: Closure) { element("col", closure) }
  184. public func del(_ closure: Closure) { element("del", closure) }
  185. public func dfn(_ closure: Closure) { element("dfn", closure) }
  186. public func dir(_ closure: Closure) { element("dir", closure) }
  187. public func div(_ closure: Closure) { element("div", closure) }
  188. public func img(_ closure: Closure) { element("img", closure) }
  189. public func ins(_ closure: Closure) { element("ins", closure) }
  190. public func kbd(_ closure: Closure) { element("kbd", closure) }
  191. public func map(_ closure: Closure) { element("map", closure) }
  192. public func nav(_ closure: Closure) { element("nav", closure) }
  193. public func pre(_ closure: Closure) { element("pre", closure) }
  194. public func rtc(_ closure: Closure) { element("rtc", closure) }
  195. public func sub(_ closure: Closure) { element("sub", closure) }
  196. public func sup(_ closure: Closure) { element("sup", closure) }
  197. public func varr(_ closure: Closure) { element("var", closure) }
  198. public func wbr(_ closure: Closure) { element("wbr", closure) }
  199. public func xmp(_ closure: Closure) { element("xmp", closure) }
  200. public func abbr(_ closure: Closure) { element("abbr", closure) }
  201. public func area(_ closure: Closure) { element("area", closure) }
  202. public func base(_ closure: Closure) { element("base", closure) }
  203. public func body(_ closure: Closure) { element("body", closure) }
  204. public func cite(_ closure: Closure) { element("cite", closure) }
  205. public func code(_ closure: Closure) { element("code", closure) }
  206. public func data(_ closure: Closure) { element("data", closure) }
  207. public func font(_ closure: Closure) { element("font", closure) }
  208. public func form(_ closure: Closure) { element("form", closure) }
  209. public func head(_ closure: Closure) { element("head", closure) }
  210. public func html(_ closure: Closure) { element("html", closure) }
  211. public func link(_ closure: Closure) { element("link", closure) }
  212. public func main(_ closure: Closure) { element("main", closure) }
  213. public func mark(_ closure: Closure) { element("mark", closure) }
  214. public func menu(_ closure: Closure) { element("menu", closure) }
  215. public func meta(_ closure: Closure) { element("meta", closure) }
  216. public func nobr(_ closure: Closure) { element("nobr", closure) }
  217. public func ruby(_ closure: Closure) { element("ruby", closure) }
  218. public func samp(_ closure: Closure) { element("samp", closure) }
  219. public func span(_ closure: Closure) { element("span", closure) }
  220. public func time(_ closure: Closure) { element("time", closure) }
  221. public func aside(_ closure: Closure) { element("aside", closure) }
  222. public func audio(_ closure: Closure) { element("audio", closure) }
  223. public func blink(_ closure: Closure) { element("blink", closure) }
  224. public func embed(_ closure: Closure) { element("embed", closure) }
  225. public func frame(_ closure: Closure) { element("frame", closure) }
  226. public func image(_ closure: Closure) { element("image", closure) }
  227. public func input(_ closure: Closure) { element("input", closure) }
  228. public func label(_ closure: Closure) { element("label", closure) }
  229. public func meter(_ closure: Closure) { element("meter", closure) }
  230. public func param(_ closure: Closure) { element("param", closure) }
  231. public func small(_ closure: Closure) { element("small", closure) }
  232. public func style(_ closure: Closure) { element("style", closure) }
  233. public func table(_ closure: Closure) { element("table", closure) }
  234. public func table<T: Sequence>(_ collection: T, closure: @escaping (T.Iterator.Element) -> Void) {
  235. element("table", {
  236. for item in collection {
  237. closure(item)
  238. }
  239. })
  240. }
  241. public func tbody(_ closure: Closure) { element("tbody", closure) }
  242. public func tbody<T: Sequence>(_ collection: T, closure: @escaping (T.Iterator.Element) -> Void) {
  243. element("tbody", {
  244. for item in collection {
  245. closure(item)
  246. }
  247. })
  248. }
  249. public func tfoot(_ closure: Closure) { element("tfoot", closure) }
  250. public func thead(_ closure: Closure) { element("thead", closure) }
  251. public func title(_ closure: Closure) { element("title", closure) }
  252. public func track(_ closure: Closure) { element("track", closure) }
  253. public func video(_ closure: Closure) { element("video", closure) }
  254. public func applet(_ closure: Closure) { element("applet", closure) }
  255. public func button(_ closure: Closure) { element("button", closure) }
  256. public func canvas(_ closure: Closure) { element("canvas", closure) }
  257. public func center(_ closure: Closure) { element("center", closure) }
  258. public func dialog(_ closure: Closure) { element("dialog", closure) }
  259. public func figure(_ closure: Closure) { element("figure", closure) }
  260. public func footer(_ closure: Closure) { element("footer", closure) }
  261. public func header(_ closure: Closure) { element("header", closure) }
  262. public func hgroup(_ closure: Closure) { element("hgroup", closure) }
  263. public func iframe(_ closure: Closure) { element("iframe", closure) }
  264. public func keygen(_ closure: Closure) { element("keygen", closure) }
  265. public func legend(_ closure: Closure) { element("legend", closure) }
  266. public func object(_ closure: Closure) { element("object", closure) }
  267. public func option(_ closure: Closure) { element("option", closure) }
  268. public func output(_ closure: Closure) { element("output", closure) }
  269. public func script(_ closure: Closure) { element("script", closure) }
  270. public func select(_ closure: Closure) { element("select", closure) }
  271. public func shadow(_ closure: Closure) { element("shadow", closure) }
  272. public func source(_ closure: Closure) { element("source", closure) }
  273. public func spacer(_ closure: Closure) { element("spacer", closure) }
  274. public func strike(_ closure: Closure) { element("strike", closure) }
  275. public func strong(_ closure: Closure) { element("strong", closure) }
  276. public func acronym(_ closure: Closure) { element("acronym", closure) }
  277. public func address(_ closure: Closure) { element("address", closure) }
  278. public func article(_ closure: Closure) { element("article", closure) }
  279. public func bgsound(_ closure: Closure) { element("bgsound", closure) }
  280. public func caption(_ closure: Closure) { element("caption", closure) }
  281. public func command(_ closure: Closure) { element("command", closure) }
  282. public func content(_ closure: Closure) { element("content", closure) }
  283. public func details(_ closure: Closure) { element("details", closure) }
  284. public func elementt(_ closure: Closure) { element("element", closure) }
  285. public func isindex(_ closure: Closure) { element("isindex", closure) }
  286. public func listing(_ closure: Closure) { element("listing", closure) }
  287. public func marquee(_ closure: Closure) { element("marquee", closure) }
  288. public func noembed(_ closure: Closure) { element("noembed", closure) }
  289. public func picture(_ closure: Closure) { element("picture", closure) }
  290. public func section(_ closure: Closure) { element("section", closure) }
  291. public func summary(_ closure: Closure) { element("summary", closure) }
  292. public func basefont(_ closure: Closure) { element("basefont", closure) }
  293. public func colgroup(_ closure: Closure) { element("colgroup", closure) }
  294. public func datalist(_ closure: Closure) { element("datalist", closure) }
  295. public func fieldset(_ closure: Closure) { element("fieldset", closure) }
  296. public func frameset(_ closure: Closure) { element("frameset", closure) }
  297. public func menuitem(_ closure: Closure) { element("menuitem", closure) }
  298. public func multicol(_ closure: Closure) { element("multicol", closure) }
  299. public func noframes(_ closure: Closure) { element("noframes", closure) }
  300. public func noscript(_ closure: Closure) { element("noscript", closure) }
  301. public func optgroup(_ closure: Closure) { element("optgroup", closure) }
  302. public func progress(_ closure: Closure) { element("progress", closure) }
  303. public func template(_ closure: Closure) { element("template", closure) }
  304. public func textarea(_ closure: Closure) { element("textarea", closure) }
  305. public func plaintext(_ closure: Closure) { element("plaintext", closure) }
  306. public func javascript(_ closure: Closure) { element("script", ["type": "text/javascript"], closure) }
  307. public func blockquote(_ closure: Closure) { element("blockquote", closure) }
  308. public func figcaption(_ closure: Closure) { element("figcaption", closure) }
  309. public func stylesheet(_ closure: Closure) { element("link", ["rel": "stylesheet", "type": "text/css"], closure) }
  310. public func element(_ node: String, _ closure: Closure) { evaluate(node, [:], closure) }
  311. public func element(_ node: String, _ attrs: [String: String?] = [:], _ closure: Closure) { evaluate(node, attrs, closure) }
  312. var scopesBuffer = [UInt64: String]()
  313. // swiftlint:disable cyclomatic_complexity function_body_length
  314. private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closure: Closure) {
  315. // Push the attributes.
  316. let stackid = idd
  317. let stackdir = dir
  318. let stackrel = rel
  319. let stackrev = rev
  320. let stackalt = alt
  321. let stackfor = forr
  322. let stacksrc = src
  323. let stacktype = type
  324. let stackhref = href
  325. let stacktext = text
  326. let stackabbr = abbr
  327. let stacksize = size
  328. let stackface = face
  329. let stackchar = char
  330. let stackcite = cite
  331. let stackspan = span
  332. let stackdata = data
  333. let stackaxis = axis
  334. let stackName = Name
  335. let stackname = name
  336. let stackcode = code
  337. let stacklink = link
  338. let stacklang = lang
  339. let stackcols = cols
  340. let stackrows = rows
  341. let stackismap = ismap
  342. let stackshape = shape
  343. let stackstyle = style
  344. let stackalink = alink
  345. let stackwidth = width
  346. let stackrules = rules
  347. let stackalign = align
  348. let stackframe = frame
  349. let stackvlink = vlink
  350. let stackdefer = deferr
  351. let stackcolor = color
  352. let stackmedia = media
  353. let stacktitle = title
  354. let stackscope = scope
  355. let stackclass = classs
  356. let stackmanifest = manifest
  357. let stackvalue = value
  358. let stackclear = clear
  359. let stackstart = start
  360. let stacklabel = label
  361. let stackaction = action
  362. let stackheight = height
  363. let stackmethod = method
  364. let stackaccept = acceptt
  365. let stackobject = object
  366. let stackscheme = scheme
  367. let stackcoords = coords
  368. let stackusemap = usemap
  369. let stackonblur = onblur
  370. let stacknohref = nohref
  371. let stacknowrap = nowrap
  372. let stackhspace = hspace
  373. let stackborder = border
  374. let stackvalign = valign
  375. let stackvspace = vspace
  376. let stackonload = onload
  377. let stacktarget = target
  378. let stackprompt = prompt
  379. let stackonfocus = onfocus
  380. let stackenctype = enctype
  381. let stackonclick = onclick
  382. let stackontouchstart = ontouchstart
  383. let stackonkeyup = onkeyup
  384. let stackprofile = profile
  385. let stackversion = version
  386. let stackonreset = onreset
  387. let stackcharset = charset
  388. let stackstandby = standby
  389. let stackcolspan = colspan
  390. let stackcharoff = charoff
  391. let stackclassid = classid
  392. let stackcompact = compact
  393. let stackdeclare = declare
  394. let stackrowspan = rowspan
  395. let stackchecked = checked
  396. let stackarchive = archive
  397. let stackbgcolor = bgcolor
  398. let stackcontent = content
  399. let stacknoshade = noshade
  400. let stacksummary = summary
  401. let stackheaders = headers
  402. let stackonselect = onselect
  403. let stackreadonly = readonly
  404. let stacktabindex = tabindex
  405. let stackonchange = onchange
  406. let stacknoresize = noresize
  407. let stackdisabled = disabled
  408. let stacklongdesc = longdesc
  409. let stackcodebase = codebase
  410. let stacklanguage = language
  411. let stackdatetime = datetime
  412. let stackselected = selected
  413. let stackhreflang = hreflang
  414. let stackonsubmit = onsubmit
  415. let stackmultiple = multiple
  416. let stackonunload = onunload
  417. let stackcodetype = codetype
  418. let stackscrolling = scrolling
  419. let stackonkeydown = onkeydown
  420. let stackmaxlength = maxlength
  421. let stackvaluetype = valuetype
  422. let stackaccesskey = accesskey
  423. let stackonmouseup = onmouseup
  424. let stackonkeypress = onkeypress
  425. let stackondblclick = ondblclick
  426. let stackonmouseout = onmouseout
  427. let stackhttpEquiv = httpEquiv
  428. let stackdataText = dataText
  429. let stackbackground = background
  430. let stackonmousemove = onmousemove
  431. let stackonmouseover = onmouseover
  432. let stackcellpadding = cellpadding
  433. let stackonmousedown = onmousedown
  434. let stackframeborder = frameborder
  435. let stackmarginwidth = marginwidth
  436. let stackcellspacing = cellspacing
  437. let stackplaceholder = placeholder
  438. let stackmarginheight = marginheight
  439. let stackacceptCharset = acceptCharset
  440. let stackinner = inner
  441. // Reset the values before a nested scope evalutation.
  442. idd = nil
  443. dir = nil
  444. rel = nil
  445. rev = nil
  446. alt = nil
  447. forr = nil
  448. src = nil
  449. type = nil
  450. href = nil
  451. text = nil
  452. abbr = nil
  453. size = nil
  454. face = nil
  455. char = nil
  456. cite = nil
  457. span = nil
  458. data = nil
  459. axis = nil
  460. Name = nil
  461. name = nil
  462. code = nil
  463. link = nil
  464. lang = nil
  465. cols = nil
  466. rows = nil
  467. ismap = nil
  468. shape = nil
  469. style = nil
  470. alink = nil
  471. width = nil
  472. rules = nil
  473. align = nil
  474. frame = nil
  475. vlink = nil
  476. deferr = nil
  477. color = nil
  478. media = nil
  479. title = nil
  480. scope = nil
  481. classs = nil
  482. manifest = nil
  483. value = nil
  484. clear = nil
  485. start = nil
  486. label = nil
  487. action = nil
  488. height = nil
  489. method = nil
  490. acceptt = nil
  491. object = nil
  492. scheme = nil
  493. coords = nil
  494. usemap = nil
  495. onblur = nil
  496. nohref = nil
  497. nowrap = nil
  498. hspace = nil
  499. border = nil
  500. valign = nil
  501. vspace = nil
  502. onload = nil
  503. target = nil
  504. prompt = nil
  505. onfocus = nil
  506. enctype = nil
  507. onclick = nil
  508. ontouchstart = nil
  509. onkeyup = nil
  510. profile = nil
  511. version = nil
  512. onreset = nil
  513. charset = nil
  514. standby = nil
  515. colspan = nil
  516. charoff = nil
  517. classid = nil
  518. compact = nil
  519. declare = nil
  520. rowspan = nil
  521. checked = nil
  522. archive = nil
  523. bgcolor = nil
  524. content = nil
  525. noshade = nil
  526. summary = nil
  527. headers = nil
  528. onselect = nil
  529. readonly = nil
  530. tabindex = nil
  531. onchange = nil
  532. noresize = nil
  533. disabled = nil
  534. longdesc = nil
  535. codebase = nil
  536. language = nil
  537. datetime = nil
  538. selected = nil
  539. hreflang = nil
  540. onsubmit = nil
  541. multiple = nil
  542. onunload = nil
  543. codetype = nil
  544. scrolling = nil
  545. onkeydown = nil
  546. maxlength = nil
  547. valuetype = nil
  548. accesskey = nil
  549. onmouseup = nil
  550. onkeypress = nil
  551. ondblclick = nil
  552. onmouseout = nil
  553. httpEquiv = nil
  554. dataText = nil
  555. background = nil
  556. onmousemove = nil
  557. onmouseover = nil
  558. cellpadding = nil
  559. onmousedown = nil
  560. frameborder = nil
  561. placeholder = nil
  562. marginwidth = nil
  563. cellspacing = nil
  564. marginheight = nil
  565. acceptCharset = nil
  566. inner = nil
  567. scopesBuffer[Process.tid] = (scopesBuffer[Process.tid] ?? "") + "<" + node
  568. // Save the current output before the nested scope evalutation.
  569. var output = scopesBuffer[Process.tid] ?? ""
  570. // Clear the output buffer for the evalutation.
  571. scopesBuffer[Process.tid] = ""
  572. // Evaluate the nested scope.
  573. closure()
  574. // Render attributes set by the evalutation.
  575. var mergedAttributes = [String: String?]()
  576. if let idd = idd { mergedAttributes["id"] = idd }
  577. if let dir = dir { mergedAttributes["dir"] = dir }
  578. if let rel = rel { mergedAttributes["rel"] = rel }
  579. if let rev = rev { mergedAttributes["rev"] = rev }
  580. if let alt = alt { mergedAttributes["alt"] = alt }
  581. if let forr = forr { mergedAttributes["for"] = forr }
  582. if let src = src { mergedAttributes["src"] = src }
  583. if let type = type { mergedAttributes["type"] = type }
  584. if let href = href { mergedAttributes["href"] = href }
  585. if let text = text { mergedAttributes["text"] = text }
  586. if let abbr = abbr { mergedAttributes["abbr"] = abbr }
  587. if let size = size { mergedAttributes["size"] = size }
  588. if let face = face { mergedAttributes["face"] = face }
  589. if let char = char { mergedAttributes["char"] = char }
  590. if let cite = cite { mergedAttributes["cite"] = cite }
  591. if let span = span { mergedAttributes["span"] = span }
  592. if let data = data { mergedAttributes["data"] = data }
  593. if let axis = axis { mergedAttributes["axis"] = axis }
  594. if let Name = Name { mergedAttributes["Name"] = Name }
  595. if let name = name { mergedAttributes["name"] = name }
  596. if let code = code { mergedAttributes["code"] = code }
  597. if let link = link { mergedAttributes["link"] = link }
  598. if let lang = lang { mergedAttributes["lang"] = lang }
  599. if let cols = cols { mergedAttributes["cols"] = cols }
  600. if let rows = rows { mergedAttributes["rows"] = rows }
  601. if let ismap = ismap { mergedAttributes["ismap"] = ismap }
  602. if let shape = shape { mergedAttributes["shape"] = shape }
  603. if let style = style { mergedAttributes["style"] = style }
  604. if let alink = alink { mergedAttributes["alink"] = alink }
  605. if let width = width { mergedAttributes["width"] = width }
  606. if let rules = rules { mergedAttributes["rules"] = rules }
  607. if let align = align { mergedAttributes["align"] = align }
  608. if let frame = frame { mergedAttributes["frame"] = frame }
  609. if let vlink = vlink { mergedAttributes["vlink"] = vlink }
  610. if let deferr = deferr { mergedAttributes["defer"] = deferr }
  611. if let color = color { mergedAttributes["color"] = color }
  612. if let media = media { mergedAttributes["media"] = media }
  613. if let title = title { mergedAttributes["title"] = title }
  614. if let scope = scope { mergedAttributes["scope"] = scope }
  615. if let classs = classs { mergedAttributes["class"] = classs }
  616. if let manifest = manifest { mergedAttributes["manifest"] = manifest }
  617. if let value = value { mergedAttributes["value"] = value }
  618. if let clear = clear { mergedAttributes["clear"] = clear }
  619. if let start = start { mergedAttributes["start"] = start }
  620. if let label = label { mergedAttributes["label"] = label }
  621. if let action = action { mergedAttributes["action"] = action }
  622. if let height = height { mergedAttributes["height"] = height }
  623. if let method = method { mergedAttributes["method"] = method }
  624. if let acceptt = acceptt { mergedAttributes["accept"] = acceptt }
  625. if let object = object { mergedAttributes["object"] = object }
  626. if let scheme = scheme { mergedAttributes["scheme"] = scheme }
  627. if let coords = coords { mergedAttributes["coords"] = coords }
  628. if let usemap = usemap { mergedAttributes["usemap"] = usemap }
  629. if let onblur = onblur { mergedAttributes["onblur"] = onblur }
  630. if let nohref = nohref { mergedAttributes["nohref"] = nohref }
  631. if let nowrap = nowrap { mergedAttributes["nowrap"] = nowrap }
  632. if let hspace = hspace { mergedAttributes["hspace"] = hspace }
  633. if let border = border { mergedAttributes["border"] = border }
  634. if let valign = valign { mergedAttributes["valign"] = valign }
  635. if let vspace = vspace { mergedAttributes["vspace"] = vspace }
  636. if let onload = onload { mergedAttributes["onload"] = onload }
  637. if let target = target { mergedAttributes["target"] = target }
  638. if let prompt = prompt { mergedAttributes["prompt"] = prompt }
  639. if let onfocus = onfocus { mergedAttributes["onfocus"] = onfocus }
  640. if let enctype = enctype { mergedAttributes["enctype"] = enctype }
  641. if let onclick = onclick { mergedAttributes["onclick"] = onclick }
  642. if let ontouchstart = ontouchstart { mergedAttributes["ontouchstart"] = ontouchstart }
  643. if let onkeyup = onkeyup { mergedAttributes["onkeyup"] = onkeyup }
  644. if let profile = profile { mergedAttributes["profile"] = profile }
  645. if let version = version { mergedAttributes["version"] = version }
  646. if let onreset = onreset { mergedAttributes["onreset"] = onreset }
  647. if let charset = charset { mergedAttributes["charset"] = charset }
  648. if let standby = standby { mergedAttributes["standby"] = standby }
  649. if let colspan = colspan { mergedAttributes["colspan"] = colspan }
  650. if let charoff = charoff { mergedAttributes["charoff"] = charoff }
  651. if let classid = classid { mergedAttributes["classid"] = classid }
  652. if let compact = compact { mergedAttributes["compact"] = compact }
  653. if let declare = declare { mergedAttributes["declare"] = declare }
  654. if let rowspan = rowspan { mergedAttributes["rowspan"] = rowspan }
  655. if let checked = checked { mergedAttributes["checked"] = checked }
  656. if let archive = archive { mergedAttributes["archive"] = archive }
  657. if let bgcolor = bgcolor { mergedAttributes["bgcolor"] = bgcolor }
  658. if let content = content { mergedAttributes["content"] = content }
  659. if let noshade = noshade { mergedAttributes["noshade"] = noshade }
  660. if let summary = summary { mergedAttributes["summary"] = summary }
  661. if let headers = headers { mergedAttributes["headers"] = headers }
  662. if let onselect = onselect { mergedAttributes["onselect"] = onselect }
  663. if let readonly = readonly { mergedAttributes["readonly"] = readonly }
  664. if let tabindex = tabindex { mergedAttributes["tabindex"] = tabindex }
  665. if let onchange = onchange { mergedAttributes["onchange"] = onchange }
  666. if let noresize = noresize { mergedAttributes["noresize"] = noresize }
  667. if let disabled = disabled { mergedAttributes["disabled"] = disabled }
  668. if let longdesc = longdesc { mergedAttributes["longdesc"] = longdesc }
  669. if let codebase = codebase { mergedAttributes["codebase"] = codebase }
  670. if let language = language { mergedAttributes["language"] = language }
  671. if let datetime = datetime { mergedAttributes["datetime"] = datetime }
  672. if let selected = selected { mergedAttributes["selected"] = selected }
  673. if let hreflang = hreflang { mergedAttributes["hreflang"] = hreflang }
  674. if let onsubmit = onsubmit { mergedAttributes["onsubmit"] = onsubmit }
  675. if let multiple = multiple { mergedAttributes["multiple"] = multiple }
  676. if let onunload = onunload { mergedAttributes["onunload"] = onunload }
  677. if let codetype = codetype { mergedAttributes["codetype"] = codetype }
  678. if let scrolling = scrolling { mergedAttributes["scrolling"] = scrolling }
  679. if let onkeydown = onkeydown { mergedAttributes["onkeydown"] = onkeydown }
  680. if let maxlength = maxlength { mergedAttributes["maxlength"] = maxlength }
  681. if let valuetype = valuetype { mergedAttributes["valuetype"] = valuetype }
  682. if let accesskey = accesskey { mergedAttributes["accesskey"] = accesskey }
  683. if let onmouseup = onmouseup { mergedAttributes["onmouseup"] = onmouseup }
  684. if let onkeypress = onkeypress { mergedAttributes["onkeypress"] = onkeypress }
  685. if let ondblclick = ondblclick { mergedAttributes["ondblclick"] = ondblclick }
  686. if let onmouseout = onmouseout { mergedAttributes["onmouseout"] = onmouseout }
  687. if let httpEquiv = httpEquiv { mergedAttributes["http-equiv"] = httpEquiv }
  688. if let dataText = dataText { mergedAttributes["data-text"] = dataText }
  689. if let background = background { mergedAttributes["background"] = background }
  690. if let onmousemove = onmousemove { mergedAttributes["onmousemove"] = onmousemove }
  691. if let onmouseover = onmouseover { mergedAttributes["onmouseover"] = onmouseover }
  692. if let cellpadding = cellpadding { mergedAttributes["cellpadding"] = cellpadding }
  693. if let onmousedown = onmousedown { mergedAttributes["onmousedown"] = onmousedown }
  694. if let frameborder = frameborder { mergedAttributes["frameborder"] = frameborder }
  695. if let marginwidth = marginwidth { mergedAttributes["marginwidth"] = marginwidth }
  696. if let cellspacing = cellspacing { mergedAttributes["cellspacing"] = cellspacing }
  697. if let placeholder = placeholder { mergedAttributes["placeholder"] = placeholder }
  698. if let marginheight = marginheight { mergedAttributes["marginheight"] = marginheight }
  699. if let acceptCharset = acceptCharset { mergedAttributes["accept-charset"] = acceptCharset }
  700. for item in attrs.enumerated() {
  701. mergedAttributes.updateValue(item.element.1, forKey: item.element.0)
  702. }
  703. output += mergedAttributes.reduce("") { result, item in
  704. if let value = item.value {
  705. return result + " \(item.key)=\"\(value)\""
  706. } else {
  707. return result
  708. }
  709. }
  710. if let inner = inner {
  711. scopesBuffer[Process.tid] = output + ">" + (inner) + "</" + node + ">"
  712. } else {
  713. let current = scopesBuffer[Process.tid] ?? ""
  714. scopesBuffer[Process.tid] = output + ">" + current + "</" + node + ">"
  715. }
  716. // Pop the attributes.
  717. idd = stackid
  718. dir = stackdir
  719. rel = stackrel
  720. rev = stackrev
  721. alt = stackalt
  722. forr = stackfor
  723. src = stacksrc
  724. type = stacktype
  725. href = stackhref
  726. text = stacktext
  727. abbr = stackabbr
  728. size = stacksize
  729. face = stackface
  730. char = stackchar
  731. cite = stackcite
  732. span = stackspan
  733. data = stackdata
  734. axis = stackaxis
  735. Name = stackName
  736. name = stackname
  737. code = stackcode
  738. link = stacklink
  739. lang = stacklang
  740. cols = stackcols
  741. rows = stackrows
  742. ismap = stackismap
  743. shape = stackshape
  744. style = stackstyle
  745. alink = stackalink
  746. width = stackwidth
  747. rules = stackrules
  748. align = stackalign
  749. frame = stackframe
  750. vlink = stackvlink
  751. deferr = stackdefer
  752. color = stackcolor
  753. media = stackmedia
  754. title = stacktitle
  755. scope = stackscope
  756. classs = stackclass
  757. manifest = stackmanifest
  758. value = stackvalue
  759. clear = stackclear
  760. start = stackstart
  761. label = stacklabel
  762. action = stackaction
  763. height = stackheight
  764. method = stackmethod
  765. acceptt = stackaccept
  766. object = stackobject
  767. scheme = stackscheme
  768. coords = stackcoords
  769. usemap = stackusemap
  770. onblur = stackonblur
  771. nohref = stacknohref
  772. nowrap = stacknowrap
  773. hspace = stackhspace
  774. border = stackborder
  775. valign = stackvalign
  776. vspace = stackvspace
  777. onload = stackonload
  778. target = stacktarget
  779. prompt = stackprompt
  780. onfocus = stackonfocus
  781. enctype = stackenctype
  782. onclick = stackonclick
  783. ontouchstart = stackontouchstart
  784. onkeyup = stackonkeyup
  785. profile = stackprofile
  786. version = stackversion
  787. onreset = stackonreset
  788. charset = stackcharset
  789. standby = stackstandby
  790. colspan = stackcolspan
  791. charoff = stackcharoff
  792. classid = stackclassid
  793. compact = stackcompact
  794. declare = stackdeclare
  795. rowspan = stackrowspan
  796. checked = stackchecked
  797. archive = stackarchive
  798. bgcolor = stackbgcolor
  799. content = stackcontent
  800. noshade = stacknoshade
  801. summary = stacksummary
  802. headers = stackheaders
  803. onselect = stackonselect
  804. readonly = stackreadonly
  805. tabindex = stacktabindex
  806. onchange = stackonchange
  807. noresize = stacknoresize
  808. disabled = stackdisabled
  809. longdesc = stacklongdesc
  810. codebase = stackcodebase
  811. language = stacklanguage
  812. datetime = stackdatetime
  813. selected = stackselected
  814. hreflang = stackhreflang
  815. onsubmit = stackonsubmit
  816. multiple = stackmultiple
  817. onunload = stackonunload
  818. codetype = stackcodetype
  819. scrolling = stackscrolling
  820. onkeydown = stackonkeydown
  821. maxlength = stackmaxlength
  822. valuetype = stackvaluetype
  823. accesskey = stackaccesskey
  824. onmouseup = stackonmouseup
  825. onkeypress = stackonkeypress
  826. ondblclick = stackondblclick
  827. onmouseout = stackonmouseout
  828. httpEquiv = stackhttpEquiv
  829. dataText = stackdataText
  830. background = stackbackground
  831. onmousemove = stackonmousemove
  832. onmouseover = stackonmouseover
  833. cellpadding = stackcellpadding
  834. onmousedown = stackonmousedown
  835. frameborder = stackframeborder
  836. placeholder = stackplaceholder
  837. marginwidth = stackmarginwidth
  838. cellspacing = stackcellspacing
  839. marginheight = stackmarginheight
  840. acceptCharset = stackacceptCharset
  841. inner = stackinner
  842. }