Skip to content

Commit 65129d6

Browse files
committed
fix some testcases and make sure we no longer move <style> to <head> when it's in <body>
--HG-- extra : convert_revision : svn%3Aacbfec75-9323-0410-a652-858a13e371e0/trunk%40475
1 parent 77afe43 commit 65129d6

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

src/html5parser.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@ def __init__(self, parser, tree):
389389

390390
self.startTagHandler = utils.MethodDispatcher([
391391
("html", self.startTagHtml),
392-
(("title", "style"), self.startTagTitleStyle),
392+
("title", self.startTagTitle),
393+
("style", self.startTagStyle),
393394
("script", self.startTagScript),
394395
(("base", "link", "meta"), self.startTagBaseLinkMeta),
395396
("head", self.startTagHead)
@@ -430,25 +431,31 @@ def startTagHead(self, name, attributes):
430431
self.tree.headPointer = self.tree.openElements[-1]
431432
self.parser.phase = self.parser.phases["inHead"]
432433

433-
def startTagTitleStyle(self, name, attributes):
434-
cmFlags = {"title":"RCDATA", "style":"CDATA"}
434+
def startTagTitle(self, name, attributes):
435435
element = self.tree.createElement(name, attributes)
436436
self.appendToHead(element)
437437
self.tree.openElements.append(element)
438-
self.parser.tokenizer.contentModelFlag =\
439-
contentModelFlags[cmFlags[name]]
438+
self.parser.tokenizer.contentModelFlag = contentModelFlags["RCDATA"]
439+
440+
def startTagStyle(self, name, attributes):
441+
element = self.tree.createElement(name, attributes)
442+
if self.tree.headPointer is not None and\
443+
self.parser.phase == self.parser.phases["inHead"]:
444+
self.appendToHead(element)
445+
else:
446+
self.tree.openElements[-1].appendChild(element)
447+
self.tree.openElements.append(element)
448+
self.parser.tokenizer.contentModelFlag = contentModelFlags["CDATA"]
440449

441450
def startTagScript(self, name, attributes):
442451
element = self.tree.createElement(name, attributes)
443452
element._flags.append("parser-inserted")
444-
445-
# XXX in theory we should check if we're actually in the InHead state
446-
# here and if the headElementPointer is not zero but it seems to work
447-
# without that being the case.
448-
self.tree.openElements[-1].appendChild(element)
453+
if self.tree.headPointer is not None and\
454+
self.parser.phase == self.parser.phases["inHead"]:
455+
self.appendToHead(element)
456+
else:
457+
self.tree.openElements[-1].appendChild(element)
449458
self.tree.openElements.append(element)
450-
451-
# XXX AT we could use self.tree.insertElement(name, attributes) ...
452459
self.parser.tokenizer.contentModelFlag = contentModelFlags["CDATA"]
453460

454461
def startTagBaseLinkMeta(self, name, attributes):
@@ -518,7 +525,7 @@ def startTagFrameset(self, name, attributes):
518525

519526
def startTagFromHead(self, name, attributes):
520527
self.parser.parseError(_(u"Unexpected start tag (" + name +\
521-
") that belongs in head. Moved."))
528+
") that can be in head. Moved."))
522529
self.parser.phase = self.parser.phases["inHead"]
523530
self.parser.phase.processStartTag(name, attributes)
524531

@@ -542,8 +549,8 @@ def __init__(self, parser, tree):
542549
Phase.__init__(self, parser, tree)
543550
self.startTagHandler = utils.MethodDispatcher([
544551
("html", self.startTagHtml),
545-
("script", self.startTagScript),
546-
(("base", "link", "meta", "style", "title"),
552+
(("script", "style"), self.startTagScriptStyle),
553+
(("base", "link", "meta", "title"),
547554
self.startTagFromHead),
548555
("body", self.startTagBody),
549556
(("address", "blockquote", "center", "dir", "div", "dl",
@@ -615,7 +622,7 @@ def processCharacters(self, data):
615622
self.tree.reconstructActiveFormattingElements()
616623
self.tree.insertText(data)
617624

618-
def startTagScript(self, name, attributes):
625+
def startTagScriptStyle(self, name, attributes):
619626
self.parser.phases["inHead"].processStartTag(name, attributes)
620627

621628
def startTagFromHead(self, name, attributes):

0 commit comments

Comments
 (0)