-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove
for
attr in <label> if it contains <input>
- Loading branch information
Showing
4 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,30 @@ | ||
from ...abbreviation import AbbreviationNode, AbbreviationAttribute | ||
from ..utils import find | ||
|
||
def label(node: AbbreviationNode): | ||
if node.name == 'label': | ||
input = find(node, lambda n: n.name == 'input' or n.name == 'textarea') | ||
if input: | ||
# Remove empty `for` attribute | ||
if node.attributes: | ||
node.attributes = [attr for attr in node.attributes if not (attr.name == 'for' and is_empty_attribute(attr))] | ||
|
||
# Remove empty `id` attribute | ||
if input.attributes: | ||
input.attributes = [attr for attr in input.attributes if not (attr.name == 'id' and is_empty_attribute(attr))] | ||
|
||
|
||
def is_empty_attribute(attr: AbbreviationAttribute): | ||
if not attr.value: | ||
return True | ||
|
||
|
||
if len(attr.value) == 1: | ||
token = attr.value[0] | ||
if token and not isinstance(token, str) and not token.name: | ||
# Attribute contains field | ||
return True | ||
|
||
return False | ||
|
||
|
This file contains 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
This file contains 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