@@ -10,6 +10,7 @@ export type Directive = {
1010export type Options = {
1111 directives ?: Directive [ ] ;
1212 sourceLocations ?: boolean ;
13+ recognizeNoValueAttribute ?: boolean ;
1314} & ParserOptions ;
1415
1516export type Tag = string | boolean ;
@@ -45,6 +46,7 @@ export const parser = (html: string, options: Options = {}): Node[] => {
4546 const bufArray : Node [ ] = [ ] ;
4647 const results : Node [ ] = [ ] ;
4748 let lastOpenTagEndIndex = 0 ;
49+ let noValueAttributes : Record < string , true > = { } ;
4850
4951 function bufferArrayLast ( ) : Node {
5052 return bufArray [ bufArray . length - 1 ] ;
@@ -70,6 +72,11 @@ export const parser = (html: string, options: Options = {}): Node[] => {
7072 const object : Attributes = { } ;
7173
7274 object [ key ] = String ( attrs [ key ] ) . replace ( / & q u o t ; / g, '"' ) ;
75+
76+ if ( options . recognizeNoValueAttribute && noValueAttributes [ key ] ) {
77+ object [ key ] = true ;
78+ }
79+
7380 Object . assign ( result , object ) ;
7481 } ) ;
7582
@@ -122,6 +129,17 @@ export const parser = (html: string, options: Options = {}): Node[] => {
122129 }
123130 }
124131
132+ function onattribute ( name : string , value : string , quote ?: string | undefined | null ) {
133+ // Quote: Quotes used around the attribute.
134+ // `null` if the attribute has no quotes around the value,
135+ // `undefined` if the attribute has no value.
136+ if ( quote === undefined ) {
137+ // `true` is recognized by posthtml-render as attrubute without value
138+ // See: https://github.com/posthtml/posthtml-render/blob/master/src/index.ts#L268
139+ noValueAttributes [ name ] = true ;
140+ }
141+ }
142+
125143 function onopentag ( tag : string , attrs : Attributes ) {
126144 const buf : NodeTag = { tag } ;
127145
@@ -137,6 +155,10 @@ export const parser = (html: string, options: Options = {}): Node[] => {
137155 buf . attrs = normalizeArributes ( attrs ) ;
138156 }
139157
158+ // Always reset after normalizeArributes
159+ // Reason: onopentag callback will fire after all attrubutes have been processed
160+ noValueAttributes = { } ;
161+
140162 bufArray . push ( buf ) ;
141163 }
142164
@@ -201,6 +223,7 @@ export const parser = (html: string, options: Options = {}): Node[] => {
201223 const parser = new Parser ( {
202224 onprocessinginstruction,
203225 oncomment,
226+ onattribute,
204227 onopentag,
205228 onclosetag,
206229 ontext
0 commit comments