use of org.w3c.dom.stylesheets.MediaList in project webtools.sourceediting by eclipse.
the class MediaRuleFormatter method formatBefore.
/**
*/
protected void formatBefore(ICSSNode node, ICSSNode child, String toAppend, StringBuffer source, IRegion exceptFor) {
ICSSNode prev = (child != null) ? child.getPreviousSibling() : node.getLastChild();
int start = (prev != null) ? ((IndexedRegion) prev).getEndOffset() : 0;
int end = (child != null) ? ((IndexedRegion) child).getStartOffset() : 0;
if (start > 0 && start < end) {
// source formatting
CSSCleanupStrategy stgy = getCleanupStrategy(node);
IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
// get meaning regions
CompoundRegion[] regions = null;
if (exceptFor == null)
regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
else {
String pickupType = null;
if ((prev != null && prev.getNodeType() == ICSSNode.MEDIALIST_NODE) || (prev == null && (child == null || child.getNodeType() != ICSSNode.MEDIALIST_NODE))) {
pickupType = CSSRegionContexts.CSS_LBRACE;
}
regions = getRegions(structuredDocument, new FormatRegion(start, end - start), exceptFor, pickupType);
}
// extract source
if (child != null && child.getNodeType() == ICSSNode.MEDIALIST_NODE && ((MediaList) child).getLength() > 0) {
// "@media" and mediatype
for (int i = 0; i < regions.length; i++) {
appendSpaceBefore(node, regions[i], source);
// must
source.append(decoratedRegion(regions[i], 0, stgy));
// be comments
}
appendSpaceBefore(node, toAppend, source);
} else if (child != null && (child.getPreviousSibling() == null || child.getPreviousSibling().getNodeType() == ICSSNode.MEDIALIST_NODE)) {
// mediatype and the first style rule
for (int i = 0; i < regions.length; i++) {
appendSpaceBefore(node, regions[i], source);
// must
source.append(decoratedRegion(regions[i], 0, stgy));
// be comments
}
appendDelimBefore(node, null, source);
} else {
// between styles
for (int i = 0; i < regions.length; i++) {
appendDelimBefore(node, regions[i], source);
// must
source.append(decoratedRegion(regions[i], 0, stgy));
// be comments
}
appendDelimBefore(node, null, source);
}
} else {
// source generation
if (child == null && prev != null && prev.getNodeType() != ICSSNode.MEDIALIST_NODE) {
// after
// the last style rule
appendDelimBefore(node.getParentNode(), null, source);
} else if (child != null && child.getNodeType() == ICSSNode.MEDIALIST_NODE && ((MediaList) child).getLength() > 0) {
// between
// "@media" and mediatype
appendSpaceBefore(node, toAppend, source);
} else if (prev != null && prev.getNodeType() == ICSSNode.MEDIALIST_NODE && ((MediaList) prev).getLength() > 0) {
// between
// mediatype and the first style rule
// $NON-NLS-1$
appendSpaceBefore(node, "{", source);
// $NON-NLS-1$
source.append("{");
if (child != null)
appendDelimBefore(node, null, source);
else
appendDelimBefore(node.getParentNode(), null, source);
} else {
// normal case
appendDelimBefore(node, null, source);
}
}
}
Aggregations