@@ -6,6 +6,7 @@ const expressions = require('posthtml-expressions');
66const scriptDataLocals = require ( 'posthtml-expressions/lib/locals' ) ;
77const { parser : parseToPostHtml } = require ( 'posthtml-parser' ) ;
88const parseAttrs = require ( 'posthtml-attrs-parser' ) ;
9+ // const matchHelper = require('posthtml-match-helper');
910const { match} = require ( 'posthtml/lib/api' ) ;
1011const merge = require ( 'deepmerge' ) ;
1112const findPathFromTagName = require ( './find-path' ) ;
@@ -61,19 +62,24 @@ function processNodes(tree, options, messages) {
6162
6263 const index = node . content . findIndex ( content => typeof content === 'object' ) ;
6364
64- const nodeAttrs = parseAttrs ( node . content [ index ] . attrs ) ;
65-
66- Object . keys ( attributes ) . forEach ( attr => {
67- if ( typeof defaultLocals [ attr ] === 'undefined' ) {
68- if ( [ 'class' ] . includes ( attr ) ) {
69- nodeAttrs [ attr ] . push ( attributes [ attr ] ) ;
70- } else if ( [ 'style' ] . includes ( attr ) ) {
71- nodeAttrs [ attr ] = attributes [ attr ] ;
65+ if ( index !== - 1 ) {
66+ // Map component attributes that it's not defined
67+ // as locals to first element of node
68+ // for now only class and style
69+ const nodeAttrs = parseAttrs ( node . content [ index ] . attrs ) ;
70+
71+ Object . keys ( attributes ) . forEach ( attr => {
72+ if ( typeof defaultLocals [ attr ] === 'undefined' ) {
73+ if ( [ 'class' ] . includes ( attr ) ) {
74+ nodeAttrs [ attr ] . push ( attributes [ attr ] ) ;
75+ } else if ( [ 'style' ] . includes ( attr ) ) {
76+ nodeAttrs [ attr ] = attributes [ attr ] ;
77+ }
7278 }
73- }
74- } ) ;
79+ } ) ;
7580
76- node . content [ index ] . attrs = nodeAttrs . compose ( ) ;
81+ node . content [ index ] . attrs = nodeAttrs . compose ( ) ;
82+ }
7783
7884 messages . push ( {
7985 type : 'dependency' ,
@@ -89,17 +95,17 @@ function processNodes(tree, options, messages) {
8995
9096/**
9197 * Parse locals from attributes, globals and via script
92- * @param {Object } tree - posthtml tree
9398 * @param {Object } options - plugin options
94- * @param {Object } html - posthtml tree
99+ * @param {Object } tree - PostHTML Node
100+ * @param {Array } html - PostHTML Tree Nodes
95101 * @return {Object } merged locals and default
96102 */
97103function parseLocals ( options , { attrs} , html ) {
98104 let attributes = { ...attrs } ;
99105
100106 Object . keys ( attributes ) . forEach ( attribute => {
101107 try {
102- // Use merge()
108+ // Use merge() ?
103109 attributes = { ...attributes , ...JSON . parse ( attributes [ attribute ] ) } ;
104110 } catch { }
105111 } ) ;
@@ -122,14 +128,22 @@ function parseLocals(options, {attrs}, html) {
122128/**
123129 * Merge slots content
124130 * @param {Object } tree
125- * @param {Object } component
131+ * @param {Object } node
126132 * @param {Boolean } strict
127133 * @param {String } slotTagName
128134 * @return {Object } tree
129135 */
130- function mergeSlots ( tree , component , strict , slotTagName ) {
136+ function mergeSlots ( tree , node , strict , slotTagName ) {
131137 const slots = getSlots ( slotTagName , tree ) ; // Slot in component.html
132- const fillSlots = getSlots ( slotTagName , component . content ) ; // Slot in page.html
138+ const fillSlots = getSlots ( slotTagName , node . content ) ; // Slot in page.html
139+
140+ // Retrieve main content, means everything that is not inside slots
141+ if ( node . content ) {
142+ const contentOutsideSlots = node . content . filter ( content => content . tag !== 'slot' ) ;
143+ if ( contentOutsideSlots . length > 0 ) {
144+ fillSlots [ defaultSlotName ] = [ { tag : 'slot' , attrs : { name : defaultSlotName } , content : [ ...contentOutsideSlots ] } ] ;
145+ }
146+ }
133147
134148 for ( const slotName of Object . keys ( slots ) ) {
135149 const fillSlotNodes = fillSlots [ slotName ] ;
@@ -145,6 +159,12 @@ function mergeSlots(tree, component, strict, slotTagName) {
145159 continue ;
146160 }
147161
162+ if ( ! slotNode . attrs ) {
163+ slotNode . attrs = {
164+ type : defaultSlotType
165+ } ;
166+ }
167+
148168 let slotType = slotTypes [ ( slotNode . attrs . type || defaultSlotType ) . toLowerCase ( ) ] || defaultSlotType ;
149169 slotType = typeof slotNode . attrs . append === 'undefined' ? ( typeof slotNode . attrs . prepend === 'undefined' ? slotType : slotTypes . prepend ) : slotTypes . append ;
150170
@@ -211,6 +231,8 @@ function getSlots(tag, content = []) {
211231 node . attrs = { } ;
212232 }
213233
234+ // When missing name try to retrieve from attribute
235+ // so slot name can be shorthands like <slot content> => <slot name="content">
214236 if ( ! node . attrs . name ) {
215237 node . attrs . name = Object . keys ( { ...node . attrs } ) . find ( name => ! Object . keys ( slotTypes ) . includes ( name ) && name !== 'type' && name !== defaultSlotType ) ;
216238 }
@@ -257,6 +279,7 @@ module.exports = (options = {}) => {
257279 fileExtension : 'html' ,
258280 tagPrefix : 'x-' ,
259281 tagRegExp : new RegExp ( `^${ options . tagPrefix || 'x-' } ` , 'i' ) ,
282+ defaultSlotRegex : new RegExp ( '^((?!(slot)).)*$' ) ,
260283 slotTagName : 'slot' ,
261284 tagName : 'component' ,
262285 locals : { } ,
0 commit comments