matthiasblaesing commented on code in PR #4485: URL: https://github.com/apache/netbeans/pull/4485#discussion_r1052611390
########## ide/html.editor/src/org/netbeans/modules/html/editor/hints/other/AddMissingAltAttributeHint.java: ########## @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.html.editor.hints.other; + +import java.awt.EventQueue; +import java.util.Collections; +import javax.swing.text.BadLocationException; +import org.netbeans.modules.csl.api.Hint; +import org.netbeans.modules.csl.api.HintFix; +import org.netbeans.modules.csl.api.OffsetRange; +import org.netbeans.modules.csl.api.RuleContext; +import org.netbeans.modules.html.editor.utils.HtmlTagContextUtils; +import org.openide.util.Exceptions; + +/** + * + * @author Chris Review Comment: I suggest to either replace this with something more distinctive (there are multiple instances of Chris) or remove it (history holds the full author information). ########## ide/html.editor/src/org/netbeans/modules/html/editor/hints/other/AddMissingAltAttributeRule.java: ########## @@ -0,0 +1,234 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.html.editor.hints.other; + +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.atomic.AtomicReference; +import javax.swing.text.BadLocationException; +import javax.swing.text.Document; +import org.netbeans.api.html.lexer.HTMLTokenId; +import org.netbeans.api.lexer.Token; +import org.netbeans.api.lexer.TokenHierarchy; +import org.netbeans.api.lexer.TokenSequence; +import org.netbeans.api.project.FileOwnerQuery; +import org.netbeans.api.project.Project; +import org.netbeans.modules.csl.api.Hint; +import org.netbeans.modules.csl.api.HintSeverity; +import org.netbeans.modules.csl.api.OffsetRange; +import org.netbeans.modules.csl.api.RuleContext; +import org.netbeans.modules.html.editor.api.Utils; +import org.netbeans.modules.html.editor.api.gsf.HtmlParserResult; +import org.netbeans.modules.html.editor.hints.HtmlRule; +import org.netbeans.modules.html.editor.hints.HtmlRuleContext; +import org.netbeans.modules.html.editor.refactoring.InlinedStyleInfo; +import org.netbeans.modules.html.editor.utils.HtmlTagContextUtils; +import org.netbeans.modules.web.common.api.WebUtils; +import org.openide.filesystems.FileObject; +import org.openide.util.Exceptions; +import org.openide.util.NbBundle; + +/** + * + * @author Chris + */ [email protected]("AddMissingAltAttributeRuleName=Insert required 'alt' attribute") +public class AddMissingAltAttributeRule extends HtmlRule { + + public static AddMissingAltAttributeRule SINGLETON = new AddMissingAltAttributeRule(); + private RuleContext context; + + @Override + public boolean appliesTo(RuleContext context) { + this.context = context; + + HtmlParserResult result = (HtmlParserResult) context.parserResult; + FileObject file = result.getSnapshot().getSource().getFileObject(); + + if (file == null) { + return false; + } + + Project project = FileOwnerQuery.getOwner(file); + + if (project == null) { + return false; + } + + Document doc = context.parserResult.getSnapshot().getSource().getDocument(true); + + if (doc == null) { + return false; + } + + return isAltAttributeRequiredAndMissing(doc, new OffsetRange(context.caretOffset, context.caretOffset)); + } + + @Override + public String getDisplayName() { + return Bundle.AddMissingAltAttributeRuleName(); + } + + @Override + public boolean showInTasklist() { + return false; + } + + @Override + public HintSeverity getDefaultSeverity() { + return HintSeverity.CURRENT_LINE_WARNING; + } + + @Override + protected void run(HtmlRuleContext context, List<Hint> result) { + result.add(new AddMissingAltAttributeHint(this.context, new OffsetRange(this.context.caretOffset, this.context.caretOffset))); + } + + private boolean isAltAttributeRequiredAndMissing(Document doc, OffsetRange range) { + OffsetRange adjusted = HtmlTagContextUtils.adjustContextRange(doc, range.getStart(), range.getEnd(), false); + + return AddMissingAltAttributeRule.isAltAttributeRequired(doc, adjusted.getStart(), adjusted.getEnd()) && !AddMissingAltAttributeRule.isAltAttributeAvailable(doc, adjusted.getStart(), adjusted.getEnd()); + } + + private static boolean isAltAttributeRequired(final Document doc, final int from, final int to) { + final AtomicReference<List<Boolean>> result = new AtomicReference<>(); + + doc.render(() -> { + List<Boolean> found = new LinkedList<>(); + result.set(found); + + TokenHierarchy th = TokenHierarchy.get(doc); + TokenSequence<HTMLTokenId> ts = Utils.getJoinedHtmlSequence(th, from); + + if (ts == null) { + //XXX - try to search backward and forward to find some html code??? + return; + } + + //the joined ts is moved to the from offset and a token already selected (moveNext/Previous() == true) + //seek for all tag's attributes with css embedding representing an inlined style + String tag = null; + + do { + Token<HTMLTokenId> t = ts.token(); + if (t.id() == HTMLTokenId.TAG_OPEN && t.text().toString().matches("img|applet|area")) { + tag = t.text().toString(); + } else if (t.id() == HTMLTokenId.TAG_CLOSE_SYMBOL) { + //closing tag, produce the info + if (tag != null) { + // no alt tag found but required tag + found.add(true); + tag = null; + } + } + } while (ts.moveNext() && ts.offset() <= to); + }); + + return !result.get().isEmpty(); + } + + private static boolean isAltAttributeAvailable(final Document doc, final int from, final int to) { + final AtomicReference<List<InlinedStyleInfo>> result = new AtomicReference<>(); + + doc.render(() -> { + List<InlinedStyleInfo> found = new LinkedList<>(); + result.set(found); + + TokenHierarchy th = TokenHierarchy.get(doc); + TokenSequence<HTMLTokenId> ts = Utils.getJoinedHtmlSequence(th, from); + + if (ts == null) { + //XXX - try to search backward and forward to find some html code??? + return; + } + + //the joined ts is moved to the from offset and a token already selected (moveNext/Previous() == true) + //seek for all tag's attributes with css embedding representing an inlined style Review Comment: This looks to complex to find the alt Attribute. The variables here indicate extraction of class, alt, id - is this copied from somewhere else? ########## ide/html.editor/src/org/netbeans/modules/html/editor/hints/other/AddMissingAltAttributeRule.java: ########## @@ -0,0 +1,234 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.html.editor.hints.other; + +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.atomic.AtomicReference; +import javax.swing.text.BadLocationException; +import javax.swing.text.Document; +import org.netbeans.api.html.lexer.HTMLTokenId; +import org.netbeans.api.lexer.Token; +import org.netbeans.api.lexer.TokenHierarchy; +import org.netbeans.api.lexer.TokenSequence; +import org.netbeans.api.project.FileOwnerQuery; +import org.netbeans.api.project.Project; +import org.netbeans.modules.csl.api.Hint; +import org.netbeans.modules.csl.api.HintSeverity; +import org.netbeans.modules.csl.api.OffsetRange; +import org.netbeans.modules.csl.api.RuleContext; +import org.netbeans.modules.html.editor.api.Utils; +import org.netbeans.modules.html.editor.api.gsf.HtmlParserResult; +import org.netbeans.modules.html.editor.hints.HtmlRule; +import org.netbeans.modules.html.editor.hints.HtmlRuleContext; +import org.netbeans.modules.html.editor.refactoring.InlinedStyleInfo; +import org.netbeans.modules.html.editor.utils.HtmlTagContextUtils; +import org.netbeans.modules.web.common.api.WebUtils; +import org.openide.filesystems.FileObject; +import org.openide.util.Exceptions; +import org.openide.util.NbBundle; + +/** + * + * @author Chris + */ [email protected]("AddMissingAltAttributeRuleName=Insert required 'alt' attribute") Review Comment: This message key should be `AddMissingAltAttributeRule` - you can then drop the implementation of `getDisplayName` as the superclass already implements it: https://github.com/apache/netbeans/blob/a8edf3bf92e38ac22a83e24ec910f29094f0fee3/ide/html.editor/src/org/netbeans/modules/html/editor/hints/HtmlRule.java#L86-L89 with https://github.com/apache/netbeans/blob/a8edf3bf92e38ac22a83e24ec910f29094f0fee3/ide/html.editor/src/org/netbeans/modules/html/editor/hints/HtmlRule.java#L60-L63 You also need to define a message named `AddMissingAltAttributeRule_Desc` (you get an exception when you click on the hint in the option panel): https://github.com/apache/netbeans/blob/a8edf3bf92e38ac22a83e24ec910f29094f0fee3/ide/html.editor/src/org/netbeans/modules/html/editor/hints/HtmlRule.java#L65-L68 I suggest to use : `Provide Text Alternatives` for the display name (to get it more in line with the rest of the entries) and `Find img/applet/area elements where no alt attribute is provided.` (for the description)  ########## ide/html.editor/src/org/netbeans/modules/html/editor/hints/other/AddMissingAltAttributeRule.java: ########## @@ -0,0 +1,234 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.html.editor.hints.other; + +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.atomic.AtomicReference; +import javax.swing.text.BadLocationException; +import javax.swing.text.Document; +import org.netbeans.api.html.lexer.HTMLTokenId; +import org.netbeans.api.lexer.Token; +import org.netbeans.api.lexer.TokenHierarchy; +import org.netbeans.api.lexer.TokenSequence; +import org.netbeans.api.project.FileOwnerQuery; +import org.netbeans.api.project.Project; +import org.netbeans.modules.csl.api.Hint; +import org.netbeans.modules.csl.api.HintSeverity; +import org.netbeans.modules.csl.api.OffsetRange; +import org.netbeans.modules.csl.api.RuleContext; +import org.netbeans.modules.html.editor.api.Utils; +import org.netbeans.modules.html.editor.api.gsf.HtmlParserResult; +import org.netbeans.modules.html.editor.hints.HtmlRule; +import org.netbeans.modules.html.editor.hints.HtmlRuleContext; +import org.netbeans.modules.html.editor.refactoring.InlinedStyleInfo; +import org.netbeans.modules.html.editor.utils.HtmlTagContextUtils; +import org.netbeans.modules.web.common.api.WebUtils; +import org.openide.filesystems.FileObject; +import org.openide.util.Exceptions; +import org.openide.util.NbBundle; + +/** + * + * @author Chris + */ [email protected]("AddMissingAltAttributeRuleName=Insert required 'alt' attribute") +public class AddMissingAltAttributeRule extends HtmlRule { + + public static AddMissingAltAttributeRule SINGLETON = new AddMissingAltAttributeRule(); + private RuleContext context; + + @Override + public boolean appliesTo(RuleContext context) { + this.context = context; + + HtmlParserResult result = (HtmlParserResult) context.parserResult; + FileObject file = result.getSnapshot().getSource().getFileObject(); + + if (file == null) { + return false; + } + + Project project = FileOwnerQuery.getOwner(file); + + if (project == null) { + return false; + } + + Document doc = context.parserResult.getSnapshot().getSource().getDocument(true); + + if (doc == null) { + return false; + } + + return isAltAttributeRequiredAndMissing(doc, new OffsetRange(context.caretOffset, context.caretOffset)); + } + + @Override + public String getDisplayName() { + return Bundle.AddMissingAltAttributeRuleName(); + } + + @Override + public boolean showInTasklist() { + return false; + } + + @Override + public HintSeverity getDefaultSeverity() { + return HintSeverity.CURRENT_LINE_WARNING; + } + + @Override + protected void run(HtmlRuleContext context, List<Hint> result) { + result.add(new AddMissingAltAttributeHint(this.context, new OffsetRange(this.context.caretOffset, this.context.caretOffset))); + } + + private boolean isAltAttributeRequiredAndMissing(Document doc, OffsetRange range) { + OffsetRange adjusted = HtmlTagContextUtils.adjustContextRange(doc, range.getStart(), range.getEnd(), false); + + return AddMissingAltAttributeRule.isAltAttributeRequired(doc, adjusted.getStart(), adjusted.getEnd()) && !AddMissingAltAttributeRule.isAltAttributeAvailable(doc, adjusted.getStart(), adjusted.getEnd()); + } + + private static boolean isAltAttributeRequired(final Document doc, final int from, final int to) { + final AtomicReference<List<Boolean>> result = new AtomicReference<>(); + + doc.render(() -> { + List<Boolean> found = new LinkedList<>(); + result.set(found); + + TokenHierarchy th = TokenHierarchy.get(doc); + TokenSequence<HTMLTokenId> ts = Utils.getJoinedHtmlSequence(th, from); + + if (ts == null) { + //XXX - try to search backward and forward to find some html code??? + return; + } + + //the joined ts is moved to the from offset and a token already selected (moveNext/Previous() == true) + //seek for all tag's attributes with css embedding representing an inlined style Review Comment: this comment looks as if it does not match. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
