1- import { nodeType } from "../types"
1+ import { DiffDOMOptionsPartial , nodeType } from "../types"
22
33// from html-parse-stringify (MIT)
44
55const tagRE =
66 / < \s * \/ * [ a - z A - Z : _ ] [ a - z A - Z 0 - 9 : _ \- . ] * \s * (?: " [ ^ " ] * " [ ' " ] * | ' [ ^ ' ] * ' [ ' " ] * | [ ^ ' " / > ] ) * \/ * \s * > | < ! - - (?: .| \n | \r ) * ?- - > / g
77
8- // re-used obj for quick lookups of components
9- const empty = Object . create ? Object . create ( null ) : { }
108const attrRE = / \s ( [ ^ ' " / \s > < ] + ?) [ \s / > ] | ( [ ^ \s = ] + ) = \s ? ( " .* ?" | ' .* ?' ) / g
119
1210function unescape ( string : string ) {
@@ -97,7 +95,10 @@ const parseTag = (tag: string) => {
9795 }
9896}
9997
100- export const stringToObj = ( html : string , options = { components : empty } ) => {
98+ export const stringToObj = (
99+ html : string ,
100+ options : DiffDOMOptionsPartial = { valueDiffing : true }
101+ ) => {
101102 const result : nodeType [ ] = [ ]
102103 let current : { type : string ; node : nodeType ; voidElement : boolean }
103104 let level = - 1
@@ -147,13 +148,6 @@ export const stringToObj = (html: string, options = { components: empty }) => {
147148 if ( isOpen ) {
148149 current = parseTag ( tag )
149150 level ++
150- if (
151- current . type === "tag" &&
152- options . components [ current . node . nodeName ]
153- ) {
154- current . type = "component"
155- inComponent = true
156- }
157151 if (
158152 ! current . voidElement &&
159153 ! inComponent &&
@@ -163,10 +157,19 @@ export const stringToObj = (html: string, options = { components: empty }) => {
163157 if ( ! current . node . childNodes ) {
164158 current . node . childNodes = [ ]
165159 }
160+ const data = unescape (
161+ html . slice ( start , html . indexOf ( "<" , start ) )
162+ )
166163 current . node . childNodes . push ( {
167164 nodeName : "#text" ,
168- data : unescape ( html . slice ( start , html . indexOf ( "<" , start ) ) ) ,
165+ data,
169166 } )
167+ if (
168+ options . valueDiffing &&
169+ current . node . nodeName === "TEXTAREA"
170+ ) {
171+ current . node . value = data
172+ }
170173 }
171174 // if we're at root, push new base node
172175 if ( level === 0 && current . node . nodeName ) {
0 commit comments