|
18 | 18 | */
|
19 | 19 | package org.netbeans.modules.web.jsf.editor;
|
20 | 20 |
|
21 |
| -import java.util.Arrays; |
22 | 21 | import java.util.Collection;
|
23 | 22 | import java.util.Collections;
|
| 23 | +import java.util.EnumSet; |
24 | 24 | import java.util.HashMap;
|
| 25 | +import java.util.List; |
25 | 26 | import java.util.Map;
|
| 27 | + |
26 | 28 | import javax.swing.text.Document;
|
| 29 | + |
27 | 30 | import org.netbeans.api.html.lexer.HTMLTokenId;
|
28 | 31 | import org.netbeans.api.lexer.InputAttributes;
|
29 | 32 | import org.netbeans.modules.html.editor.api.gsf.HtmlParserResult;
|
|
34 | 37 | import org.netbeans.modules.parsing.spi.SchedulerTask;
|
35 | 38 | import org.netbeans.modules.parsing.spi.ParserResultTask;
|
36 | 39 | import org.netbeans.modules.parsing.spi.TaskFactory;
|
| 40 | +import org.netbeans.modules.web.jsfapi.api.Attribute; |
37 | 41 | import org.netbeans.modules.web.jsfapi.api.DefaultLibraryInfo;
|
38 | 42 | import org.netbeans.modules.web.jsfapi.api.Library;
|
39 | 43 | import org.netbeans.modules.web.jsfapi.api.LibraryComponent;
|
40 |
| -import org.netbeans.modules.web.jsfapi.api.NamespaceUtils; |
| 44 | +import org.netbeans.modules.web.jsfapi.api.LibraryInfo; |
41 | 45 | import org.netbeans.modules.web.jsfapi.api.Tag;
|
42 | 46 |
|
43 | 47 | /**
|
44 |
| - * |
| 48 | + * |
45 | 49 | * @author Marek Fukala
|
46 | 50 | */
|
47 | 51 | public final class HtmlSourceTask extends ParserResultTask<HtmlParserResult> {
|
48 | 52 |
|
49 | 53 | private static final String CSS_CLASS_MAP_PROPERTY_KEY = "cssClassTagAttrMap"; //semi api - defined in HtmlLexer
|
50 |
| - private static final String STYLE_CLASS_ATTR_NAME = "styleClass"; //NOI18N |
| 54 | + private static final String CLASS = "Class"; //NOI18N |
| 55 | + private static final String CLASSES = "Classes"; //NOI18N |
| 56 | + private static final EnumSet<DefaultLibraryInfo> LIBRARIES_TO_SKIP = EnumSet.of(DefaultLibraryInfo.FACELETS, DefaultLibraryInfo.JSF, |
| 57 | + DefaultLibraryInfo.COMPOSITE, DefaultLibraryInfo.JSF_CORE, DefaultLibraryInfo.JSTL_CORE, DefaultLibraryInfo.JSTL_CORE_FUNCTIONS, |
| 58 | + DefaultLibraryInfo.PASSTHROUGH); |
51 | 59 |
|
52 | 60 | public static class Factory extends TaskFactory {
|
53 | 61 |
|
@@ -105,45 +113,43 @@ public void run(HtmlParserResult result, SchedulerEvent event) {
|
105 | 113 | }
|
106 | 114 |
|
107 | 115 | //enable css class embedding in default facelets libraries tags
|
108 |
| - //TODO this should be done in some more generic way but so far I haven't |
109 |
| - //found a way how to get an info if a tag's attribute represents css class or not. |
110 |
| - //It seems that almost only html library contains such tags, we should |
111 |
| - //probably create some metadata also for third party libraries |
112 |
| - |
113 |
| - //check if the default html library is defined |
114 |
| - String prefix = NamespaceUtils.getForNs(result.getNamespaces(), DefaultLibraryInfo.HTML.getNamespace()); |
115 |
| - if (prefix != null) { |
116 |
| - //html lib declared, lets build a map of tags containing attributes whose values are |
117 |
| - //supposed to represent a css class. The map is then put into the document's |
118 |
| - //input attributes and then html lexer takes this information into account |
119 |
| - //when lexing the html code |
120 |
| - Map<String, Collection<String>> cssClassTagAttrMap = new HashMap<>(); |
121 |
| - Library lib = sup.getLibrary(DefaultLibraryInfo.HTML.getNamespace()); |
| 116 | + Map<String, Collection<String>> cssClassTagAttrMap = new HashMap<>(); |
| 117 | + |
| 118 | + //lets build a map of tags containing attributes whose values are |
| 119 | + //supposed to represent a css class. The map is then put into the document's |
| 120 | + //input attributes and then html lexer takes this information into account |
| 121 | + //when lexing the html code |
| 122 | + for (Map.Entry<String, String> entry : result.getNamespaces().entrySet()) { |
| 123 | + String prefix = entry.getValue(); |
| 124 | + if (prefix == null) { |
| 125 | + continue; |
| 126 | + } |
| 127 | + String namespace = entry.getKey(); |
| 128 | + LibraryInfo libraryInfo = DefaultLibraryInfo.forNamespace(namespace); |
| 129 | + if (libraryInfo instanceof DefaultLibraryInfo dli && LIBRARIES_TO_SKIP.contains(dli)) { |
| 130 | + continue; |
| 131 | + } |
| 132 | + |
| 133 | + Library lib = sup.getLibrary(namespace); |
122 | 134 | if (lib != null) {
|
123 | 135 | Collection<? extends LibraryComponent> components = lib.getComponents();
|
124 | 136 | for (LibraryComponent comp : components) {
|
125 | 137 | Tag tag = comp.getTag();
|
126 |
| - //hacking datatable's attributes embedding - waiting for Tomasz' tag metadata API |
127 |
| - if ("dataTable".equals(tag.getName())) { //NOI18N |
128 |
| - cssClassTagAttrMap.put(prefix + ":" + tag.getName(), |
129 |
| - Arrays.asList(new String[]{STYLE_CLASS_ATTR_NAME, |
130 |
| - "headerClass", "footerClass", "rowClasses", "columnClasses", "captionClass"})); //NOI18N |
131 |
| - } else { |
132 |
| - if (tag.getAttribute(STYLE_CLASS_ATTR_NAME) != null) { |
133 |
| - cssClassTagAttrMap.put(prefix + ":" + tag.getName(), Collections.singletonList(STYLE_CLASS_ATTR_NAME)); |
134 |
| - } |
| 138 | + if (tag == null) { |
| 139 | + continue; |
| 140 | + } |
| 141 | + List<String> cssClassAttributes = tag.getAttributes().stream() |
| 142 | + .map(Attribute::getName) |
| 143 | + .filter(name -> name.endsWith(CLASS) || name.endsWith(CLASSES)) |
| 144 | + .toList(); |
| 145 | + if (!cssClassAttributes.isEmpty()) { |
| 146 | + cssClassTagAttrMap.put(prefix + ":" + tag.getName(), cssClassAttributes); |
135 | 147 | }
|
136 | 148 | }
|
137 | 149 | }
|
138 |
| - |
139 |
| - inputAttributes.setValue(HTMLTokenId.language(), CSS_CLASS_MAP_PROPERTY_KEY, cssClassTagAttrMap, true); |
140 |
| - |
141 |
| - } else { |
142 |
| - //remove the map, the html library is not declared (anymore) |
143 |
| - inputAttributes.setValue(HTMLTokenId.language(), CSS_CLASS_MAP_PROPERTY_KEY, null, true); |
144 | 150 | }
|
145 | 151 |
|
| 152 | + inputAttributes.setValue(HTMLTokenId.language(), CSS_CLASS_MAP_PROPERTY_KEY, cssClassTagAttrMap, true); |
146 | 153 |
|
147 | 154 | }
|
148 | 155 | }
|
149 |
| - |
|
0 commit comments