Search in sources :

Example 1 with HtmlContext

use of com.google.template.soy.soytree.HtmlContext in project closure-templates by google.

the class Context method computeContextAfterAttributeDelimiter.

/**
 * Computes the context after an attribute delimiter is seen.
 *
 * @param elType The type of element whose tag the attribute appears in.
 * @param attrType The type of attribute whose value the delimiter starts.
 * @param delim The type of delimiter that will mark the end of the attribute value.
 * @param templateNestDepth The number of (@code <template>} elements on the open element stack.
 * @return A context suitable for the start of the attribute value.
 */
static Context computeContextAfterAttributeDelimiter(ElementType elType, AttributeType attrType, AttributeEndDelimiter delim, UriType uriType, int templateNestDepth) {
    HtmlContext state;
    JsFollowingSlash slash = JsFollowingSlash.NONE;
    UriPart uriPart = UriPart.NONE;
    switch(attrType) {
        case PLAIN_TEXT:
            state = HtmlContext.HTML_NORMAL_ATTR_VALUE;
            break;
        case SCRIPT:
            state = HtmlContext.JS;
            // Start a JS block in a regex state since
            // /foo/.test(str) && doSideEffect();
            // which starts with a regular expression literal is a valid and possibly useful program,
            // but there is no valid program which starts with a division operator.
            slash = JsFollowingSlash.REGEX;
            break;
        case STYLE:
            state = HtmlContext.CSS;
            break;
        case URI:
            state = HtmlContext.URI;
            uriPart = UriPart.START;
            break;
        // NONE is not a valid AttributeType inside an attribute value.
        default:
            throw new AssertionError("Unexpected attribute type " + attrType);
    }
    Preconditions.checkArgument((uriType != UriType.NONE) == (attrType == AttributeType.URI), "uriType=%s but attrType=%s", uriType, attrType);
    return new Context(state, elType, attrType, delim, slash, uriPart, uriType, templateNestDepth, 0);
}
Also used : HtmlContext(com.google.template.soy.soytree.HtmlContext) HtmlContext(com.google.template.soy.soytree.HtmlContext)

Example 2 with HtmlContext

use of com.google.template.soy.soytree.HtmlContext in project closure-templates by google.

the class RawTextContextUpdater method makeCssUriTransition.

/**
 * Matches the beginning of a CSS URI with the delimiter, if any, in group 1.
 */
private static Transition makeCssUriTransition(Pattern regex, final UriType uriType) {
    return new Transition(regex) {

        @Override
        Context computeNextContext(Context prior, Matcher matcher) {
            String delim = matcher.group(1);
            HtmlContext state;
            if ("\"".equals(delim)) {
                state = HtmlContext.CSS_DQ_URI;
            } else if ("'".equals(delim)) {
                state = HtmlContext.CSS_SQ_URI;
            } else {
                state = HtmlContext.CSS_URI;
            }
            return prior.toBuilder().withState(state).withUriType(uriType).withUriPart(UriPart.START).build();
        }
    };
}
Also used : HtmlContext(com.google.template.soy.soytree.HtmlContext) Matcher(java.util.regex.Matcher) HtmlContext(com.google.template.soy.soytree.HtmlContext)

Aggregations

HtmlContext (com.google.template.soy.soytree.HtmlContext)2 Matcher (java.util.regex.Matcher)1