下面列出了怎么用org.w3c.dom.stylesheets.MediaList的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Verifies if the provided node is a link node pointing to an active stylesheet.
*
* @param domNode the mode to check
* @return true if the provided node is a stylesheet link
*/
public boolean isActiveStyleSheetLink(final DomNode domNode) {
if (domNode instanceof HtmlLink) {
final HtmlLink link = (HtmlLink) domNode;
String rel = link.getRelAttribute();
if (rel != null) {
rel = rel.trim();
}
if ("stylesheet".equalsIgnoreCase(rel)) {
final String media = link.getMediaAttribute();
if (StringUtils.isBlank(media)) {
return true;
}
final WebClient webClient = getWindow().getWebWindow().getWebClient();
final MediaList mediaList = CSSStyleSheet.parseMedia(webClient.getCssErrorHandler(), media);
return CSSStyleSheet.isActive(this, mediaList);
}
}
return false;
}
/**
* Parses the given media string. If anything at all goes wrong, this
* method returns an empty MediaList list.
*
* @param source the source from which to retrieve the media to be parsed
* @return the media parsed from the specified input source
*/
static MediaList parseMedia(final CSSErrorHandler errorHandler, final String mediaString) {
MediaList media = media_.get(mediaString);
if (media != null) {
return media;
}
try {
final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
parser.setErrorHandler(errorHandler);
final InputSource source = new InputSource(new StringReader(mediaString));
media = new MediaListImpl(parser.parseMedia(source));
if (media != null) {
media_.put(mediaString, media);
return media;
}
}
catch (final Exception e) {
LOG.error("Error parsing CSS media from '" + mediaString + "': " + e.getMessage(), e);
}
media = new MediaListImpl(null);
media_.put(mediaString, media);
return media;
}
/**
* Returns {@code true} if this stylesheet is active, based on the media types it is associated with (if any).
* @return {@code true} if this stylesheet is active, based on the media types it is associated with (if any)
*/
public boolean isActive() {
final String media;
final HtmlElement e = ownerNode_.getDomNodeOrNull();
if (e instanceof HtmlStyle) {
final HtmlStyle style = (HtmlStyle) e;
media = style.getMediaAttribute();
}
else if (e instanceof HtmlLink) {
final HtmlLink link = (HtmlLink) e;
media = link.getMediaAttribute();
}
else {
return true;
}
if (StringUtils.isBlank(media)) {
return true;
}
final WebClient webClient = getWindow().getWebWindow().getWebClient();
final MediaList mediaList = parseMedia(webClient.getCssErrorHandler(), media);
return isActive(this, mediaList);
}
private void fixPotentialEmptyMaskedMediaRule(ICSSNode node) {
CSSMediaRule mediaRule = (CSSMediaRule) node;
IndexedRegion mediaRuleRegion = (IndexedRegion) mediaRule;
if (!containsEmptyMaskedMediaRule(mediaRule, mediaRuleRegion)) {
return;
}
// Set the range to a valid value (it won't be proper since we don't have
// any additional words that can be categorized as CSS_MEDIUM.)
MediaList mediaList = mediaRule.getMedia();
IStructuredDocumentRegion[] structuredDocumentRegions = structuredDocument.getStructuredDocumentRegions(
mediaRuleRegion.getStartOffset(), mediaRuleRegion.getLength());
// The value we set is a 0-length region starting where the next word would
// have been
ITextRegion textRegion = new ContextRegion(CSSRegionContexts.CSS_MEDIUM,
structuredDocumentRegions[0].getEndOffset()
- structuredDocumentRegions[0].getStartOffset(), 0, 0);
try {
callSetRangeRegion(mediaList, structuredDocumentRegions, textRegion);
} catch (Throwable e) {
GWTPluginLog.logError(e, "Could not clean up the @else in the CSS model.");
}
}
private void fixPotentialEmptyMaskedMediaRule(ICSSNode node) {
CSSMediaRule mediaRule = (CSSMediaRule) node;
IndexedRegion mediaRuleRegion = (IndexedRegion) mediaRule;
if (!containsEmptyMaskedMediaRule(mediaRule, mediaRuleRegion)) {
return;
}
// Set the range to a valid value (it won't be proper since we don't have
// any additional words that can be categorized as CSS_MEDIUM.)
MediaList mediaList = mediaRule.getMedia();
IStructuredDocumentRegion[] structuredDocumentRegions = structuredDocument.getStructuredDocumentRegions(
mediaRuleRegion.getStartOffset(), mediaRuleRegion.getLength());
// The value we set is a 0-length region starting where the next word would
// have been
ITextRegion textRegion = new ContextRegion(CSSRegionContexts.CSS_MEDIUM,
structuredDocumentRegions[0].getEndOffset()
- structuredDocumentRegions[0].getStartOffset(), 0, 0);
try {
callSetRangeRegion(mediaList, structuredDocumentRegions, textRegion);
} catch (Throwable e) {
GWTPluginLog.logError(e, "Could not clean up the @else in the CSS model.");
}
}
private static boolean isScreenMedia(MediaList media) {
// https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries
// https://developers.google.com/gmail/design/reference/supported_css#supported_types
if (media instanceof MediaListImpl) {
MediaListImpl _media = (MediaListImpl) media;
for (int i = 0; i < _media.getLength(); i++) {
String query = _media.mediaQuery(i).getCssText(null);
if ("all".equals(query) ||
"screen".equals(query) || "only screen".equals(query))
return true;
}
} else
Log.e("Media class=" + media.getClass().getName());
return false;
}
/**
* Returns whether the document currently matches the media query list or not.
* @return whether the document currently matches the media query list or not
*/
@JsxGetter
public boolean isMatches() {
final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
final MediaList mediaList = CSSStyleSheet.parseMedia(errorHandler, media_);
return CSSStyleSheet.isActive(this, mediaList);
}
/**
* Returns whether the specified media is supported by the object that displays the document object.
* @param media the media query
* @return whether the specified media is supported or not
*/
@JsxFunction
public boolean matchMedium(final String media) {
final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
final MediaList mediaList = CSSStyleSheet.parseMedia(errorHandler, media);
return CSSStyleSheet.isActive(this, mediaList);
}
/**
* Calls the {@link MediaList} <code>setRangeRegion</code> method.
*
* @param mediaList the MediaList object that receives the call
* @param structuredDocumentRegions the first parameter of the method
* @param textRegion the second parameter of the method
* @throws Throwable for safeguarding against any reflection issues (nothing
* is logged in this method since it doesn't have proper context of
* the scenario)
*/
private static void callSetRangeRegion(MediaList mediaList,
IStructuredDocumentRegion[] structuredDocumentRegions,
ITextRegion textRegion) throws Throwable {
ClassLoader classLoader = CSSSourceFormatter.class.getClassLoader();
Class<?> cssRegionContainerClass = classLoader.loadClass("org.eclipse.wst.css.core.internal.document.CSSRegionContainer");
Method declaredMethod = cssRegionContainerClass.getDeclaredMethod(
"setRangeRegion", IStructuredDocumentRegion.class, ITextRegion.class,
ITextRegion.class);
declaredMethod.setAccessible(true);
declaredMethod.invoke(mediaList, structuredDocumentRegions[0], textRegion,
textRegion);
}
/**
* Calls the {@link MediaList} <code>setRangeRegion</code> method.
*
* @param mediaList the MediaList object that receives the call
* @param structuredDocumentRegions the first parameter of the method
* @param textRegion the second parameter of the method
* @throws Throwable for safeguarding against any reflection issues (nothing
* is logged in this method since it doesn't have proper context of
* the scenario)
*/
private static void callSetRangeRegion(MediaList mediaList,
IStructuredDocumentRegion[] structuredDocumentRegions,
ITextRegion textRegion) throws Throwable {
ClassLoader classLoader = CSSSourceFormatter.class.getClassLoader();
Class<?> cssRegionContainerClass = classLoader.loadClass("org.eclipse.wst.css.core.internal.document.CSSRegionContainer");
Method declaredMethod = cssRegionContainerClass.getDeclaredMethod(
"setRangeRegion", IStructuredDocumentRegion.class, ITextRegion.class,
ITextRegion.class);
declaredMethod.setAccessible(true);
declaredMethod.invoke(mediaList, structuredDocumentRegions[0], textRegion,
textRegion);
}
public MediaList getMedia( )
{
return null;
}
/**
* A list of media types for which this style sheet may be used.
*/
public MediaList getMedia();
/**
* A list of media types for this rule.
*/
public MediaList getMedia();
/**
* A list of media types for which this style sheet may be used.
*/
public MediaList getMedia();
/**
* A list of media types for this rule.
*/
public MediaList getMedia();
/**
* A list of media types for which this style sheet may be used.
*/
public MediaList getMedia();
/**
* A list of media types for this rule.
*/
public MediaList getMedia();
/**
* A list of media types for which this style sheet may be used.
*/
public MediaList getMedia();
/**
* A list of media types for this rule.
*/
public MediaList getMedia();
/**
* A list of media types for which this style sheet may be used.
*/
public MediaList getMedia();
/**
* A list of media types for this rule.
*/
public MediaList getMedia();
/**
* A list of media types for which this style sheet may be used.
*/
public MediaList getMedia();
/**
* A list of media types for this rule.
*/
public MediaList getMedia();
/**
* A list of media types for which this style sheet may be used.
*/
public MediaList getMedia();
/**
* A list of media types for this rule.
*/
public MediaList getMedia();
/**
* A list of media types for which this style sheet may be used.
*/
public MediaList getMedia();
/**
* A list of media types for this rule.
*/
public MediaList getMedia();
/**
* A list of media types for which this style sheet may be used.
*/
public MediaList getMedia();
/**
* A list of media types for this rule.
*/
public MediaList getMedia();
/**
* A list of media types for which this style sheet may be used.
*/
public MediaList getMedia();