parse.go 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package html
  5. import (
  6. "errors"
  7. "fmt"
  8. "io"
  9. "strings"
  10. a "golang.org/x/net/html/atom"
  11. )
  12. // A parser implements the HTML5 parsing algorithm:
  13. // https://html.spec.whatwg.org/multipage/syntax.html#tree-construction
  14. type parser struct {
  15. // tokenizer provides the tokens for the parser.
  16. tokenizer *Tokenizer
  17. // tok is the most recently read token.
  18. tok Token
  19. // Self-closing tags like <hr/> are treated as start tags, except that
  20. // hasSelfClosingToken is set while they are being processed.
  21. hasSelfClosingToken bool
  22. // doc is the document root element.
  23. doc *Node
  24. // The stack of open elements (section 12.2.3.2) and active formatting
  25. // elements (section 12.2.3.3).
  26. oe, afe nodeStack
  27. // Element pointers (section 12.2.3.4).
  28. head, form *Node
  29. // Other parsing state flags (section 12.2.3.5).
  30. scripting, framesetOK bool
  31. // im is the current insertion mode.
  32. im insertionMode
  33. // originalIM is the insertion mode to go back to after completing a text
  34. // or inTableText insertion mode.
  35. originalIM insertionMode
  36. // fosterParenting is whether new elements should be inserted according to
  37. // the foster parenting rules (section 12.2.5.3).
  38. fosterParenting bool
  39. // quirks is whether the parser is operating in "quirks mode."
  40. quirks bool
  41. // fragment is whether the parser is parsing an HTML fragment.
  42. fragment bool
  43. // context is the context element when parsing an HTML fragment
  44. // (section 12.4).
  45. context *Node
  46. }
  47. func (p *parser) top() *Node {
  48. if n := p.oe.top(); n != nil {
  49. return n
  50. }
  51. return p.doc
  52. }
  53. // Stop tags for use in popUntil. These come from section 12.2.3.2.
  54. var (
  55. defaultScopeStopTags = map[string][]a.Atom{
  56. "": {a.Applet, a.Caption, a.Html, a.Table, a.Td, a.Th, a.Marquee, a.Object, a.Template},
  57. "math": {a.AnnotationXml, a.Mi, a.Mn, a.Mo, a.Ms, a.Mtext},
  58. "svg": {a.Desc, a.ForeignObject, a.Title},
  59. }
  60. )
  61. type scope int
  62. const (
  63. defaultScope scope = iota
  64. listItemScope
  65. buttonScope
  66. tableScope
  67. tableRowScope
  68. tableBodyScope
  69. selectScope
  70. )
  71. // popUntil pops the stack of open elements at the highest element whose tag
  72. // is in matchTags, provided there is no higher element in the scope's stop
  73. // tags (as defined in section 12.2.3.2). It returns whether or not there was
  74. // such an element. If there was not, popUntil leaves the stack unchanged.
  75. //
  76. // For example, the set of stop tags for table scope is: "html", "table". If
  77. // the stack was:
  78. // ["html", "body", "font", "table", "b", "i", "u"]
  79. // then popUntil(tableScope, "font") would return false, but
  80. // popUntil(tableScope, "i") would return true and the stack would become:
  81. // ["html", "body", "font", "table", "b"]
  82. //
  83. // If an element's tag is in both the stop tags and matchTags, then the stack
  84. // will be popped and the function returns true (provided, of course, there was
  85. // no higher element in the stack that was also in the stop tags). For example,
  86. // popUntil(tableScope, "table") returns true and leaves:
  87. // ["html", "body", "font"]
  88. func (p *parser) popUntil(s scope, matchTags ...a.Atom) bool {
  89. if i := p.indexOfElementInScope(s, matchTags...); i != -1 {
  90. p.oe = p.oe[:i]
  91. return true
  92. }
  93. return false
  94. }
  95. // indexOfElementInScope returns the index in p.oe of the highest element whose
  96. // tag is in matchTags that is in scope. If no matching element is in scope, it
  97. // returns -1.
  98. func (p *parser) indexOfElementInScope(s scope, matchTags ...a.Atom) int {
  99. for i := len(p.oe) - 1; i >= 0; i-- {
  100. tagAtom := p.oe[i].DataAtom
  101. if p.oe[i].Namespace == "" {
  102. for _, t := range matchTags {
  103. if t == tagAtom {
  104. return i
  105. }
  106. }
  107. switch s {
  108. case defaultScope:
  109. // No-op.
  110. case listItemScope:
  111. if tagAtom == a.Ol || tagAtom == a.Ul {
  112. return -1
  113. }
  114. case buttonScope:
  115. if tagAtom == a.Button {
  116. return -1
  117. }
  118. case tableScope:
  119. if tagAtom == a.Html || tagAtom == a.Table {
  120. return -1
  121. }
  122. case selectScope:
  123. if tagAtom != a.Optgroup && tagAtom != a.Option {
  124. return -1
  125. }
  126. default:
  127. panic("unreachable")
  128. }
  129. }
  130. switch s {
  131. case defaultScope, listItemScope, buttonScope:
  132. for _, t := range defaultScopeStopTags[p.oe[i].Namespace] {
  133. if t == tagAtom {
  134. return -1
  135. }
  136. }
  137. }
  138. }
  139. return -1
  140. }
  141. // elementInScope is like popUntil, except that it doesn't modify the stack of
  142. // open elements.
  143. func (p *parser) elementInScope(s scope, matchTags ...a.Atom) bool {
  144. return p.indexOfElementInScope(s, matchTags...) != -1
  145. }
  146. // clearStackToContext pops elements off the stack of open elements until a
  147. // scope-defined element is found.
  148. func (p *parser) clearStackToContext(s scope) {
  149. for i := len(p.oe) - 1; i >= 0; i-- {
  150. tagAtom := p.oe[i].DataAtom
  151. switch s {
  152. case tableScope:
  153. if tagAtom == a.Html || tagAtom == a.Table {
  154. p.oe = p.oe[:i+1]
  155. return
  156. }
  157. case tableRowScope:
  158. if tagAtom == a.Html || tagAtom == a.Tr {
  159. p.oe = p.oe[:i+1]
  160. return
  161. }
  162. case tableBodyScope:
  163. if tagAtom == a.Html || tagAtom == a.Tbody || tagAtom == a.Tfoot || tagAtom == a.Thead {
  164. p.oe = p.oe[:i+1]
  165. return
  166. }
  167. default:
  168. panic("unreachable")
  169. }
  170. }
  171. }
  172. // generateImpliedEndTags pops nodes off the stack of open elements as long as
  173. // the top node has a tag name of dd, dt, li, option, optgroup, p, rp, or rt.
  174. // If exceptions are specified, nodes with that name will not be popped off.
  175. func (p *parser) generateImpliedEndTags(exceptions ...string) {
  176. var i int
  177. loop:
  178. for i = len(p.oe) - 1; i >= 0; i-- {
  179. n := p.oe[i]
  180. if n.Type == ElementNode {
  181. switch n.DataAtom {
  182. case a.Dd, a.Dt, a.Li, a.Option, a.Optgroup, a.P, a.Rp, a.Rt:
  183. for _, except := range exceptions {
  184. if n.Data == except {
  185. break loop
  186. }
  187. }
  188. continue
  189. }
  190. }
  191. break
  192. }
  193. p.oe = p.oe[:i+1]
  194. }
  195. // addChild adds a child node n to the top element, and pushes n onto the stack
  196. // of open elements if it is an element node.
  197. func (p *parser) addChild(n *Node) {
  198. if p.shouldFosterParent() {
  199. p.fosterParent(n)
  200. } else {
  201. p.top().AppendChild(n)
  202. }
  203. if n.Type == ElementNode {
  204. p.oe = append(p.oe, n)
  205. }
  206. }
  207. // shouldFosterParent returns whether the next node to be added should be
  208. // foster parented.
  209. func (p *parser) shouldFosterParent() bool {
  210. if p.fosterParenting {
  211. switch p.top().DataAtom {
  212. case a.Table, a.Tbody, a.Tfoot, a.Thead, a.Tr:
  213. return true
  214. }
  215. }
  216. return false
  217. }
  218. // fosterParent adds a child node according to the foster parenting rules.
  219. // Section 12.2.5.3, "foster parenting".
  220. func (p *parser) fosterParent(n *Node) {
  221. var table, parent, prev *Node
  222. var i int
  223. for i = len(p.oe) - 1; i >= 0; i-- {
  224. if p.oe[i].DataAtom == a.Table {
  225. table = p.oe[i]
  226. break
  227. }
  228. }
  229. if table == nil {
  230. // The foster parent is the html element.
  231. parent = p.oe[0]
  232. } else {
  233. parent = table.Parent
  234. }
  235. if parent == nil {
  236. parent = p.oe[i-1]
  237. }
  238. if table != nil {
  239. prev = table.PrevSibling
  240. } else {
  241. prev = parent.LastChild
  242. }
  243. if prev != nil && prev.Type == TextNode && n.Type == TextNode {
  244. prev.Data += n.Data
  245. return
  246. }
  247. parent.InsertBefore(n, table)
  248. }
  249. // addText adds text to the preceding node if it is a text node, or else it
  250. // calls addChild with a new text node.
  251. func (p *parser) addText(text string) {
  252. if text == "" {
  253. return
  254. }
  255. if p.shouldFosterParent() {
  256. p.fosterParent(&Node{
  257. Type: TextNode,
  258. Data: text,
  259. })
  260. return
  261. }
  262. t := p.top()
  263. if n := t.LastChild; n != nil && n.Type == TextNode {
  264. n.Data += text
  265. return
  266. }
  267. p.addChild(&Node{
  268. Type: TextNode,
  269. Data: text,
  270. })
  271. }
  272. // addElement adds a child element based on the current token.
  273. func (p *parser) addElement() {
  274. p.addChild(&Node{
  275. Type: ElementNode,
  276. DataAtom: p.tok.DataAtom,
  277. Data: p.tok.Data,
  278. Attr: p.tok.Attr,
  279. })
  280. }
  281. // Section 12.2.3.3.
  282. func (p *parser) addFormattingElement() {
  283. tagAtom, attr := p.tok.DataAtom, p.tok.Attr
  284. p.addElement()
  285. // Implement the Noah's Ark clause, but with three per family instead of two.
  286. identicalElements := 0
  287. findIdenticalElements:
  288. for i := len(p.afe) - 1; i >= 0; i-- {
  289. n := p.afe[i]
  290. if n.Type == scopeMarkerNode {
  291. break
  292. }
  293. if n.Type != ElementNode {
  294. continue
  295. }
  296. if n.Namespace != "" {
  297. continue
  298. }
  299. if n.DataAtom != tagAtom {
  300. continue
  301. }
  302. if len(n.Attr) != len(attr) {
  303. continue
  304. }
  305. compareAttributes:
  306. for _, t0 := range n.Attr {
  307. for _, t1 := range attr {
  308. if t0.Key == t1.Key && t0.Namespace == t1.Namespace && t0.Val == t1.Val {
  309. // Found a match for this attribute, continue with the next attribute.
  310. continue compareAttributes
  311. }
  312. }
  313. // If we get here, there is no attribute that matches a.
  314. // Therefore the element is not identical to the new one.
  315. continue findIdenticalElements
  316. }
  317. identicalElements++
  318. if identicalElements >= 3 {
  319. p.afe.remove(n)
  320. }
  321. }
  322. p.afe = append(p.afe, p.top())
  323. }
  324. // Section 12.2.3.3.
  325. func (p *parser) clearActiveFormattingElements() {
  326. for {
  327. n := p.afe.pop()
  328. if len(p.afe) == 0 || n.Type == scopeMarkerNode {
  329. return
  330. }
  331. }
  332. }
  333. // Section 12.2.3.3.
  334. func (p *parser) reconstructActiveFormattingElements() {
  335. n := p.afe.top()
  336. if n == nil {
  337. return
  338. }
  339. if n.Type == scopeMarkerNode || p.oe.index(n) != -1 {
  340. return
  341. }
  342. i := len(p.afe) - 1
  343. for n.Type != scopeMarkerNode && p.oe.index(n) == -1 {
  344. if i == 0 {
  345. i = -1
  346. break
  347. }
  348. i--
  349. n = p.afe[i]
  350. }
  351. for {
  352. i++
  353. clone := p.afe[i].clone()
  354. p.addChild(clone)
  355. p.afe[i] = clone
  356. if i == len(p.afe)-1 {
  357. break
  358. }
  359. }
  360. }
  361. // Section 12.2.4.
  362. func (p *parser) acknowledgeSelfClosingTag() {
  363. p.hasSelfClosingToken = false
  364. }
  365. // An insertion mode (section 12.2.3.1) is the state transition function from
  366. // a particular state in the HTML5 parser's state machine. It updates the
  367. // parser's fields depending on parser.tok (where ErrorToken means EOF).
  368. // It returns whether the token was consumed.
  369. type insertionMode func(*parser) bool
  370. // setOriginalIM sets the insertion mode to return to after completing a text or
  371. // inTableText insertion mode.
  372. // Section 12.2.3.1, "using the rules for".
  373. func (p *parser) setOriginalIM() {
  374. if p.originalIM != nil {
  375. panic("html: bad parser state: originalIM was set twice")
  376. }
  377. p.originalIM = p.im
  378. }
  379. // Section 12.2.3.1, "reset the insertion mode".
  380. func (p *parser) resetInsertionMode() {
  381. for i := len(p.oe) - 1; i >= 0; i-- {
  382. n := p.oe[i]
  383. if i == 0 && p.context != nil {
  384. n = p.context
  385. }
  386. switch n.DataAtom {
  387. case a.Select:
  388. p.im = inSelectIM
  389. case a.Td, a.Th:
  390. p.im = inCellIM
  391. case a.Tr:
  392. p.im = inRowIM
  393. case a.Tbody, a.Thead, a.Tfoot:
  394. p.im = inTableBodyIM
  395. case a.Caption:
  396. p.im = inCaptionIM
  397. case a.Colgroup:
  398. p.im = inColumnGroupIM
  399. case a.Table:
  400. p.im = inTableIM
  401. case a.Head:
  402. p.im = inBodyIM
  403. case a.Body:
  404. p.im = inBodyIM
  405. case a.Frameset:
  406. p.im = inFramesetIM
  407. case a.Html:
  408. p.im = beforeHeadIM
  409. default:
  410. continue
  411. }
  412. return
  413. }
  414. p.im = inBodyIM
  415. }
  416. const whitespace = " \t\r\n\f"
  417. // Section 12.2.5.4.1.
  418. func initialIM(p *parser) bool {
  419. switch p.tok.Type {
  420. case TextToken:
  421. p.tok.Data = strings.TrimLeft(p.tok.Data, whitespace)
  422. if len(p.tok.Data) == 0 {
  423. // It was all whitespace, so ignore it.
  424. return true
  425. }
  426. case CommentToken:
  427. p.doc.AppendChild(&Node{
  428. Type: CommentNode,
  429. Data: p.tok.Data,
  430. })
  431. return true
  432. case DoctypeToken:
  433. n, quirks := parseDoctype(p.tok.Data)
  434. p.doc.AppendChild(n)
  435. p.quirks = quirks
  436. p.im = beforeHTMLIM
  437. return true
  438. }
  439. p.quirks = true
  440. p.im = beforeHTMLIM
  441. return false
  442. }
  443. // Section 12.2.5.4.2.
  444. func beforeHTMLIM(p *parser) bool {
  445. switch p.tok.Type {
  446. case DoctypeToken:
  447. // Ignore the token.
  448. return true
  449. case TextToken:
  450. p.tok.Data = strings.TrimLeft(p.tok.Data, whitespace)
  451. if len(p.tok.Data) == 0 {
  452. // It was all whitespace, so ignore it.
  453. return true
  454. }
  455. case StartTagToken:
  456. if p.tok.DataAtom == a.Html {
  457. p.addElement()
  458. p.im = beforeHeadIM
  459. return true
  460. }
  461. case EndTagToken:
  462. switch p.tok.DataAtom {
  463. case a.Head, a.Body, a.Html, a.Br:
  464. p.parseImpliedToken(StartTagToken, a.Html, a.Html.String())
  465. return false
  466. default:
  467. // Ignore the token.
  468. return true
  469. }
  470. case CommentToken:
  471. p.doc.AppendChild(&Node{
  472. Type: CommentNode,
  473. Data: p.tok.Data,
  474. })
  475. return true
  476. }
  477. p.parseImpliedToken(StartTagToken, a.Html, a.Html.String())
  478. return false
  479. }
  480. // Section 12.2.5.4.3.
  481. func beforeHeadIM(p *parser) bool {
  482. switch p.tok.Type {
  483. case TextToken:
  484. p.tok.Data = strings.TrimLeft(p.tok.Data, whitespace)
  485. if len(p.tok.Data) == 0 {
  486. // It was all whitespace, so ignore it.
  487. return true
  488. }
  489. case StartTagToken:
  490. switch p.tok.DataAtom {
  491. case a.Head:
  492. p.addElement()
  493. p.head = p.top()
  494. p.im = inHeadIM
  495. return true
  496. case a.Html:
  497. return inBodyIM(p)
  498. }
  499. case EndTagToken:
  500. switch p.tok.DataAtom {
  501. case a.Head, a.Body, a.Html, a.Br:
  502. p.parseImpliedToken(StartTagToken, a.Head, a.Head.String())
  503. return false
  504. default:
  505. // Ignore the token.
  506. return true
  507. }
  508. case CommentToken:
  509. p.addChild(&Node{
  510. Type: CommentNode,
  511. Data: p.tok.Data,
  512. })
  513. return true
  514. case DoctypeToken:
  515. // Ignore the token.
  516. return true
  517. }
  518. p.parseImpliedToken(StartTagToken, a.Head, a.Head.String())
  519. return false
  520. }
  521. // Section 12.2.5.4.4.
  522. func inHeadIM(p *parser) bool {
  523. switch p.tok.Type {
  524. case TextToken:
  525. s := strings.TrimLeft(p.tok.Data, whitespace)
  526. if len(s) < len(p.tok.Data) {
  527. // Add the initial whitespace to the current node.
  528. p.addText(p.tok.Data[:len(p.tok.Data)-len(s)])
  529. if s == "" {
  530. return true
  531. }
  532. p.tok.Data = s
  533. }
  534. case StartTagToken:
  535. switch p.tok.DataAtom {
  536. case a.Html:
  537. return inBodyIM(p)
  538. case a.Base, a.Basefont, a.Bgsound, a.Command, a.Link, a.Meta:
  539. p.addElement()
  540. p.oe.pop()
  541. p.acknowledgeSelfClosingTag()
  542. return true
  543. case a.Script, a.Title, a.Noscript, a.Noframes, a.Style:
  544. p.addElement()
  545. p.setOriginalIM()
  546. p.im = textIM
  547. return true
  548. case a.Head:
  549. // Ignore the token.
  550. return true
  551. }
  552. case EndTagToken:
  553. switch p.tok.DataAtom {
  554. case a.Head:
  555. n := p.oe.pop()
  556. if n.DataAtom != a.Head {
  557. panic("html: bad parser state: <head> element not found, in the in-head insertion mode")
  558. }
  559. p.im = afterHeadIM
  560. return true
  561. case a.Body, a.Html, a.Br:
  562. p.parseImpliedToken(EndTagToken, a.Head, a.Head.String())
  563. return false
  564. default:
  565. // Ignore the token.
  566. return true
  567. }
  568. case CommentToken:
  569. p.addChild(&Node{
  570. Type: CommentNode,
  571. Data: p.tok.Data,
  572. })
  573. return true
  574. case DoctypeToken:
  575. // Ignore the token.
  576. return true
  577. }
  578. p.parseImpliedToken(EndTagToken, a.Head, a.Head.String())
  579. return false
  580. }
  581. // Section 12.2.5.4.6.
  582. func afterHeadIM(p *parser) bool {
  583. switch p.tok.Type {
  584. case TextToken:
  585. s := strings.TrimLeft(p.tok.Data, whitespace)
  586. if len(s) < len(p.tok.Data) {
  587. // Add the initial whitespace to the current node.
  588. p.addText(p.tok.Data[:len(p.tok.Data)-len(s)])
  589. if s == "" {
  590. return true
  591. }
  592. p.tok.Data = s
  593. }
  594. case StartTagToken:
  595. switch p.tok.DataAtom {
  596. case a.Html:
  597. return inBodyIM(p)
  598. case a.Body:
  599. p.addElement()
  600. p.framesetOK = false
  601. p.im = inBodyIM
  602. return true
  603. case a.Frameset:
  604. p.addElement()
  605. p.im = inFramesetIM
  606. return true
  607. case a.Base, a.Basefont, a.Bgsound, a.Link, a.Meta, a.Noframes, a.Script, a.Style, a.Title:
  608. p.oe = append(p.oe, p.head)
  609. defer p.oe.remove(p.head)
  610. return inHeadIM(p)
  611. case a.Head:
  612. // Ignore the token.
  613. return true
  614. }
  615. case EndTagToken:
  616. switch p.tok.DataAtom {
  617. case a.Body, a.Html, a.Br:
  618. // Drop down to creating an implied <body> tag.
  619. default:
  620. // Ignore the token.
  621. return true
  622. }
  623. case CommentToken:
  624. p.addChild(&Node{
  625. Type: CommentNode,
  626. Data: p.tok.Data,
  627. })
  628. return true
  629. case DoctypeToken:
  630. // Ignore the token.
  631. return true
  632. }
  633. p.parseImpliedToken(StartTagToken, a.Body, a.Body.String())
  634. p.framesetOK = true
  635. return false
  636. }
  637. // copyAttributes copies attributes of src not found on dst to dst.
  638. func copyAttributes(dst *Node, src Token) {
  639. if len(src.Attr) == 0 {
  640. return
  641. }
  642. attr := map[string]string{}
  643. for _, t := range dst.Attr {
  644. attr[t.Key] = t.Val
  645. }
  646. for _, t := range src.Attr {
  647. if _, ok := attr[t.Key]; !ok {
  648. dst.Attr = append(dst.Attr, t)
  649. attr[t.Key] = t.Val
  650. }
  651. }
  652. }
  653. // Section 12.2.5.4.7.
  654. func inBodyIM(p *parser) bool {
  655. switch p.tok.Type {
  656. case TextToken:
  657. d := p.tok.Data
  658. switch n := p.oe.top(); n.DataAtom {
  659. case a.Pre, a.Listing:
  660. if n.FirstChild == nil {
  661. // Ignore a newline at the start of a <pre> block.
  662. if d != "" && d[0] == '\r' {
  663. d = d[1:]
  664. }
  665. if d != "" && d[0] == '\n' {
  666. d = d[1:]
  667. }
  668. }
  669. }
  670. d = strings.Replace(d, "\x00", "", -1)
  671. if d == "" {
  672. return true
  673. }
  674. p.reconstructActiveFormattingElements()
  675. p.addText(d)
  676. if p.framesetOK && strings.TrimLeft(d, whitespace) != "" {
  677. // There were non-whitespace characters inserted.
  678. p.framesetOK = false
  679. }
  680. case StartTagToken:
  681. switch p.tok.DataAtom {
  682. case a.Html:
  683. copyAttributes(p.oe[0], p.tok)
  684. case a.Base, a.Basefont, a.Bgsound, a.Command, a.Link, a.Meta, a.Noframes, a.Script, a.Style, a.Title:
  685. return inHeadIM(p)
  686. case a.Body:
  687. if len(p.oe) >= 2 {
  688. body := p.oe[1]
  689. if body.Type == ElementNode && body.DataAtom == a.Body {
  690. p.framesetOK = false
  691. copyAttributes(body, p.tok)
  692. }
  693. }
  694. case a.Frameset:
  695. if !p.framesetOK || len(p.oe) < 2 || p.oe[1].DataAtom != a.Body {
  696. // Ignore the token.
  697. return true
  698. }
  699. body := p.oe[1]
  700. if body.Parent != nil {
  701. body.Parent.RemoveChild(body)
  702. }
  703. p.oe = p.oe[:1]
  704. p.addElement()
  705. p.im = inFramesetIM
  706. return true
  707. case a.Address, a.Article, a.Aside, a.Blockquote, a.Center, a.Details, a.Dir, a.Div, a.Dl, a.Fieldset, a.Figcaption, a.Figure, a.Footer, a.Header, a.Hgroup, a.Menu, a.Nav, a.Ol, a.P, a.Section, a.Summary, a.Ul:
  708. p.popUntil(buttonScope, a.P)
  709. p.addElement()
  710. case a.H1, a.H2, a.H3, a.H4, a.H5, a.H6:
  711. p.popUntil(buttonScope, a.P)
  712. switch n := p.top(); n.DataAtom {
  713. case a.H1, a.H2, a.H3, a.H4, a.H5, a.H6:
  714. p.oe.pop()
  715. }
  716. p.addElement()
  717. case a.Pre, a.Listing:
  718. p.popUntil(buttonScope, a.P)
  719. p.addElement()
  720. // The newline, if any, will be dealt with by the TextToken case.
  721. p.framesetOK = false
  722. case a.Form:
  723. if p.form == nil {
  724. p.popUntil(buttonScope, a.P)
  725. p.addElement()
  726. p.form = p.top()
  727. }
  728. case a.Li:
  729. p.framesetOK = false
  730. for i := len(p.oe) - 1; i >= 0; i-- {
  731. node := p.oe[i]
  732. switch node.DataAtom {
  733. case a.Li:
  734. p.oe = p.oe[:i]
  735. case a.Address, a.Div, a.P:
  736. continue
  737. default:
  738. if !isSpecialElement(node) {
  739. continue
  740. }
  741. }
  742. break
  743. }
  744. p.popUntil(buttonScope, a.P)
  745. p.addElement()
  746. case a.Dd, a.Dt:
  747. p.framesetOK = false
  748. for i := len(p.oe) - 1; i >= 0; i-- {
  749. node := p.oe[i]
  750. switch node.DataAtom {
  751. case a.Dd, a.Dt:
  752. p.oe = p.oe[:i]
  753. case a.Address, a.Div, a.P:
  754. continue
  755. default:
  756. if !isSpecialElement(node) {
  757. continue
  758. }
  759. }
  760. break
  761. }
  762. p.popUntil(buttonScope, a.P)
  763. p.addElement()
  764. case a.Plaintext:
  765. p.popUntil(buttonScope, a.P)
  766. p.addElement()
  767. case a.Button:
  768. p.popUntil(defaultScope, a.Button)
  769. p.reconstructActiveFormattingElements()
  770. p.addElement()
  771. p.framesetOK = false
  772. case a.A:
  773. for i := len(p.afe) - 1; i >= 0 && p.afe[i].Type != scopeMarkerNode; i-- {
  774. if n := p.afe[i]; n.Type == ElementNode && n.DataAtom == a.A {
  775. p.inBodyEndTagFormatting(a.A)
  776. p.oe.remove(n)
  777. p.afe.remove(n)
  778. break
  779. }
  780. }
  781. p.reconstructActiveFormattingElements()
  782. p.addFormattingElement()
  783. case a.B, a.Big, a.Code, a.Em, a.Font, a.I, a.S, a.Small, a.Strike, a.Strong, a.Tt, a.U:
  784. p.reconstructActiveFormattingElements()
  785. p.addFormattingElement()
  786. case a.Nobr:
  787. p.reconstructActiveFormattingElements()
  788. if p.elementInScope(defaultScope, a.Nobr) {
  789. p.inBodyEndTagFormatting(a.Nobr)
  790. p.reconstructActiveFormattingElements()
  791. }
  792. p.addFormattingElement()
  793. case a.Applet, a.Marquee, a.Object:
  794. p.reconstructActiveFormattingElements()
  795. p.addElement()
  796. p.afe = append(p.afe, &scopeMarker)
  797. p.framesetOK = false
  798. case a.Table:
  799. if !p.quirks {
  800. p.popUntil(buttonScope, a.P)
  801. }
  802. p.addElement()
  803. p.framesetOK = false
  804. p.im = inTableIM
  805. return true
  806. case a.Area, a.Br, a.Embed, a.Img, a.Input, a.Keygen, a.Wbr:
  807. p.reconstructActiveFormattingElements()
  808. p.addElement()
  809. p.oe.pop()
  810. p.acknowledgeSelfClosingTag()
  811. if p.tok.DataAtom == a.Input {
  812. for _, t := range p.tok.Attr {
  813. if t.Key == "type" {
  814. if strings.ToLower(t.Val) == "hidden" {
  815. // Skip setting framesetOK = false
  816. return true
  817. }
  818. }
  819. }
  820. }
  821. p.framesetOK = false
  822. case a.Param, a.Source, a.Track:
  823. p.addElement()
  824. p.oe.pop()
  825. p.acknowledgeSelfClosingTag()
  826. case a.Hr:
  827. p.popUntil(buttonScope, a.P)
  828. p.addElement()
  829. p.oe.pop()
  830. p.acknowledgeSelfClosingTag()
  831. p.framesetOK = false
  832. case a.Image:
  833. p.tok.DataAtom = a.Img
  834. p.tok.Data = a.Img.String()
  835. return false
  836. case a.Isindex:
  837. if p.form != nil {
  838. // Ignore the token.
  839. return true
  840. }
  841. action := ""
  842. prompt := "This is a searchable index. Enter search keywords: "
  843. attr := []Attribute{{Key: "name", Val: "isindex"}}
  844. for _, t := range p.tok.Attr {
  845. switch t.Key {
  846. case "action":
  847. action = t.Val
  848. case "name":
  849. // Ignore the attribute.
  850. case "prompt":
  851. prompt = t.Val
  852. default:
  853. attr = append(attr, t)
  854. }
  855. }
  856. p.acknowledgeSelfClosingTag()
  857. p.popUntil(buttonScope, a.P)
  858. p.parseImpliedToken(StartTagToken, a.Form, a.Form.String())
  859. if action != "" {
  860. p.form.Attr = []Attribute{{Key: "action", Val: action}}
  861. }
  862. p.parseImpliedToken(StartTagToken, a.Hr, a.Hr.String())
  863. p.parseImpliedToken(StartTagToken, a.Label, a.Label.String())
  864. p.addText(prompt)
  865. p.addChild(&Node{
  866. Type: ElementNode,
  867. DataAtom: a.Input,
  868. Data: a.Input.String(),
  869. Attr: attr,
  870. })
  871. p.oe.pop()
  872. p.parseImpliedToken(EndTagToken, a.Label, a.Label.String())
  873. p.parseImpliedToken(StartTagToken, a.Hr, a.Hr.String())
  874. p.parseImpliedToken(EndTagToken, a.Form, a.Form.String())
  875. case a.Textarea:
  876. p.addElement()
  877. p.setOriginalIM()
  878. p.framesetOK = false
  879. p.im = textIM
  880. case a.Xmp:
  881. p.popUntil(buttonScope, a.P)
  882. p.reconstructActiveFormattingElements()
  883. p.framesetOK = false
  884. p.addElement()
  885. p.setOriginalIM()
  886. p.im = textIM
  887. case a.Iframe:
  888. p.framesetOK = false
  889. p.addElement()
  890. p.setOriginalIM()
  891. p.im = textIM
  892. case a.Noembed, a.Noscript:
  893. p.addElement()
  894. p.setOriginalIM()
  895. p.im = textIM
  896. case a.Select:
  897. p.reconstructActiveFormattingElements()
  898. p.addElement()
  899. p.framesetOK = false
  900. p.im = inSelectIM
  901. return true
  902. case a.Optgroup, a.Option:
  903. if p.top().DataAtom == a.Option {
  904. p.oe.pop()
  905. }
  906. p.reconstructActiveFormattingElements()
  907. p.addElement()
  908. case a.Rp, a.Rt:
  909. if p.elementInScope(defaultScope, a.Ruby) {
  910. p.generateImpliedEndTags()
  911. }
  912. p.addElement()
  913. case a.Math, a.Svg:
  914. p.reconstructActiveFormattingElements()
  915. if p.tok.DataAtom == a.Math {
  916. adjustAttributeNames(p.tok.Attr, mathMLAttributeAdjustments)
  917. } else {
  918. adjustAttributeNames(p.tok.Attr, svgAttributeAdjustments)
  919. }
  920. adjustForeignAttributes(p.tok.Attr)
  921. p.addElement()
  922. p.top().Namespace = p.tok.Data
  923. if p.hasSelfClosingToken {
  924. p.oe.pop()
  925. p.acknowledgeSelfClosingTag()
  926. }
  927. return true
  928. case a.Caption, a.Col, a.Colgroup, a.Frame, a.Head, a.Tbody, a.Td, a.Tfoot, a.Th, a.Thead, a.Tr:
  929. // Ignore the token.
  930. default:
  931. p.reconstructActiveFormattingElements()
  932. p.addElement()
  933. }
  934. case EndTagToken:
  935. switch p.tok.DataAtom {
  936. case a.Body:
  937. if p.elementInScope(defaultScope, a.Body) {
  938. p.im = afterBodyIM
  939. }
  940. case a.Html:
  941. if p.elementInScope(defaultScope, a.Body) {
  942. p.parseImpliedToken(EndTagToken, a.Body, a.Body.String())
  943. return false
  944. }
  945. return true
  946. case a.Address, a.Article, a.Aside, a.Blockquote, a.Button, a.Center, a.Details, a.Dir, a.Div, a.Dl, a.Fieldset, a.Figcaption, a.Figure, a.Footer, a.Header, a.Hgroup, a.Listing, a.Menu, a.Nav, a.Ol, a.Pre, a.Section, a.Summary, a.Ul:
  947. p.popUntil(defaultScope, p.tok.DataAtom)
  948. case a.Form:
  949. node := p.form
  950. p.form = nil
  951. i := p.indexOfElementInScope(defaultScope, a.Form)
  952. if node == nil || i == -1 || p.oe[i] != node {
  953. // Ignore the token.
  954. return true
  955. }
  956. p.generateImpliedEndTags()
  957. p.oe.remove(node)
  958. case a.P:
  959. if !p.elementInScope(buttonScope, a.P) {
  960. p.parseImpliedToken(StartTagToken, a.P, a.P.String())
  961. }
  962. p.popUntil(buttonScope, a.P)
  963. case a.Li:
  964. p.popUntil(listItemScope, a.Li)
  965. case a.Dd, a.Dt:
  966. p.popUntil(defaultScope, p.tok.DataAtom)
  967. case a.H1, a.H2, a.H3, a.H4, a.H5, a.H6:
  968. p.popUntil(defaultScope, a.H1, a.H2, a.H3, a.H4, a.H5, a.H6)
  969. case a.A, a.B, a.Big, a.Code, a.Em, a.Font, a.I, a.Nobr, a.S, a.Small, a.Strike, a.Strong, a.Tt, a.U:
  970. p.inBodyEndTagFormatting(p.tok.DataAtom)
  971. case a.Applet, a.Marquee, a.Object:
  972. if p.popUntil(defaultScope, p.tok.DataAtom) {
  973. p.clearActiveFormattingElements()
  974. }
  975. case a.Br:
  976. p.tok.Type = StartTagToken
  977. return false
  978. default:
  979. p.inBodyEndTagOther(p.tok.DataAtom)
  980. }
  981. case CommentToken:
  982. p.addChild(&Node{
  983. Type: CommentNode,
  984. Data: p.tok.Data,
  985. })
  986. }
  987. return true
  988. }
  989. func (p *parser) inBodyEndTagFormatting(tagAtom a.Atom) {
  990. // This is the "adoption agency" algorithm, described at
  991. // https://html.spec.whatwg.org/multipage/syntax.html#adoptionAgency
  992. // TODO: this is a fairly literal line-by-line translation of that algorithm.
  993. // Once the code successfully parses the comprehensive test suite, we should
  994. // refactor this code to be more idiomatic.
  995. // Steps 1-4. The outer loop.
  996. for i := 0; i < 8; i++ {
  997. // Step 5. Find the formatting element.
  998. var formattingElement *Node
  999. for j := len(p.afe) - 1; j >= 0; j-- {
  1000. if p.afe[j].Type == scopeMarkerNode {
  1001. break
  1002. }
  1003. if p.afe[j].DataAtom == tagAtom {
  1004. formattingElement = p.afe[j]
  1005. break
  1006. }
  1007. }
  1008. if formattingElement == nil {
  1009. p.inBodyEndTagOther(tagAtom)
  1010. return
  1011. }
  1012. feIndex := p.oe.index(formattingElement)
  1013. if feIndex == -1 {
  1014. p.afe.remove(formattingElement)
  1015. return
  1016. }
  1017. if !p.elementInScope(defaultScope, tagAtom) {
  1018. // Ignore the tag.
  1019. return
  1020. }
  1021. // Steps 9-10. Find the furthest block.
  1022. var furthestBlock *Node
  1023. for _, e := range p.oe[feIndex:] {
  1024. if isSpecialElement(e) {
  1025. furthestBlock = e
  1026. break
  1027. }
  1028. }
  1029. if furthestBlock == nil {
  1030. e := p.oe.pop()
  1031. for e != formattingElement {
  1032. e = p.oe.pop()
  1033. }
  1034. p.afe.remove(e)
  1035. return
  1036. }
  1037. // Steps 11-12. Find the common ancestor and bookmark node.
  1038. commonAncestor := p.oe[feIndex-1]
  1039. bookmark := p.afe.index(formattingElement)
  1040. // Step 13. The inner loop. Find the lastNode to reparent.
  1041. lastNode := furthestBlock
  1042. node := furthestBlock
  1043. x := p.oe.index(node)
  1044. // Steps 13.1-13.2
  1045. for j := 0; j < 3; j++ {
  1046. // Step 13.3.
  1047. x--
  1048. node = p.oe[x]
  1049. // Step 13.4 - 13.5.
  1050. if p.afe.index(node) == -1 {
  1051. p.oe.remove(node)
  1052. continue
  1053. }
  1054. // Step 13.6.
  1055. if node == formattingElement {
  1056. break
  1057. }
  1058. // Step 13.7.
  1059. clone := node.clone()
  1060. p.afe[p.afe.index(node)] = clone
  1061. p.oe[p.oe.index(node)] = clone
  1062. node = clone
  1063. // Step 13.8.
  1064. if lastNode == furthestBlock {
  1065. bookmark = p.afe.index(node) + 1
  1066. }
  1067. // Step 13.9.
  1068. if lastNode.Parent != nil {
  1069. lastNode.Parent.RemoveChild(lastNode)
  1070. }
  1071. node.AppendChild(lastNode)
  1072. // Step 13.10.
  1073. lastNode = node
  1074. }
  1075. // Step 14. Reparent lastNode to the common ancestor,
  1076. // or for misnested table nodes, to the foster parent.
  1077. if lastNode.Parent != nil {
  1078. lastNode.Parent.RemoveChild(lastNode)
  1079. }
  1080. switch commonAncestor.DataAtom {
  1081. case a.Table, a.Tbody, a.Tfoot, a.Thead, a.Tr:
  1082. p.fosterParent(lastNode)
  1083. default:
  1084. commonAncestor.AppendChild(lastNode)
  1085. }
  1086. // Steps 15-17. Reparent nodes from the furthest block's children
  1087. // to a clone of the formatting element.
  1088. clone := formattingElement.clone()
  1089. reparentChildren(clone, furthestBlock)
  1090. furthestBlock.AppendChild(clone)
  1091. // Step 18. Fix up the list of active formatting elements.
  1092. if oldLoc := p.afe.index(formattingElement); oldLoc != -1 && oldLoc < bookmark {
  1093. // Move the bookmark with the rest of the list.
  1094. bookmark--
  1095. }
  1096. p.afe.remove(formattingElement)
  1097. p.afe.insert(bookmark, clone)
  1098. // Step 19. Fix up the stack of open elements.
  1099. p.oe.remove(formattingElement)
  1100. p.oe.insert(p.oe.index(furthestBlock)+1, clone)
  1101. }
  1102. }
  1103. // inBodyEndTagOther performs the "any other end tag" algorithm for inBodyIM.
  1104. // "Any other end tag" handling from 12.2.5.5 The rules for parsing tokens in foreign content
  1105. // https://html.spec.whatwg.org/multipage/syntax.html#parsing-main-inforeign
  1106. func (p *parser) inBodyEndTagOther(tagAtom a.Atom) {
  1107. for i := len(p.oe) - 1; i >= 0; i-- {
  1108. if p.oe[i].DataAtom == tagAtom {
  1109. p.oe = p.oe[:i]
  1110. break
  1111. }
  1112. if isSpecialElement(p.oe[i]) {
  1113. break
  1114. }
  1115. }
  1116. }
  1117. // Section 12.2.5.4.8.
  1118. func textIM(p *parser) bool {
  1119. switch p.tok.Type {
  1120. case ErrorToken:
  1121. p.oe.pop()
  1122. case TextToken:
  1123. d := p.tok.Data
  1124. if n := p.oe.top(); n.DataAtom == a.Textarea && n.FirstChild == nil {
  1125. // Ignore a newline at the start of a <textarea> block.
  1126. if d != "" && d[0] == '\r' {
  1127. d = d[1:]
  1128. }
  1129. if d != "" && d[0] == '\n' {
  1130. d = d[1:]
  1131. }
  1132. }
  1133. if d == "" {
  1134. return true
  1135. }
  1136. p.addText(d)
  1137. return true
  1138. case EndTagToken:
  1139. p.oe.pop()
  1140. }
  1141. p.im = p.originalIM
  1142. p.originalIM = nil
  1143. return p.tok.Type == EndTagToken
  1144. }
  1145. // Section 12.2.5.4.9.
  1146. func inTableIM(p *parser) bool {
  1147. switch p.tok.Type {
  1148. case ErrorToken:
  1149. // Stop parsing.
  1150. return true
  1151. case TextToken:
  1152. p.tok.Data = strings.Replace(p.tok.Data, "\x00", "", -1)
  1153. switch p.oe.top().DataAtom {
  1154. case a.Table, a.Tbody, a.Tfoot, a.Thead, a.Tr:
  1155. if strings.Trim(p.tok.Data, whitespace) == "" {
  1156. p.addText(p.tok.Data)
  1157. return true
  1158. }
  1159. }
  1160. case StartTagToken:
  1161. switch p.tok.DataAtom {
  1162. case a.Caption:
  1163. p.clearStackToContext(tableScope)
  1164. p.afe = append(p.afe, &scopeMarker)
  1165. p.addElement()
  1166. p.im = inCaptionIM
  1167. return true
  1168. case a.Colgroup:
  1169. p.clearStackToContext(tableScope)
  1170. p.addElement()
  1171. p.im = inColumnGroupIM
  1172. return true
  1173. case a.Col:
  1174. p.parseImpliedToken(StartTagToken, a.Colgroup, a.Colgroup.String())
  1175. return false
  1176. case a.Tbody, a.Tfoot, a.Thead:
  1177. p.clearStackToContext(tableScope)
  1178. p.addElement()
  1179. p.im = inTableBodyIM
  1180. return true
  1181. case a.Td, a.Th, a.Tr:
  1182. p.parseImpliedToken(StartTagToken, a.Tbody, a.Tbody.String())
  1183. return false
  1184. case a.Table:
  1185. if p.popUntil(tableScope, a.Table) {
  1186. p.resetInsertionMode()
  1187. return false
  1188. }
  1189. // Ignore the token.
  1190. return true
  1191. case a.Style, a.Script:
  1192. return inHeadIM(p)
  1193. case a.Input:
  1194. for _, t := range p.tok.Attr {
  1195. if t.Key == "type" && strings.ToLower(t.Val) == "hidden" {
  1196. p.addElement()
  1197. p.oe.pop()
  1198. return true
  1199. }
  1200. }
  1201. // Otherwise drop down to the default action.
  1202. case a.Form:
  1203. if p.form != nil {
  1204. // Ignore the token.
  1205. return true
  1206. }
  1207. p.addElement()
  1208. p.form = p.oe.pop()
  1209. case a.Select:
  1210. p.reconstructActiveFormattingElements()
  1211. switch p.top().DataAtom {
  1212. case a.Table, a.Tbody, a.Tfoot, a.Thead, a.Tr:
  1213. p.fosterParenting = true
  1214. }
  1215. p.addElement()
  1216. p.fosterParenting = false
  1217. p.framesetOK = false
  1218. p.im = inSelectInTableIM
  1219. return true
  1220. }
  1221. case EndTagToken:
  1222. switch p.tok.DataAtom {
  1223. case a.Table:
  1224. if p.popUntil(tableScope, a.Table) {
  1225. p.resetInsertionMode()
  1226. return true
  1227. }
  1228. // Ignore the token.
  1229. return true
  1230. case a.Body, a.Caption, a.Col, a.Colgroup, a.Html, a.Tbody, a.Td, a.Tfoot, a.Th, a.Thead, a.Tr:
  1231. // Ignore the token.
  1232. return true
  1233. }
  1234. case CommentToken:
  1235. p.addChild(&Node{
  1236. Type: CommentNode,
  1237. Data: p.tok.Data,
  1238. })
  1239. return true
  1240. case DoctypeToken:
  1241. // Ignore the token.
  1242. return true
  1243. }
  1244. p.fosterParenting = true
  1245. defer func() { p.fosterParenting = false }()
  1246. return inBodyIM(p)
  1247. }
  1248. // Section 12.2.5.4.11.
  1249. func inCaptionIM(p *parser) bool {
  1250. switch p.tok.Type {
  1251. case StartTagToken:
  1252. switch p.tok.DataAtom {
  1253. case a.Caption, a.Col, a.Colgroup, a.Tbody, a.Td, a.Tfoot, a.Thead, a.Tr:
  1254. if p.popUntil(tableScope, a.Caption) {
  1255. p.clearActiveFormattingElements()
  1256. p.im = inTableIM
  1257. return false
  1258. } else {
  1259. // Ignore the token.
  1260. return true
  1261. }
  1262. case a.Select:
  1263. p.reconstructActiveFormattingElements()
  1264. p.addElement()
  1265. p.framesetOK = false
  1266. p.im = inSelectInTableIM
  1267. return true
  1268. }
  1269. case EndTagToken:
  1270. switch p.tok.DataAtom {
  1271. case a.Caption:
  1272. if p.popUntil(tableScope, a.Caption) {
  1273. p.clearActiveFormattingElements()
  1274. p.im = inTableIM
  1275. }
  1276. return true
  1277. case a.Table:
  1278. if p.popUntil(tableScope, a.Caption) {
  1279. p.clearActiveFormattingElements()
  1280. p.im = inTableIM
  1281. return false
  1282. } else {
  1283. // Ignore the token.
  1284. return true
  1285. }
  1286. case a.Body, a.Col, a.Colgroup, a.Html, a.Tbody, a.Td, a.Tfoot, a.Th, a.Thead, a.Tr:
  1287. // Ignore the token.
  1288. return true
  1289. }
  1290. }
  1291. return inBodyIM(p)
  1292. }
  1293. // Section 12.2.5.4.12.
  1294. func inColumnGroupIM(p *parser) bool {
  1295. switch p.tok.Type {
  1296. case TextToken:
  1297. s := strings.TrimLeft(p.tok.Data, whitespace)
  1298. if len(s) < len(p.tok.Data) {
  1299. // Add the initial whitespace to the current node.
  1300. p.addText(p.tok.Data[:len(p.tok.Data)-len(s)])
  1301. if s == "" {
  1302. return true
  1303. }
  1304. p.tok.Data = s
  1305. }
  1306. case CommentToken:
  1307. p.addChild(&Node{
  1308. Type: CommentNode,
  1309. Data: p.tok.Data,
  1310. })
  1311. return true
  1312. case DoctypeToken:
  1313. // Ignore the token.
  1314. return true
  1315. case StartTagToken:
  1316. switch p.tok.DataAtom {
  1317. case a.Html:
  1318. return inBodyIM(p)
  1319. case a.Col:
  1320. p.addElement()
  1321. p.oe.pop()
  1322. p.acknowledgeSelfClosingTag()
  1323. return true
  1324. }
  1325. case EndTagToken:
  1326. switch p.tok.DataAtom {
  1327. case a.Colgroup:
  1328. if p.oe.top().DataAtom != a.Html {
  1329. p.oe.pop()
  1330. p.im = inTableIM
  1331. }
  1332. return true
  1333. case a.Col:
  1334. // Ignore the token.
  1335. return true
  1336. }
  1337. }
  1338. if p.oe.top().DataAtom != a.Html {
  1339. p.oe.pop()
  1340. p.im = inTableIM
  1341. return false
  1342. }
  1343. return true
  1344. }
  1345. // Section 12.2.5.4.13.
  1346. func inTableBodyIM(p *parser) bool {
  1347. switch p.tok.Type {
  1348. case StartTagToken:
  1349. switch p.tok.DataAtom {
  1350. case a.Tr:
  1351. p.clearStackToContext(tableBodyScope)
  1352. p.addElement()
  1353. p.im = inRowIM
  1354. return true
  1355. case a.Td, a.Th:
  1356. p.parseImpliedToken(StartTagToken, a.Tr, a.Tr.String())
  1357. return false
  1358. case a.Caption, a.Col, a.Colgroup, a.Tbody, a.Tfoot, a.Thead:
  1359. if p.popUntil(tableScope, a.Tbody, a.Thead, a.Tfoot) {
  1360. p.im = inTableIM
  1361. return false
  1362. }
  1363. // Ignore the token.
  1364. return true
  1365. }
  1366. case EndTagToken:
  1367. switch p.tok.DataAtom {
  1368. case a.Tbody, a.Tfoot, a.Thead:
  1369. if p.elementInScope(tableScope, p.tok.DataAtom) {
  1370. p.clearStackToContext(tableBodyScope)
  1371. p.oe.pop()
  1372. p.im = inTableIM
  1373. }
  1374. return true
  1375. case a.Table:
  1376. if p.popUntil(tableScope, a.Tbody, a.Thead, a.Tfoot) {
  1377. p.im = inTableIM
  1378. return false
  1379. }
  1380. // Ignore the token.
  1381. return true
  1382. case a.Body, a.Caption, a.Col, a.Colgroup, a.Html, a.Td, a.Th, a.Tr:
  1383. // Ignore the token.
  1384. return true
  1385. }
  1386. case CommentToken:
  1387. p.addChild(&Node{
  1388. Type: CommentNode,
  1389. Data: p.tok.Data,
  1390. })
  1391. return true
  1392. }
  1393. return inTableIM(p)
  1394. }
  1395. // Section 12.2.5.4.14.
  1396. func inRowIM(p *parser) bool {
  1397. switch p.tok.Type {
  1398. case StartTagToken:
  1399. switch p.tok.DataAtom {
  1400. case a.Td, a.Th:
  1401. p.clearStackToContext(tableRowScope)
  1402. p.addElement()
  1403. p.afe = append(p.afe, &scopeMarker)
  1404. p.im = inCellIM
  1405. return true
  1406. case a.Caption, a.Col, a.Colgroup, a.Tbody, a.Tfoot, a.Thead, a.Tr:
  1407. if p.popUntil(tableScope, a.Tr) {
  1408. p.im = inTableBodyIM
  1409. return false
  1410. }
  1411. // Ignore the token.
  1412. return true
  1413. }
  1414. case EndTagToken:
  1415. switch p.tok.DataAtom {
  1416. case a.Tr:
  1417. if p.popUntil(tableScope, a.Tr) {
  1418. p.im = inTableBodyIM
  1419. return true
  1420. }
  1421. // Ignore the token.
  1422. return true
  1423. case a.Table:
  1424. if p.popUntil(tableScope, a.Tr) {
  1425. p.im = inTableBodyIM
  1426. return false
  1427. }
  1428. // Ignore the token.
  1429. return true
  1430. case a.Tbody, a.Tfoot, a.Thead:
  1431. if p.elementInScope(tableScope, p.tok.DataAtom) {
  1432. p.parseImpliedToken(EndTagToken, a.Tr, a.Tr.String())
  1433. return false
  1434. }
  1435. // Ignore the token.
  1436. return true
  1437. case a.Body, a.Caption, a.Col, a.Colgroup, a.Html, a.Td, a.Th:
  1438. // Ignore the token.
  1439. return true
  1440. }
  1441. }
  1442. return inTableIM(p)
  1443. }
  1444. // Section 12.2.5.4.15.
  1445. func inCellIM(p *parser) bool {
  1446. switch p.tok.Type {
  1447. case StartTagToken:
  1448. switch p.tok.DataAtom {
  1449. case a.Caption, a.Col, a.Colgroup, a.Tbody, a.Td, a.Tfoot, a.Th, a.Thead, a.Tr:
  1450. if p.popUntil(tableScope, a.Td, a.Th) {
  1451. // Close the cell and reprocess.
  1452. p.clearActiveFormattingElements()
  1453. p.im = inRowIM
  1454. return false
  1455. }
  1456. // Ignore the token.
  1457. return true
  1458. case a.Select:
  1459. p.reconstructActiveFormattingElements()
  1460. p.addElement()
  1461. p.framesetOK = false
  1462. p.im = inSelectInTableIM
  1463. return true
  1464. }
  1465. case EndTagToken:
  1466. switch p.tok.DataAtom {
  1467. case a.Td, a.Th:
  1468. if !p.popUntil(tableScope, p.tok.DataAtom) {
  1469. // Ignore the token.
  1470. return true
  1471. }
  1472. p.clearActiveFormattingElements()
  1473. p.im = inRowIM
  1474. return true
  1475. case a.Body, a.Caption, a.Col, a.Colgroup, a.Html:
  1476. // Ignore the token.
  1477. return true
  1478. case a.Table, a.Tbody, a.Tfoot, a.Thead, a.Tr:
  1479. if !p.elementInScope(tableScope, p.tok.DataAtom) {
  1480. // Ignore the token.
  1481. return true
  1482. }
  1483. // Close the cell and reprocess.
  1484. p.popUntil(tableScope, a.Td, a.Th)
  1485. p.clearActiveFormattingElements()
  1486. p.im = inRowIM
  1487. return false
  1488. }
  1489. }
  1490. return inBodyIM(p)
  1491. }
  1492. // Section 12.2.5.4.16.
  1493. func inSelectIM(p *parser) bool {
  1494. switch p.tok.Type {
  1495. case ErrorToken:
  1496. // Stop parsing.
  1497. return true
  1498. case TextToken:
  1499. p.addText(strings.Replace(p.tok.Data, "\x00", "", -1))
  1500. case StartTagToken:
  1501. switch p.tok.DataAtom {
  1502. case a.Html:
  1503. return inBodyIM(p)
  1504. case a.Option:
  1505. if p.top().DataAtom == a.Option {
  1506. p.oe.pop()
  1507. }
  1508. p.addElement()
  1509. case a.Optgroup:
  1510. if p.top().DataAtom == a.Option {
  1511. p.oe.pop()
  1512. }
  1513. if p.top().DataAtom == a.Optgroup {
  1514. p.oe.pop()
  1515. }
  1516. p.addElement()
  1517. case a.Select:
  1518. p.tok.Type = EndTagToken
  1519. return false
  1520. case a.Input, a.Keygen, a.Textarea:
  1521. if p.elementInScope(selectScope, a.Select) {
  1522. p.parseImpliedToken(EndTagToken, a.Select, a.Select.String())
  1523. return false
  1524. }
  1525. // In order to properly ignore <textarea>, we need to change the tokenizer mode.
  1526. p.tokenizer.NextIsNotRawText()
  1527. // Ignore the token.
  1528. return true
  1529. case a.Script:
  1530. return inHeadIM(p)
  1531. }
  1532. case EndTagToken:
  1533. switch p.tok.DataAtom {
  1534. case a.Option:
  1535. if p.top().DataAtom == a.Option {
  1536. p.oe.pop()
  1537. }
  1538. case a.Optgroup:
  1539. i := len(p.oe) - 1
  1540. if p.oe[i].DataAtom == a.Option {
  1541. i--
  1542. }
  1543. if p.oe[i].DataAtom == a.Optgroup {
  1544. p.oe = p.oe[:i]
  1545. }
  1546. case a.Select:
  1547. if p.popUntil(selectScope, a.Select) {
  1548. p.resetInsertionMode()
  1549. }
  1550. }
  1551. case CommentToken:
  1552. p.addChild(&Node{
  1553. Type: CommentNode,
  1554. Data: p.tok.Data,
  1555. })
  1556. case DoctypeToken:
  1557. // Ignore the token.
  1558. return true
  1559. }
  1560. return true
  1561. }
  1562. // Section 12.2.5.4.17.
  1563. func inSelectInTableIM(p *parser) bool {
  1564. switch p.tok.Type {
  1565. case StartTagToken, EndTagToken:
  1566. switch p.tok.DataAtom {
  1567. case a.Caption, a.Table, a.Tbody, a.Tfoot, a.Thead, a.Tr, a.Td, a.Th:
  1568. if p.tok.Type == StartTagToken || p.elementInScope(tableScope, p.tok.DataAtom) {
  1569. p.parseImpliedToken(EndTagToken, a.Select, a.Select.String())
  1570. return false
  1571. } else {
  1572. // Ignore the token.
  1573. return true
  1574. }
  1575. }
  1576. }
  1577. return inSelectIM(p)
  1578. }
  1579. // Section 12.2.5.4.18.
  1580. func afterBodyIM(p *parser) bool {
  1581. switch p.tok.Type {
  1582. case ErrorToken:
  1583. // Stop parsing.
  1584. return true
  1585. case TextToken:
  1586. s := strings.TrimLeft(p.tok.Data, whitespace)
  1587. if len(s) == 0 {
  1588. // It was all whitespace.
  1589. return inBodyIM(p)
  1590. }
  1591. case StartTagToken:
  1592. if p.tok.DataAtom == a.Html {
  1593. return inBodyIM(p)
  1594. }
  1595. case EndTagToken:
  1596. if p.tok.DataAtom == a.Html {
  1597. if !p.fragment {
  1598. p.im = afterAfterBodyIM
  1599. }
  1600. return true
  1601. }
  1602. case CommentToken:
  1603. // The comment is attached to the <html> element.
  1604. if len(p.oe) < 1 || p.oe[0].DataAtom != a.Html {
  1605. panic("html: bad parser state: <html> element not found, in the after-body insertion mode")
  1606. }
  1607. p.oe[0].AppendChild(&Node{
  1608. Type: CommentNode,
  1609. Data: p.tok.Data,
  1610. })
  1611. return true
  1612. }
  1613. p.im = inBodyIM
  1614. return false
  1615. }
  1616. // Section 12.2.5.4.19.
  1617. func inFramesetIM(p *parser) bool {
  1618. switch p.tok.Type {
  1619. case CommentToken:
  1620. p.addChild(&Node{
  1621. Type: CommentNode,
  1622. Data: p.tok.Data,
  1623. })
  1624. case TextToken:
  1625. // Ignore all text but whitespace.
  1626. s := strings.Map(func(c rune) rune {
  1627. switch c {
  1628. case ' ', '\t', '\n', '\f', '\r':
  1629. return c
  1630. }
  1631. return -1
  1632. }, p.tok.Data)
  1633. if s != "" {
  1634. p.addText(s)
  1635. }
  1636. case StartTagToken:
  1637. switch p.tok.DataAtom {
  1638. case a.Html:
  1639. return inBodyIM(p)
  1640. case a.Frameset:
  1641. p.addElement()
  1642. case a.Frame:
  1643. p.addElement()
  1644. p.oe.pop()
  1645. p.acknowledgeSelfClosingTag()
  1646. case a.Noframes:
  1647. return inHeadIM(p)
  1648. }
  1649. case EndTagToken:
  1650. switch p.tok.DataAtom {
  1651. case a.Frameset:
  1652. if p.oe.top().DataAtom != a.Html {
  1653. p.oe.pop()
  1654. if p.oe.top().DataAtom != a.Frameset {
  1655. p.im = afterFramesetIM
  1656. return true
  1657. }
  1658. }
  1659. }
  1660. default:
  1661. // Ignore the token.
  1662. }
  1663. return true
  1664. }
  1665. // Section 12.2.5.4.20.
  1666. func afterFramesetIM(p *parser) bool {
  1667. switch p.tok.Type {
  1668. case CommentToken:
  1669. p.addChild(&Node{
  1670. Type: CommentNode,
  1671. Data: p.tok.Data,
  1672. })
  1673. case TextToken:
  1674. // Ignore all text but whitespace.
  1675. s := strings.Map(func(c rune) rune {
  1676. switch c {
  1677. case ' ', '\t', '\n', '\f', '\r':
  1678. return c
  1679. }
  1680. return -1
  1681. }, p.tok.Data)
  1682. if s != "" {
  1683. p.addText(s)
  1684. }
  1685. case StartTagToken:
  1686. switch p.tok.DataAtom {
  1687. case a.Html:
  1688. return inBodyIM(p)
  1689. case a.Noframes:
  1690. return inHeadIM(p)
  1691. }
  1692. case EndTagToken:
  1693. switch p.tok.DataAtom {
  1694. case a.Html:
  1695. p.im = afterAfterFramesetIM
  1696. return true
  1697. }
  1698. default:
  1699. // Ignore the token.
  1700. }
  1701. return true
  1702. }
  1703. // Section 12.2.5.4.21.
  1704. func afterAfterBodyIM(p *parser) bool {
  1705. switch p.tok.Type {
  1706. case ErrorToken:
  1707. // Stop parsing.
  1708. return true
  1709. case TextToken:
  1710. s := strings.TrimLeft(p.tok.Data, whitespace)
  1711. if len(s) == 0 {
  1712. // It was all whitespace.
  1713. return inBodyIM(p)
  1714. }
  1715. case StartTagToken:
  1716. if p.tok.DataAtom == a.Html {
  1717. return inBodyIM(p)
  1718. }
  1719. case CommentToken:
  1720. p.doc.AppendChild(&Node{
  1721. Type: CommentNode,
  1722. Data: p.tok.Data,
  1723. })
  1724. return true
  1725. case DoctypeToken:
  1726. return inBodyIM(p)
  1727. }
  1728. p.im = inBodyIM
  1729. return false
  1730. }
  1731. // Section 12.2.5.4.22.
  1732. func afterAfterFramesetIM(p *parser) bool {
  1733. switch p.tok.Type {
  1734. case CommentToken:
  1735. p.doc.AppendChild(&Node{
  1736. Type: CommentNode,
  1737. Data: p.tok.Data,
  1738. })
  1739. case TextToken:
  1740. // Ignore all text but whitespace.
  1741. s := strings.Map(func(c rune) rune {
  1742. switch c {
  1743. case ' ', '\t', '\n', '\f', '\r':
  1744. return c
  1745. }
  1746. return -1
  1747. }, p.tok.Data)
  1748. if s != "" {
  1749. p.tok.Data = s
  1750. return inBodyIM(p)
  1751. }
  1752. case StartTagToken:
  1753. switch p.tok.DataAtom {
  1754. case a.Html:
  1755. return inBodyIM(p)
  1756. case a.Noframes:
  1757. return inHeadIM(p)
  1758. }
  1759. case DoctypeToken:
  1760. return inBodyIM(p)
  1761. default:
  1762. // Ignore the token.
  1763. }
  1764. return true
  1765. }
  1766. const whitespaceOrNUL = whitespace + "\x00"
  1767. // Section 12.2.5.5.
  1768. func parseForeignContent(p *parser) bool {
  1769. switch p.tok.Type {
  1770. case TextToken:
  1771. if p.framesetOK {
  1772. p.framesetOK = strings.TrimLeft(p.tok.Data, whitespaceOrNUL) == ""
  1773. }
  1774. p.tok.Data = strings.Replace(p.tok.Data, "\x00", "\ufffd", -1)
  1775. p.addText(p.tok.Data)
  1776. case CommentToken:
  1777. p.addChild(&Node{
  1778. Type: CommentNode,
  1779. Data: p.tok.Data,
  1780. })
  1781. case StartTagToken:
  1782. b := breakout[p.tok.Data]
  1783. if p.tok.DataAtom == a.Font {
  1784. loop:
  1785. for _, attr := range p.tok.Attr {
  1786. switch attr.Key {
  1787. case "color", "face", "size":
  1788. b = true
  1789. break loop
  1790. }
  1791. }
  1792. }
  1793. if b {
  1794. for i := len(p.oe) - 1; i >= 0; i-- {
  1795. n := p.oe[i]
  1796. if n.Namespace == "" || htmlIntegrationPoint(n) || mathMLTextIntegrationPoint(n) {
  1797. p.oe = p.oe[:i+1]
  1798. break
  1799. }
  1800. }
  1801. return false
  1802. }
  1803. switch p.top().Namespace {
  1804. case "math":
  1805. adjustAttributeNames(p.tok.Attr, mathMLAttributeAdjustments)
  1806. case "svg":
  1807. // Adjust SVG tag names. The tokenizer lower-cases tag names, but
  1808. // SVG wants e.g. "foreignObject" with a capital second "O".
  1809. if x := svgTagNameAdjustments[p.tok.Data]; x != "" {
  1810. p.tok.DataAtom = a.Lookup([]byte(x))
  1811. p.tok.Data = x
  1812. }
  1813. adjustAttributeNames(p.tok.Attr, svgAttributeAdjustments)
  1814. default:
  1815. panic("html: bad parser state: unexpected namespace")
  1816. }
  1817. adjustForeignAttributes(p.tok.Attr)
  1818. namespace := p.top().Namespace
  1819. p.addElement()
  1820. p.top().Namespace = namespace
  1821. if namespace != "" {
  1822. // Don't let the tokenizer go into raw text mode in foreign content
  1823. // (e.g. in an SVG <title> tag).
  1824. p.tokenizer.NextIsNotRawText()
  1825. }
  1826. if p.hasSelfClosingToken {
  1827. p.oe.pop()
  1828. p.acknowledgeSelfClosingTag()
  1829. }
  1830. case EndTagToken:
  1831. for i := len(p.oe) - 1; i >= 0; i-- {
  1832. if p.oe[i].Namespace == "" {
  1833. return p.im(p)
  1834. }
  1835. if strings.EqualFold(p.oe[i].Data, p.tok.Data) {
  1836. p.oe = p.oe[:i]
  1837. break
  1838. }
  1839. }
  1840. return true
  1841. default:
  1842. // Ignore the token.
  1843. }
  1844. return true
  1845. }
  1846. // Section 12.2.5.
  1847. func (p *parser) inForeignContent() bool {
  1848. if len(p.oe) == 0 {
  1849. return false
  1850. }
  1851. n := p.oe[len(p.oe)-1]
  1852. if n.Namespace == "" {
  1853. return false
  1854. }
  1855. if mathMLTextIntegrationPoint(n) {
  1856. if p.tok.Type == StartTagToken && p.tok.DataAtom != a.Mglyph && p.tok.DataAtom != a.Malignmark {
  1857. return false
  1858. }
  1859. if p.tok.Type == TextToken {
  1860. return false
  1861. }
  1862. }
  1863. if n.Namespace == "math" && n.DataAtom == a.AnnotationXml && p.tok.Type == StartTagToken && p.tok.DataAtom == a.Svg {
  1864. return false
  1865. }
  1866. if htmlIntegrationPoint(n) && (p.tok.Type == StartTagToken || p.tok.Type == TextToken) {
  1867. return false
  1868. }
  1869. if p.tok.Type == ErrorToken {
  1870. return false
  1871. }
  1872. return true
  1873. }
  1874. // parseImpliedToken parses a token as though it had appeared in the parser's
  1875. // input.
  1876. func (p *parser) parseImpliedToken(t TokenType, dataAtom a.Atom, data string) {
  1877. realToken, selfClosing := p.tok, p.hasSelfClosingToken
  1878. p.tok = Token{
  1879. Type: t,
  1880. DataAtom: dataAtom,
  1881. Data: data,
  1882. }
  1883. p.hasSelfClosingToken = false
  1884. p.parseCurrentToken()
  1885. p.tok, p.hasSelfClosingToken = realToken, selfClosing
  1886. }
  1887. // parseCurrentToken runs the current token through the parsing routines
  1888. // until it is consumed.
  1889. func (p *parser) parseCurrentToken() {
  1890. if p.tok.Type == SelfClosingTagToken {
  1891. p.hasSelfClosingToken = true
  1892. p.tok.Type = StartTagToken
  1893. }
  1894. consumed := false
  1895. for !consumed {
  1896. if p.inForeignContent() {
  1897. consumed = parseForeignContent(p)
  1898. } else {
  1899. consumed = p.im(p)
  1900. }
  1901. }
  1902. if p.hasSelfClosingToken {
  1903. // This is a parse error, but ignore it.
  1904. p.hasSelfClosingToken = false
  1905. }
  1906. }
  1907. func (p *parser) parse() error {
  1908. // Iterate until EOF. Any other error will cause an early return.
  1909. var err error
  1910. for err != io.EOF {
  1911. // CDATA sections are allowed only in foreign content.
  1912. n := p.oe.top()
  1913. p.tokenizer.AllowCDATA(n != nil && n.Namespace != "")
  1914. // Read and parse the next token.
  1915. p.tokenizer.Next()
  1916. p.tok = p.tokenizer.Token()
  1917. if p.tok.Type == ErrorToken {
  1918. err = p.tokenizer.Err()
  1919. if err != nil && err != io.EOF {
  1920. return err
  1921. }
  1922. }
  1923. p.parseCurrentToken()
  1924. }
  1925. return nil
  1926. }
  1927. // Parse returns the parse tree for the HTML from the given Reader.
  1928. // The input is assumed to be UTF-8 encoded.
  1929. func Parse(r io.Reader) (*Node, error) {
  1930. p := &parser{
  1931. tokenizer: NewTokenizer(r),
  1932. doc: &Node{
  1933. Type: DocumentNode,
  1934. },
  1935. scripting: true,
  1936. framesetOK: true,
  1937. im: initialIM,
  1938. }
  1939. err := p.parse()
  1940. if err != nil {
  1941. return nil, err
  1942. }
  1943. return p.doc, nil
  1944. }
  1945. // ParseFragment parses a fragment of HTML and returns the nodes that were
  1946. // found. If the fragment is the InnerHTML for an existing element, pass that
  1947. // element in context.
  1948. func ParseFragment(r io.Reader, context *Node) ([]*Node, error) {
  1949. contextTag := ""
  1950. if context != nil {
  1951. if context.Type != ElementNode {
  1952. return nil, errors.New("html: ParseFragment of non-element Node")
  1953. }
  1954. // The next check isn't just context.DataAtom.String() == context.Data because
  1955. // it is valid to pass an element whose tag isn't a known atom. For example,
  1956. // DataAtom == 0 and Data = "tagfromthefuture" is perfectly consistent.
  1957. if context.DataAtom != a.Lookup([]byte(context.Data)) {
  1958. return nil, fmt.Errorf("html: inconsistent Node: DataAtom=%q, Data=%q", context.DataAtom, context.Data)
  1959. }
  1960. contextTag = context.DataAtom.String()
  1961. }
  1962. p := &parser{
  1963. tokenizer: NewTokenizerFragment(r, contextTag),
  1964. doc: &Node{
  1965. Type: DocumentNode,
  1966. },
  1967. scripting: true,
  1968. fragment: true,
  1969. context: context,
  1970. }
  1971. root := &Node{
  1972. Type: ElementNode,
  1973. DataAtom: a.Html,
  1974. Data: a.Html.String(),
  1975. }
  1976. p.doc.AppendChild(root)
  1977. p.oe = nodeStack{root}
  1978. p.resetInsertionMode()
  1979. for n := context; n != nil; n = n.Parent {
  1980. if n.Type == ElementNode && n.DataAtom == a.Form {
  1981. p.form = n
  1982. break
  1983. }
  1984. }
  1985. err := p.parse()
  1986. if err != nil {
  1987. return nil, err
  1988. }
  1989. parent := p.doc
  1990. if context != nil {
  1991. parent = root
  1992. }
  1993. var result []*Node
  1994. for c := parent.FirstChild; c != nil; {
  1995. next := c.NextSibling
  1996. parent.RemoveChild(c)
  1997. result = append(result, c)
  1998. c = next
  1999. }
  2000. return result, nil
  2001. }