AngleSharp / AngleSharp.Css

:angel: Library to enable support for cascading stylesheets in AngleSharp.
https://anglesharp.github.io
MIT License
72 stars 34 forks source link

Single quotes within style attributes are converted to " on Html output #75

Closed Kolibarus closed 2 years ago

Kolibarus commented 3 years ago

Reproducе:

var context = BrowsingContext.New(Configuration.Default.WithDefaultLoader().WithCss());
var parser = context.GetService<IHtmlParser>();
var source = "<span style=\"font-size: 12pt; font-family: 'Times New Roman', 'serif'\">Test string</span>";
var document = parser.ParseDocument(source);

IElement element = document.GetElementsByTagName("span").First();

ICssStyleDeclaration styleDeclaration = element.GetStyle();

string css = styleDeclaration.ToCss();

element.SetStyle(css);
var res = element.OuterHtml;
// <span style="font-size: 12pt; font-family: &quot;Times New Roman&quot;, &quot;serif&quot;">Test string</span>

Single quotes in CSS font-family are becoming double due to the fact that the Core CssString() method is used in the class Label.

https://github.com/AngleSharp/AngleSharp.Css/blob/4cccb13447da93e962632582f8526ae1b760780e/src/AngleSharp.Css/Values/Primitives/Label.cs#L40

Core CSString() frames a string Times New Roman with double quotes, which then turn into &quot;Times New Roman&quot; in HtmlMarkupFormatter: https://github.com/AngleSharp/AngleSharp/blob/33f8826f93be863170a125c50048e1f07e3176a6/src/AngleSharp/Html/HtmlMarkupFormatter.cs#L140

FlorianRappl commented 3 years ago

Yes, I'd say this is a fix / improvement for the HTML markup formatter, which should choose a better representation here. The CSS stringification does not (need to) know where it's result should be used.

Kolibarus commented 3 years ago

How about making IMarkupFormatter as DI for BrowsingContext or at least make HtmlMarkupFormatter.Instance not readonly?

jogibear9988 commented 2 years ago

I saw chrome does the same...

   $0.style.fontFamily="'bbb ccc'"

creates

 '<div style="font-family: &quot;bbb ccc&quot;;"></div>'
jogibear9988 commented 2 years ago

I think it would be a problem if this gets also converted:

     div::before { content: "\'" }

==> but after a test this seems to be correct