Skip to content

Commit

Permalink
Remove for attr in <label> if it contains <input>
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeche committed Mar 13, 2024
1 parent cbd1da5 commit 00915e4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions emmet/markup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .lorem import lorem
from .addon.xsl import xsl
from .addon.bem import bem
from .addon.label import label
from .format import html, haml, slim, pug
from .utils import walk

Expand Down Expand Up @@ -62,5 +63,8 @@ def transform(node: AbbreviationNode, ancestors: list, config: Config):
if config.syntax == 'xsl':
xsl(node)

if config.type == 'markup':
label(node)

if config.options.get('bem.enabled'):
bem(node, ancestors, config)
30 changes: 30 additions & 0 deletions emmet/markup/addon/label.py
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


11 changes: 11 additions & 0 deletions emmet/markup/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,14 @@ def find_deepest(node: AbbreviationNode):
node = node.children[-1]

return (parent, node)


def find(node: AbbreviationNode, callback: callable):
"Finds first child node that matches given `callback`"
for child in node.children:
if callback(child):
return child

result = find(child, callback)
if result:
return result
6 changes: 6 additions & 0 deletions tests/test_markup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ def test_xsl(self):
self.assertEqual(expand('xsl:variable[select]>div', config), '<xsl:variable><div></div></xsl:variable>')
self.assertEqual(expand('xsl:with-param[select]{foo}', config), '<xsl:with-param>foo</xsl:with-param>')

class TestLabelPreprocessor(unittest.TestCase):
def test_label(self):
self.assertEqual(expand('label>input'), '<label><input type="${1:text}" /></label>')
self.assertEqual(expand('label>inp'), '<label><input type="${1:text}" name="${1}" /></label>')
self.assertEqual(expand('label>span>input'), '<label><span><input type="${1:text}" /></span></label>')
self.assertEqual(expand('label+inp'), '<label for=""></label><input type="${1:text}" name="${1}" id="${1}" />')

class TestBEMTransform(unittest.TestCase):
def test_modifiers(self):
Expand Down

0 comments on commit 00915e4

Please sign in to comment.