prasath-ideas2it / flying-saucer

Automatically exported from code.google.com/p/flying-saucer
0 stars 0 forks source link

Changing css style through the APIs. #108

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
*** This issue was imported from http://java.net/jira/browse/XHTMLRENDERER-116

It was reported by juanmarco on 20.08.2005 17:52:20 +0200 and last updated in 
the previous bug tracker on 20.07.2008 17:58:19 +0200

Found in
Operating System: All
Platform: All

The priority for this issue at migration was Major.

Original description: 
Hi,
It would be nice if CSS could be modified using the APIs.

For example, I made this modification on
org.xhtmlrenderer.css.newmatch.CascadedStyle : 
Line 85
replace cascadedProperties.put(prop.getCSSName(), prop);
with    addProperty(prop);

Plus, add this function : 
/**
 * Adds a property declaration to this CSS. If a property with the same name
 * already exists, it will be replaced and returned.
 * @param prop the property to add.
 * @return the old property if exists, null otherwise.
 */
public PropertyDeclaration addProperty(PropertyDeclaration prop) {
  return (PropertyDeclaration) cascadedProperties.put(prop.getCSSName(), prop);
}

Now I can add or replace a css property easily like this : 
// create and init a dom parser
DOMParser parser = new DOMParser();
parser.parse(...);

// create and init the XHTMLPanel
Document doc = parser.getDocument();
XHTMLPanel xhtml = new XHTMLPanel();
xhtml.setDocument(doc, null);
RenderingContext renderingContext = xhtml.getRenderingContext();
StyleReference styleReference = renderingContext.getStyleReference();

// pick the body tag and its associated stylesheet
Element element = (Element) doc.getElementsByTagName("body").item(0);
CascadedStyle cs = styleReference.getCascadedStyle(element, false);

// CSSColor is my own implementation of CSSPrimitiveValue to handle color
CSSColor cssColor = new CSSColor();

// new function here
cs.addProperty(new PropertyDeclaration(CSSName.COLOR, cssColor, false,
StylesheetInfo.AUTHOR));
//end

At this point, the css attribute "color" of the body has been set or replaced.
In order to change the color I just have to : 
1. modify the CSSColor
2. repaint the XHTMLPanel

I don't know if it's a good solution or not and if it works in all situations.

Thanks

Original issue reported on code.google.com by pdoubl...@gmail.com on 16 Feb 2011 at 10:03

GoogleCodeExporter commented 8 years ago
pdoubleya wrote on 25.07.2006 16:48:52 +0200:
Defer to R7 at least. There is a certain amount of caching going on, plus we
have to figure out what specificity the user-supplied CSS should have. Should
check out JavaScript APIs for styling in Firefox, Opera, etc.

Original comment by pdoubl...@gmail.com on 16 Feb 2011 at 10:03

GoogleCodeExporter commented 8 years ago
pdoubleya wrote on 20.07.2008 17:58:19 +0200:
Deferring; currently we treat the CSS handling as "internal" and don't expose
the APIs to modify it. We'd need to have someone volunteer to work on providing
access to these APIs and figuring out the best way to interact with cache
values, etc.

Original comment by pdoubl...@gmail.com on 16 Feb 2011 at 10:03