@@ -11,26 +11,57 @@ import { elementFromPoint } from '../../utils/src/polyfill';
1111import { VRange } from '../../core/src/VRange' ;
1212import { InsertTextParams , Char } from '../../plugin-char/src/Char' ;
1313import { Context } from '../../core/src/ContextManager' ;
14+ import { AbstractParser } from '../../plugin-parser/src/AbstractParser' ;
15+ import { Constructor } from '../../utils/src/utils' ;
16+ import { HtmlDomParsingEngine } from '../../plugin-html/src/HtmlDomParsingEngine' ;
17+ import {
18+ TableRowXmlDomParser ,
19+ TableSectionAttributes ,
20+ } from '../../plugin-table/src/TableRowXmlDomParser' ;
21+ import {
22+ ListItemXmlDomParser ,
23+ ListItemAttributes ,
24+ } from '../../plugin-list/src/ListItemXmlDomParser' ;
1425
1526export class DomHelpers < T extends JWPluginConfig = JWPluginConfig > extends JWPlugin < T > {
27+ static dependencies = [ Parser ] ;
28+ private _specializedAttributes : Map < AbstractParser < Node > , Constructor < Attributes > > = new Map ( ) ;
29+
30+ async start ( ) : Promise < void > {
31+ await super . start ( ) ;
32+ const engine = this . editor . plugins . get ( Parser ) . engines [
33+ HtmlDomParsingEngine . id
34+ ] as HtmlDomParsingEngine ;
35+ for ( const parser of engine . parsers ) {
36+ if ( parser . constructor === TableRowXmlDomParser ) {
37+ this . _specializedAttributes . set ( parser , TableSectionAttributes ) ;
38+ } else if ( parser . constructor === ListItemXmlDomParser ) {
39+ this . _specializedAttributes . set ( parser , ListItemAttributes ) ;
40+ }
41+ }
42+ }
43+
1644 //--------------------------------------------------------------------------
1745 // Public
1846 //--------------------------------------------------------------------------
19-
2047 /**
2148 * Add a class or a list of classes to a DOM node or a list of DOM nodes.
2249 *
2350 * @param params
2451 */
2552 async addClass (
2653 context : ExecutionContext ,
27- domNode : Node | Node [ ] ,
54+ originalDomNode : Node | Node [ ] ,
2855 className : string | string [ ] ,
2956 ) : Promise < ExecCommandResult > {
3057 const domHelpersAddClass = async ( ) : Promise < void > => {
31- const classes = Array . isArray ( className ) ? className : [ className ] ;
32- for ( const node of this . getNodes ( domNode ) ) {
33- node . modifiers . get ( Attributes ) . classList . add ( ...classes ) ;
58+ const domNodes = Array . isArray ( originalDomNode ) ? originalDomNode : [ originalDomNode ] ;
59+ for ( const domNode of domNodes ) {
60+ const Attributes = this . _getAttributesConstructor ( domNode ) ;
61+ const classes = Array . isArray ( className ) ? className : [ className ] ;
62+ for ( const node of this . getNodes ( domNode ) ) {
63+ node . modifiers . get ( Attributes ) . classList . add ( ...classes ) ;
64+ }
3465 }
3566 } ;
3667 return context . execCommand ( domHelpersAddClass ) ;
@@ -42,15 +73,19 @@ export class DomHelpers<T extends JWPluginConfig = JWPluginConfig> extends JWPlu
4273 */
4374 async removeClass (
4475 context : ExecutionContext ,
45- domNode : Node | Node [ ] ,
76+ originalDomNode : Node | Node [ ] ,
4677 className : string | string [ ] ,
4778 ) : Promise < ExecCommandResult > {
4879 const domHelpersRemoveClass = async ( ) : Promise < void > => {
4980 const classes = Array . isArray ( className ) ? className : [ className ] ;
50- for ( const node of this . getNodes ( domNode ) ) {
51- node . modifiers . find ( Attributes ) ?. classList . remove ( ...classes ) ;
52- for ( const modifier of node . modifiers . filter ( Format ) ) {
53- modifier . modifiers . find ( Attributes ) ?. classList . remove ( ...classes ) ;
81+ const domNodes = Array . isArray ( originalDomNode ) ? originalDomNode : [ originalDomNode ] ;
82+ for ( const domNode of domNodes ) {
83+ const Attributes = this . _getAttributesConstructor ( domNode ) ;
84+ for ( const node of this . getNodes ( domNode ) ) {
85+ node . modifiers . find ( Attributes ) ?. classList . remove ( ...classes ) ;
86+ for ( const modifier of node . modifiers . filter ( Format ) ) {
87+ modifier . modifiers . find ( Attributes ) ?. classList . remove ( ...classes ) ;
88+ }
5489 }
5590 }
5691 } ;
@@ -64,13 +99,17 @@ export class DomHelpers<T extends JWPluginConfig = JWPluginConfig> extends JWPlu
6499 */
65100 async toggleClass (
66101 context : ExecutionContext ,
67- domNode : Node | Node [ ] ,
102+ originalDomNode : Node | Node [ ] ,
68103 className : string ,
69104 ) : Promise < ExecCommandResult > {
70105 const domHelpersToggleClass = async ( ) : Promise < void > => {
71106 const classes = Array . isArray ( className ) ? className : [ className ] ;
72- for ( const node of this . getNodes ( domNode ) ) {
73- node . modifiers . get ( Attributes ) . classList . toggle ( ...classes ) ;
107+ const domNodes = Array . isArray ( originalDomNode ) ? originalDomNode : [ originalDomNode ] ;
108+ for ( const domNode of domNodes ) {
109+ const Attributes = this . _getAttributesConstructor ( domNode ) ;
110+ for ( const node of this . getNodes ( domNode ) ) {
111+ node . modifiers . get ( Attributes ) . classList . toggle ( ...classes ) ;
112+ }
74113 }
75114 } ;
76115 return context . execCommand ( domHelpersToggleClass ) ;
@@ -82,13 +121,17 @@ export class DomHelpers<T extends JWPluginConfig = JWPluginConfig> extends JWPlu
82121 */
83122 async setAttribute (
84123 context : ExecutionContext ,
85- domNode : Node | Node [ ] ,
124+ originalDomNode : Node | Node [ ] ,
86125 name : string ,
87126 value : string ,
88127 ) : Promise < ExecCommandResult > {
89128 const domHelpersSetAttribute = async ( ) : Promise < void > => {
90- for ( const node of this . getNodes ( domNode ) ) {
91- node . modifiers . get ( Attributes ) . set ( name , value ) ;
129+ const domNodes = Array . isArray ( originalDomNode ) ? originalDomNode : [ originalDomNode ] ;
130+ for ( const domNode of domNodes ) {
131+ const Attributes = this . _getAttributesConstructor ( domNode ) ;
132+ for ( const node of this . getNodes ( domNode ) ) {
133+ node . modifiers . get ( Attributes ) . set ( name , value ) ;
134+ }
92135 }
93136 } ;
94137 return context . execCommand ( domHelpersSetAttribute ) ;
@@ -100,14 +143,18 @@ export class DomHelpers<T extends JWPluginConfig = JWPluginConfig> extends JWPlu
100143 */
101144 async updateAttributes (
102145 context : ExecutionContext ,
103- domNode : Node | Node [ ] ,
146+ originalDomNode : Node | Node [ ] ,
104147 attributes : { [ key : string ] : string } ,
105148 ) : Promise < ExecCommandResult > {
106149 const domHelpersUpdateAttribute = async ( ) : Promise < void > => {
107- for ( const node of this . getNodes ( domNode ) ) {
108- node . modifiers . get ( Attributes ) . clear ( ) ;
109- for ( const [ name , value ] of Object . entries ( attributes ) ) {
110- node . modifiers . get ( Attributes ) . set ( name , value ) ;
150+ const domNodes = Array . isArray ( originalDomNode ) ? originalDomNode : [ originalDomNode ] ;
151+ for ( const domNode of domNodes ) {
152+ const Attributes = this . _getAttributesConstructor ( domNode ) ;
153+ for ( const node of this . getNodes ( domNode ) ) {
154+ node . modifiers . get ( Attributes ) . clear ( ) ;
155+ for ( const [ name , value ] of Object . entries ( attributes ) ) {
156+ node . modifiers . get ( Attributes ) . set ( name , value ) ;
157+ }
111158 }
112159 }
113160 } ;
@@ -120,15 +167,19 @@ export class DomHelpers<T extends JWPluginConfig = JWPluginConfig> extends JWPlu
120167 */
121168 async setStyle (
122169 context : ExecutionContext ,
123- domNode : Node | Node [ ] ,
170+ originalDomNode : Node | Node [ ] ,
124171 name : string ,
125172 value : string ,
126173 important ?: boolean ,
127174 ) : Promise < ExecCommandResult > {
128175 const domHelpersSetStyle = async ( ) : Promise < void > => {
129- for ( const node of this . getNodes ( domNode ) ) {
130- value = important ? value + ' !important' : value ;
131- node . modifiers . get ( Attributes ) . style . set ( name , value ) ;
176+ const domNodes = Array . isArray ( originalDomNode ) ? originalDomNode : [ originalDomNode ] ;
177+ for ( const domNode of domNodes ) {
178+ const Attributes = this . _getAttributesConstructor ( domNode ) ;
179+ for ( const node of this . getNodes ( domNode ) ) {
180+ value = important ? value + ' !important' : value ;
181+ node . modifiers . get ( Attributes ) . style . set ( name , value ) ;
182+ }
132183 }
133184 } ;
134185 return context . execCommand ( domHelpersSetStyle ) ;
@@ -419,4 +470,12 @@ export class DomHelpers<T extends JWPluginConfig = JWPluginConfig> extends JWPlu
419470 div . innerHTML = html ;
420471 return parser . parse ( 'dom/html' , ...div . childNodes ) ;
421472 }
473+ private _getAttributesConstructor ( node : Node ) : Constructor < Attributes > {
474+ for ( const [ parser , Attributes ] of this . _specializedAttributes ) {
475+ if ( parser . predicate ( node ) ) {
476+ return Attributes ;
477+ }
478+ }
479+ return Attributes ;
480+ }
422481}
0 commit comments