3
3
const { match} = require ( 'posthtml/lib/api' ) ;
4
4
const parseAttrs = require ( 'posthtml-attrs-parser' ) ;
5
5
const styleToObject = require ( 'style-to-object' ) ;
6
+ const validAttributes = require ( './valid-attributes' ) ;
6
7
const keys = require ( 'lodash/keys' ) ;
7
8
const union = require ( 'lodash/union' ) ;
8
9
const pick = require ( 'lodash/pick' ) ;
@@ -12,7 +13,7 @@ const has = require('lodash/has');
12
13
const extend = require ( 'lodash/extend' ) ;
13
14
const isString = require ( 'lodash/isString' ) ;
14
15
const isObject = require ( 'lodash/isObject' ) ;
15
- const validAttributes = require ( './valid-attributes ' ) ;
16
+ const isEmpty = require ( 'lodash/isEmpty ' ) ;
16
17
17
18
/**
18
19
* Map component attributes that it's not defined as props to first element of node
@@ -45,10 +46,27 @@ module.exports = (currentNode, attributes, props, options, aware) => {
45
46
46
47
const nodeAttrs = parseAttrs ( mainNode . attrs , options . attrsParserRules ) ;
47
48
49
+ // Merge elementAttributes and blacklistAttributes with options provided
50
+ validAttributes . blacklistAttributes = union ( validAttributes . blacklistAttributes , options . blacklistAttributes ) ;
51
+ validAttributes . safelistAttributes = union ( validAttributes . safelistAttributes , options . safelistAttributes ) ;
52
+
53
+ // Merge or override elementAttributes from options provided
54
+ if ( ! isEmpty ( options . elementAttributes ) ) {
55
+ each ( options . elementAttributes , ( tagName , modifier ) => {
56
+ if ( typeof modifier === 'function' && isString ( tagName ) ) {
57
+ tagName = tagName . toUpperCase ( ) ;
58
+ const attributes = modifier ( validAttributes . elementAttributes [ tagName ] ) ;
59
+ if ( Array . isArray ( attributes ) ) {
60
+ validAttributes . elementAttributes [ tagName ] = attributes ;
61
+ }
62
+ }
63
+ } ) ;
64
+ }
65
+
48
66
// Attributes to be excluded
49
- const excludeAttributes = union ( validAttributes . blacklist , keys ( props ) , [ options . attribute ] , keys ( aware ) , keys ( options . props ) , [ '$slots' ] ) ;
67
+ const excludeAttributes = union ( validAttributes . blacklistAttributes , keys ( props ) , [ options . attribute ] , keys ( aware ) , keys ( options . props ) , [ '$slots' ] ) ;
50
68
// All valid HTML attributes for the main element
51
- const allValidElementAttributes = validAttributes . elementAttributes [ mainNode . tag . toUpperCase ( ) ] ;
69
+ const allValidElementAttributes = isString ( mainNode . tag ) && has ( validAttributes . elementAttributes , mainNode . tag . toUpperCase ( ) ) ? validAttributes . elementAttributes [ mainNode . tag . toUpperCase ( ) ] : [ ] ;
52
70
// Valid HTML attributes without the excluded
53
71
const validElementAttributes = difference ( allValidElementAttributes , excludeAttributes ) ;
54
72
// Add override attributes
@@ -59,7 +77,7 @@ module.exports = (currentNode, attributes, props, options, aware) => {
59
77
60
78
// Get additional specified attributes
61
79
each ( attributes , ( value , attr ) => {
62
- each ( validAttributes . additionalAttributes , additionalAttr => {
80
+ each ( validAttributes . safelistAttributes , additionalAttr => {
63
81
if ( additionalAttr === attr || ( additionalAttr . endsWith ( '*' ) && attr . startsWith ( additionalAttr . replace ( '*' , '' ) ) ) ) {
64
82
mainNodeAttributes [ attr ] = value ;
65
83
}
0 commit comments