/** * Handle tag <head> * * @param tag */ private void handleHeadTag(ComponentTag tag) { // we found <head> if (tag.isOpen()) { if (foundHead) { throw new MarkupException( new MarkupStream(markup), "Tag <head> is not allowed at this position (do you have multiple <head> tags in your markup?)."); } foundHead = true; if (tag.getId() == null) { tag.setId(HEADER_ID); tag.setAutoComponentTag(true); tag.setModified(true); } } else if (tag.isClose()) { if (foundHeaderItemsTag) { // revert the settings from above ComponentTag headOpenTag = tag.getOpenTag(); // change the id because it is special. See HtmlHeaderResolver headOpenTag.setId(HEADER_ID + "-Ignored"); headOpenTag.setAutoComponentTag(false); headOpenTag.setModified(false); headOpenTag.setFlag(ComponentTag.RENDER_RAW, true); } foundClosingHead = true; } }
/** Test that a null model does not throw null pointers. */ @Test public void nullModelDoesNotThrowNullPointerExceptions() { AttributeModifier modifier = new AttributeModifier("test", null); XmlTag xmlTag = new XmlTag(); ComponentTag tag = new ComponentTag(xmlTag); tag.setId("foo"); tag.setName("test"); modifier.replaceAttributeValue(null, tag); Map<String, Object> attributes = tag.getAttributes(); assertTrue(attributes.isEmpty()); }
/** Test simple model replacement. */ @Test public void testModelReplacement() { AttributeModifier modifier = new AttributeModifier("test", Model.of("Ellioth Smith Rocks")); XmlTag xmlTag = new XmlTag(); ComponentTag tag = new ComponentTag(xmlTag); tag.setId("test"); tag.setName("id"); modifier.replaceAttributeValue(null, tag); Map<String, Object> attributes = tag.getAttributes(); assertTrue(!attributes.isEmpty()); String replacement = (String) attributes.get("test"); assertNotNull(replacement); assertEquals("Ellioth Smith Rocks", replacement); }
/** Insert <head> open and close tag (with empty body) to the current position. */ private void insertHeadTag() { // Note: only the open-tag must be a AutoComponentTag final ComponentTag openTag = new ComponentTag(HEAD, TagType.OPEN); openTag.setId(HEADER_ID); openTag.setAutoComponentTag(true); openTag.setModified(true); final ComponentTag closeTag = new ComponentTag(HEAD, TagType.CLOSE); closeTag.setOpenTag(openTag); closeTag.setModified(true); // insert the tags into the markup stream markup.addMarkupElement(openTag); markup.addMarkupElement(closeTag); }
/** * Handle tag <wicket:header-items> * * @param tag */ private void handleHeaderItemsTag(ComponentTag tag) { if (foundHeaderItemsTag) { throw new MarkupException( new MarkupStream(markup), "More than one <wicket:header-items/> detected in the <head> element. Only one is allowed."); } else if (foundClosingHead) { throw new MarkupException( new MarkupStream(markup), "Detected <wicket:header-items/> after the closing </head> element."); } foundHeaderItemsTag = true; tag.setId(HEADER_ID); tag.setAutoComponentTag(true); tag.setModified(true); }
/** * Resolves the child component which is the controller of this Enclosure * * @param markupStream the markup stream of this Enclosure * @param enclosureParent the non-auto parent component of this Enclosure * @return The component associated with the {@linkplain #childId} */ private Component getChildComponent( final MarkupStream markupStream, MarkupContainer enclosureParent) { String fullChildId = getChildId(); Component controller = enclosureParent.get(fullChildId); if (controller == null) { int orgIndex = markupStream.getCurrentIndex(); try { while (markupStream.hasMore()) { markupStream.next(); if (markupStream.skipUntil(ComponentTag.class)) { ComponentTag tag = markupStream.getTag(); if ((tag != null) && (tag.isOpen() || tag.isOpenClose())) { String tagId = tag.getId(); if (fullChildId.equals(tagId)) { ComponentTag fullComponentTag = new ComponentTag(tag); fullComponentTag.setId(childId.toString()); controller = ComponentResolvers.resolve( enclosureParent, markupStream, fullComponentTag, new ResolverFilter() { @Override public boolean ignoreResolver(final IComponentResolver resolver) { return resolver instanceof EnclosureHandler; } }); break; } else if (fullChildId.startsWith(tagId + PATH_SEPARATOR)) { fullChildId = Strings.afterFirst(fullChildId, PATH_SEPARATOR); } } } } } finally { markupStream.setCurrentIndex(orgIndex); } } checkChildComponent(controller); return controller; }
/** Test that that the attribute modifier does nothing with not enabled. */ @Test public void testNoNewValueWhenNotEnabled() { AttributeModifier modifier = new AttributeModifier("test", Model.of("Ellioth Smith Rocks")) { @Override public boolean isEnabled(Component component) { return false; } }; XmlTag xmlTag = new XmlTag(); ComponentTag tag = new ComponentTag(xmlTag); tag.setId("test"); tag.setName("id"); Map<String, Object> attributes = tag.getAttributes(); attributes.put("test", "My mother rocks"); modifier.replaceAttributeValue(null, tag); String replacement = (String) attributes.get("test"); assertNotNull(replacement); assertEquals("My mother rocks", replacement); }
/** Test overriding newValue (and using a null model). */ @Test public void testNewValue() { AttributeModifier modifier = new AttributeModifier("test", null) { private static final long serialVersionUID = 1L; @Override protected String newValue(String currentValue, String replacementValue) { return "the replacement"; } }; XmlTag xmlTag = new XmlTag(); ComponentTag tag = new ComponentTag(xmlTag); tag.setId("test"); tag.setName("id"); modifier.replaceAttributeValue(null, tag); Map<String, Object> attributes = tag.getAttributes(); assertTrue(!attributes.isEmpty()); String replacement = (String) attributes.get("test"); assertNotNull(replacement); assertEquals("the replacement", replacement); }
@Override protected final MarkupElement onComponentTag(ComponentTag tag) throws ParseException { if (tag.isClose()) { return tag; } final String wicketMessageAttribute = tag.getAttributes().getString(getWicketMessageAttrName()); if ((wicketMessageAttribute != null) && (wicketMessageAttribute.trim().length() > 0)) { // check if this tag is raw markup if (tag.getId() == null) { // if this is a raw tag we need to set the id to something so // that wicket will not merge this as raw markup and instead // pass it on to a resolver tag.setId(WICKET_MESSAGE_CONTAINER_ID); tag.setAutoComponentTag(true); tag.setModified(true); } tag.addBehavior(new AttributeLocalizer(getWicketMessageAttrName())); } return tag; }
@Override protected MarkupElement onComponentTag(final ComponentTag tag) throws ParseException { // We only need ComponentTags if (tag instanceof WicketTag) { return tag; } // Has wicket:enclosure attribute? String enclosureAttr = getAttribute(tag, null); if (enclosureAttr != null) { if (tag.isOpen()) { // Make sure 'wicket:id' and 'id' are consistent String htmlId = tag.getAttribute("id"); if ((tag.getId() != null) && !Strings.isEmpty(htmlId) && !htmlId.equals(tag.getId())) { throw new ParseException( "Make sure that 'id' and 'wicket:id' are the same if both are provided. Tag:" + tag.toString(), tag.getPos()); } // if it doesn't have a wicket-id already, then assign one now. if (Strings.isEmpty(tag.getId())) { if (Strings.isEmpty(htmlId)) { String id = getWicketNamespace() + "_" + INLINE_ENCLOSURE_ID_PREFIX + getRequestUniqueId(); tag.setId(id); } else { tag.setId(htmlId); } tag.setAutoComponentTag(true); tag.setAutoComponentFactory( new ComponentTag.IAutoComponentFactory() { @Override public Component newComponent(MarkupContainer container, ComponentTag tag) { String attributeName = getInlineEnclosureAttributeName(null); String childId = tag.getAttribute(attributeName); return new InlineEnclosure(tag.getId(), childId); } }); tag.setModified(true); } // Put the enclosure on the stack. The most current one will be on top if (enclosures == null) { enclosures = new ArrayDeque<>(); } enclosures.push(tag); } else { throw new ParseException( "Open-close tags don't make sense for InlineEnclosure. Tag:" + tag.toString(), tag.getPos()); } } // Are we within an enclosure? else if ((enclosures != null) && (enclosures.size() > 0)) { // In case the enclosure tag did not provide a child component id, then assign the // first ComponentTag's id found as the controlling child to the enclosure. if (tag.isOpen() && (tag.getId() != null) && !(tag instanceof WicketTag) && !tag.isAutoComponentTag()) { Iterator<ComponentTag> componentTagIterator = enclosures.descendingIterator(); while (componentTagIterator.hasNext()) { ComponentTag lastEnclosure = componentTagIterator.next(); String attr = getAttribute(lastEnclosure, null); if (Strings.isEmpty(attr) == true) { lastEnclosure.getAttributes().put(getInlineEnclosureAttributeName(null), tag.getId()); lastEnclosure.setModified(true); } } } else if (tag.isClose() && tag.closes(enclosures.peek())) { ComponentTag lastEnclosure = enclosures.pop(); String attr = getAttribute(lastEnclosure, null); if (Strings.isEmpty(attr) == true) { throw new ParseException( "Did not find any child for InlineEnclosure. Tag:" + lastEnclosure.toString(), tag.getPos()); } } } return tag; }