-
Notifications
You must be signed in to change notification settings - Fork 915
Add HTML rule and hint+fix for missing alt attribute for tag img, applet and area #4485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Chris2011
wants to merge
11
commits into
apache:master
Choose a base branch
from
Chris2011:add-alt-attribute-hint-fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a84cc67
Add AltAttributeVisitor to find the correct tag and attribute to show…
Chris2011 c7fb071
Add NOI18N, null checks and clean up as requested
Chris2011 07405c3
Merge branch 'master' of https://github.com/apache/netbeans into add-…
ff1fd4e
Add test for the new hint as requested
Chris2011 b2055c9
Merge branch 'master' of https://github.com/apache/netbeans into add-…
09d31d9
Add test for hintfix too and change test code to figure out why the t…
Chris2011 d54d461
Merge branch 'master' of https://github.com/apache/netbeans into add-…
f5ebc51
Fix missing test for applying the hint due to removing EDT logic.
Chris2011 ee1a6d9
Merge branch 'master' of https://github.com/apache/netbeans into add-…
Chris2011 581c627
Revert CslTestBase changes.
Chris2011 9cdbbd3
Code cleanup as requestsd
Chris2011 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4813,4 +4813,4 @@ public CaretLineOffset(int offset, String caretLine) { | |
| } | ||
|
|
||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
208 changes: 153 additions & 55 deletions
208
ide/html.editor/src/org/netbeans/modules/html/editor/hints/HtmlHintsProvider.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...l.editor/src/org/netbeans/modules/html/editor/hints/other/AddMissingAltAttributeHint.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * 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.Collections; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import javax.swing.text.BadLocationException; | ||
| import org.netbeans.editor.BaseDocument; | ||
| 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.html.editor.hints.HtmlRuleContext; | ||
| import org.netbeans.modules.html.editor.utils.HtmlTagContextUtils; | ||
|
|
||
| /** | ||
| * | ||
| * @author Christian Lenz | ||
| */ | ||
| public class AddMissingAltAttributeHint extends Hint { | ||
|
|
||
| public AddMissingAltAttributeHint(HtmlRuleContext context, OffsetRange range) { | ||
| super(AddMissingAltAttributeRule.getInstance(), | ||
| AddMissingAltAttributeRule.getInstance().getDescription(), | ||
| context.getFile(), | ||
| range, | ||
| Collections.<HintFix>singletonList(new AddMissingAltAttributeHintFix(context, range)), | ||
| 10); | ||
| } | ||
|
|
||
| private static class AddMissingAltAttributeHintFix implements HintFix { | ||
|
|
||
| private static final Logger LOGGER = Logger.getLogger(AddMissingAltAttributeHintFix.class.getSimpleName()); | ||
|
|
||
| private final HtmlRuleContext context; | ||
| private final OffsetRange range; | ||
|
|
||
| public AddMissingAltAttributeHintFix(HtmlRuleContext context, OffsetRange range) { | ||
| this.context = context; | ||
| this.range = range; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return AddMissingAltAttributeRule.getInstance().getDisplayName(); | ||
| } | ||
|
|
||
| @Override | ||
| public void implement() throws Exception { | ||
| BaseDocument document = (BaseDocument) context.getSnapshot().getSource().getDocument(true); | ||
| document.runAtomic(() -> { | ||
| try { | ||
| OffsetRange adjustedRange = HtmlTagContextUtils.adjustContextRange(document, range.getStart(), range.getEnd(), true); | ||
| String tagContent = document.getText(adjustedRange.getStart(), adjustedRange.getLength()); | ||
|
|
||
| // Find last self-closing or non self-closing tag | ||
| Pattern closingTagPattern = Pattern.compile("(/?>)"); | ||
| Matcher closingTagMatcher = closingTagPattern.matcher(tagContent); | ||
|
|
||
| if (closingTagMatcher.find()) { | ||
| int altInsertPosition = adjustedRange.getStart() + closingTagMatcher.start(1); | ||
|
|
||
| // Check whether a space before alt is needed or not | ||
| boolean needsSpaceBefore = altInsertPosition == 0 || tagContent.charAt(closingTagMatcher.start(1) - 1) != ' '; | ||
| boolean isSelfClosing = closingTagMatcher.group(0).endsWith("/>"); | ||
| String altAttribute = (needsSpaceBefore ? " " : "") + "alt=\"\"" + (isSelfClosing ? " " : ""); // NOI18N | ||
|
|
||
| document.insertString(altInsertPosition, altAttribute, null); | ||
| } | ||
| } catch (BadLocationException ex) { | ||
| LOGGER.log(Level.WARNING, "Invalid offset: {0}", ex.offsetRequested()); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSafe() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isInteractive() { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
74 changes: 74 additions & 0 deletions
74
...l.editor/src/org/netbeans/modules/html/editor/hints/other/AddMissingAltAttributeRule.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * 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.io.IOException; | ||
| import java.util.List; | ||
| import org.netbeans.modules.csl.api.Hint; | ||
| import org.netbeans.modules.csl.api.HintSeverity; | ||
| import org.netbeans.modules.csl.api.RuleContext; | ||
| 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.lib.api.elements.ElementType; | ||
| import org.netbeans.modules.html.editor.lib.api.elements.ElementUtils; | ||
| import org.openide.util.Exceptions; | ||
|
|
||
| /** | ||
| * | ||
| * @author Christian Lenz | ||
| */ | ||
| public class AddMissingAltAttributeRule extends HtmlRule { | ||
|
|
||
| private final static AddMissingAltAttributeRule INSTANCE = new AddMissingAltAttributeRule(); | ||
|
|
||
| private AddMissingAltAttributeRule() { | ||
| } | ||
|
|
||
| public static AddMissingAltAttributeRule getInstance() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean appliesTo(RuleContext context) { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean showInTasklist() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public HintSeverity getDefaultSeverity() { | ||
| return HintSeverity.CURRENT_LINE_WARNING; | ||
| } | ||
|
|
||
| @Override | ||
| protected void run(HtmlRuleContext context, List<Hint> result) { | ||
| try { | ||
| HtmlParserResult parserResult = context.getHtmlParserResult(); | ||
| AltAttributeVisitor visitor = new AltAttributeVisitor(this, context, result); | ||
|
|
||
| ElementUtils.visitChildren(parserResult.root(), visitor, ElementType.OPEN_TAG); | ||
| } catch (IOException ioe) { | ||
| Exceptions.printStackTrace(ioe); | ||
| } | ||
| } | ||
| } |
63 changes: 63 additions & 0 deletions
63
ide/html.editor/src/org/netbeans/modules/html/editor/hints/other/AltAttributeVisitor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * 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.io.IOException; | ||
| import java.util.*; | ||
| import org.netbeans.modules.csl.api.Hint; | ||
| import org.netbeans.modules.csl.api.OffsetRange; | ||
| import org.netbeans.modules.csl.api.Rule; | ||
| import org.netbeans.modules.html.editor.hints.HtmlRuleContext; | ||
| import org.netbeans.modules.html.editor.lib.api.elements.*; | ||
|
|
||
| /** | ||
| * | ||
| * @author Christian Lenz | ||
| */ | ||
| public class AltAttributeVisitor implements ElementVisitor { | ||
|
|
||
| private static final String ALT_ATTR = "alt"; // NOI18N | ||
|
|
||
| private final HtmlRuleContext context; | ||
| private final List<Hint> hints; | ||
|
|
||
| public AltAttributeVisitor(Rule rule, HtmlRuleContext context, List<Hint> hints) throws IOException { | ||
| this.context = context; | ||
| this.hints = hints; | ||
| } | ||
|
|
||
| @Override | ||
| public void visit(Element node) { | ||
| // We should only be invoked for opening tags | ||
| if (!(node instanceof OpenTag)) { | ||
| return; | ||
| } | ||
|
|
||
| // We are only interested in img, area, applet elements | ||
| String lowerCaseTag = node.id().toString().toLowerCase(); | ||
| if (!(lowerCaseTag.equals("img") || lowerCaseTag.equals("area") || lowerCaseTag.equals("applet"))) { // NOI18N | ||
| return; | ||
| } | ||
|
|
||
| OpenTag ot = (OpenTag) node; | ||
| if (ot.getAttribute(ALT_ATTR) == null) { | ||
| hints.add(new AddMissingAltAttributeHint(context, new OffsetRange(node.from(), node.to()))); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might lead to wrong results. Consider PHP with embedded HTML. The document could look like this:
I would use the HTML token stream and either use the start of the closing token or, if that does not exist, the start of the last HTML token + length.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH, I didn't try PHP yet, will do it too. Thx for the hint. If I guess it correct, your example will not trigger the hint, because you already have the alt attribute. But yes It would be better to take the HTML token instead of just looking for the closing tag symbol with an regex.