diff options
author | Billy Huang | 2012-12-13 00:08:19 +0000 |
---|---|---|
committer | Steffen Pingel | 2012-12-15 18:59:15 +0000 |
commit | babe0839e127d35d1fcab867b9afafb60504029b (patch) | |
tree | 2bb6bdf93517d5203df50f3bb959eeb08665cd15 | |
parent | 6df7e6b86c0da4d192eddb7e79e10ccc80d1d245 (diff) | |
download | org.eclipse.mylyn.docs-babe0839e127d35d1fcab867b9afafb60504029b.tar.gz org.eclipse.mylyn.docs-babe0839e127d35d1fcab867b9afafb60504029b.tar.xz org.eclipse.mylyn.docs-babe0839e127d35d1fcab867b9afafb60504029b.zip |
396332: html cleaner should validate and repair CSS colour styles
* Added processor to HtmlCleaner that searches for elements with "color"
styles and checks that they contain "#" if they are using a hex color
string.
Change-Id: I25eece93125626af995c43f731c535678f32c36c
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=396332
3 files changed, 157 insertions, 0 deletions
diff --git a/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/internal/wikitext/core/parser/html/HtmlCleaner.java b/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/internal/wikitext/core/parser/html/HtmlCleaner.java index af39a0baa..684615418 100644 --- a/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/internal/wikitext/core/parser/html/HtmlCleaner.java +++ b/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/internal/wikitext/core/parser/html/HtmlCleaner.java @@ -7,6 +7,7 @@ * * Contributors: * David Green - initial API and implementation + * Billy Huang - Bug 396332 *******************************************************************************/ package org.eclipse.mylyn.internal.wikitext.core.parser.html; @@ -28,6 +29,7 @@ public class HtmlCleaner { processors.add(new WhitespaceCleanupProcessor()); // ORDER DEPENDENCY - should come first processors.add(new RemoveEmptySpansProcessor()); processors.add(new RemoveExcessiveStylesProcessor()); + processors.add(new RepairBrokenCSSColorStylesProcessor()); } public void configure(HtmlParser parser) { diff --git a/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/internal/wikitext/core/parser/html/RepairBrokenCSSColorStylesProcessor.java b/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/internal/wikitext/core/parser/html/RepairBrokenCSSColorStylesProcessor.java new file mode 100644 index 000000000..40d1615a4 --- /dev/null +++ b/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/internal/wikitext/core/parser/html/RepairBrokenCSSColorStylesProcessor.java @@ -0,0 +1,84 @@ +/******************************************************************************* + * Copyright (c) 2011, 2012 Tasktop Technologies. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * David Green - initial API and implementation + * Billy Huang - Bug 396332 + *******************************************************************************/ + +package org.eclipse.mylyn.internal.wikitext.core.parser.html; + +import java.util.Iterator; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.eclipse.mylyn.internal.wikitext.core.util.css.CssParser; +import org.eclipse.mylyn.internal.wikitext.core.util.css.CssRule; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Selector; + +/** + * Repairs missing "#"-prefix in CSS color styles using hex color values + * + * @author Billy Huang + */ +public class RepairBrokenCSSColorStylesProcessor extends DocumentProcessor { + + @Override + public void process(Document document) { + Element body = document.body(); + + CssParser cssParser = new CssParser(); + + for (Element element : Selector.select("[style]", body)) { //$NON-NLS-1$ + String style = element.attr("style"); //$NON-NLS-1$ + + String newStyle = ""; //$NON-NLS-1$ + List<CssRule> rules = null; + CssRule newRule = null; + + if (style != null && style.length() > 0) { + rules = cssParser.parseBlockContent(style); + + Iterator<CssRule> ruleIt = rules.iterator(); + while (ruleIt.hasNext()) { + CssRule rule = ruleIt.next(); + if ("color".equals(rule.name)) { //$NON-NLS-1$ + String color = rule.value; + // no 3- or 6-character CSS color names are written in hex characters + Matcher invalidHexColorMatcher = Pattern.compile( + "^\\s*([0-9a-fA-F]{6}|[0-9a-fA-F]{3})(?:\\s+(.*))?\\s*$") //$NON-NLS-1$ + .matcher(color); + if (invalidHexColorMatcher.matches()) { + String newColor = "#" + invalidHexColorMatcher.group(1); //$NON-NLS-1$ + String additionalDeclarations = invalidHexColorMatcher.group(2); + if (additionalDeclarations != null) { + newColor += " " + additionalDeclarations; //$NON-NLS-1$ + } + ruleIt.remove(); + newRule = new CssRule("color", newColor.trim(), 0, 0, 0, 0); //$NON-NLS-1$ + } + } + } + } + + if (rules != null && newRule != null) { + newStyle = addRuleToStyle(newStyle, newRule); + for (CssRule rule : rules) { + newStyle = addRuleToStyle(newStyle, rule); + } + element.attr("style", newStyle); //$NON-NLS-1$ + } + } + } + + private String addRuleToStyle(String style, CssRule rule) { + return style += rule.name + ": " + rule.value + ";"; //$NON-NLS-1$ //$NON-NLS-2$ + } +} diff --git a/org.eclipse.mylyn.wikitext.tests/src/org/eclipse/mylyn/internal/wikitext/core/parser/html/HtmlCleanerTest.java b/org.eclipse.mylyn.wikitext.tests/src/org/eclipse/mylyn/internal/wikitext/core/parser/html/HtmlCleanerTest.java index 6bea72efe..3062c5a1f 100644 --- a/org.eclipse.mylyn.wikitext.tests/src/org/eclipse/mylyn/internal/wikitext/core/parser/html/HtmlCleanerTest.java +++ b/org.eclipse.mylyn.wikitext.tests/src/org/eclipse/mylyn/internal/wikitext/core/parser/html/HtmlCleanerTest.java @@ -7,6 +7,7 @@ * * Contributors: * David Green - initial API and implementation + * Billy Huang - Bug 396332 *******************************************************************************/ package org.eclipse.mylyn.internal.wikitext.core.parser.html; @@ -99,6 +100,76 @@ public class HtmlCleanerTest { assertTrue(result.contains("<p>foo bar</p>")); } + @Test + public void testRepairSpanContainingMalformedCssColorStyle3CharactersNonHex() { + String result = clean("<p><span style=\"color: 123\">foo bar</span></p>"); + TestUtil.println(result); + assertTrue(result.contains("<p><span style=\"color: #123;\">foo bar</span></p>")); + } + + @Test + public void testRepairSpanContainingMalformedCssColorStyle6CharactersNonHex() { + String result = clean("<p><span style=\"color: 123456\">foo bar</span></p>"); + TestUtil.println(result); + assertTrue(result.contains("<p><span style=\"color: #123456;\">foo bar</span></p>")); + } + + @Test + public void testRepairSpanContainingMalformedCssColorStyle3CharactersHex() { + String result = clean("<p><span style=\"color: adc\">foo bar</span></p>"); + TestUtil.println(result); + assertTrue(result.contains("<p><span style=\"color: #adc;\">foo bar</span></p>")); + } + + @Test + public void testRepairSpanContainingMalformedCssColorStyle6CharactersHex() { + String result = clean("<p><span style=\"color: afcebd\">foo bar</span></p>"); + TestUtil.println(result); + assertTrue(result.contains("<p><span style=\"color: #afcebd;\">foo bar</span></p>")); + } + + @Test + public void testRepairSpanContainingMalformedCssColorStyle6CharactersMixed() { + String result = clean("<p><span style=\"color: A1B2C3\">foo bar</span></p>"); + TestUtil.println(result); + assertTrue(result.contains("<p><span style=\"color: #A1B2C3;\">foo bar</span></p>")); + } + + @Test + public void testRepairSpanContainingValidCssColorStyleHexNotChanged() { + String result = clean("<p><span style=\"color: #ABCDEF\">foo bar</span></p>"); + TestUtil.println(result); + assertTrue(result.contains("<p><span style=\"color: #ABCDEF;\">foo bar</span></p>")); + } + + @Test + public void testRepairSpanContainingValidCssColorStyleNonHexNotChanged() { + String result = clean("<p><span style=\"color: red\">foo bar</span></p>"); + TestUtil.println(result); + assertTrue(result.contains("<p><span style=\"color: red;\">foo bar</span></p>")); + } + + @Test + public void testRepairSpanContainingValidCssColorStyleNonHexLotsOfStylesNotChanged() { + String result = clean("<p><span style=\"font-style: italic;font-weight: bold;color: red\">foo bar</span></p>"); + TestUtil.println(result); + assertTrue(result.contains("<p><span style=\"font-style: italic;font-weight: bold;color: red;\">foo bar</span></p>")); + } + + @Test + public void testRepairSpanContainingMalformedCssColorStyle6CharactersMixedWithImportantDeclaration() { + String result = clean("<p><span style=\"color: A1B2C3 !important\">foo bar</span></p>"); + TestUtil.println(result); + assertTrue(result.contains("<p><span style=\"color: #A1B2C3 !important;\">foo bar</span></p>")); + } + + @Test + public void testRepairSpanContainingMalformedCssColorStyle6CharactersMixedWithImportantDeclarationLotsOfWhitespace() { + String result = clean("<p><span style=\" color: A1B2C3 ! important \">foo bar</span></p>"); + TestUtil.println(result); + assertTrue(result.contains("<p><span style=\"color: #A1B2C3 ! important;\">foo bar</span></p>")); + } + private String clean(String originalHtml) { Document document = Jsoup.parse(originalHtml); new HtmlCleaner().apply(document); |