Chris2011 commented on code in PR #4485: URL: https://github.com/apache/netbeans/pull/4485#discussion_r1054805499
########## 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: Yes, also as you can see the comments, it was copied from the method findInlineStyle of class ExtractInlinedStyleRule. I started to copy stuff around, test some stuff and didn't fix code that much what was already there. Sry for that. I just wanted to get it working first. I will try to find anouther solution maybe with that what I already used for the HtmlCompletionProvider ` LexerUtils.followsToken(...`. -- 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
