@@ -72,6 +72,7 @@ export const defaultParserOptions: MergedParserOptions = {
7272 getNamespace : ( ) => Namespaces . HTML ,
7373 isVoidTag : NO ,
7474 isPreTag : NO ,
75+ isIgnoreNewlineTag : NO ,
7576 isCustomElement : NO ,
7677 onError : defaultOnError ,
7778 onWarn : defaultOnWarn ,
@@ -633,7 +634,7 @@ function onCloseTag(el: ElementNode, end: number, isImplied = false) {
633634 }
634635
635636 // refine element type
636- const { tag, ns } = el
637+ const { tag, ns, children } = el
637638 if ( ! inVPre ) {
638639 if ( tag === 'slot' ) {
639640 el . tagType = ElementTypes . SLOT
@@ -646,8 +647,18 @@ function onCloseTag(el: ElementNode, end: number, isImplied = false) {
646647
647648 // whitespace management
648649 if ( ! tokenizer . inRCDATA ) {
649- el . children = condenseWhitespace ( el . children , el . tag )
650+ el . children = condenseWhitespace ( children , tag )
650651 }
652+
653+ if ( ns === Namespaces . HTML && currentOptions . isIgnoreNewlineTag ( tag ) ) {
654+ // remove leading newline for <textarea> and <pre> per html spec
655+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inbody
656+ const first = children [ 0 ]
657+ if ( first && first . type === NodeTypes . TEXT ) {
658+ first . content = first . content . replace ( / ^ \r ? \n / , '' )
659+ }
660+ }
661+
651662 if ( ns === Namespaces . HTML && currentOptions . isPreTag ( tag ) ) {
652663 inPre --
653664 }
@@ -869,14 +880,6 @@ function condenseWhitespace(
869880 }
870881 }
871882 }
872- if ( inPre && tag && currentOptions . isPreTag ( tag ) ) {
873- // remove leading newline per html spec
874- // https://html.spec.whatwg.org/multipage/grouping-content.html#the-pre-element
875- const first = nodes [ 0 ]
876- if ( first && first . type === NodeTypes . TEXT ) {
877- first . content = first . content . replace ( / ^ \r ? \n / , '' )
878- }
879- }
880883 return removedWhitespace ? nodes . filter ( Boolean ) : nodes
881884}
882885
0 commit comments