@@ -194,9 +194,8 @@ func render1(w writer, n *Node) error {
194194 }
195195 }
196196
197- // Render any child nodes.
198- switch n .Data {
199- case "iframe" , "noembed" , "noframes" , "noscript" , "plaintext" , "script" , "style" , "xmp" :
197+ // Render any child nodes
198+ if childTextNodesAreLiteral (n ) {
200199 for c := n .FirstChild ; c != nil ; c = c .NextSibling {
201200 if c .Type == TextNode {
202201 if _ , err := w .WriteString (c .Data ); err != nil {
@@ -213,7 +212,7 @@ func render1(w writer, n *Node) error {
213212 // last element in the file, with no closing tag.
214213 return plaintextAbort
215214 }
216- default :
215+ } else {
217216 for c := n .FirstChild ; c != nil ; c = c .NextSibling {
218217 if err := render1 (w , c ); err != nil {
219218 return err
@@ -231,6 +230,27 @@ func render1(w writer, n *Node) error {
231230 return w .WriteByte ('>' )
232231}
233232
233+ func childTextNodesAreLiteral (n * Node ) bool {
234+ // Per WHATWG HTML 13.3, if the parent of the current node is a style,
235+ // script, xmp, iframe, noembed, noframes, or plaintext element, and the
236+ // current node is a text node, append the value of the node's data
237+ // literally. The specification is not explicit about it, but we only
238+ // enforce this if we are in the HTML namespace (i.e. when the namespace is
239+ // "").
240+ // NOTE: we also always include noscript elements, although the
241+ // specification states that they should only be rendered as such if
242+ // scripting is enabled for the node (which is not something we track).
243+ if n .Namespace != "" {
244+ return false
245+ }
246+ switch n .Data {
247+ case "iframe" , "noembed" , "noframes" , "noscript" , "plaintext" , "script" , "style" , "xmp" :
248+ return true
249+ default :
250+ return false
251+ }
252+ }
253+
234254// writeQuoted writes s to w surrounded by quotes. Normally it will use double
235255// quotes, but if s contains a double quote, it will use single quotes.
236256// It is used for writing the identifiers in a doctype declaration.
0 commit comments