Semantic-UI icon indicating copy to clipboard operation
Semantic-UI copied to clipboard

Duplicate class names are removed by Meteor

Open williamkhshea opened this issue 11 years ago • 36 comments

It is not an issue of semantic-ui per se. I am using Meteor with semantic-ui and trying to make my columns responsive for device like this:

<div class="ui grid">
  <div class="sixteen wide mobile eight wide tablet four wide computer column">
    <p></p>
</div>

Meteor removes all the duplicated class "wide" and it becomes:

<div class="ui grid">
  <div class="sixteen wide mobile eight tablet four computer column">
    <p></p>
</div>

Just wonder do you have any workarounds?

williamkhshea avatar Dec 29 '14 08:12 williamkhshea

I imagine that this will impact other word-order dependencies as well. Example:

<div class="ui right floated right labeled icon button">

anestv avatar Dec 29 '14 09:12 anestv

So am I understanding correctly that this actually breaks Semantic UI?

Because if so, it's not just a framework-specific issue, even the browser Native classList.add does the same thing: https://developer.mozilla.org/en-US/docs/Web/API/Element.classList

HenrikJoreteg avatar Jan 12 '15 18:01 HenrikJoreteg

@HenrikJoreteg Yes. Fortunately word-order dependent duplicate class names are not used extensively in semantic-ui.

williamkhshea avatar Jan 12 '15 18:01 williamkhshea

Ok, thanks for the heads up, I was not aware of this. In the cross-linked issue above, we will not support significant word-orders or duplicate classes. Especially given how the native APIs work.

HenrikJoreteg avatar Jan 12 '15 18:01 HenrikJoreteg

@HenrikJoreteg @williamkhshea I was just thinking, a new child element with the remaining classes should totally fix it, shouldn't it?

@jlukic Is there any case where it doesn't?

Anachron avatar Jan 12 '15 19:01 Anachron

I've left some comments over in the other discussion https://github.com/AmpersandJS/amp/issues/52#issuecomment-69646723

I think dupe removal should be an optional feature of APIs that add/remove class names, with the default of being enabled. The amount of SUI that is 'word order dependent' is very small. So only in certain cases would it be necessary for adjustments with mutator methods.

jlukic avatar Jan 12 '15 21:01 jlukic

I've solved with clean javascript:

Template.myTemplate.rendered = function() {
    document.getElementById('myDiv').setAttribute( 'class', 'ui sixteen wide tablet only sixteen wide mobile only ten wide computer only column');
}

stephannv avatar Mar 03 '15 14:03 stephannv

Is there any update on this? My solution for now is

$(document).ready(function() {
    setTimeout(function() {
        $('*[classs]').each(function() {
            $(this).attr('class', $(this).attr('classs'))
        })
    }, 0)
})

Means that I just rename [...] class="... wide ... wide" [...] to [...] classs="... wide ... wide" [...]. But I would like to have an official way to do it.

fentas avatar Jun 29 '15 19:06 fentas

I dont have any solution but is this issue being worked on ? I use semantic ui and i need a class definition like - "ui middle aligned center aligned grid" which gets invalidated when use the FlowRouter /FlowLayout . Interestingly this only happens when I use a layout + template.

the solution above is ok but I would it combine with @stephanngamedev solution which is a bit neater in a meteor context.

teejayhh avatar Jul 12 '15 08:07 teejayhh

This should be a design flaw.

zhubofei avatar Jul 27 '15 15:07 zhubofei

I'd also like to get an official fix on this, the methods described above seem pretty hacky to me. I don't know if this is related, but the doubling class is not working for me either.

ianmartorell avatar Jul 27 '15 16:07 ianmartorell

@ianmartorell If you're having specific issues with doubling, please create a test case and a new thread.

This bug is specific to how each framework implements their version of managing class names. Ideally frameworks would allow specifying space separated names for class names that should appear together.

For example @HenrikJoreteg was gracious enough to support space separated class names in Ambersand JS implementation of addClass, making it easier to ensure 'word order significant' class names are added together in the same function call. However, even this approach will still have issues when one of the class names is already in classList, for example changing from right floated left aligned to right floated right aligned might get you right floated aligned

For me the use of significant class order is an ideological stand. I think that programming languages will come to see class names as sharing similar semantic features to words, where meaning is often derived from context. Otherwise as programmers how could we possibly define "right"ness, without acknowledging the importance of contextual significance, like "right aligned" or "right floated". In most natural languages this contextual significance is expressed through word order, and I admit the implementation of that currently in javascript is somewhat premature.

I think one possible working solution moving forward is to support both hyphenated class names and word order significant class names for, at least to start, the most commonly used variations, like four wide column three column grid. The caveat though is this will add a lot of bulk to the css, and will take significant restructuring.

jlukic avatar Jul 27 '15 17:07 jlukic

@jlukic I completely agree with you, and I think in this case Meteor is at fault in removing duplicate classes as there is no real benefit to it and many frameworks use them. How semantic would sentences be if we removed all duplicate words in them? :) Has anyone filed a bug report or opened an issue on this matter with Meteor? Maybe that should be the first step forward.

ianmartorell avatar Jul 27 '15 17:07 ianmartorell

I also tried to implement hyphen-separated classes for the ones I was using in my project, but I couldn't get it to work properly.

@import (multiple) '../../theme.config.import.less';

@media only screen and (min-width: @tabletBreakpoint) and (max-width: @largestTabletScreen) {
  .ten-wide-tablet {
    width: @tenWide !important;
  }
  .eight-wide-tablet {
    width: @eightWide !important;
  }
  .six-wide-tablet {
    width: @sixWide !important;
  }
}

@media only screen and (min-width: @computerBreakpoint) {
  .five-wide-computer {
    width: @fiveWide !important;
  }
  .four-wide-computer {
    width: @fourWide !important;
  }
  .three-wide-computer {
    width: @threeWide !important;
  }

ianmartorell avatar Jul 27 '15 17:07 ianmartorell

Both Meteor and Semantic UI are freely interpreting the HTML5 spec (http://www.w3.org/html/wg/drafts/html/master/dom.html#classes), not always usefully. The spec notes that classes are a set of space-separated tokens.

Meteor is incorrect in removing duplicates, in that the HTML5 spec doesn't specify that the class set should be unique.

Semantic UI is adding a constraint on the use of classes by requiring class names to be a non-unique ordered set, a type of set not defined in the specification. Common understanding of class usage is that individual tokens have the same meaning in the same context – the DOM element they refer to – as tokenisation in the HTML spec by definition cannot group tokens. Ordered token groups is really asking for trouble.

xtfer avatar Jul 28 '15 11:07 xtfer

@xtfer That is all explaining constraints as defined by spec, created by W3C. This is equivalent to making a legal argument in a debate over moral philosophy. I'm not disputing that my approach is abnormal given current programming norms.

My believe is that these accepted programming norms are unusual constraints on conveying meaning, and that any simple demonstration given other developed systems of conveying meaning (read natural languages) will show that most meaning cannot be interpreted without significant word order.

jlukic avatar Jul 28 '15 15:07 jlukic

I'm unsure of what the problem is here, I'd say this is a problem with Meteor more than it is a problem with Semantic UI - the alternative would seem to make Semantic UI less semantic, simply because one framework doesn't follow the W3C specification.

undercase avatar Jul 28 '15 15:07 undercase

Hey, I wrote a quick and dirty workaround function to last me until the problem gets resolved. Feel free to use it if you like the approach. Slap it in your client lib and call it from your template rendering function. Performance and elegance haven't been taken into consideration - I just didn't feel like writing duct tape CSS.

/**
* Makes sure the positional 'wide' class is present for every device-specific semantic-ui rule
*/
fixSemanticGrids = function () {
  var deviceList = ['computer', 'tablet', 'mobile'];

  $('.wide.column').each(function (i, e) { 
    var classes = $(e).attr('class').split(/\s+/);
    var newClasses = new Array();
    $.each(classes, function (j, c) {
      if($.inArray(c, deviceList) >= 0 && j > 0 && 'wide' != classes[j-1]) {
        newClasses.push('wide');
      }
      newClasses.push(c);
    });
    $(e).attr('class', newClasses.join(' '));
  });
};

phrone avatar Jul 28 '15 15:07 phrone

Thanks @phrone! That works quite well for now.

ianmartorell avatar Jul 28 '15 17:07 ianmartorell

@jlukic No it isn't. Code is not moral philosophy. When you ignore the spec you get Internet Explorer. My point, however, was that if you freely interpret the specification in a way that is different to how everyone else does, at some point you are going to run in to problems.

In any case, on reviewing it again, Semantic UI does actually break the spec, Meteor is correct.

"The classes that an HTML element has assigned to it consists of all the classes returned when the value of the class attribute is split on spaces. (Duplicates are ignored.)".

  • http://www.w3.org/TR/html5/dom.html#classes

EDIT: Added reference

xtfer avatar Jul 28 '15 21:07 xtfer